git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 0/7] strbuf cleanups

From
Calvin Wan <calvinwan@google.com>
Date
May 11, 2023, 19:44 UTC
Message-ID
<20230511194446.1492907-1-calvinwan@google.com>
In-Reply-To
<20230508165728.525603-1-calvinwan@google.com>

This reroll includes suggestions made by Eric and Phillip for better documentation on patch 1 and removal of an extraneous parameter in patch 7.

Calvin Wan (7):
  strbuf: clarify API boundary
  abspath: move related functions to abspath
  credential-store: move related functions to credential-store file
  object-name: move related functions to object-name
  path: move related function to path
  strbuf: clarify dependency
  strbuf: remove global variable
 abspath.c                  |  36 ++++++++++++
 abspath.h                  |  21 +++++++
 add-patch.c                |  12 ++--
 builtin/am.c               |   2 +-
 builtin/branch.c           |   4 +-
 builtin/commit.c           |   2 +-
 builtin/credential-store.c |  19 +++++++
 builtin/merge.c            |  10 ++--
 builtin/notes.c            |  16 +++---
 builtin/rebase.c           |   2 +-
 builtin/stripspace.c       |   6 +-
 builtin/tag.c              |   9 ++-
 fmt-merge-msg.c            |   9 ++-
 gpg-interface.c            |   5 +-
 hook.c                     |   1 +
 object-name.c              |  15 +++++
 object-name.h              |   9 +++
 path.c                     |  20 +++++++
 path.h                     |   5 ++
 pretty.c                   |   1 +
 rebase-interactive.c       |  15 ++---
 sequencer.c                |  24 +++++---
 strbuf.c                   | 112 ++++---------------------------------
 strbuf.h                   |  59 ++++++-------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +-
 26 files changed, 229 insertions(+), 192 deletions(-)
Range-diff against v4:
1:  e0dd3f5295 ! 1:  287e787c9c strbuf: clarify API boundary
    @@ Commit message
         strbuf: clarify API boundary
     
         strbuf, as a generic and widely used structure across the codebase,
    -    should be limited as a libary to only interact with primitives. Add
    -    documentation so future functions can be appropriately be placed. Older
    +    should be limited as a library to only interact with primitives. Add
    +    documentation so future functions can appropriately be placed. Older
         functions that do not follow this boundary should eventually be moved or
         refactored.
     
      ## strbuf.h ##
    -@@ strbuf.h: struct string_list;
    +@@
    + #ifndef STRBUF_H
    + #define STRBUF_H
    + 
    ++/*
    ++ * NOTE FOR STRBUF DEVELOPERS
    ++ *
    ++ * The objects that this API interacts with should be limited to other
    ++ * primitives, however, there are older functions in here that interact
    ++ * with non-primitive objects which should eventually be moved out or
    ++ * refactored.
    ++ */
    ++
    + struct string_list;
      
      /**
    -  * strbuf's are meant to be used with all the usual C string and memory
    -- * APIs. Given that the length of the buffer is known, it's often better to
    -+ * APIs. The objects that this API interacts with in this file should be 
    -+ * limited to other primitives, however, there are older functions in here 
    -+ * that should eventually be moved out or refactored. 
    -+ * 
    -+ * Given that the length of the buffer is known, it's often better to
    -  * use the mem* functions than a str* one (memchr vs. strchr e.g.).
    -  * Though, one has to be careful about the fact that str* functions often
    -  * stop on NULs and that strbufs may have embedded NULs.
2:  48fb5db28b = 2:  5bd27cad7a abspath: move related functions to abspath
3:  a663f91819 = 3:  08e3e40de6 credential-store: move related functions to credential-store file
4:  ccef9dd5f2 = 4:  1a21d4fdd1 object-name: move related functions to object-name
5:  0d6b9cf0f7 = 5:  03b20f3384 path: move related function to path
6:  5655c56a6d = 6:  b55f4a39e1 strbuf: clarify dependency
7:  874d0efac3 ! 7:  6ea36d7730 strbuf: remove global variable
    @@ Commit message
         not utilize the comment_line_char global variable within its
         functions. Therefore, add an additional parameter for functions that use
         comment_line_char and refactor callers to pass it in instead.
    +    strbuf_stripspace() removes the skip_comments boolean and checks if
    +    comment_line_char is a non-NULL character to determine whether to skip
    +    comments or not.
     
      ## add-patch.c ##
     @@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
    @@ builtin/am.c: static int parse_mail(struct am_state *state, const char *mail)
      	strbuf_addstr(&msg, "\n\n");
      	strbuf_addbuf(&msg, &mi.log_message);
     -	strbuf_stripspace(&msg, 0);
    -+	strbuf_stripspace(&msg, 0, comment_line_char);
    ++	strbuf_stripspace(&msg, '\0');
      
      	assert(!state->author_name);
      	state->author_name = strbuf_detach(&author_name, NULL);
    @@ builtin/branch.c: static int edit_branch_description(const char *branch_name)
      		return -1;
      	}
     -	strbuf_stripspace(&buf, 1);
    -+	strbuf_stripspace(&buf, 1, comment_line_char);
    ++	strbuf_stripspace(&buf, comment_line_char);
      
      	strbuf_addf(&name, "branch.%s.description", branch_name);
      	if (buf.len || exists)
    @@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const cha
      
      	if (clean_message_contents)
     -		strbuf_stripspace(&sb, 0);
    -+		strbuf_stripspace(&sb, 0, comment_line_char);
    ++		strbuf_stripspace(&sb, '\0');
      
      	if (signoff)
      		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
    @@ builtin/notes.c: static void prepare_note_data(const struct object_id *object, s
      			die(_("please supply the note contents using either -m or -F option"));
      		}
     -		strbuf_stripspace(&d->buf, 1);
    -+		strbuf_stripspace(&d->buf, 1, comment_line_char);
    ++		strbuf_stripspace(&d->buf, comment_line_char);
      	}
      }
      
    @@ builtin/notes.c: static int parse_msg_arg(const struct option *opt, const char *
      		strbuf_addch(&d->buf, '\n');
      	strbuf_addstr(&d->buf, arg);
     -	strbuf_stripspace(&d->buf, 0);
    -+	strbuf_stripspace(&d->buf, 0, comment_line_char);
    ++	strbuf_stripspace(&d->buf, '\0');
      
      	d->given = 1;
      	return 0;
    @@ builtin/notes.c: static int parse_file_arg(const struct option *opt, const char
      	} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
      		die_errno(_("could not open or read '%s'"), arg);
     -	strbuf_stripspace(&d->buf, 0);
    -+	strbuf_stripspace(&d->buf, 0, comment_line_char);
    ++	strbuf_stripspace(&d->buf, '\0');
      
      	d->given = 1;
      	return 0;
    @@ builtin/rebase.c: static int edit_todo_file(unsigned flags)
      		return error_errno(_("could not read '%s'."), todo_file);
      
     -	strbuf_stripspace(&todo_list.buf, 1);
    -+	strbuf_stripspace(&todo_list.buf, 1, comment_line_char);
    ++	strbuf_stripspace(&todo_list.buf, comment_line_char);
      	res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
      	if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
      					    NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
    @@ builtin/stripspace.c: int cmd_stripspace(int argc, const char **argv, const char
      
      	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
     -		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
    -+		strbuf_stripspace(&buf, mode == STRIP_COMMENTS,
    -+				  comment_line_char);
    ++		strbuf_stripspace(&buf,
    ++			  mode == STRIP_COMMENTS ? comment_line_char : '\0');
      	else
      		comment_lines(&buf);
      
    @@ builtin/tag.c: static void create_tag(const struct object_id *object, const char
      
      	if (opt->cleanup_mode != CLEANUP_NONE)
     -		strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
    -+		strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL,
    -+				  comment_line_char);
    ++		strbuf_stripspace(buf,
    ++		  opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
      
      	if (!opt->message_given && !buf->len)
      		die(_("no tag message?"));
    @@ gpg-interface.c: static int verify_ssh_signed_buffer(struct signature_check *sig
      
     -	strbuf_stripspace(&ssh_keygen_out, 0);
     -	strbuf_stripspace(&ssh_keygen_err, 0);
    -+	strbuf_stripspace(&ssh_keygen_out, 0, comment_line_char);
    -+	strbuf_stripspace(&ssh_keygen_err, 0, comment_line_char);
    ++	strbuf_stripspace(&ssh_keygen_out, '\0');
    ++	strbuf_stripspace(&ssh_keygen_err, '\0');
      	/* Add stderr outputs to show the user actual ssh-keygen errors */
      	strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
      	strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);
    @@ rebase-interactive.c: int edit_todo_list(struct repository *r, struct todo_list
      		return -2;
      
     -	strbuf_stripspace(&new_todo->buf, 1);
    -+	strbuf_stripspace(&new_todo->buf, 1, comment_line_char);
    ++	strbuf_stripspace(&new_todo->buf, comment_line_char);
      	if (initial && new_todo->buf.len == 0)
      		return -3;
      
    @@ sequencer.c: void cleanup_message(struct strbuf *msgbuf,
      		strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
      	if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
     -		strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
    -+		strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
    -+				  comment_line_char);
    ++		strbuf_stripspace(msgbuf,
    ++		  cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
      }
      
      /*
    @@ sequencer.c: int template_untouched(const struct strbuf *sb, const char *templat
      		return 0;
      
     -	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
    -+	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
    -+			  comment_line_char);
    ++	strbuf_stripspace(&tmpl,
    ++	  cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
      	if (!skip_prefix(sb->buf, tmpl.buf, &start))
      		start = sb->buf;
      	strbuf_release(&tmpl);
    @@ sequencer.c: static int try_to_commit(struct repository *r,
      
      	if (cleanup != COMMIT_MSG_CLEANUP_NONE)
     -		strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
    -+		strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL,
    -+				  comment_line_char);
    ++		strbuf_stripspace(msg,
    ++		  cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
      	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
      		res = 1; /* run 'git commit' to display error message */
      		goto out;
    @@ strbuf.c: void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
      		sb->buf[--sb->len] = '\0';
      
     @@ strbuf.c: static size_t cleanup(char *line, size_t len)
    -  * Enable skip_comments to skip every line starting with comment
    -  * character.
    +  *
    +  * If last line does not have a newline at the end, one is added.
    +  *
    +- * Enable skip_comments to skip every line starting with comment
    +- * character.
    ++ * Pass a non-NULL comment_line_char to skip every line starting
    ++ * with it
       */
     -void strbuf_stripspace(struct strbuf *sb, int skip_comments)
    -+void strbuf_stripspace(struct strbuf *sb, int skip_comments,
    -+		       char comment_line_char)
    ++void strbuf_stripspace(struct strbuf *sb, char comment_line_char)
      {
      	size_t empties = 0;
      	size_t i, j, len, newlen;
    +@@ strbuf.c: void strbuf_stripspace(struct strbuf *sb, int skip_comments)
    + 		eol = memchr(sb->buf + i, '\n', sb->len - i);
    + 		len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
    + 
    +-		if (skip_comments && len && sb->buf[i] == comment_line_char) {
    ++		if (comment_line_char != '\0' && len &&
    ++		    sb->buf[i] == comment_line_char) {
    + 			newlen = 0;
    + 			continue;
    + 		}
     
      ## strbuf.h ##
     @@ strbuf.h: void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
    @@ strbuf.h: void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
      
      __attribute__((format (printf,2,0)))
      void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
    -@@ strbuf.h: int strbuf_normalize_path(struct strbuf *sb);
    +@@ strbuf.h: int strbuf_getcwd(struct strbuf *sb);
    + int strbuf_normalize_path(struct strbuf *sb);
      
      /**
    -  * Strip whitespace from a buffer. The second parameter controls if
    +- * Strip whitespace from a buffer. The second parameter controls if
     - * comments are considered contents to be removed or not.
    -+ * comments are considered contents to be removed or not. The third parameter
    -+ * is the comment character that determines whether a line is a comment or not.
    ++ * Strip whitespace from a buffer. The second parameter is the comment
    ++ * character that determines whether a line is a comment or not. If the
    ++ * second parameter is a non-NULL character, comments are considered
    ++ * contents to be removed.
       */
     -void strbuf_stripspace(struct strbuf *buf, int skip_comments);
    -+void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
    ++void strbuf_stripspace(struct strbuf *buf, char comment_line_char);
      
      static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
      {
-- 
2.40.1.606.ga4b1b128d6-goog
Previous: Felipe ContrerasNext: Calvin Wan
Message 53 of 85 in “strbuf cleanups”
  1. 0/6 strbuf cleanupsCalvin Wan, May 2, 2023
  2. 1/6 abspath: move related functions to abspathCalvin Wan, May 2, 2023
  3. Junio C HamanoMay 2, 2023
  4. 2/6 credential-store: move related functions to credential-store fileCalvin Wan, May 2, 2023
  5. Junio C HamanoMay 2, 2023
  6. Jeff KingMay 3, 2023
  7. Jeff KingMay 3, 2023
  8. Calvin WanMay 3, 2023
  9. 3/6 object-name: move related functions to object-nameCalvin Wan, May 2, 2023
  10. 4/6 path: move related function to pathCalvin Wan, May 2, 2023
  11. 5/6 strbuf: clarify dependencyCalvin Wan, May 2, 2023
  12. Elijah NewrenMay 3, 2023
  13. 6/6 strbuf: remove environment variablesCalvin Wan, May 2, 2023
  14. Elijah NewrenMay 3, 2023
  15. Junio C HamanoMay 2, 2023
  16. Junio C HamanoMay 2, 2023
  17. Felipe ContrerasMay 2, 2023
  18. Calvin WanMay 2, 2023
  19. Elijah NewrenMay 3, 2023
  20. Calvin WanMay 3, 2023
  21. Elijah NewrenMay 7, 2023
  22. Jeff KingMay 7, 2023
  23. 0/7 strbuf cleanupsCalvin Wan, May 3, 2023
  24. 1/7 strbuf: clarify API boundaryCalvin Wan, May 3, 2023
  25. 2/7 abspath: move related functions to abspathCalvin Wan, May 3, 2023
  26. 3/7 credential-store: move related functions to credential-store fileCalvin Wan, May 3, 2023
  27. 5/7 path: move related function to pathCalvin Wan, May 3, 2023
  28. 4/7 object-name: move related functions to object-nameCalvin Wan, May 3, 2023
  29. 6/7 strbuf: clarify dependencyCalvin Wan, May 3, 2023
  30. Junio C HamanoMay 3, 2023
  31. 7/7 strbuf: remove environment variablesCalvin Wan, May 3, 2023
  32. Junio C HamanoMay 3, 2023
  33. Calvin WanMay 3, 2023
  34. Junio C HamanoMay 3, 2023
  35. 7/7 strbuf: remove environment variableCalvin Wan, May 3, 2023
  36. Junio C HamanoMay 5, 2023
  37. Calvin WanMay 8, 2023
  38. Elijah NewrenMay 7, 2023
  39. Felipe ContrerasMay 7, 2023
  40. 0/7 strbuf cleanupsCalvin Wan, May 8, 2023
  41. 1/7 strbuf: clarify API boundaryCalvin Wan, May 8, 2023
  42. Eric SunshineMay 8, 2023
  43. Junio C HamanoMay 10, 2023
  44. 3/7 credential-store: move related functions to credential-store fileCalvin Wan, May 8, 2023
  45. 5/7 path: move related function to pathCalvin Wan, May 8, 2023
  46. 2/7 abspath: move related functions to abspathCalvin Wan, May 8, 2023
  47. 4/7 object-name: move related functions to object-nameCalvin Wan, May 8, 2023
  48. 6/7 strbuf: clarify dependencyCalvin Wan, May 8, 2023
  49. 7/7 strbuf: remove global variableCalvin Wan, May 8, 2023
  50. Phillip WoodMay 10, 2023
  51. Elijah NewrenMay 9, 2023
  52. Felipe ContrerasMay 9, 2023
  53. 0/7 strbuf cleanupsCalvin Wan, May 11, 2023
  54. 1/7 strbuf: clarify API boundaryCalvin Wan, May 11, 2023
  55. Eric SunshineMay 11, 2023
  56. Calvin WanMay 11, 2023
  57. 2/7 abspath: move related functions to abspathCalvin Wan, May 11, 2023
  58. 3/7 credential-store: move related functions to credential-store fileCalvin Wan, May 11, 2023
  59. 6/7 strbuf: clarify dependencyCalvin Wan, May 11, 2023
  60. 5/7 path: move related function to pathCalvin Wan, May 11, 2023
  61. 4/7 object-name: move related functions to object-nameCalvin Wan, May 11, 2023
  62. 7/7 strbuf: remove global variableCalvin Wan, May 11, 2023
  63. Eric SunshineMay 11, 2023
  64. Junio C HamanoMay 11, 2023
  65. Phillip WoodMay 12, 2023
  66. Phillip WoodMay 12, 2023
  67. Junio C HamanoMay 12, 2023
  68. 0/7 strbuf cleanupsCalvin Wan, May 12, 2023
  69. 1/7 strbuf: clarify API boundaryCalvin Wan, May 12, 2023
  70. 2/7 abspath: move related functions to abspathCalvin Wan, May 12, 2023
  71. 4/7 object-name: move related functions to object-nameCalvin Wan, May 12, 2023
  72. 3/7 credential-store: move related functions to credential-store fileCalvin Wan, May 12, 2023
  73. 5/7 path: move related function to pathCalvin Wan, May 12, 2023
  74. 6/7 strbuf: clarify dependencyCalvin Wan, May 12, 2023
  75. 7/7 strbuf: remove global variableCalvin Wan, May 12, 2023
  76. Junio C HamanoMay 12, 2023
  77. Eric SunshineMay 13, 2023
  78. 0/7 strbuf cleanupsCalvin Wan, Jun 6, 2023
  79. 1/7 strbuf: clarify API boundaryCalvin Wan, Jun 6, 2023
  80. 2/7 strbuf: clarify dependencyCalvin Wan, Jun 6, 2023
  81. 6/7 path: move related function to pathCalvin Wan, Jun 6, 2023
  82. 3/7 abspath: move related functions to abspathCalvin Wan, Jun 6, 2023
  83. 5/7 object-name: move related functions to object-nameCalvin Wan, Jun 6, 2023
  84. 4/7 credential-store: move related functions to credential-store fileCalvin Wan, Jun 6, 2023
  85. 7/7 strbuf: remove global variableCalvin Wan, Jun 6, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.