Git Mailing List Archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] strbuf cleanups
@ 2023-05-02 21:14 Calvin Wan
  2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
                   ` (8 more replies)
  0 siblings, 9 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

Strbuf is a basic structure that should function as a low-level library
due to how generic it is. Over time certain functions inside of
strbuf.[ch] have been added with dependencies to higher level objects
and functions. This series cleans up some of those higher level
dependencies by moving the offending functions to the files they
interact with. With the goal of eventually being able to stand up strbuf
as a libary, this series also removes the use of environment variables
from strbuf.

Calvin Wan (6):
  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 environment variables

 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                   | 106 +++----------------------------------
 strbuf.h                   |  44 ++-------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +--
 26 files changed, 213 insertions(+), 187 deletions(-)

-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH 1/6] abspath: move related functions to abspath
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-02 21:42   ` Junio C Hamano
  2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
they do not belong in a low-level library.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 76e322f580..2d8706371e 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..c3b6d48797 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..6b4ad63266 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -527,28 +527,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH 2/6] credential-store: move related functions to credential-store file
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
  2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-02 21:52   ` Junio C Hamano
  2023-05-03 16:28   ` Jeff King
  2023-05-02 21:14 ` [PATCH 3/6] object-name: move related functions to object-name Calvin Wan
                   ` (6 subsequent siblings)
  8 siblings, 2 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 8977604eb9..4776118331 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index c3b6d48797..da2693b21f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -809,25 +809,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index 6b4ad63266..1a4c07a053 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -682,9 +682,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH 3/6] object-name: move related functions to object-name
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
  2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
  2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-02 21:14 ` [PATCH 4/6] path: move related function to path Calvin Wan
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
since they do not belong in a low-level library.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 5 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/object-name.c b/object-name.c
index 538e8a8f62..c2e82aceea 100644
--- a/object-name.c
+++ b/object-name.c
@@ -766,6 +766,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index da2693b21f..6533559e95 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "refs.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1023,21 +1022,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 1a4c07a053..b6d53c1cbe 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -608,16 +608,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH 4/6] path: move related function to path
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (2 preceding siblings ...)
  2023-05-02 21:14 ` [PATCH 3/6] object-name: move related functions to object-name Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-02 21:14 ` [PATCH 5/6] strbuf: clarify dependency Calvin Wan
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

Move path-related function from strbuf.[ch] to path.[ch] since it does
not belong in a low-level library.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 20 --------------------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 6533559e95..178d75f250 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1088,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH 5/6] strbuf: clarify dependency
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (3 preceding siblings ...)
  2023-05-02 21:14 ` [PATCH 4/6] path: move related function to path Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-03  1:56   ` Elijah Newren
  2023-05-02 21:14 ` [PATCH 6/6] strbuf: remove environment variables Calvin Wan
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 178d75f250..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "refs.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH 6/6] strbuf: remove environment variables
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (4 preceding siblings ...)
  2023-05-02 21:14 ` [PATCH 5/6] strbuf: clarify dependency Calvin Wan
@ 2023-05-02 21:14 ` Calvin Wan
  2023-05-03  2:15   ` Elijah Newren
  2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 21:14 UTC (permalink / raw)
  To: git; +Cc: newren, Calvin Wan

As a lower level library, strbuf should not directly access environment
variables within its functions. Therefore, add an additional variable to
function signatures for functions that use an environment variable and
refactor callers to pass in the environment variable.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 12 +++++++-----
 strbuf.h             |  9 +++++----
 wt-status.c          |  6 +++---
 16 files changed, 84 insertions(+), 59 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..306489db13 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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);
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..e52afcf4d7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, 1, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..6d3cc4d9fe 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, 0, comment_line_char);
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..0918b92752 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			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);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, 0, comment_line_char);
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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);
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..d092e06dc7 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	strbuf_stripspace(&todo_list.buf, 1, 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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..e182c2a14e 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf, mode == STRIP_COMMENTS,
+				  comment_line_char);
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..c713d14489 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag,comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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);
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..b625f17460 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	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);
 	/* 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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..c6027e73c1 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, 1, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..9675b62bcb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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);
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
+			  comment_line_char);
 	if (!skip_prefix(sb->buf, tmpl.buf, &start))
 		start = sb->buf;
 	strbuf_release(&tmpl);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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);
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..eba65ca421 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1054,7 +1055,8 @@ static size_t cleanup(char *line, size_t len)
  * Enable skip_comments to skip every line starting with comment
  * character.
  */
-void strbuf_stripspace(struct strbuf *sb, int skip_comments)
+void strbuf_stripspace(struct strbuf *sb, int skip_comments,
+		       char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
diff --git a/strbuf.h b/strbuf.h
index b6d53c1cbe..f90da8859f 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -283,7 +283,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -412,8 +413,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -538,7 +539,7 @@ int strbuf_normalize_path(struct strbuf *sb);
  * Strip whitespace from a buffer. The second parameter controls if
  * comments are considered contents to be removed or not.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.495.gc816e09b53d-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH 1/6] abspath: move related functions to abspath
  2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
@ 2023-05-02 21:42   ` Junio C Hamano
  0 siblings, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-02 21:42 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren

Calvin Wan <calvinwan@google.com> writes:

> Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
> they do not belong in a low-level library.

Given a relative path and turning it into absolute is still
low-level enough, but that apparently is not what you meant.

It would be nice to define what you mean by "low-level" here, and
update the comments at the beginning of <strbuf.h> with that
definition.

I think what you are trying to do is to move anything that does more
than straight string manipulation out of "strbuf.c"; roughly, things
that have system dependencies that are more than malloc/realloc.

For this particular step, I think it is a natural thing to move a
function that computes an absolute version of a given path to
abspath.[ch] that already exists.  The same goes for its realpath
counterpart.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 2/6] credential-store: move related functions to credential-store file
  2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-02 21:52   ` Junio C Hamano
  2023-05-03 16:28   ` Jeff King
  1 sibling, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-02 21:52 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren

Calvin Wan <calvinwan@google.com> writes:

> is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
> called from builtin/credential-store.c and they are only relevant to that
> file so move those functions and make them static.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>  builtin/credential-store.c | 19 +++++++++++++++++++
>  strbuf.c                   | 19 -------------------
>  strbuf.h                   |  3 ---
>  3 files changed, 19 insertions(+), 22 deletions(-)
>
> diff --git a/builtin/credential-store.c b/builtin/credential-store.c
> index 8977604eb9..4776118331 100644
> --- a/builtin/credential-store.c
> +++ b/builtin/credential-store.c
> @@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
>  		die_errno("unable to write credential store");
>  }
>  
> +static int is_rfc3986_unreserved(char ch)
> +{
> +	return isalnum(ch) ||
> +		ch == '-' || ch == '_' || ch == '.' || ch == '~';
> +}
> +
> +static int is_rfc3986_reserved_or_unreserved(char ch)
> +{
> +	if (is_rfc3986_unreserved(ch))
> +		return 1;
> +	switch (ch) {
> +		case '!': case '*': case '\'': case '(': case ')': case ';':
> +		case ':': case '@': case '&': case '=': case '+': case '$':
> +		case ',': case '/': case '?': case '#': case '[': case ']':
> +			return 1;
> +	}
> +	return 0;
> +}

Both the moved ones being static means we even lose the declarations
in the header file.  Very nice.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (5 preceding siblings ...)
  2023-05-02 21:14 ` [PATCH 6/6] strbuf: remove environment variables Calvin Wan
@ 2023-05-02 22:20 ` Junio C Hamano
  2023-05-02 22:31   ` Junio C Hamano
  2023-05-02 22:36   ` Calvin Wan
  2023-05-03  2:37 ` Elijah Newren
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
  8 siblings, 2 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-02 22:20 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren

Calvin Wan <calvinwan@google.com> writes:

> Strbuf is a basic structure that should function as a low-level library
> due to how generic it is. Over time certain functions inside of
> strbuf.[ch] have been added with dependencies to higher level objects
> and functions. This series cleans up some of those higher level
> dependencies by moving the offending functions to the files they
> interact with. With the goal of eventually being able to stand up strbuf
> as a libary, this series also removes the use of environment variables
> from strbuf.

I touched a bit of the same in my review for 1/6, but the main
thrust of the series is that you want to make strbuf.[ch] mostly
about "string processing primitives".

Any meaningful features relate to more than one conceptual things,
e.g. pathname functions work on "paths" (that is one) and it may
store computed result in a "strbuf" (that is another).  We have
historically called them strbuf_do_something() and gave the pointer
to a strbuf that receives the result.  And this series wants to
reverse the course and want to see strbuf that is more pure.

That's fine.  The strbuf has become so successful a data structure,
its use has become as ubiquitous as a plain string or an integer in
our codebase.

But if we were moving in that direction, I have to wonder if some of
these functions also need to be renamed to lose their strbuf_
prefix.  A function that takes a pathname and creates a directory
there is not called string_mkdir() just because it takes a pathname
as a string.  A function that takes a string buffer and a path, and
reads the symbolic link content is not called string_readlink() just
because it learns the symlink target in a string buffer.  What their
parameters mean matters a lot more than what type these parameters
are represented as.

With some other topics in flight, merging this to 'seen' and merging
this to 'next' may require different merge fixes, but I'll manage.

Thanks.


^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
@ 2023-05-02 22:31   ` Junio C Hamano
  2023-05-02 23:51     ` Felipe Contreras
  2023-05-02 22:36   ` Calvin Wan
  1 sibling, 1 reply; 85+ messages in thread
From: Junio C Hamano @ 2023-05-02 22:31 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren

Junio C Hamano <gitster@pobox.com> writes:

> But if we were moving in that direction, I have to wonder if some of
> these functions also need to be renamed to lose their strbuf_
> prefix.

Just to avoid misunderstanding.  I do not mean to suggest renaming
these inside this series.  It would make things too noisy and even
more distracting.  But in the longer term, as we treat strbuf more
and more as one of our basic data structures, it would make sense to
lose strbuf_ from functions that are thrown out of strbuf.[ch] with
this series, and reserve the prefix to functions that are left in
strbuf.[ch], i.e. those that are about string operations.


^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
  2023-05-02 22:31   ` Junio C Hamano
@ 2023-05-02 22:36   ` Calvin Wan
  1 sibling, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-02 22:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, newren

On Tue, May 2, 2023 at 3:20 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Calvin Wan <calvinwan@google.com> writes:
>
> > Strbuf is a basic structure that should function as a low-level library
> > due to how generic it is. Over time certain functions inside of
> > strbuf.[ch] have been added with dependencies to higher level objects
> > and functions. This series cleans up some of those higher level
> > dependencies by moving the offending functions to the files they
> > interact with. With the goal of eventually being able to stand up strbuf
> > as a libary, this series also removes the use of environment variables
> > from strbuf.
>
> I touched a bit of the same in my review for 1/6, but the main
> thrust of the series is that you want to make strbuf.[ch] mostly
> about "string processing primitives".

Thanks for clarifying the boundary I was attempting to explain in my
cover letter, but unsuccessfully did so.

>
> Any meaningful features relate to more than one conceptual things,
> e.g. pathname functions work on "paths" (that is one) and it may
> store computed result in a "strbuf" (that is another).  We have
> historically called them strbuf_do_something() and gave the pointer
> to a strbuf that receives the result.  And this series wants to
> reverse the course and want to see strbuf that is more pure.
>
> That's fine.  The strbuf has become so successful a data structure,
> its use has become as ubiquitous as a plain string or an integer in
> our codebase.
>
> But if we were moving in that direction, I have to wonder if some of
> these functions also need to be renamed to lose their strbuf_
> prefix.  A function that takes a pathname and creates a directory
> there is not called string_mkdir() just because it takes a pathname
> as a string.  A function that takes a string buffer and a path, and
> reads the symbolic link content is not called string_readlink() just
> because it learns the symlink target in a string buffer.  What their
> parameters mean matters a lot more than what type these parameters
> are represented as.

I agree that with the functions that are moved out from strbuf, they
should be refactored in the future to the form, taking absolute path
as an example, strbuf_addstr(sb, get_absolute_path(path)) or something
similar.

>
> With some other topics in flight, merging this to 'seen' and merging
> this to 'next' may require different merge fixes, but I'll manage.
>
> Thanks.
>

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-02 22:31   ` Junio C Hamano
@ 2023-05-02 23:51     ` Felipe Contreras
  0 siblings, 0 replies; 85+ messages in thread
From: Felipe Contreras @ 2023-05-02 23:51 UTC (permalink / raw)
  To: Junio C Hamano, Calvin Wan; +Cc: git, newren

Junio C Hamano wrote:
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > But if we were moving in that direction, I have to wonder if some of
> > these functions also need to be renamed to lose their strbuf_
> > prefix.
> 
> Just to avoid misunderstanding.  I do not mean to suggest renaming
> these inside this series.  It would make things too noisy and even
> more distracting.  But in the longer term, as we treat strbuf more
> and more as one of our basic data structures, it would make sense to
> lose strbuf_ from functions that are thrown out of strbuf.[ch] with
> this series, and reserve the prefix to functions that are left in
> strbuf.[ch], i.e. those that are about string operations.

I thought precisely the same thing: 1) it's good to move them away, 2)
they should lose the "strbuf_" prefix, 3) that doesn't need to happen in
this series.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 5/6] strbuf: clarify dependency
  2023-05-02 21:14 ` [PATCH 5/6] strbuf: clarify dependency Calvin Wan
@ 2023-05-03  1:56   ` Elijah Newren
  0 siblings, 0 replies; 85+ messages in thread
From: Elijah Newren @ 2023-05-03  1:56 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git

On Tue, May 2, 2023 at 2:15 PM Calvin Wan <calvinwan@google.com> wrote:
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>  strbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/strbuf.c b/strbuf.c
> index 178d75f250..d5978fee4e 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -3,7 +3,7 @@
>  #include "environment.h"
>  #include "gettext.h"
>  #include "hex.h"
> -#include "refs.h"
> +#include "strbuf.h"
>  #include "string-list.h"
>  #include "utf8.h"
>  #include "date.h"
> --
> 2.40.1.495.gc816e09b53d-goog

The commit message feels misleading.  A little digging shows that
refs.h was once upon a time needed, but no longer was as of
6bab74e7fb8 ("strbuf: move strbuf_branchname to sha1_name.c",
2010-11-06).  So, you're removing an unnecessary include, but adding
one back that was missing.  I guess that might count as "clarifying",
but maybe some of that extra context in the commit message would be
useful?

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 6/6] strbuf: remove environment variables
  2023-05-02 21:14 ` [PATCH 6/6] strbuf: remove environment variables Calvin Wan
@ 2023-05-03  2:15   ` Elijah Newren
  0 siblings, 0 replies; 85+ messages in thread
From: Elijah Newren @ 2023-05-03  2:15 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git

On Tue, May 2, 2023 at 2:15 PM Calvin Wan <calvinwan@google.com> wrote:
>
> As a lower level library, strbuf should not directly access environment
> variables within its functions. Therefore, add an additional variable to
> function signatures for functions that use an environment variable and
> refactor callers to pass in the environment variable.

Yaay!  I hate the pervasive implicit use of globals throughout the
codebase, and like seeing changes that at least make them less hidden.

I might have split this patch into three, one for each of the
functions you are changing.  The reason is just that it makes review
so much easier; a simple --color-words view then becomes easy
to rapidly scan through on each patch.  When one patch includes all
three, reviewers have to double check the position of the added
parameter in the argument list against the name of the function.  But
that's a really minor point, and some might prefer as you have it here
with the three squashed together.

[...]
> diff --git a/strbuf.h b/strbuf.h
> index b6d53c1cbe..f90da8859f 100644
> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -283,7 +283,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
>   * by a comment character and a blank.
>   */
>  void strbuf_add_commented_lines(struct strbuf *out,
> -                               const char *buf, size_t size);
> +                               const char *buf, size_t size,
> +                               char comment_line_char);
>
>
>  /**
> @@ -412,8 +413,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
>   * Add a formatted string prepended by a comment character and a
>   * blank to the buffer.
>   */
> -__attribute__((format (printf, 2, 3)))
> -void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
> +__attribute__((format (printf, 3, 4)))
> +void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
>
>  __attribute__((format (printf,2,0)))
>  void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
> @@ -538,7 +539,7 @@ int strbuf_normalize_path(struct strbuf *sb);
>   * Strip whitespace from a buffer. The second parameter controls if
>   * comments are considered contents to be removed or not.

Should this documentation string be updated, given that it previously
documented all arguments to the function?

>   */
> -void strbuf_stripspace(struct strbuf *buf, int skip_comments);
> +void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
>
>  static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
>  {
[...]

The rest looks good to me.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (6 preceding siblings ...)
  2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
@ 2023-05-03  2:37 ` Elijah Newren
  2023-05-03 18:00   ` Calvin Wan
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
  8 siblings, 1 reply; 85+ messages in thread
From: Elijah Newren @ 2023-05-03  2:37 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git

On Tue, May 2, 2023 at 2:15 PM Calvin Wan <calvinwan@google.com> wrote:
>
> Strbuf is a basic structure that should function as a low-level library
> due to how generic it is. Over time certain functions inside of
> strbuf.[ch] have been added with dependencies to higher level objects
> and functions. This series cleans up some of those higher level
> dependencies by moving the offending functions to the files they
> interact with. With the goal of eventually being able to stand up strbuf
> as a libary, this series also removes the use of environment variables
> from strbuf.
>
> Calvin Wan (6):
>   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 environment variables
>
>  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                   | 106 +++----------------------------------
>  strbuf.h                   |  44 ++-------------
>  tempfile.c                 |   1 +
>  wt-status.c                |   6 +--
>  26 files changed, 213 insertions(+), 187 deletions(-)
>
> --
> 2.40.1.495.gc816e09b53d-goog

The series looks pretty good to me.  I left a couple small comments on
5/6 and 6/6.  One other high-level note:

As Ævar noted over at
https://lore.kernel.org/git/230501.86a5yohsme.gmgdl@evledraar.gmail.com/,
strbuf_add_separated_string_list() is currently only used by
merge-ort.c and merge-recursive.c (both of which include
merge-recursive.h).  It may make sense to move that function, and that
would permit you to drop the forward declaration of 'struct
string_list;' from strbuf.c as well.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 2/6] credential-store: move related functions to credential-store file
  2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
  2023-05-02 21:52   ` Junio C Hamano
@ 2023-05-03 16:28   ` Jeff King
  2023-05-03 16:34     ` Jeff King
  1 sibling, 1 reply; 85+ messages in thread
From: Jeff King @ 2023-05-03 16:28 UTC (permalink / raw)
  To: Calvin Wan; +Cc: Junio C Hamano, git, newren

On Tue, May 02, 2023 at 09:14:50PM +0000, Calvin Wan wrote:

> is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
> called from builtin/credential-store.c and they are only relevant to that
> file so move those functions and make them static.

This is probably OK to do, though I think "and they are only relevant to
that file" is overstating things a bit.

They are callbacks to be used with strbuf_add_urlencode(). Originally
they were static-locals next to that function, with a flag bit to
trigger which was used.

Later, c2694952e3 (strbuf: give URL-encoding API a char predicate fn,
2019-06-27) replaced the flag with a callback mechanism, which meant the
functions had to be public. But all callers except the new one added in
that series still needed them (that other caller is not encoding a URL,
so it wants different rules).

And then finally, 644de29e22 (http: drop support for curl < 7.19.4,
2021-07-30) dropped the http.c callers, leaving only the ones in
credential-store.

So yes, it's true that only credential-store.c needs them right now. But
separating them from strbuf_add_urlencode() is making that function next
to useless for any future callers, as they'd have to reimplement the
logic about which characters are reserved. I say "next to useless",
because that one "not a URL" caller doesn't need that logic.

So I dunno. It's largely academic until somebody else needs to encode
bits of a URL. But when they do, the first thing they'd need to do is
make these functions public again.

I think the main reason we do not have other callers is that urlmatch.c
implements its own percent-encoding code, and that's where we do most of
our URL handling. It does make me wonder if credential-store could
simply switch to using that, but that is probably out of scope for your
series.

-Peff

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 2/6] credential-store: move related functions to credential-store file
  2023-05-03 16:28   ` Jeff King
@ 2023-05-03 16:34     ` Jeff King
  2023-05-03 18:38       ` Calvin Wan
  0 siblings, 1 reply; 85+ messages in thread
From: Jeff King @ 2023-05-03 16:34 UTC (permalink / raw)
  To: Calvin Wan; +Cc: Junio C Hamano, git, newren

On Wed, May 03, 2023 at 12:28:58PM -0400, Jeff King wrote:

> I think the main reason we do not have other callers is that urlmatch.c
> implements its own percent-encoding code, and that's where we do most of
> our URL handling. It does make me wonder if credential-store could
> simply switch to using that, but that is probably out of scope for your
> series.

Poking at it a bit, I think the answer is "not easily". credential-store
has broken-out elements of a URL, and wants to turn them back into one.
And urlmatch.c's url_normalize() always takes a URL string as input.
Even though it finds the broken-out fields as part of its work, the
parsing and reconstruction of the URL are inter-mingled in a complex
way (i.e., it's not just "parse it, then turn the fields back into a
normalized string).

I'm sure it could be refactored, but it's probably not worth it (and
again, this tangent is somewhat orthogonal to your series, except that
it would make these is_rfc3986 functions go away entirely).

-Peff

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-03  2:37 ` Elijah Newren
@ 2023-05-03 18:00   ` Calvin Wan
  2023-05-07  0:14     ` Elijah Newren
  0 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:00 UTC (permalink / raw)
  To: Elijah Newren; +Cc: git

While moving strbuf_add_separated_string_list() to a separate file
would mean that strbuf would no longer have a dependency on
string-list, I don't think that dependency is problematic to begin
with. Widening the boundary for strbuf as a string manipulation
library to a string and string list manipulation library seems
reasonable to me.


On Tue, May 2, 2023 at 7:37 PM Elijah Newren <newren@gmail.com> wrote:
>
> On Tue, May 2, 2023 at 2:15 PM Calvin Wan <calvinwan@google.com> wrote:
> >
> > Strbuf is a basic structure that should function as a low-level library
> > due to how generic it is. Over time certain functions inside of
> > strbuf.[ch] have been added with dependencies to higher level objects
> > and functions. This series cleans up some of those higher level
> > dependencies by moving the offending functions to the files they
> > interact with. With the goal of eventually being able to stand up strbuf
> > as a libary, this series also removes the use of environment variables
> > from strbuf.
> >
> > Calvin Wan (6):
> >   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 environment variables
> >
> >  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                   | 106 +++----------------------------------
> >  strbuf.h                   |  44 ++-------------
> >  tempfile.c                 |   1 +
> >  wt-status.c                |   6 +--
> >  26 files changed, 213 insertions(+), 187 deletions(-)
> >
> > --
> > 2.40.1.495.gc816e09b53d-goog
>
> The series looks pretty good to me.  I left a couple small comments on
> 5/6 and 6/6.  One other high-level note:
>
> As Ævar noted over at
> https://lore.kernel.org/git/230501.86a5yohsme.gmgdl@evledraar.gmail.com/,
> strbuf_add_separated_string_list() is currently only used by
> merge-ort.c and merge-recursive.c (both of which include
> merge-recursive.h).  It may make sense to move that function, and that
> would permit you to drop the forward declaration of 'struct
> string_list;' from strbuf.c as well.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 2/6] credential-store: move related functions to credential-store file
  2023-05-03 16:34     ` Jeff King
@ 2023-05-03 18:38       ` Calvin Wan
  0 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:38 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git, newren

Thanks for digging up the history and context for these functions. I
agree that refactoring these functions right now is not worth it and
can be saved for when there would be new callers to them.

On Wed, May 3, 2023 at 9:34 AM Jeff King <peff@peff.net> wrote:
>
> On Wed, May 03, 2023 at 12:28:58PM -0400, Jeff King wrote:
>
> > I think the main reason we do not have other callers is that urlmatch.c
> > implements its own percent-encoding code, and that's where we do most of
> > our URL handling. It does make me wonder if credential-store could
> > simply switch to using that, but that is probably out of scope for your
> > series.
>
> Poking at it a bit, I think the answer is "not easily". credential-store
> has broken-out elements of a URL, and wants to turn them back into one.
> And urlmatch.c's url_normalize() always takes a URL string as input.
> Even though it finds the broken-out fields as part of its work, the
> parsing and reconstruction of the URL are inter-mingled in a complex
> way (i.e., it's not just "parse it, then turn the fields back into a
> normalized string).
>
> I'm sure it could be refactored, but it's probably not worth it (and
> again, this tangent is somewhat orthogonal to your series, except that
> it would make these is_rfc3986 functions go away entirely).
>
> -Peff

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v2 0/7] strbuf cleanups
  2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
                   ` (7 preceding siblings ...)
  2023-05-03  2:37 ` Elijah Newren
@ 2023-05-03 18:48 ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 1/7] strbuf: clarify API boundary Calvin Wan
                     ` (9 more replies)
  8 siblings, 10 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Strbuf is a widely used basic structure that should only interact with
other primitives in strbuf.[ch]. Over time certain functions inside of
strbuf.[ch] have been added to interact with higher level objects and
functions. This series cleans up some of those higher level interactions
by moving the offending functions to the files they interact with and
adding documentation to strbuf.h. With the goal of eventually being able
to stand up strbuf as a libary, this series also removes the use of
environment variables from strbuf.

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 environment variables

 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                   | 106 +++----------------------------------
 strbuf.h                   |  53 +++++--------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +--
 26 files changed, 220 insertions(+), 189 deletions(-)

Range-diff against v1:
-:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary
1:  283771c088 ! 2:  ec1ea6ae4f abspath: move related functions to abspath
    @@ Commit message
         abspath: move related functions to abspath
     
         Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
    -    they do not belong in a low-level library.
    +    paths are not primitive objects and therefore strbuf should not interact
    +    with them.
     
      ## abspath.c ##
     @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
2:  58f78b8ae0 = 3:  2d74561b91 credential-store: move related functions to credential-store file
3:  88ab90c079 ! 4:  30b5e635cb object-name: move related functions to object-name
    @@ Commit message
         object-name: move related functions to object-name
     
         Move object-name-related functions from strbuf.[ch] to object-name.[ch]
    -    since they do not belong in a low-level library.
    +    since paths are not a primitive object that strbuf should directly
    +    interact with.
     
      ## object-name.c ##
     @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
4:  30b7de5a81 ! 5:  6905618470 path: move related function to path
    @@ Metadata
      ## Commit message ##
         path: move related function to path
     
    -    Move path-related function from strbuf.[ch] to path.[ch] since it does
    -    not belong in a low-level library.
    +    Move path-related function from strbuf.[ch] to path.[ch] since path is
    +    not a primitive object and therefore strbuf should not directly interact
    +    with it.
     
      ## path.c ##
     @@ path.c: int normalize_path_copy(char *dst, const char *src)
5:  7b6d6353de ! 6:  caf3482bf7 strbuf: clarify dependency
    @@ Metadata
      ## Commit message ##
         strbuf: clarify dependency
     
    +    refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
    +    move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
    +    included thru refs.h, so removing refs.h requires strbuf.h to be added
    +    back.
    +
      ## strbuf.c ##
     @@
      #include "environment.h"
6:  ffacd1cbe5 ! 7:  a7f23488f8 strbuf: remove enviroment variables
    @@ Metadata
     Author: Calvin Wan <calvinwan@google.com>
     
      ## Commit message ##
    -    strbuf: remove enviroment variables
    +    strbuf: remove environment variables
     
    -    As a lower level library, strbuf should not directly access environment
    -    variables within its functions. Therefore, add an additional variable to
    -    function signatures for functions that use an environment variable and
    -    refactor callers to pass in the environment variable.
    +    As a library that only interacts with other primitives, strbuf should
    +    not directly access environment variables within its
    +    functions. Therefore, add an additional variable to function signatures
    +    for functions that use an environment variable and refactor callers to
    +    pass in the environment variable.
     
      ## add-patch.c ##
     @@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
    @@ 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);
    + 
    + /**
       * 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.
    ++ * 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.
       */
     -void strbuf_stripspace(struct strbuf *buf, int skip_comments);
     +void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v2 1/7] strbuf: clarify API boundary
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 2/7] abspath: move related functions to abspath Calvin Wan
                     ` (8 subsequent siblings)
  9 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

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
functions that do not follow this boundary should eventually be moved or
refactored.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..c856253216 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -5,7 +5,11 @@ 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.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 2/7] abspath: move related functions to abspath
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 3/7] credential-store: move related functions to credential-store file Calvin Wan
                     ` (7 subsequent siblings)
  9 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
paths are not primitive objects and therefore strbuf should not interact
with them.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 76e322f580..2d8706371e 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..c3b6d48797 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index c856253216..20d0e37d9c 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -531,28 +531,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 3/7] credential-store: move related functions to credential-store file
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 1/7] strbuf: clarify API boundary Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 2/7] abspath: move related functions to abspath Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 4/7] object-name: move related functions to object-name Calvin Wan
                     ` (6 subsequent siblings)
  9 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 8977604eb9..4776118331 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index c3b6d48797..da2693b21f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -809,25 +809,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index 20d0e37d9c..9e52fe7706 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -686,9 +686,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 4/7] object-name: move related functions to object-name
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (2 preceding siblings ...)
  2023-05-03 18:50   ` [PATCH v2 3/7] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 5/7] path: move related function to path Calvin Wan
                     ` (5 subsequent siblings)
  9 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
since paths are not a primitive object that strbuf should directly
interact with.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 5 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/object-name.c b/object-name.c
index 538e8a8f62..c2e82aceea 100644
--- a/object-name.c
+++ b/object-name.c
@@ -766,6 +766,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index da2693b21f..6533559e95 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "refs.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1023,21 +1022,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 9e52fe7706..1bae7e0f47 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -612,16 +612,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 5/7] path: move related function to path
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (3 preceding siblings ...)
  2023-05-03 18:50   ` [PATCH v2 4/7] object-name: move related functions to object-name Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 18:50   ` [PATCH v2 6/7] strbuf: clarify dependency Calvin Wan
                     ` (4 subsequent siblings)
  9 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move path-related function from strbuf.[ch] to path.[ch] since path is
not a primitive object and therefore strbuf should not directly interact
with it.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 20 --------------------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 6533559e95..178d75f250 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1088,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 6/7] strbuf: clarify dependency
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (4 preceding siblings ...)
  2023-05-03 18:50   ` [PATCH v2 5/7] path: move related function to path Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 19:26     ` Junio C Hamano
  2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
                     ` (3 subsequent siblings)
  9 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
included thru refs.h, so removing refs.h requires strbuf.h to be added
back.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 178d75f250..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "refs.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v2 7/7] strbuf: remove environment variables
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (5 preceding siblings ...)
  2023-05-03 18:50   ` [PATCH v2 6/7] strbuf: clarify dependency Calvin Wan
@ 2023-05-03 18:50   ` Calvin Wan
  2023-05-03 19:24     ` Junio C Hamano
  2023-05-03 19:42     ` [PATCH v3 7/7] strbuf: remove environment variable Calvin Wan
  2023-05-05 22:33   ` [PATCH v2 0/7] strbuf cleanups Junio C Hamano
                     ` (2 subsequent siblings)
  9 siblings, 2 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 18:50 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

As a library that only interacts with other primitives, strbuf should
not directly access environment variables within its
functions. Therefore, add an additional variable to function signatures
for functions that use an environment variable and refactor callers to
pass in the environment variable.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 12 +++++++-----
 strbuf.h             | 12 +++++++-----
 wt-status.c          |  6 +++---
 16 files changed, 86 insertions(+), 60 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..306489db13 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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);
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..e52afcf4d7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, 1, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..6d3cc4d9fe 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, 0, comment_line_char);
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..0918b92752 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			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);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, 0, comment_line_char);
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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);
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..d092e06dc7 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	strbuf_stripspace(&todo_list.buf, 1, 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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..e182c2a14e 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf, mode == STRIP_COMMENTS,
+				  comment_line_char);
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..c713d14489 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag,comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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);
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..b625f17460 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	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);
 	/* 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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..c6027e73c1 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, 1, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..9675b62bcb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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);
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
+			  comment_line_char);
 	if (!skip_prefix(sb->buf, tmpl.buf, &start))
 		start = sb->buf;
 	strbuf_release(&tmpl);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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);
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..eba65ca421 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1054,7 +1055,8 @@ static size_t cleanup(char *line, size_t len)
  * Enable skip_comments to skip every line starting with comment
  * character.
  */
-void strbuf_stripspace(struct strbuf *sb, int skip_comments)
+void strbuf_stripspace(struct strbuf *sb, int skip_comments,
+		       char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
diff --git a/strbuf.h b/strbuf.h
index 1bae7e0f47..bc16fa4ef9 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -287,7 +287,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -416,8 +417,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -540,9 +541,10 @@ int strbuf_normalize_path(struct strbuf *sb);
 
 /**
  * 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.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 7/7] strbuf: remove environment variables
  2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
@ 2023-05-03 19:24     ` Junio C Hamano
  2023-05-03 19:41       ` Calvin Wan
  2023-05-03 19:42     ` [PATCH v3 7/7] strbuf: remove environment variable Calvin Wan
  1 sibling, 1 reply; 85+ messages in thread
From: Junio C Hamano @ 2023-05-03 19:24 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff

Calvin Wan <calvinwan@google.com> writes:

> As a library that only interacts with other primitives, strbuf should
> not directly access environment variables within its

"environment variables" is a misnomer, as there is no getenv()
removed by this patch.

    strbuf: stop depending on global 'comment_line_char'

or something is what you meant, I think.  This patch is about only
one single variable, not about many environment variables, right?

> functions. Therefore, add an additional variable to function signatures

These things that are enclosed in a pair of () after the function
definition are usually called parameters.

> for functions that use an environment variable and refactor callers to
> pass in the environment variable.

Likewise.

> -				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
> +				strbuf_commented_addf(&buf, comment_line_char,
> +				      _(tag_template), tag,comment_line_char);

Style?

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 6/7] strbuf: clarify dependency
  2023-05-03 18:50   ` [PATCH v2 6/7] strbuf: clarify dependency Calvin Wan
@ 2023-05-03 19:26     ` Junio C Hamano
  0 siblings, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-03 19:26 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff

Calvin Wan <calvinwan@google.com> writes:

> refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
> move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
> included thru refs.h, so removing refs.h requires strbuf.h to be added
> back.

OK.  Will queue.  Thanks.

>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>  strbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/strbuf.c b/strbuf.c
> index 178d75f250..d5978fee4e 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -3,7 +3,7 @@
>  #include "environment.h"
>  #include "gettext.h"
>  #include "hex.h"
> -#include "refs.h"
> +#include "strbuf.h"
>  #include "string-list.h"
>  #include "utf8.h"
>  #include "date.h"

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 7/7] strbuf: remove environment variables
  2023-05-03 19:24     ` Junio C Hamano
@ 2023-05-03 19:41       ` Calvin Wan
  2023-05-03 19:45         ` Junio C Hamano
  0 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 19:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, newren, peff

I'll reword and reroll just this patch real quick.


On Wed, May 3, 2023 at 12:24 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Calvin Wan <calvinwan@google.com> writes:
>
> > As a library that only interacts with other primitives, strbuf should
> > not directly access environment variables within its
>
> "environment variables" is a misnomer, as there is no getenv()
> removed by this patch.
>
>     strbuf: stop depending on global 'comment_line_char'
>
> or something is what you meant, I think.  This patch is about only
> one single variable, not about many environment variables, right?
>
> > functions. Therefore, add an additional variable to function signatures
>
> These things that are enclosed in a pair of () after the function
> definition are usually called parameters.
>
> > for functions that use an environment variable and refactor callers to
> > pass in the environment variable.
>
> Likewise.
>
> > -                             strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
> > +                             strbuf_commented_addf(&buf, comment_line_char,
> > +                                   _(tag_template), tag,comment_line_char);
>
> Style?

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v3 7/7] strbuf: remove environment variable
  2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
  2023-05-03 19:24     ` Junio C Hamano
@ 2023-05-03 19:42     ` Calvin Wan
  1 sibling, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-03 19:42 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

As a library that only interacts with other primitives, strbuf should
not utilize the comment_line_char environment variable within its
functions. Therefore, add an additional parameter for functions that use
comment_line_char and refactor callers to pass it in instead.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 12 +++++++-----
 strbuf.h             | 12 +++++++-----
 wt-status.c          |  6 +++---
 16 files changed, 86 insertions(+), 60 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..306489db13 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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);
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..e52afcf4d7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, 1, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..6d3cc4d9fe 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, 0, comment_line_char);
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..0918b92752 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			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);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, 0, comment_line_char);
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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);
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..d092e06dc7 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	strbuf_stripspace(&todo_list.buf, 1, 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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..e182c2a14e 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf, mode == STRIP_COMMENTS,
+				  comment_line_char);
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..e7e2511f0c 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag, comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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);
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..b625f17460 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	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);
 	/* 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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..c6027e73c1 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, 1, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..9675b62bcb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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);
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
+			  comment_line_char);
 	if (!skip_prefix(sb->buf, tmpl.buf, &start))
 		start = sb->buf;
 	strbuf_release(&tmpl);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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);
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..eba65ca421 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1054,7 +1055,8 @@ static size_t cleanup(char *line, size_t len)
  * Enable skip_comments to skip every line starting with comment
  * character.
  */
-void strbuf_stripspace(struct strbuf *sb, int skip_comments)
+void strbuf_stripspace(struct strbuf *sb, int skip_comments,
+		       char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
diff --git a/strbuf.h b/strbuf.h
index 1bae7e0f47..bc16fa4ef9 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -287,7 +287,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -416,8 +417,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -540,9 +541,10 @@ int strbuf_normalize_path(struct strbuf *sb);
 
 /**
  * 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.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 7/7] strbuf: remove environment variables
  2023-05-03 19:41       ` Calvin Wan
@ 2023-05-03 19:45         ` Junio C Hamano
  0 siblings, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-03 19:45 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff

Calvin Wan <calvinwan@google.com> writes:

> I'll reword and reroll just this patch real quick.

No need to be "real quick".  Other patches looked like they were
reasonably well proofread before they were sent, but I somehow got
an impression that this one (and possibly your v3---I saw its
presence but haven't read it yet) was rushed.

Thanks.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 0/7] strbuf cleanups
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (6 preceding siblings ...)
  2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
@ 2023-05-05 22:33   ` Junio C Hamano
  2023-05-08 16:38     ` Calvin Wan
  2023-05-07  0:40   ` Elijah Newren
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
  9 siblings, 1 reply; 85+ messages in thread
From: Junio C Hamano @ 2023-05-05 22:33 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff

Calvin Wan <calvinwan@google.com> writes:

> Strbuf is a widely used basic structure that should only interact with
> other primitives in strbuf.[ch]. Over time certain functions inside of
> strbuf.[ch] have been added to interact with higher level objects and
> functions. This series cleans up some of those higher level interactions
> by moving the offending functions to the files they interact with and
> adding documentation to strbuf.h. With the goal of eventually being able
> to stand up strbuf as a libary, this series also removes the use of
> environment variables from strbuf.

This round hasn't seen any comments (mine does not count ;-).  The
7/7-only v3 still says "environment variable" to refer to a global
variable, so I am not sure where we stand.  Is this back-burnered?


^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-03 18:00   ` Calvin Wan
@ 2023-05-07  0:14     ` Elijah Newren
  2023-05-07 13:14       ` Jeff King
  0 siblings, 1 reply; 85+ messages in thread
From: Elijah Newren @ 2023-05-07  0:14 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, Jeff King

On Wed, May 3, 2023 at 11:00 AM Calvin Wan <calvinwan@google.com> wrote:
>
> While moving strbuf_add_separated_string_list() to a separate file
> would mean that strbuf would no longer have a dependency on
> string-list, I don't think that dependency is problematic to begin
> with. Widening the boundary for strbuf as a string manipulation
> library to a string and string list manipulation library seems
> reasonable to me.

Oh, the high level idea behind string-list might make sense at this
level, but I was assuming Peff would show up at some point and
highlight the evils of the current string-list API[1][2][3] and how we
should avoid using, depending on, or implementing something that acts
like it.  :-)

Of course, you're explicitly not trying to make any API or ABI
guarantees, so it's certainly fine to shelve or defer such cleanups
for later.

[1] https://lore.kernel.org/git/Y7lx1hUpZ7zOP1Lo@coredump.intra.peff.net/
[2] https://lore.kernel.org/git/20180906191203.GA26184@sigill.intra.peff.net/
[3] https://lore.kernel.org/git/20200821200121.GF1165@coredump.intra.peff.net/

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 0/7] strbuf cleanups
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (7 preceding siblings ...)
  2023-05-05 22:33   ` [PATCH v2 0/7] strbuf cleanups Junio C Hamano
@ 2023-05-07  0:40   ` Elijah Newren
  2023-05-07 21:47     ` Felipe Contreras
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
  9 siblings, 1 reply; 85+ messages in thread
From: Elijah Newren @ 2023-05-07  0:40 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, peff

On Wed, May 3, 2023 at 11:48 AM Calvin Wan <calvinwan@google.com> wrote:
>
> Strbuf is a widely used basic structure that should only interact with
> other primitives in strbuf.[ch].

"should"?  I agree with moving things in this direction, but the
wording here suggests there's a pre-existing directive or guideline;
but if so, I'm unfamiliar with it.  Maybe this should be reworded to
"This series modifies strbuf to focus on string manipulation with
minimal other dependencies"?

> Over time certain functions inside of
> strbuf.[ch] have been added to interact with higher level objects and
> functions. This series cleans up some of those higher level interactions
> by moving the offending functions to the files they interact with and
> adding documentation to strbuf.h. With the goal of eventually being able
> to stand up strbuf as a libary, this series also removes the use of
> environment variables from strbuf.

As Junio pointed out, "environment variables" is misleading; see below.

> Range-diff against v1:
> -:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary

I read this separately; this patch looks good to me.

> 1:  283771c088 ! 2:  ec1ea6ae4f abspath: move related functions to abspath
>     @@ Commit message
>          abspath: move related functions to abspath
>
>          Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
>     -    they do not belong in a low-level library.
>     +    paths are not primitive objects and therefore strbuf should not interact
>     +    with them.

This applies to patches 4 & 5 as well:

Would this perhaps be better worded as:

    Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
    strbuf is focused on string manipulation routines with minimal dependencies.

?

>       ## abspath.c ##
>      @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
> 2:  58f78b8ae0 = 3:  2d74561b91 credential-store: move related functions to credential-store file
> 3:  88ab90c079 ! 4:  30b5e635cb object-name: move related functions to object-name
>     @@ Commit message
>          object-name: move related functions to object-name
>
>          Move object-name-related functions from strbuf.[ch] to object-name.[ch]
>     -    since they do not belong in a low-level library.
>     +    since paths are not a primitive object that strbuf should directly
>     +    interact with.
>
>       ## object-name.c ##
>      @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
> 4:  30b7de5a81 ! 5:  6905618470 path: move related function to path
>     @@ Metadata
>       ## Commit message ##
>          path: move related function to path
>
>     -    Move path-related function from strbuf.[ch] to path.[ch] since it does
>     -    not belong in a low-level library.
>     +    Move path-related function from strbuf.[ch] to path.[ch] since path is
>     +    not a primitive object and therefore strbuf should not directly interact
>     +    with it.
>
>       ## path.c ##
>      @@ path.c: int normalize_path_copy(char *dst, const char *src)
> 5:  7b6d6353de ! 6:  caf3482bf7 strbuf: clarify dependency
>     @@ Metadata
>       ## Commit message ##
>          strbuf: clarify dependency
>
>     +    refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
>     +    move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
>     +    included thru refs.h, so removing refs.h requires strbuf.h to be added
>     +    back.
>     +

Thanks.

> 6:  ffacd1cbe5 ! 7:  a7f23488f8 strbuf: remove enviroment variables
>     @@ Metadata
>      Author: Calvin Wan <calvinwan@google.com>
>
>       ## Commit message ##
>     -    strbuf: remove enviroment variables
>     +    strbuf: remove environment variables
>
>     -    As a lower level library, strbuf should not directly access environment
>     -    variables within its functions. Therefore, add an additional variable to
>     -    function signatures for functions that use an environment variable and
>     -    refactor callers to pass in the environment variable.
>     +    As a library that only interacts with other primitives, strbuf should
>     +    not directly access environment variables within its
>     +    functions. Therefore, add an additional variable to function signatures
>     +    for functions that use an environment variable and refactor callers to
>     +    pass in the environment variable.

"environment variable" makes one think not of "variable from
environment.c [that happens to be a global]" but of
https://en.wikipedia.org/wiki/Environment_variable.  The fact that the
variable is defined in environment.c isn't even relevant here, though,
while the fact that it is a global is relevant.  I'd just
s/environment variable/global variable/ in all 4 places (er, 5 if you
include the cover letter).

Your v3 7/7 has an important correction, but you didn't send out a
range diff for v3 so I'm putting my comments on your v2 range-diff.
:-)

>     + /**
>        * 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.
>     ++ * 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.
>        */

Thanks.


This series fixes two of my comments on v1, and the other was just
"here's another cleanup that could be added" -- it's fine to punt on.

I only had a couple minor comments on this v2/v3 that should be easy
to fix up, all included in this response to the cover letter.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH 0/6] strbuf cleanups
  2023-05-07  0:14     ` Elijah Newren
@ 2023-05-07 13:14       ` Jeff King
  0 siblings, 0 replies; 85+ messages in thread
From: Jeff King @ 2023-05-07 13:14 UTC (permalink / raw)
  To: Elijah Newren; +Cc: Calvin Wan, git

On Sat, May 06, 2023 at 05:14:55PM -0700, Elijah Newren wrote:

> On Wed, May 3, 2023 at 11:00 AM Calvin Wan <calvinwan@google.com> wrote:
> >
> > While moving strbuf_add_separated_string_list() to a separate file
> > would mean that strbuf would no longer have a dependency on
> > string-list, I don't think that dependency is problematic to begin
> > with. Widening the boundary for strbuf as a string manipulation
> > library to a string and string list manipulation library seems
> > reasonable to me.
> 
> Oh, the high level idea behind string-list might make sense at this
> level, but I was assuming Peff would show up at some point and
> highlight the evils of the current string-list API[1][2][3] and how we
> should avoid using, depending on, or implementing something that acts
> like it.  :-)

You rang? :)

Yes, IMHO this strbuf_add_separated_string_list() is another example of
why string-list sucks: it doesn't degrade to a natural array type
(because the "util" magic requires a struct). If it were a strvec or
similar, we could just pass "const char **str, size_t len", which would
make the helper function simpler and more generally useful.

I know there may be other reasons to use a string-list in the caller
here, though (looks like it uses "nodup"). So as usual, the situation is
not so simple as "we should just switch types".

-Peff

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 0/7] strbuf cleanups
  2023-05-07  0:40   ` Elijah Newren
@ 2023-05-07 21:47     ` Felipe Contreras
  0 siblings, 0 replies; 85+ messages in thread
From: Felipe Contreras @ 2023-05-07 21:47 UTC (permalink / raw)
  To: Elijah Newren, Calvin Wan; +Cc: git, peff

Elijah Newren wrote:
> On Wed, May 3, 2023 at 11:48 AM Calvin Wan <calvinwan@google.com> wrote:
> >
> > Strbuf is a widely used basic structure that should only interact with
> > other primitives in strbuf.[ch].
> 
> "should"?  I agree with moving things in this direction, but the
> wording here suggests there's a pre-existing directive or guideline;

No, it doesn't.

"You should see a doctor about that cough" is not a directive, it's merely an
advice.

> Maybe this should be reworded to "This series modifies strbuf to focus on
> string manipulation with minimal other dependencies"?

And ironoically you use the word "should" to provide your own advice on how to
avoid using "should" as an advice.

I agree it would be ideal if strbuf interacted only with other primitives in
strbuf.[ch], so I agree "should" is a perfectly fine word to describe such a
normative statement.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v2 0/7] strbuf cleanups
  2023-05-05 22:33   ` [PATCH v2 0/7] strbuf cleanups Junio C Hamano
@ 2023-05-08 16:38     ` Calvin Wan
  0 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:38 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, newren, peff

I intend to reroll it, as well as rewording the commit patches 4 and 5
as per Elijah's suggestions. I didn't originally realize you were
referring to s/environment variable/global variable as the misnomer
(since the dependency I was removing was from environment.h)

On Fri, May 5, 2023 at 3:33 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Calvin Wan <calvinwan@google.com> writes:
>
> > Strbuf is a widely used basic structure that should only interact with
> > other primitives in strbuf.[ch]. Over time certain functions inside of
> > strbuf.[ch] have been added to interact with higher level objects and
> > functions. This series cleans up some of those higher level interactions
> > by moving the offending functions to the files they interact with and
> > adding documentation to strbuf.h. With the goal of eventually being able
> > to stand up strbuf as a libary, this series also removes the use of
> > environment variables from strbuf.
>
> This round hasn't seen any comments (mine does not count ;-).  The
> 7/7-only v3 still says "environment variable" to refer to a global
> variable, so I am not sure where we stand.  Is this back-burnered?
>

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v4 0/7] strbuf cleanups
  2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
                     ` (8 preceding siblings ...)
  2023-05-07  0:40   ` Elijah Newren
@ 2023-05-08 16:57   ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
                       ` (8 more replies)
  9 siblings, 9 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:57 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

This reroll include suggestions made by Elijah and Junio, clarifying
commit messages in patches 2/4/5 and s/environment variable/global
variable 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                   | 106 +++----------------------------------
 strbuf.h                   |  53 +++++--------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +--
 26 files changed, 220 insertions(+), 189 deletions(-)

Range-diff against v3:
-:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary
1:  ec1ea6ae4f ! 2:  48fb5db28b abspath: move related functions to abspath
    @@ Metadata
      ## Commit message ##
         abspath: move related functions to abspath
     
    -    Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
    -    paths are not primitive objects and therefore strbuf should not interact
    -    with them.
    +    Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
    +    strbuf is focused on string manipulation routines with minimal
    +    dependencies.
     
      ## abspath.c ##
     @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
2:  2d74561b91 = 3:  a663f91819 credential-store: move related functions to credential-store file
3:  30b5e635cb ! 4:  ccef9dd5f2 object-name: move related functions to object-name
    @@ Commit message
         object-name: move related functions to object-name
     
         Move object-name-related functions from strbuf.[ch] to object-name.[ch]
    -    since paths are not a primitive object that strbuf should directly
    -    interact with.
    +    so that strbuf is focused on string manipulation routines with minimal
    +    dependencies.
     
      ## object-name.c ##
     @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
4:  6905618470 ! 5:  0d6b9cf0f7 path: move related function to path
    @@ Metadata
      ## Commit message ##
         path: move related function to path
     
    -    Move path-related function from strbuf.[ch] to path.[ch] since path is
    -    not a primitive object and therefore strbuf should not directly interact
    -    with it.
    +    Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
    +    is focused on string manipulation routines with minimal dependencies.
     
      ## path.c ##
     @@ path.c: int normalize_path_copy(char *dst, const char *src)
5:  caf3482bf7 = 6:  5655c56a6d strbuf: clarify dependency
6:  3bbaebf292 ! 7:  874d0efac3 strbuf: remove environment variable
    @@ Metadata
     Author: Calvin Wan <calvinwan@google.com>
     
      ## Commit message ##
    -    strbuf: remove environment variable
    +    strbuf: remove global variable
     
         As a library that only interacts with other primitives, strbuf should
    -    not utilize the comment_line_char environment variable within its
    +    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.
     
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v4 1/7] strbuf: clarify API boundary
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 17:22       ` Eric Sunshine
  2023-05-08 16:59     ` [PATCH v4 2/7] abspath: move related functions to abspath Calvin Wan
                       ` (7 subsequent siblings)
  8 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

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
functions that do not follow this boundary should eventually be moved or
refactored.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..c856253216 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -5,7 +5,11 @@ 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.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 2/7] abspath: move related functions to abspath
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 3/7] credential-store: move related functions to credential-store file Calvin Wan
                       ` (6 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 76e322f580..2d8706371e 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..c3b6d48797 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index c856253216..20d0e37d9c 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -531,28 +531,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 3/7] credential-store: move related functions to credential-store file
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 2/7] abspath: move related functions to abspath Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 4/7] object-name: move related functions to object-name Calvin Wan
                       ` (5 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 8977604eb9..4776118331 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index c3b6d48797..da2693b21f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -809,25 +809,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index 20d0e37d9c..9e52fe7706 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -686,9 +686,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 4/7] object-name: move related functions to object-name
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (2 preceding siblings ...)
  2023-05-08 16:59     ` [PATCH v4 3/7] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 5/7] path: move related function to path Calvin Wan
                       ` (4 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
so that strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 5 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/object-name.c b/object-name.c
index 538e8a8f62..c2e82aceea 100644
--- a/object-name.c
+++ b/object-name.c
@@ -766,6 +766,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index da2693b21f..6533559e95 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "refs.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1023,21 +1022,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 9e52fe7706..1bae7e0f47 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -612,16 +612,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 5/7] path: move related function to path
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (3 preceding siblings ...)
  2023-05-08 16:59     ` [PATCH v4 4/7] object-name: move related functions to object-name Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 6/7] strbuf: clarify dependency Calvin Wan
                       ` (3 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
is focused on string manipulation routines with minimal dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 20 --------------------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 6533559e95..178d75f250 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1088,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 6/7] strbuf: clarify dependency
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (4 preceding siblings ...)
  2023-05-08 16:59     ` [PATCH v4 5/7] path: move related function to path Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-08 16:59     ` [PATCH v4 7/7] strbuf: remove global variable Calvin Wan
                       ` (2 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
included thru refs.h, so removing refs.h requires strbuf.h to be added
back.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 178d75f250..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "refs.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v4 7/7] strbuf: remove global variable
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (5 preceding siblings ...)
  2023-05-08 16:59     ` [PATCH v4 6/7] strbuf: clarify dependency Calvin Wan
@ 2023-05-08 16:59     ` Calvin Wan
  2023-05-10  8:12       ` Phillip Wood
  2023-05-09  1:57     ` [PATCH v4 0/7] strbuf cleanups Elijah Newren
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
  8 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-08 16:59 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff

As a library that only interacts with other primitives, strbuf should
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.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 12 +++++++-----
 strbuf.h             | 12 +++++++-----
 wt-status.c          |  6 +++---
 16 files changed, 86 insertions(+), 60 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..306489db13 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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);
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..e52afcf4d7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, 1, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..6d3cc4d9fe 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, 0, comment_line_char);
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..0918b92752 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			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);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, 0, comment_line_char);
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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);
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..d092e06dc7 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	strbuf_stripspace(&todo_list.buf, 1, 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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..e182c2a14e 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf, mode == STRIP_COMMENTS,
+				  comment_line_char);
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..e7e2511f0c 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag, comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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);
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..b625f17460 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	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);
 	/* 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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..c6027e73c1 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, 1, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..9675b62bcb 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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);
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL,
+			  comment_line_char);
 	if (!skip_prefix(sb->buf, tmpl.buf, &start))
 		start = sb->buf;
 	strbuf_release(&tmpl);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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);
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..eba65ca421 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1054,7 +1055,8 @@ static size_t cleanup(char *line, size_t len)
  * Enable skip_comments to skip every line starting with comment
  * character.
  */
-void strbuf_stripspace(struct strbuf *sb, int skip_comments)
+void strbuf_stripspace(struct strbuf *sb, int skip_comments,
+		       char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
diff --git a/strbuf.h b/strbuf.h
index 1bae7e0f47..bc16fa4ef9 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -287,7 +287,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -416,8 +417,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -540,9 +541,10 @@ int strbuf_normalize_path(struct strbuf *sb);
 
 /**
  * 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.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, int skip_comments, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.521.gf1e218fcd8-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH v4 1/7] strbuf: clarify API boundary
  2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-08 17:22       ` Eric Sunshine
  2023-05-10 22:51         ` Junio C Hamano
  0 siblings, 1 reply; 85+ messages in thread
From: Eric Sunshine @ 2023-05-08 17:22 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff

On Mon, May 8, 2023 at 1:05 PM Calvin Wan <calvinwan@google.com> wrote:
> strbuf, as a generic and widely used structure across the codebase,
> should be limited as a libary to only interact with primitives. Add

s/libary/library/

> documentation so future functions can be appropriately be placed. Older

Too many "be"'s.

> functions that do not follow this boundary should eventually be moved or
> refactored.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
> diff --git a/strbuf.h b/strbuf.h
> @@ -5,7 +5,11 @@ 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.

The new text is administrative in nature, aimed at people who will be
modifying strbuf itself. As such, it is unclear why it is being
inserted into documentation aimed at _consumers_ of the strbuf API.
Moreover, with it buried in existing API documentation like this, I
fear that those at whom it is aimed will almost certainly overlook it.

To increase the likelihood that the target audience will indeed read
the new text, I'd suggest placing it in its own comment block very
near the top of the file, possibly prefixed with a loud "NOTE FOR
STRBUF DEVELOPERS" or some such. Further, as the new text is aimed at
strbuf developers, not strbuf consumers, it would make more sense to
use a plain /*...*/ comment block rather than a /**...*/ block.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v4 0/7] strbuf cleanups
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (6 preceding siblings ...)
  2023-05-08 16:59     ` [PATCH v4 7/7] strbuf: remove global variable Calvin Wan
@ 2023-05-09  1:57     ` Elijah Newren
  2023-05-09  2:13       ` Felipe Contreras
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
  8 siblings, 1 reply; 85+ messages in thread
From: Elijah Newren @ 2023-05-09  1:57 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, peff

On Mon, May 8, 2023 at 9:57 AM Calvin Wan <calvinwan@google.com> wrote:
>
> This reroll include suggestions made by Elijah and Junio, clarifying
> commit messages in patches 2/4/5 and s/environment variable/global
> variable 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                   | 106 +++----------------------------------
>  strbuf.h                   |  53 +++++--------------
>  tempfile.c                 |   1 +
>  wt-status.c                |   6 +--
>  26 files changed, 220 insertions(+), 189 deletions(-)
>
> Range-diff against v3:
> -:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary

Huh?  I thought v3 had this patch.  v2 certainly did, and with the
same contents.  Did you just give the wrong range for the range-diff?

Aside: Can I plug gitgitgadget for a minute?  It
  * handles the range-diff consistently
  * sets up reply-to nicely
  * ensures testing on a variety of platforms
  * makes it _trivial_ to download the series via a simple fetch, with
remote/branch/command documented in the cover letter

I think the last point is particularly cool.  Yes, I know of b4, and
it helps a lot, but a simple fetch, especially one where the cover
letter contains the command, makes things easier for someone who comes
along to review.  I know I'm happier as a reviewer when I see a patch
series sent by gitgitgadget, because of this last point.  (Granted,
folks could just copy that really nice usability touch by hand in
their cover letters if they like send-email, but I suspect it's enough
of a hassle to the sender that people just don't do it.).  Anyway,
just some food for thought.

> 1:  ec1ea6ae4f ! 2:  48fb5db28b abspath: move related functions to abspath
>     @@ Metadata
>       ## Commit message ##
>          abspath: move related functions to abspath
>
>     -    Move abspath-related functions from strbuf.[ch] to abspath.[ch] since
>     -    paths are not primitive objects and therefore strbuf should not interact
>     -    with them.
>     +    Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
>     +    strbuf is focused on string manipulation routines with minimal
>     +    dependencies.
>
>       ## abspath.c ##
>      @@ abspath.c: char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
> 2:  2d74561b91 = 3:  a663f91819 credential-store: move related functions to credential-store file
> 3:  30b5e635cb ! 4:  ccef9dd5f2 object-name: move related functions to object-name
>     @@ Commit message
>          object-name: move related functions to object-name
>
>          Move object-name-related functions from strbuf.[ch] to object-name.[ch]
>     -    since paths are not a primitive object that strbuf should directly
>     -    interact with.
>     +    so that strbuf is focused on string manipulation routines with minimal
>     +    dependencies.
>
>       ## object-name.c ##
>      @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
> 4:  6905618470 ! 5:  0d6b9cf0f7 path: move related function to path
>     @@ Metadata
>       ## Commit message ##
>          path: move related function to path
>
>     -    Move path-related function from strbuf.[ch] to path.[ch] since path is
>     -    not a primitive object and therefore strbuf should not directly interact
>     -    with it.
>     +    Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
>     +    is focused on string manipulation routines with minimal dependencies.
>
>       ## path.c ##
>      @@ path.c: int normalize_path_copy(char *dst, const char *src)
> 5:  caf3482bf7 = 6:  5655c56a6d strbuf: clarify dependency
> 6:  3bbaebf292 ! 7:  874d0efac3 strbuf: remove environment variable
>     @@ Metadata
>      Author: Calvin Wan <calvinwan@google.com>
>
>       ## Commit message ##
>     -    strbuf: remove environment variable
>     +    strbuf: remove global variable
>
>          As a library that only interacts with other primitives, strbuf should
>     -    not utilize the comment_line_char environment variable within its
>     +    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.
>
> --
> 2.40.1.521.gf1e218fcd8-goog

Other than the off-by-one in the range-diff of the cover letter, this
all looks good to me:

Reviewed-by: Elijah Newren <newren@gmail.com>

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v4 0/7] strbuf cleanups
  2023-05-09  1:57     ` [PATCH v4 0/7] strbuf cleanups Elijah Newren
@ 2023-05-09  2:13       ` Felipe Contreras
  0 siblings, 0 replies; 85+ messages in thread
From: Felipe Contreras @ 2023-05-09  2:13 UTC (permalink / raw)
  To: Elijah Newren, Calvin Wan; +Cc: git, peff

Elijah Newren wrote:
> On Mon, May 8, 2023 at 9:57 AM Calvin Wan <calvinwan@google.com> wrote:

> > Range-diff against v3:
> > -:  ---------- > 1:  e0dd3f5295 strbuf: clarify API boundary
> 
> Huh?  I thought v3 had this patch.  v2 certainly did, and with the
> same contents.  Did you just give the wrong range for the range-diff?
> 
> Aside: Can I plug gitgitgadget for a minute?

I guess I can plug git-send-series [1] as well.

>   * handles the range-diff consistently
>   * sets up reply-to nicely

Same with git-send-email.

>   * ensures testing on a variety of platforms
>   * makes it _trivial_ to download the series via a simple fetch, with
> remote/branch/command documented in the cover letter

This is orthogonal to sending a patch series, but anyone pushing to
GitHub can do that.

Advantages of git-send-email that gitgitgadget doesn't have:

 * Everything about it can be handled locally
 * It's a standalone script fully in your control
 * It's not tied to any organization or corporation

[1] https://github.com/felipec/git-send-series

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v4 7/7] strbuf: remove global variable
  2023-05-08 16:59     ` [PATCH v4 7/7] strbuf: remove global variable Calvin Wan
@ 2023-05-10  8:12       ` Phillip Wood
  0 siblings, 0 replies; 85+ messages in thread
From: Phillip Wood @ 2023-05-10  8:12 UTC (permalink / raw)
  To: Calvin Wan, git; +Cc: newren, peff

Hi Calvin

On 08/05/2023 17:59, Calvin Wan wrote:
> As a library that only interacts with other primitives, strbuf should
> 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.

I find the revised subject and commit message much easier to understand.

> diff --git a/strbuf.c b/strbuf.c
> index d5978fee4e..eba65ca421 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -1,6 +1,5 @@
>   #include "git-compat-util.h"
>   #include "alloc.h"
> -#include "environment.h"
>   #include "gettext.h"
>   #include "hex.h"
>   #include "strbuf.h"
> @@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
>   	strbuf_complete_line(out);
>   }
>   
> -void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
> +void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
> +				size_t size, char comment_line_char)

I don't really object to this change as I can understand why you are 
making it, but it does make this function more cumbersome to use within 
git itself where we now have to pass the global comment_line_char 
explicitly.

> @@ -1054,7 +1055,8 @@ static size_t cleanup(char *line, size_t len)
>    * Enable skip_comments to skip every line starting with comment
>    * character.
>    */
> -void strbuf_stripspace(struct strbuf *sb, int skip_comments)
> +void strbuf_stripspace(struct strbuf *sb, int skip_comments,
> +		       char comment_line_char)

Rather than adding a new parameter here could we change the signature to

	void strbuf_stripspace(struct strbuf *sb, char comment_char)

and not strip comments if comment_char == '\0'? There doesn't seem much 
point in forcing callers to pass comment_line_char when they don't want 
to strip comments.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v4 1/7] strbuf: clarify API boundary
  2023-05-08 17:22       ` Eric Sunshine
@ 2023-05-10 22:51         ` Junio C Hamano
  0 siblings, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-10 22:51 UTC (permalink / raw)
  To: Calvin Wan, Eric Sunshine; +Cc: git, newren, peff

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Mon, May 8, 2023 at 1:05 PM Calvin Wan <calvinwan@google.com> wrote:
>> strbuf, as a generic and widely used structure across the codebase,
>> should be limited as a libary to only interact with primitives. Add
>
> s/libary/library/
>
>> documentation so future functions can be appropriately be placed. Older
>
> Too many "be"'s.
>
>> functions that do not follow this boundary should eventually be moved or
>> refactored.
>>
>> Signed-off-by: Calvin Wan <calvinwan@google.com>
>> ---
>> diff --git a/strbuf.h b/strbuf.h
>> @@ -5,7 +5,11 @@ 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.
>
> The new text is administrative in nature, aimed at people who will be
> modifying strbuf itself. As such, it is unclear why it is being
> inserted into documentation aimed at _consumers_ of the strbuf API.
> Moreover, with it buried in existing API documentation like this, I
> fear that those at whom it is aimed will almost certainly overlook it.
>
> To increase the likelihood that the target audience will indeed read
> the new text, I'd suggest placing it in its own comment block very
> near the top of the file, possibly prefixed with a loud "NOTE FOR
> STRBUF DEVELOPERS" or some such. Further, as the new text is aimed at
> strbuf developers, not strbuf consumers, it would make more sense to
> use a plain /*...*/ comment block rather than a /**...*/ block.

All look good suggestions to make.

If there is nothing else outstanding, let's see a small and
hopefully final reroll so that the topic can be merged to 'next'
soonish.

Thanks.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v5 0/7] strbuf cleanups
  2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
                       ` (7 preceding siblings ...)
  2023-05-09  1:57     ` [PATCH v4 0/7] strbuf cleanups Elijah Newren
@ 2023-05-11 19:44     ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
                         ` (7 more replies)
  8 siblings, 8 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:44 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

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


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v5 1/7] strbuf: clarify API boundary
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:57         ` Eric Sunshine
  2023-05-11 19:48       ` [PATCH v5 2/7] abspath: move related functions to abspath Calvin Wan
                         ` (6 subsequent siblings)
  7 siblings, 1 reply; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

strbuf, as a generic and widely used structure across the codebase,
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.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..0256114002 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -1,6 +1,15 @@
 #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;
 
 /**
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 2/7] abspath: move related functions to abspath
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 3/7] credential-store: move related functions to credential-store file Calvin Wan
                         ` (5 subsequent siblings)
  7 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 76e322f580..2d8706371e 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..c3b6d48797 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index 0256114002..e1c0c326f2 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -536,28 +536,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 3/7] credential-store: move related functions to credential-store file
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 2/7] abspath: move related functions to abspath Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 4/7] object-name: move related functions to object-name Calvin Wan
                         ` (4 subsequent siblings)
  7 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 8977604eb9..4776118331 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index c3b6d48797..da2693b21f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -809,25 +809,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index e1c0c326f2..90ad8b8892 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -691,9 +691,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 4/7] object-name: move related functions to object-name
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
                         ` (2 preceding siblings ...)
  2023-05-11 19:48       ` [PATCH v5 3/7] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 5/7] path: move related function to path Calvin Wan
                         ` (3 subsequent siblings)
  7 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
so that strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 5 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/object-name.c b/object-name.c
index 538e8a8f62..c2e82aceea 100644
--- a/object-name.c
+++ b/object-name.c
@@ -766,6 +766,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index da2693b21f..6533559e95 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "refs.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1023,21 +1022,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 90ad8b8892..b38bfd34af 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -617,16 +617,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 5/7] path: move related function to path
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
                         ` (3 preceding siblings ...)
  2023-05-11 19:48       ` [PATCH v5 4/7] object-name: move related functions to object-name Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 6/7] strbuf: clarify dependency Calvin Wan
                         ` (2 subsequent siblings)
  7 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
is focused on string manipulation routines with minimal dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 20 --------------------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 6533559e95..178d75f250 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1088,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 6/7] strbuf: clarify dependency
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
                         ` (4 preceding siblings ...)
  2023-05-11 19:48       ` [PATCH v5 5/7] path: move related function to path Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
  7 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
included thru refs.h, so removing refs.h requires strbuf.h to be added
back.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 178d75f250..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "refs.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v5 7/7] strbuf: remove global variable
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
                         ` (5 preceding siblings ...)
  2023-05-11 19:48       ` [PATCH v5 6/7] strbuf: clarify dependency Calvin Wan
@ 2023-05-11 19:48       ` Calvin Wan
  2023-05-11 20:24         ` Eric Sunshine
                           ` (2 more replies)
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
  7 siblings, 3 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, newren, peff, phillip.wood123, sunshine

As a library that only interacts with other primitives, strbuf should
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.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 18 ++++++++++--------
 strbuf.h             | 15 +++++++++------
 wt-status.c          |  6 +++---
 16 files changed, 91 insertions(+), 64 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..9ece2e3066 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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');
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..4560610d09 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..e002ebf070 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, '\0');
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..3bad5b458b 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			die(_("please supply the note contents using either -m or -F option"));
 		}
-		strbuf_stripspace(&d->buf, 1);
+		strbuf_stripspace(&d->buf, comment_line_char);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, '\0');
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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');
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..b1ba9fb05d 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..1987752359 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf,
+			  mode == STRIP_COMMENTS ? comment_line_char : '\0');
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..b79e0a88e6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag, comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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 : '\0');
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..c27a751b70 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	strbuf_stripspace(&ssh_keygen_out, 0);
-	strbuf_stripspace(&ssh_keygen_err, 0);
+	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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..012defbb53 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..eae4563ef7 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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 : '\0');
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	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);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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 : '\0');
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..9348ac6863 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
  *
  * 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, char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
@@ -1067,7 +1068,8 @@ 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;
 		}
diff --git a/strbuf.h b/strbuf.h
index b38bfd34af..89ae30b620 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -292,7 +292,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -421,8 +422,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -544,10 +545,12 @@ int strbuf_getcwd(struct strbuf *sb);
 int strbuf_normalize_path(struct strbuf *sb);
 
 /**
- * Strip whitespace from a buffer. The second parameter controls if
- * comments are considered contents to be removed 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, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 1/7] strbuf: clarify API boundary
  2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-11 19:57         ` Eric Sunshine
  2023-05-11 20:03           ` Calvin Wan
  0 siblings, 1 reply; 85+ messages in thread
From: Eric Sunshine @ 2023-05-11 19:57 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff, phillip.wood123

On Thu, May 11, 2023 at 3:48 PM Calvin Wan <calvinwan@google.com> wrote:
> strbuf, as a generic and widely used structure across the codebase,
> 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.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
> diff --git a/strbuf.h b/strbuf.h
> @@ -1,6 +1,15 @@
> +/*
> + * 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.
> + */

Sorry, I meant to send a follow-up to my previous email[1] but forgot.
In particular, I wanted to say that I think the second part of the
above comment (the "however" and everything which follows) ought to be
dropped since it will become stale once those "older functions" are
finally removed; it is likely nobody will remember to update this
comment. So, the above could rewritten something like this:

 /*
  * NOTE FOR STRBUF DEVELOPERS
  *
  * strbuf is a low-level primitive; as such it should interact only
  * with other low-level primitives. Do not introduce new functions
  * which interact with higher-level APIs.
  */

[1]: https://lore.kernel.org/git/CAPig+cTQg7XzORPHeD79aHEi1ggOjTPw9X02VPgxcV9uoBOBxg@mail.gmail.com/

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 1/7] strbuf: clarify API boundary
  2023-05-11 19:57         ` Eric Sunshine
@ 2023-05-11 20:03           ` Calvin Wan
  0 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-11 20:03 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: git, newren, peff, phillip.wood123

> Sorry, I meant to send a follow-up to my previous email[1] but forgot.
> In particular, I wanted to say that I think the second part of the
> above comment (the "however" and everything which follows) ought to be
> dropped since it will become stale once those "older functions" are
> finally removed; it is likely nobody will remember to update this
> comment. So, the above could rewritten something like this:
>
>  /*
>   * NOTE FOR STRBUF DEVELOPERS
>   *
>   * strbuf is a low-level primitive; as such it should interact only
>   * with other low-level primitives. Do not introduce new functions
>   * which interact with higher-level APIs.
>   */

I agree that it'll probably be forgotten about so your suggested
documentation sounds better. If others don't have any more comments
on the other patches, I'll reroll just this patch.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 7/7] strbuf: remove global variable
  2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
@ 2023-05-11 20:24         ` Eric Sunshine
  2023-05-11 21:42         ` Junio C Hamano
  2023-05-12 14:53         ` Phillip Wood
  2 siblings, 0 replies; 85+ messages in thread
From: Eric Sunshine @ 2023-05-11 20:24 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff, phillip.wood123

On Thu, May 11, 2023 at 3:48 PM Calvin Wan <calvinwan@google.com> wrote:
> As a library that only interacts with other primitives, strbuf should
> 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.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
> diff --git a/strbuf.c b/strbuf.c
> @@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
> - * 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

When speaking of a character, we normally spell this NUL rather than
NULL (which is how we spell a null pointer).

The end-of-sentence period got lost in the rewrite.

Probably not worth a reroll on its own.

> @@ -1067,7 +1068,8 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
> -               if (skip_comments && len && sb->buf[i] == comment_line_char) {
> +               if (comment_line_char != '\0' && len &&
> +                   sb->buf[i] == comment_line_char) {

In this code-base, it is more common to say:

    if (comment_line_char && len && ...

rather than:

    if (comment_line_char != '\0' && len && ...

Probably not worth a reroll.

> diff --git a/strbuf.h b/strbuf.h
> @@ -544,10 +545,12 @@ int strbuf_getcwd(struct strbuf *sb);
>  /**
> - * Strip whitespace from a buffer. The second parameter controls if
> - * comments are considered contents to be removed 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, char comment_line_char);

Ditto regarding NUL vs. NULL.

Rather than saying "the second parameter", readers would have an
easier time if you just spelled it out directly (i.e.
"comment_line_char").

The phrase "comments are considered contents to be removed" made sense
before the rewrite, but sounds a bit awkward after the rewrite.
Perhaps rewrite it something like this:

    Strip whitespace from a buffer. If comment_line_char is non-NUL,
    then lines beginning with that character are considered comments,
    thus removed.

May not be worth a reroll.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 7/7] strbuf: remove global variable
  2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
  2023-05-11 20:24         ` Eric Sunshine
@ 2023-05-11 21:42         ` Junio C Hamano
  2023-05-12 14:54           ` Phillip Wood
  2023-05-12 14:53         ` Phillip Wood
  2 siblings, 1 reply; 85+ messages in thread
From: Junio C Hamano @ 2023-05-11 21:42 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, newren, peff, phillip.wood123, sunshine

Calvin Wan <calvinwan@google.com> writes:

> 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.

Hmph, that is certainly cute.  Taking it as granted that NUL can
never serve any useful purpose in a text file (where stripspace
makes any sense), passing NUL as the comment_line_char can certainly
be used as a sign that no stripping is desired.

And those existing callers that do not want comment stripping are
already passing 0 at the position because the original parameter is
an integer (used as a Boolean); they now ought to be passing '\0'
instead, but passing 0 is acceptable.

It is not trivial to catch existing callers that pass 1 to mean
"please strip" as they are not supposed to pass comment_line_char
(usually "#", but there is a global variable for that visible to
them).  If left unconverted, they end up asking that lines that
begin with SOH ('\001') to be considered comments and get stripped.
In order to catch such a mistake, however, you cannot say "ah, the
skip_comments parameter is '1'; is that because the end user really
wanted SOH as the comment leader?  Let's warn if the value of the
comment_line_char global is not 1" for obvious reasons X-<.

So this step, while it makes sense in a vacuum, is a cute idea, and
is nicer in the longer term (because we certainly do not want to
have to pass an extra parameter to the function), raises the risk of
semantic mismerge higher for topics in flight that do want to use
stripspace to remove lines that are commented out.  My quick "git
log -S" seems to tell me there is no such topic I happened to have
picked up in 'seen' right now, though, so it may be OK.

Thanks.



^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 7/7] strbuf: remove global variable
  2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
  2023-05-11 20:24         ` Eric Sunshine
  2023-05-11 21:42         ` Junio C Hamano
@ 2023-05-12 14:53         ` Phillip Wood
  2023-05-12 19:31           ` Junio C Hamano
  2 siblings, 1 reply; 85+ messages in thread
From: Phillip Wood @ 2023-05-12 14:53 UTC (permalink / raw)
  To: Calvin Wan, git; +Cc: newren, peff, sunshine, Junio C Hamano

Hi Calvin

On 11/05/2023 20:48, Calvin Wan wrote:
> As a library that only interacts with other primitives, strbuf should
> 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.

Thanks for re-rolling, I agree with Eric's comments but would be happy 
if this was merged as-is.

Best Wishes

Phillip

> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>   add-patch.c          | 12 +++++++-----
>   builtin/am.c         |  2 +-
>   builtin/branch.c     |  4 ++--
>   builtin/commit.c     |  2 +-
>   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 +++--
>   rebase-interactive.c | 15 ++++++++-------
>   sequencer.c          | 24 +++++++++++++++---------
>   strbuf.c             | 18 ++++++++++--------
>   strbuf.h             | 15 +++++++++------
>   wt-status.c          |  6 +++---
>   16 files changed, 91 insertions(+), 64 deletions(-)
> 
> diff --git a/add-patch.c b/add-patch.c
> index 8d770d203f..9702c1aabd 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   	size_t i;
>   
>   	strbuf_reset(&s->buf);
> -	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
> -				      "a quick guide.\n"));
> +	strbuf_commented_addf(&s->buf, comment_line_char,
> +			      _("Manual hunk edit mode -- see bottom for "
> +				"a quick guide.\n"));
>   	render_hunk(s, hunk, 0, 0, &s->buf);
> -	strbuf_commented_addf(&s->buf,
> +	strbuf_commented_addf(&s->buf, comment_line_char,
>   			      _("---\n"
>   				"To remove '%c' lines, make them ' ' lines "
>   				"(context).\n"
> @@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   			      s->mode->is_reverse ? '+' : '-',
>   			      s->mode->is_reverse ? '-' : '+',
>   			      comment_line_char);
> -	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
> +	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
> +			      _(s->mode->edit_hunk_hint));
>   	/*
>   	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
>   	 * messages.
>   	 */
> -	strbuf_commented_addf(&s->buf,
> +	strbuf_commented_addf(&s->buf, comment_line_char,
>   			      _("If it does not apply cleanly, you will be "
>   				"given an opportunity to\n"
>   				"edit again.  If all lines of the hunk are "
> diff --git a/builtin/am.c b/builtin/am.c
> index 5c83f2e003..9ece2e3066 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -1283,7 +1283,7 @@ 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');
>   
>   	assert(!state->author_name);
>   	state->author_name = strbuf_detach(&author_name, NULL);
> diff --git a/builtin/branch.c b/builtin/branch.c
> index 501c47657c..4560610d09 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
>   	exists = !read_branch_desc(&buf, branch_name);
>   	if (!buf.len || buf.buf[buf.len-1] != '\n')
>   		strbuf_addch(&buf, '\n');
> -	strbuf_commented_addf(&buf,
> +	strbuf_commented_addf(&buf, comment_line_char,
>   		    _("Please edit the description for the branch\n"
>   		      "  %s\n"
>   		      "Lines starting with '%c' will be stripped.\n"),
> @@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
>   		strbuf_release(&buf);
>   		return -1;
>   	}
> -	strbuf_stripspace(&buf, 1);
> +	strbuf_stripspace(&buf, comment_line_char);
>   
>   	strbuf_addf(&name, "branch.%s.description", branch_name);
>   	if (buf.len || exists)
> diff --git a/builtin/commit.c b/builtin/commit.c
> index e67c4be221..e002ebf070 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>   	s->hints = 0;
>   
>   	if (clean_message_contents)
> -		strbuf_stripspace(&sb, 0);
> +		strbuf_stripspace(&sb, '\0');
>   
>   	if (signoff)
>   		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
> diff --git a/builtin/merge.c b/builtin/merge.c
> index 8da3e46abb..1d14767c0c 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
>   		strbuf_addch(&msg, '\n');
>   		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
>   			wt_status_append_cut_line(&msg);
> -			strbuf_commented_addf(&msg, "\n");
> +			strbuf_commented_addf(&msg, comment_line_char, "\n");
>   		}
> -		strbuf_commented_addf(&msg, _(merge_editor_comment));
> +		strbuf_commented_addf(&msg, comment_line_char,
> +				      _(merge_editor_comment));
>   		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
> -			strbuf_commented_addf(&msg, _(scissors_editor_comment));
> +			strbuf_commented_addf(&msg, comment_line_char,
> +					      _(scissors_editor_comment));
>   		else
> -			strbuf_commented_addf(&msg,
> +			strbuf_commented_addf(&msg, comment_line_char,
>   				_(no_scissors_editor_comment), comment_line_char);
>   	}
>   	if (signoff)
> diff --git a/builtin/notes.c b/builtin/notes.c
> index d5788352b6..3bad5b458b 100644
> --- a/builtin/notes.c
> +++ b/builtin/notes.c
> @@ -11,6 +11,7 @@
>   #include "config.h"
>   #include "builtin.h"
>   #include "editor.h"
> +#include "environment.h"
>   #include "gettext.h"
>   #include "hex.h"
>   #include "notes.h"
> @@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
>   
>   	if (strbuf_read(&buf, show.out, 0) < 0)
>   		die_errno(_("could not read 'show' output"));
> -	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
> +	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
>   	write_or_die(fd, cbuf.buf, cbuf.len);
>   
>   	strbuf_release(&cbuf);
> @@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
>   			copy_obj_to_fd(fd, old_note);
>   
>   		strbuf_addch(&buf, '\n');
> -		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
> -		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
> -		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
> +		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
> +		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
> +					   comment_line_char);
> +		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
>   		write_or_die(fd, buf.buf, buf.len);
>   
>   		write_commented_object(fd, object);
> @@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
>   		if (launch_editor(d->edit_path, &d->buf, NULL)) {
>   			die(_("please supply the note contents using either -m or -F option"));
>   		}
> -		strbuf_stripspace(&d->buf, 1);
> +		strbuf_stripspace(&d->buf, comment_line_char);
>   	}
>   }
>   
> @@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
>   	if (d->buf.len)
>   		strbuf_addch(&d->buf, '\n');
>   	strbuf_addstr(&d->buf, arg);
> -	strbuf_stripspace(&d->buf, 0);
> +	strbuf_stripspace(&d->buf, '\0');
>   
>   	d->given = 1;
>   	return 0;
> @@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
>   			die_errno(_("cannot read '%s'"), arg);
>   	} 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');
>   
>   	d->given = 1;
>   	return 0;
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> index ace1d5e8d1..b1ba9fb05d 100644
> --- a/builtin/rebase.c
> +++ b/builtin/rebase.c
> @@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
>   	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
>   		return error_errno(_("could not read '%s'."), todo_file);
>   
> -	strbuf_stripspace(&todo_list.buf, 1);
> +	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)))
> diff --git a/builtin/stripspace.c b/builtin/stripspace.c
> index 9451eb69ff..1987752359 100644
> --- a/builtin/stripspace.c
> +++ b/builtin/stripspace.c
> @@ -1,6 +1,7 @@
>   #include "builtin.h"
>   #include "cache.h"
>   #include "config.h"
> +#include "environment.h"
>   #include "gettext.h"
>   #include "parse-options.h"
>   #include "setup.h"
> @@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
>   	size_t len;
>   
>   	msg = strbuf_detach(buf, &len);
> -	strbuf_add_commented_lines(buf, msg, len);
> +	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
>   	free(msg);
>   }
>   
> @@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
>   		die_errno("could not read the input");
>   
>   	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
> -		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
> +		strbuf_stripspace(&buf,
> +			  mode == STRIP_COMMENTS ? comment_line_char : '\0');
>   	else
>   		comment_lines(&buf);
>   
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 1850a6a6fd..b79e0a88e6 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
>   			struct strbuf buf = STRBUF_INIT;
>   			strbuf_addch(&buf, '\n');
>   			if (opt->cleanup_mode == CLEANUP_ALL)
> -				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
> +				strbuf_commented_addf(&buf, comment_line_char,
> +				      _(tag_template), tag, comment_line_char);
>   			else
> -				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
> +				strbuf_commented_addf(&buf, comment_line_char,
> +				      _(tag_template_nocleanup), tag, comment_line_char);
>   			write_or_die(fd, buf.buf, buf.len);
>   			strbuf_release(&buf);
>   		}
> @@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
>   	}
>   
>   	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 : '\0');
>   
>   	if (!opt->message_given && !buf->len)
>   		die(_("no tag message?"));
> diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
> index 5af0d4715b..5d15e2387d 100644
> --- a/fmt-merge-msg.c
> +++ b/fmt-merge-msg.c
> @@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
>   	strbuf_complete_line(tagbuf);
>   	if (sig->len) {
>   		strbuf_addch(tagbuf, '\n');
> -		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
> +		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
> +					   comment_line_char);
>   	}
>   }
>   
> @@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
>   				strbuf_addch(&tagline, '\n');
>   				strbuf_add_commented_lines(&tagline,
>   						origins.items[first_tag].string,
> -						strlen(origins.items[first_tag].string));
> +						strlen(origins.items[first_tag].string),
> +						comment_line_char);
>   				strbuf_insert(&tagbuf, 0, tagline.buf,
>   					      tagline.len);
>   				strbuf_release(&tagline);
> @@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
>   			strbuf_addch(&tagbuf, '\n');
>   			strbuf_add_commented_lines(&tagbuf,
>   					origins.items[i].string,
> -					strlen(origins.items[i].string));
> +					strlen(origins.items[i].string),
> +					comment_line_char);
>   			fmt_tag_signature(&tagbuf, &sig, buf, len);
>   		}
>   		strbuf_release(&payload);
> diff --git a/gpg-interface.c b/gpg-interface.c
> index f3ac5acdd9..c27a751b70 100644
> --- a/gpg-interface.c
> +++ b/gpg-interface.c
> @@ -11,6 +11,7 @@
>   #include "tempfile.h"
>   #include "alias.h"
>   #include "wrapper.h"
> +#include "environment.h"
>   
>   static int git_gpg_config(const char *, const char *, void *);
>   
> @@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
>   		}
>   	}
>   
> -	strbuf_stripspace(&ssh_keygen_out, 0);
> -	strbuf_stripspace(&ssh_keygen_err, 0);
> +	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);
> diff --git a/rebase-interactive.c b/rebase-interactive.c
> index 789f407361..012defbb53 100644
> --- a/rebase-interactive.c
> +++ b/rebase-interactive.c
> @@ -71,13 +71,14 @@ void append_todo_help(int command_count,
>   
>   	if (!edit_todo) {
>   		strbuf_addch(buf, '\n');
> -		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
> -					      "Rebase %s onto %s (%d commands)",
> -					      command_count),
> +		strbuf_commented_addf(buf, comment_line_char,
> +				      Q_("Rebase %s onto %s (%d command)",
> +					 "Rebase %s onto %s (%d commands)",
> +					 command_count),
>   				      shortrevisions, shortonto, command_count);
>   	}
>   
> -	strbuf_add_commented_lines(buf, msg, strlen(msg));
> +	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
>   
>   	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
>   		msg = _("\nDo not remove any line. Use 'drop' "
> @@ -86,7 +87,7 @@ void append_todo_help(int command_count,
>   		msg = _("\nIf you remove a line here "
>   			 "THAT COMMIT WILL BE LOST.\n");
>   
> -	strbuf_add_commented_lines(buf, msg, strlen(msg));
> +	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
>   
>   	if (edit_todo)
>   		msg = _("\nYou are editing the todo file "
> @@ -97,7 +98,7 @@ void append_todo_help(int command_count,
>   		msg = _("\nHowever, if you remove everything, "
>   			"the rebase will be aborted.\n\n");
>   
> -	strbuf_add_commented_lines(buf, msg, strlen(msg));
> +	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
>   }
>   
>   int edit_todo_list(struct repository *r, struct todo_list *todo_list,
> @@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
>   	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
>   		return -2;
>   
> -	strbuf_stripspace(&new_todo->buf, 1);
> +	strbuf_stripspace(&new_todo->buf, comment_line_char);
>   	if (initial && new_todo->buf.len == 0)
>   		return -3;
>   
> diff --git a/sequencer.c b/sequencer.c
> index c88d1d9553..eae4563ef7 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
>   	}
>   
>   	strbuf_addch(msgbuf, '\n');
> -	strbuf_commented_addf(msgbuf, "Conflicts:\n");
> +	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
>   	for (i = 0; i < istate->cache_nr;) {
>   		const struct cache_entry *ce = istate->cache[i++];
>   		if (ce_stage(ce)) {
> -			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
> +			strbuf_commented_addf(msgbuf, comment_line_char,
> +					      "\t%s\n", ce->name);
>   			while (i < istate->cache_nr &&
>   			       !strcmp(ce->name, istate->cache[i]->name))
>   				i++;
> @@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
>   	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
>   		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 : '\0');
>   }
>   
>   /*
> @@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
>   	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
>   		return 0;
>   
> -	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
> +	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);
> @@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
>   		cleanup = opts->default_msg_cleanup;
>   
>   	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 : '\0');
>   	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
>   		res = 1; /* run 'git commit' to display error message */
>   		goto out;
> @@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
>   		s += count;
>   		len -= count;
>   	}
> -	strbuf_add_commented_lines(buf, s, len);
> +	strbuf_add_commented_lines(buf, s, len, comment_line_char);
>   }
>   
>   /* Does the current fixup chain contain a squash command? */
> @@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
>   	strbuf_addf(buf, _(nth_commit_msg_fmt),
>   		    ++opts->current_fixup_count + 1);
>   	strbuf_addstr(buf, "\n\n");
> -	strbuf_add_commented_lines(buf, body, commented_len);
> +	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
>   	/* buf->buf may be reallocated so store an offset into the buffer */
>   	fixup_off = buf->len;
>   	strbuf_addstr(buf, body + commented_len);
> @@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
>   			      _(first_commit_msg_str));
>   		strbuf_addstr(&buf, "\n\n");
>   		if (is_fixup_flag(command, flag))
> -			strbuf_add_commented_lines(&buf, body, strlen(body));
> +			strbuf_add_commented_lines(&buf, body, strlen(body),
> +						   comment_line_char);
>   		else
>   			strbuf_addstr(&buf, body);
>   
> @@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
>   		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
>   			    ++opts->current_fixup_count + 1);
>   		strbuf_addstr(&buf, "\n\n");
> -		strbuf_add_commented_lines(&buf, body, strlen(body));
> +		strbuf_add_commented_lines(&buf, body, strlen(body),
> +					   comment_line_char);
>   	} else
>   		return error(_("unknown command: %d"), command);
>   	repo_unuse_commit_buffer(r, commit, message);
> diff --git a/strbuf.c b/strbuf.c
> index d5978fee4e..9348ac6863 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -1,6 +1,5 @@
>   #include "git-compat-util.h"
>   #include "alloc.h"
> -#include "environment.h"
>   #include "gettext.h"
>   #include "hex.h"
>   #include "strbuf.h"
> @@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
>   	strbuf_complete_line(out);
>   }
>   
> -void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
> +void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
> +				size_t size, char comment_line_char)
>   {
>   	static char prefix1[3];
>   	static char prefix2[2];
> @@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
>   	add_lines(out, prefix1, prefix2, buf, size);
>   }
>   
> -void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
> +void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
> +			   const char *fmt, ...)
>   {
>   	va_list params;
>   	struct strbuf buf = STRBUF_INIT;
> @@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
>   	strbuf_vaddf(&buf, fmt, params);
>   	va_end(params);
>   
> -	strbuf_add_commented_lines(sb, buf.buf, buf.len);
> +	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
>   	if (incomplete_line)
>   		sb->buf[--sb->len] = '\0';
>   
> @@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
>    *
>    * 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, char comment_line_char)
>   {
>   	size_t empties = 0;
>   	size_t i, j, len, newlen;
> @@ -1067,7 +1068,8 @@ 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;
>   		}
> diff --git a/strbuf.h b/strbuf.h
> index b38bfd34af..89ae30b620 100644
> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -292,7 +292,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
>    * by a comment character and a blank.
>    */
>   void strbuf_add_commented_lines(struct strbuf *out,
> -				const char *buf, size_t size);
> +				const char *buf, size_t size,
> +				char comment_line_char);
>   
>   
>   /**
> @@ -421,8 +422,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
>    * Add a formatted string prepended by a comment character and a
>    * blank to the buffer.
>    */
> -__attribute__((format (printf, 2, 3)))
> -void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
> +__attribute__((format (printf, 3, 4)))
> +void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
>   
>   __attribute__((format (printf,2,0)))
>   void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
> @@ -544,10 +545,12 @@ int strbuf_getcwd(struct strbuf *sb);
>   int strbuf_normalize_path(struct strbuf *sb);
>   
>   /**
> - * Strip whitespace from a buffer. The second parameter controls if
> - * comments are considered contents to be removed 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, char comment_line_char);
>   
>   static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
>   {
> diff --git a/wt-status.c b/wt-status.c
> index 97b9c1c035..da7734866f 100644
> --- a/wt-status.c
> +++ b/wt-status.c
> @@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
>   	if (s->display_comment_prefix) {
>   		size_t len;
>   		summary_content = strbuf_detach(&summary, &len);
> -		strbuf_add_commented_lines(&summary, summary_content, len);
> +		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
>   		free(summary_content);
>   	}
>   
> @@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
>   {
>   	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
>   
> -	strbuf_commented_addf(buf, "%s", cut_line);
> -	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
> +	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
> +	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
>   }
>   
>   void wt_status_add_cut_line(FILE *fp)

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 7/7] strbuf: remove global variable
  2023-05-11 21:42         ` Junio C Hamano
@ 2023-05-12 14:54           ` Phillip Wood
  0 siblings, 0 replies; 85+ messages in thread
From: Phillip Wood @ 2023-05-12 14:54 UTC (permalink / raw)
  To: Junio C Hamano, Calvin Wan; +Cc: git, newren, peff, sunshine

On 11/05/2023 22:42, Junio C Hamano wrote:
> Calvin Wan <calvinwan@google.com> writes:
>
> So this step, while it makes sense in a vacuum, is a cute idea, and
> is nicer in the longer term (because we certainly do not want to
> have to pass an extra parameter to the function), raises the risk of
> semantic mismerge higher for topics in flight that do want to use
> stripspace to remove lines that are commented out.  My quick "git
> log -S" seems to tell me there is no such topic I happened to have
> picked up in 'seen' right now, though, so it may be OK.

I just grepped seen for strbuf_stripspace() and it looks like all the 
callers are converted correctly.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v6 0/7] strbuf cleanups
  2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
                         ` (6 preceding siblings ...)
  2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
@ 2023-05-12 17:14       ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 1/7] strbuf: clarify API boundary Calvin Wan
                           ` (8 more replies)
  7 siblings, 9 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:14 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

Reroll with last couple of nits fixed. Thanks again to everyone for
reviewing this series!

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                   |  57 ++++++-------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +-
 26 files changed, 227 insertions(+), 192 deletions(-)

Range-diff against v5:
1:  287e787c9c ! 1:  5ae531a1e2 strbuf: clarify API boundary
    @@ 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.
    ++ * strbuf is a low-level primitive; as such it should interact only
    ++ * with other low-level primitives. Do not introduce new functions
    ++ * which interact with higher-level APIs.
     + */
     +
      struct string_list;
2:  5bd27cad7a = 2:  3bb2b9c01a abspath: move related functions to abspath
3:  08e3e40de6 = 3:  ff91ca2fda credential-store: move related functions to credential-store file
4:  1a21d4fdd1 = 4:  10e61eb570 object-name: move related functions to object-name
5:  03b20f3384 = 5:  c3d5db4e11 path: move related function to path
6:  b55f4a39e1 = 6:  113d156195 strbuf: clarify dependency
7:  6ea36d7730 ! 7:  01c923160d strbuf: remove global variable
    @@ Commit message
         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
    +    comment_line_char is a non-NUL character to determine whether to skip
         comments or not.
     
      ## add-patch.c ##
    @@ strbuf.c: static size_t cleanup(char *line, size_t len)
       *
     - * 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
    ++ * Pass a non-NUL 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, char comment_line_char)
    @@ strbuf.c: void strbuf_stripspace(struct strbuf *sb, int skip_comments)
      		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 &&
    ++		if (comment_line_char && len &&
     +		    sb->buf[i] == comment_line_char) {
      			newlen = 0;
      			continue;
    @@ strbuf.h: int strbuf_getcwd(struct strbuf *sb);
      /**
     - * Strip whitespace from a buffer. The second parameter controls if
     - * comments are considered contents to be removed 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.
    ++ * Strip whitespace from a buffer. If comment_line_char is non-NUL,
    ++ * then lines beginning with that character are considered comments,
    ++ * thus removed.
       */
     -void strbuf_stripspace(struct strbuf *buf, int skip_comments);
     +void strbuf_stripspace(struct strbuf *buf, char comment_line_char);
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v6 1/7] strbuf: clarify API boundary
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 2/7] abspath: move related functions to abspath Calvin Wan
                           ` (7 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

strbuf, as a generic and widely used structure across the codebase,
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.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..70778c6e10 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -1,6 +1,14 @@
 #ifndef STRBUF_H
 #define STRBUF_H
 
+/*
+ * NOTE FOR STRBUF DEVELOPERS
+ *
+ * strbuf is a low-level primitive; as such it should interact only
+ * with other low-level primitives. Do not introduce new functions
+ * which interact with higher-level APIs.
+ */
+
 struct string_list;
 
 /**
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 2/7] abspath: move related functions to abspath
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 3/7] credential-store: move related functions to credential-store file Calvin Wan
                           ` (6 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 76e322f580..2d8706371e 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..c3b6d48797 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index 70778c6e10..207efb4f98 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 3/7] credential-store: move related functions to credential-store file
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 1/7] strbuf: clarify API boundary Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 2/7] abspath: move related functions to abspath Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 4/7] object-name: move related functions to object-name Calvin Wan
                           ` (5 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 8977604eb9..4776118331 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -73,6 +73,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index c3b6d48797..da2693b21f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -809,25 +809,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index 207efb4f98..114ad0c024 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -690,9 +690,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 4/7] object-name: move related functions to object-name
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (2 preceding siblings ...)
  2023-05-12 17:15         ` [PATCH v6 3/7] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 5/7] path: move related function to path Calvin Wan
                           ` (4 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
so that strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 5 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/object-name.c b/object-name.c
index 538e8a8f62..c2e82aceea 100644
--- a/object-name.c
+++ b/object-name.c
@@ -766,6 +766,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index da2693b21f..6533559e95 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "refs.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1023,21 +1022,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 114ad0c024..5d97e27b99 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -616,16 +616,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 5/7] path: move related function to path
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (3 preceding siblings ...)
  2023-05-12 17:15         ` [PATCH v6 4/7] object-name: move related functions to object-name Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 6/7] strbuf: clarify dependency Calvin Wan
                           ` (3 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
is focused on string manipulation routines with minimal dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 20 --------------------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 6533559e95..178d75f250 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1088,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 6/7] strbuf: clarify dependency
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (4 preceding siblings ...)
  2023-05-12 17:15         ` [PATCH v6 5/7] path: move related function to path Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 17:15         ` [PATCH v6 7/7] strbuf: remove global variable Calvin Wan
                           ` (2 subsequent siblings)
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
included thru refs.h, so removing refs.h requires strbuf.h to be added
back.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 178d75f250..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "refs.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v6 7/7] strbuf: remove global variable
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (5 preceding siblings ...)
  2023-05-12 17:15         ` [PATCH v6 6/7] strbuf: clarify dependency Calvin Wan
@ 2023-05-12 17:15         ` Calvin Wan
  2023-05-12 20:24         ` [PATCH v6 0/7] strbuf cleanups Junio C Hamano
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
  8 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-05-12 17:15 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, phillip.wood123, sunshine

As a library that only interacts with other primitives, strbuf should
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-NUL character to determine whether to skip
comments or not.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 18 ++++++++++--------
 strbuf.h             | 14 ++++++++------
 wt-status.c          |  6 +++---
 16 files changed, 90 insertions(+), 64 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..9ece2e3066 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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');
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..4560610d09 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -629,7 +629,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -640,7 +640,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..e002ebf070 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, '\0');
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..3bad5b458b 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			die(_("please supply the note contents using either -m or -F option"));
 		}
-		strbuf_stripspace(&d->buf, 1);
+		strbuf_stripspace(&d->buf, comment_line_char);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, '\0');
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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');
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..b1ba9fb05d 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..1987752359 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf,
+			  mode == STRIP_COMMENTS ? comment_line_char : '\0');
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..b79e0a88e6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag, comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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 : '\0');
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..c27a751b70 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -11,6 +11,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -584,8 +585,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	strbuf_stripspace(&ssh_keygen_out, 0);
-	strbuf_stripspace(&ssh_keygen_err, 0);
+	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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..012defbb53 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -71,13 +71,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -86,7 +87,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -97,7 +98,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -129,7 +130,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..eae4563ef7 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -659,11 +659,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1142,7 +1143,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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 : '\0');
 }
 
 /*
@@ -1173,7 +1175,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	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);
@@ -1545,7 +1548,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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 : '\0');
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1839,7 +1843,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1938,7 +1942,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2028,7 +2032,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2047,7 +2052,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..67e399b60a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
  *
  * 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-NUL 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, char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
@@ -1067,7 +1068,8 @@ 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 && len &&
+		    sb->buf[i] == comment_line_char) {
 			newlen = 0;
 			continue;
 		}
diff --git a/strbuf.h b/strbuf.h
index 5d97e27b99..da91c17257 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -291,7 +291,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -420,8 +421,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -543,10 +544,11 @@ int strbuf_getcwd(struct strbuf *sb);
 int strbuf_normalize_path(struct strbuf *sb);
 
 /**
- * Strip whitespace from a buffer. The second parameter controls if
- * comments are considered contents to be removed or not.
+ * Strip whitespace from a buffer. If comment_line_char is non-NUL,
+ * then lines beginning with that character are considered comments,
+ * thus removed.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 97b9c1c035..da7734866f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1023,7 +1023,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1098,8 +1098,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.40.1.606.ga4b1b128d6-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* Re: [PATCH v5 7/7] strbuf: remove global variable
  2023-05-12 14:53         ` Phillip Wood
@ 2023-05-12 19:31           ` Junio C Hamano
  0 siblings, 0 replies; 85+ messages in thread
From: Junio C Hamano @ 2023-05-12 19:31 UTC (permalink / raw)
  To: Phillip Wood; +Cc: Calvin Wan, git, newren, peff, sunshine

Phillip Wood <phillip.wood123@gmail.com> writes:

> Hi Calvin
>
> On 11/05/2023 20:48, Calvin Wan wrote:
>> As a library that only interacts with other primitives, strbuf should
>> 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.
>
> Thanks for re-rolling, I agree with Eric's comments but would be happy
> if this was merged as-is.

Thanks for reviewing.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v6 0/7] strbuf cleanups
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (6 preceding siblings ...)
  2023-05-12 17:15         ` [PATCH v6 7/7] strbuf: remove global variable Calvin Wan
@ 2023-05-12 20:24         ` Junio C Hamano
  2023-05-13  5:54           ` Eric Sunshine
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
  8 siblings, 1 reply; 85+ messages in thread
From: Junio C Hamano @ 2023-05-12 20:24 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, phillip.wood123, sunshine

Calvin Wan <calvinwan@google.com> writes:

> Reroll with last couple of nits fixed. Thanks again to everyone for
> reviewing this series!
>
> 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

Looking good.  I see Eric's comments, to which Phillip agreed to,
are all addressed in these patches.

Will replace; let's mark the topic for 'next'.


^ permalink raw reply	[flat|nested] 85+ messages in thread

* Re: [PATCH v6 0/7] strbuf cleanups
  2023-05-12 20:24         ` [PATCH v6 0/7] strbuf cleanups Junio C Hamano
@ 2023-05-13  5:54           ` Eric Sunshine
  0 siblings, 0 replies; 85+ messages in thread
From: Eric Sunshine @ 2023-05-13  5:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Calvin Wan, git, phillip.wood123

On Fri, May 12, 2023 at 4:24 PM Junio C Hamano <gitster@pobox.com> wrote:
> Calvin Wan <calvinwan@google.com> writes:
> > Reroll with last couple of nits fixed. Thanks again to everyone for
> > reviewing this series!
>
> Looking good.  I see Eric's comments, to which Phillip agreed to,
> are all addressed in these patches.

Agreed. This version is looking good; I have no further comments.
Thanks for taking my comments into consideration.

^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v7 0/7] strbuf cleanups
  2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
                           ` (7 preceding siblings ...)
  2023-05-12 20:24         ` [PATCH v6 0/7] strbuf cleanups Junio C Hamano
@ 2023-06-06 19:47         ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 1/7] strbuf: clarify API boundary Calvin Wan
                             ` (6 more replies)
  8 siblings, 7 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:47 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

I rebased this series onto 2.41 and noticed I was getting a compilation
error for a missing dependency to repository in dir.h. With all of the
other cleanups happening, I assume some dependency chain that used to
exist doesn't anymore. This reroll moves "strbuf: clarify dependency" to
the second patch so any dependency chain broken by the other cleanup
patches can be caught. And that is why patch 4 adds a forward
declaration back. I also caught an additional unnecessary dependency
that could be removed in patch 5. This series should now be able to be
rebased onto 2.41 without any issue.

Calvin Wan (7):
  strbuf: clarify API boundary
  strbuf: clarify dependency
  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: 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 ++-
 dir.h                      |   2 +
 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                   | 113 ++++---------------------------------
 strbuf.h                   |  57 ++++++-------------
 tempfile.c                 |   1 +
 wt-status.c                |   6 +-
 27 files changed, 229 insertions(+), 193 deletions(-)

Range-diff against v6:
1:  5ae531a1e2 = 1:  121788f263 strbuf: clarify API boundary
-:  ---------- > 2:  5e91404ecd strbuf: clarify dependency
2:  3bb2b9c01a = 3:  5c05f40181 abspath: move related functions to abspath
3:  ff91ca2fda = 4:  e1addc77e5 credential-store: move related functions to credential-store file
4:  10e61eb570 ! 5:  62e8c42f59 object-name: move related functions to object-name
    @@ Commit message
         so that strbuf is focused on string manipulation routines with minimal
         dependencies.
     
    +    dir.h relied on the forward declration of the repository struct in
    +    strbuf.h. Since that is removed in this patch, add the forward
    +    declaration to dir.h.
    +
    + ## dir.h ##
    +@@
    +  *
    +  */
    + 
    ++struct repository;
    ++
    + struct dir_entry {
    + 	unsigned int len;
    + 	char name[FLEX_ARRAY]; /* more */
    +
      ## object-name.c ##
     @@ object-name.c: static void find_abbrev_len_packed(struct min_abbrev_data *mad)
      		find_abbrev_len_for_pack(p, mad);
    @@ strbuf.c
      #include "gettext.h"
      #include "hex.h"
     -#include "object-name.h"
    - #include "refs.h"
    + #include "repository.h"
    + #include "strbuf.h"
      #include "string-list.h"
    - #include "utf8.h"
     @@ strbuf.c: void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
      	strbuf_setlen(sb, sb->len + len);
      }
5:  c3d5db4e11 ! 6:  0abba57acb path: move related function to path
    @@ Commit message
         Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
         is focused on string manipulation routines with minimal dependencies.
     
    +    repository.h is no longer a necessary dependency after moving this
    +    function out.
    +
      ## path.c ##
     @@ path.c: int normalize_path_copy(char *dst, const char *src)
      	return normalize_path_copy_len(dst, src, NULL);
    @@ path.h: const char *remove_leading_path(const char *in, const char *prefix);
      int daemon_avoid_alias(const char *path);
     
      ## strbuf.c ##
    +@@
    + #include "environment.h"
    + #include "gettext.h"
    + #include "hex.h"
    +-#include "repository.h"
    + #include "strbuf.h"
    + #include "string-list.h"
    + #include "utf8.h"
     @@ strbuf.c: void strbuf_stripspace(struct strbuf *sb, int skip_comments)
      	strbuf_setlen(sb, j);
      }
6:  113d156195 < -:  ---------- strbuf: clarify dependency
7:  01c923160d = 7:  d33267a390 strbuf: remove global variable
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply	[flat|nested] 85+ messages in thread

* [PATCH v7 1/7] strbuf: clarify API boundary
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 2/7] strbuf: clarify dependency Calvin Wan
                             ` (5 subsequent siblings)
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

strbuf, as a generic and widely used structure across the codebase,
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.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..70778c6e10 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -1,6 +1,14 @@
 #ifndef STRBUF_H
 #define STRBUF_H
 
+/*
+ * NOTE FOR STRBUF DEVELOPERS
+ *
+ * strbuf is a low-level primitive; as such it should interact only
+ * with other low-level primitives. Do not introduce new functions
+ * which interact with higher-level APIs.
+ */
+
 struct string_list;
 
 /**
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 2/7] strbuf: clarify dependency
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 1/7] strbuf: clarify API boundary Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 3/7] abspath: move related functions to abspath Calvin Wan
                             ` (4 subsequent siblings)
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf:
move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was
included thru refs.h, so removing refs.h requires strbuf.h to be added
back.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/strbuf.c b/strbuf.c
index 08eec8f1d8..f5dfd093a0 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -5,8 +5,8 @@
 #include "gettext.h"
 #include "hex.h"
 #include "object-name.h"
-#include "refs.h"
 #include "repository.h"
+#include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
 #include "date.h"
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 3/7] abspath: move related functions to abspath
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 1/7] strbuf: clarify API boundary Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 2/7] strbuf: clarify dependency Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 4/7] credential-store: move related functions to credential-store file Calvin Wan
                             ` (3 subsequent siblings)
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
 		return xstrdup(arg);
 	return prefix_filename(pfx, arg);
 }
+
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
+{
+	if (!*path)
+		die("The empty string is not a valid path");
+	if (!is_absolute_path(path)) {
+		struct stat cwd_stat, pwd_stat;
+		size_t orig_len = sb->len;
+		char *cwd = xgetcwd();
+		char *pwd = getenv("PWD");
+		if (pwd && strcmp(pwd, cwd) &&
+		    !stat(cwd, &cwd_stat) &&
+		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
+		    !stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino)
+			strbuf_addstr(sb, pwd);
+		else
+			strbuf_addstr(sb, cwd);
+		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
+			strbuf_addch(sb, '/');
+		free(cwd);
+	}
+	strbuf_addstr(sb, path);
+}
+
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
+{
+	if (sb->len) {
+		struct strbuf resolved = STRBUF_INIT;
+		strbuf_realpath(&resolved, path, 1);
+		strbuf_addbuf(sb, &resolved);
+		strbuf_release(&resolved);
+	} else
+		strbuf_realpath(sb, path, 1);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
 	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
 }
 
+/**
+ * Add a path to a buffer, converting a relative path to an
+ * absolute one in the process.  Symbolic links are not
+ * resolved.
+ */
+void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
+
+/**
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
+ * slashes) and append it to `sb`.  Die with an informative error
+ * message if there is a problem.
+ *
+ * The directory part of `path` (i.e., everything up to the last
+ * dir_sep) must denote a valid, existing directory, but the last
+ * component need not exist.
+ *
+ * Callers that don't mind links should use the more lightweight
+ * strbuf_add_absolute_path() instead.
+ */
+void strbuf_add_real_path(struct strbuf *sb, const char *path);
+
 #endif /* ABSPATH_H */
diff --git a/hook.c b/hook.c
index 3ca5e60895..f6306d72b3 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index f5dfd093a0..a2249ae7ed 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -900,42 +899,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 	strbuf_humanise(buf, bytes, 1);
 }
 
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
-{
-	if (!*path)
-		die("The empty string is not a valid path");
-	if (!is_absolute_path(path)) {
-		struct stat cwd_stat, pwd_stat;
-		size_t orig_len = sb->len;
-		char *cwd = xgetcwd();
-		char *pwd = getenv("PWD");
-		if (pwd && strcmp(pwd, cwd) &&
-		    !stat(cwd, &cwd_stat) &&
-		    (cwd_stat.st_dev || cwd_stat.st_ino) &&
-		    !stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino)
-			strbuf_addstr(sb, pwd);
-		else
-			strbuf_addstr(sb, cwd);
-		if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-			strbuf_addch(sb, '/');
-		free(cwd);
-	}
-	strbuf_addstr(sb, path);
-}
-
-void strbuf_add_real_path(struct strbuf *sb, const char *path)
-{
-	if (sb->len) {
-		struct strbuf resolved = STRBUF_INIT;
-		strbuf_realpath(&resolved, path, 1);
-		strbuf_addbuf(sb, &resolved);
-		strbuf_release(&resolved);
-	} else
-		strbuf_realpath(sb, path, 1);
-}
-
 int printf_ln(const char *fmt, ...)
 {
 	int ret;
diff --git a/strbuf.h b/strbuf.h
index 70778c6e10..207efb4f98 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
  */
 int strbuf_getcwd(struct strbuf *sb);
 
-/**
- * Add a path to a buffer, converting a relative path to an
- * absolute one in the process.  Symbolic links are not
- * resolved.
- */
-void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
-
-/**
- * Canonize `path` (make it absolute, resolve symlinks, remove extra
- * slashes) and append it to `sb`.  Die with an informative error
- * message if there is a problem.
- *
- * The directory part of `path` (i.e., everything up to the last
- * dir_sep) must denote a valid, existing directory, but the last
- * component need not exist.
- *
- * Callers that don't mind links should use the more lightweight
- * strbuf_add_absolute_path() instead.
- */
-void strbuf_add_real_path(struct strbuf *sb, const char *path);
-
-
 /**
  * Normalize in-place the path contained in the strbuf. See
  * normalize_path_copy() for details. If an error occurs, the contents of "sb"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 4/7] credential-store: move related functions to credential-store file
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
                             ` (2 preceding siblings ...)
  2023-06-06 19:48           ` [PATCH v7 3/7] abspath: move related functions to abspath Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 5/7] object-name: move related functions to object-name Calvin Wan
                             ` (2 subsequent siblings)
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only
called from builtin/credential-store.c and they are only relevant to that
file so move those functions and make them static.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 builtin/credential-store.c | 19 +++++++++++++++++++
 strbuf.c                   | 19 -------------------
 strbuf.h                   |  3 ---
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 30c6ccf56c..5eea5bdb58 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -74,6 +74,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
 		die_errno("unable to write credential store");
 }
 
+static int is_rfc3986_unreserved(char ch)
+{
+	return isalnum(ch) ||
+		ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+static int is_rfc3986_reserved_or_unreserved(char ch)
+{
+	if (is_rfc3986_unreserved(ch))
+		return 1;
+	switch (ch) {
+		case '!': case '*': case '\'': case '(': case ')': case ';':
+		case ':': case '@': case '&': case '=': case '+': case '$':
+		case ',': case '/': case '?': case '#': case '[': case ']':
+			return 1;
+	}
+	return 0;
+}
+
 static void store_credential_file(const char *fn, struct credential *c)
 {
 	struct strbuf buf = STRBUF_INIT;
diff --git a/strbuf.c b/strbuf.c
index a2249ae7ed..5244029d91 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -810,25 +810,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 	}
 }
 
-int is_rfc3986_reserved_or_unreserved(char ch)
-{
-	if (is_rfc3986_unreserved(ch))
-		return 1;
-	switch (ch) {
-		case '!': case '*': case '\'': case '(': case ')': case ';':
-		case ':': case '@': case '&': case '=': case '+': case '$':
-		case ',': case '/': case '?': case '#': case '[': case ']':
-			return 1;
-	}
-	return 0;
-}
-
-int is_rfc3986_unreserved(char ch)
-{
-	return isalnum(ch) ||
-		ch == '-' || ch == '_' || ch == '.' || ch == '~';
-}
-
 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 				 char_predicate allow_unencoded_fn)
 {
diff --git a/strbuf.h b/strbuf.h
index 207efb4f98..114ad0c024 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -690,9 +690,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
 typedef int (*char_predicate)(char ch);
 
-int is_rfc3986_unreserved(char ch);
-int is_rfc3986_reserved_or_unreserved(char ch);
-
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
 			     char_predicate allow_unencoded_fn);
 
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 5/7] object-name: move related functions to object-name
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
                             ` (3 preceding siblings ...)
  2023-06-06 19:48           ` [PATCH v7 4/7] credential-store: move related functions to credential-store file Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 6/7] path: move related function to path Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 7/7] strbuf: remove global variable Calvin Wan
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

Move object-name-related functions from strbuf.[ch] to object-name.[ch]
so that strbuf is focused on string manipulation routines with minimal
dependencies.

dir.h relied on the forward declration of the repository struct in
strbuf.h. Since that is removed in this patch, add the forward
declaration to dir.h.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 dir.h         |  2 ++
 object-name.c | 15 +++++++++++++++
 object-name.h |  9 +++++++++
 pretty.c      |  1 +
 strbuf.c      | 16 ----------------
 strbuf.h      | 10 ----------
 6 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/dir.h b/dir.h
index 79b85a01ee..4d83febe9e 100644
--- a/dir.h
+++ b/dir.h
@@ -40,6 +40,8 @@
  *
  */
 
+struct repository;
+
 struct dir_entry {
 	unsigned int len;
 	char name[FLEX_ARRAY]; /* more */
diff --git a/object-name.c b/object-name.c
index 6fc3fa595b..34e0f56703 100644
--- a/object-name.c
+++ b/object-name.c
@@ -768,6 +768,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 		find_abbrev_len_for_pack(p, mad);
 }
 
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len)
+{
+	int r;
+	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
+	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
+	strbuf_setlen(sb, sb->len + r);
+}
+
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len)
+{
+	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
+}
+
 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 			      const struct object_id *oid, int len)
 {
diff --git a/object-name.h b/object-name.h
index 1d63698f42..9ae5223071 100644
--- a/object-name.h
+++ b/object-name.h
@@ -40,6 +40,15 @@ struct object_context {
 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
 
+/**
+ * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
+ * the strbuf `sb`.
+ */
+void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
+				   const struct object_id *oid, int abbrev_len);
+void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
+			      int abbrev_len);
+
 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
 __attribute__((format (printf, 2, 3)))
 int get_oidf(struct object_id *oid, const char *fmt, ...);
diff --git a/pretty.c b/pretty.c
index 0bb938021b..78bac2d818 100644
--- a/pretty.c
+++ b/pretty.c
@@ -18,6 +18,7 @@
 #include "gpg-interface.h"
 #include "trailer.h"
 #include "run-command.h"
+#include "object-name.h"
 
 /*
  * The limit for formatting directives, which enable the caller to append
diff --git a/strbuf.c b/strbuf.c
index 5244029d91..80b7e051cd 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "object-name.h"
 #include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
@@ -1024,21 +1023,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
 	strbuf_setlen(sb, sb->len + len);
 }
 
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len)
-{
-	int r;
-	strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
-	r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
-	strbuf_setlen(sb, sb->len + r);
-}
-
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len)
-{
-	strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
-}
-
 /*
  * Returns the length of a line, without trailing spaces.
  *
diff --git a/strbuf.h b/strbuf.h
index 114ad0c024..5d97e27b99 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -616,16 +616,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
  */
 void strbuf_list_free(struct strbuf **list);
 
-/**
- * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
- * the strbuf `sb`.
- */
-struct repository;
-void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
-				   const struct object_id *oid, int abbrev_len);
-void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
-			      int abbrev_len);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 6/7] path: move related function to path
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
                             ` (4 preceding siblings ...)
  2023-06-06 19:48           ` [PATCH v7 5/7] object-name: move related functions to object-name Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  2023-06-06 19:48           ` [PATCH v7 7/7] strbuf: remove global variable Calvin Wan
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

Move path-related function from strbuf.[ch] to path.[ch] so that strbuf
is focused on string manipulation routines with minimal dependencies.

repository.h is no longer a necessary dependency after moving this
function out.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 path.c   | 20 ++++++++++++++++++++
 path.h   |  5 +++++
 strbuf.c | 21 ---------------------
 3 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/path.c b/path.c
index 7c1cd8182a..e17a2613c5 100644
--- a/path.c
+++ b/path.c
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
 	return normalize_path_copy_len(dst, src, NULL);
 }
 
+int strbuf_normalize_path(struct strbuf *src)
+{
+	struct strbuf dst = STRBUF_INIT;
+
+	strbuf_grow(&dst, src->len);
+	if (normalize_path_copy(dst.buf, src->buf) < 0) {
+		strbuf_release(&dst);
+		return -1;
+	}
+
+	/*
+	 * normalize_path does not tell us the new length, so we have to
+	 * compute it by looking for the new NUL it placed
+	 */
+	strbuf_setlen(&dst, strlen(dst.buf));
+	strbuf_swap(src, &dst);
+	strbuf_release(&dst);
+	return 0;
+}
+
 /*
  * path = Canonical absolute path
  * prefixes = string_list containing normalized, absolute paths without
diff --git a/path.h b/path.h
index 60e83a49a9..639372edd9 100644
--- a/path.h
+++ b/path.h
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
 int normalize_path_copy(char *dst, const char *src);
+/**
+ * Normalize in-place the path contained in the strbuf. If an error occurs,
+ * the contents of "sb" are left untouched, and -1 is returned.
+ */
+int strbuf_normalize_path(struct strbuf *src);
 int longest_ancestor_length(const char *path, struct string_list *prefixes);
 char *strip_path_suffix(const char *path, const char *suffix);
 int daemon_avoid_alias(const char *path);
diff --git a/strbuf.c b/strbuf.c
index 80b7e051cd..d5978fee4e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -3,7 +3,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1089,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 	strbuf_setlen(sb, j);
 }
 
-int strbuf_normalize_path(struct strbuf *src)
-{
-	struct strbuf dst = STRBUF_INIT;
-
-	strbuf_grow(&dst, src->len);
-	if (normalize_path_copy(dst.buf, src->buf) < 0) {
-		strbuf_release(&dst);
-		return -1;
-	}
-
-	/*
-	 * normalize_path does not tell us the new length, so we have to
-	 * compute it by looking for the new NUL it placed
-	 */
-	strbuf_setlen(&dst, strlen(dst.buf));
-	strbuf_swap(src, &dst);
-	strbuf_release(&dst);
-	return 0;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
 	char *path_sep = find_last_dir_sep(sb->buf);
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

* [PATCH v7 7/7] strbuf: remove global variable
  2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
                             ` (5 preceding siblings ...)
  2023-06-06 19:48           ` [PATCH v7 6/7] path: move related function to path Calvin Wan
@ 2023-06-06 19:48           ` Calvin Wan
  6 siblings, 0 replies; 85+ messages in thread
From: Calvin Wan @ 2023-06-06 19:48 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan

As a library that only interacts with other primitives, strbuf should
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-NUL character to determine whether to skip
comments or not.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 add-patch.c          | 12 +++++++-----
 builtin/am.c         |  2 +-
 builtin/branch.c     |  4 ++--
 builtin/commit.c     |  2 +-
 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 +++--
 rebase-interactive.c | 15 ++++++++-------
 sequencer.c          | 24 +++++++++++++++---------
 strbuf.c             | 18 ++++++++++--------
 strbuf.h             | 14 ++++++++------
 wt-status.c          |  6 +++---
 16 files changed, 90 insertions(+), 64 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..9702c1aabd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	size_t i;
 
 	strbuf_reset(&s->buf);
-	strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
-				      "a quick guide.\n"));
+	strbuf_commented_addf(&s->buf, comment_line_char,
+			      _("Manual hunk edit mode -- see bottom for "
+				"a quick guide.\n"));
 	render_hunk(s, hunk, 0, 0, &s->buf);
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("---\n"
 				"To remove '%c' lines, make them ' ' lines "
 				"(context).\n"
@@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 			      s->mode->is_reverse ? '+' : '-',
 			      s->mode->is_reverse ? '-' : '+',
 			      comment_line_char);
-	strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
+	strbuf_commented_addf(&s->buf, comment_line_char, "%s",
+			      _(s->mode->edit_hunk_hint));
 	/*
 	 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
 	 * messages.
 	 */
-	strbuf_commented_addf(&s->buf,
+	strbuf_commented_addf(&s->buf, comment_line_char,
 			      _("If it does not apply cleanly, you will be "
 				"given an opportunity to\n"
 				"edit again.  If all lines of the hunk are "
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..9ece2e3066 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1283,7 +1283,7 @@ 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');
 
 	assert(!state->author_name);
 	state->author_name = strbuf_detach(&author_name, NULL);
diff --git a/builtin/branch.c b/builtin/branch.c
index e6c2655af6..40a79e60f5 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -674,7 +674,7 @@ static int edit_branch_description(const char *branch_name)
 	exists = !read_branch_desc(&buf, branch_name);
 	if (!buf.len || buf.buf[buf.len-1] != '\n')
 		strbuf_addch(&buf, '\n');
-	strbuf_commented_addf(&buf,
+	strbuf_commented_addf(&buf, comment_line_char,
 		    _("Please edit the description for the branch\n"
 		      "  %s\n"
 		      "Lines starting with '%c' will be stripped.\n"),
@@ -685,7 +685,7 @@ static int edit_branch_description(const char *branch_name)
 		strbuf_release(&buf);
 		return -1;
 	}
-	strbuf_stripspace(&buf, 1);
+	strbuf_stripspace(&buf, comment_line_char);
 
 	strbuf_addf(&name, "branch.%s.description", branch_name);
 	if (buf.len || exists)
diff --git a/builtin/commit.c b/builtin/commit.c
index e67c4be221..e002ebf070 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	s->hints = 0;
 
 	if (clean_message_contents)
-		strbuf_stripspace(&sb, 0);
+		strbuf_stripspace(&sb, '\0');
 
 	if (signoff)
 		append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..1d14767c0c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		strbuf_addch(&msg, '\n');
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
 			wt_status_append_cut_line(&msg);
-			strbuf_commented_addf(&msg, "\n");
+			strbuf_commented_addf(&msg, comment_line_char, "\n");
 		}
-		strbuf_commented_addf(&msg, _(merge_editor_comment));
+		strbuf_commented_addf(&msg, comment_line_char,
+				      _(merge_editor_comment));
 		if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
-			strbuf_commented_addf(&msg, _(scissors_editor_comment));
+			strbuf_commented_addf(&msg, comment_line_char,
+					      _(scissors_editor_comment));
 		else
-			strbuf_commented_addf(&msg,
+			strbuf_commented_addf(&msg, comment_line_char,
 				_(no_scissors_editor_comment), comment_line_char);
 	}
 	if (signoff)
diff --git a/builtin/notes.c b/builtin/notes.c
index d5788352b6..3bad5b458b 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "editor.h"
+#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
 
 	if (strbuf_read(&buf, show.out, 0) < 0)
 		die_errno(_("could not read 'show' output"));
-	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
+	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
 	write_or_die(fd, cbuf.buf, cbuf.len);
 
 	strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 			copy_obj_to_fd(fd, old_note);
 
 		strbuf_addch(&buf, '\n');
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
-		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
-		strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
+		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
+					   comment_line_char);
+		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
 		write_or_die(fd, buf.buf, buf.len);
 
 		write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 		if (launch_editor(d->edit_path, &d->buf, NULL)) {
 			die(_("please supply the note contents using either -m or -F option"));
 		}
-		strbuf_stripspace(&d->buf, 1);
+		strbuf_stripspace(&d->buf, comment_line_char);
 	}
 }
 
@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	if (d->buf.len)
 		strbuf_addch(&d->buf, '\n');
 	strbuf_addstr(&d->buf, arg);
-	strbuf_stripspace(&d->buf, 0);
+	strbuf_stripspace(&d->buf, '\0');
 
 	d->given = 1;
 	return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 			die_errno(_("cannot read '%s'"), arg);
 	} 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');
 
 	d->given = 1;
 	return 0;
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..b1ba9fb05d 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error_errno(_("could not read '%s'."), todo_file);
 
-	strbuf_stripspace(&todo_list.buf, 1);
+	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)))
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 9451eb69ff..1987752359 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -1,6 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
 #include "config.h"
+#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "setup.h"
@@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
 	size_t len;
 
 	msg = strbuf_detach(buf, &len);
-	strbuf_add_commented_lines(buf, msg, len);
+	strbuf_add_commented_lines(buf, msg, len, comment_line_char);
 	free(msg);
 }
 
@@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		die_errno("could not read the input");
 
 	if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
-		strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
+		strbuf_stripspace(&buf,
+			  mode == STRIP_COMMENTS ? comment_line_char : '\0');
 	else
 		comment_lines(&buf);
 
diff --git a/builtin/tag.c b/builtin/tag.c
index 1850a6a6fd..b79e0a88e6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addch(&buf, '\n');
 			if (opt->cleanup_mode == CLEANUP_ALL)
-				strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template), tag, comment_line_char);
 			else
-				strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
+				strbuf_commented_addf(&buf, comment_line_char,
+				      _(tag_template_nocleanup), tag, comment_line_char);
 			write_or_die(fd, buf.buf, buf.len);
 			strbuf_release(&buf);
 		}
@@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	}
 
 	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 : '\0');
 
 	if (!opt->message_given && !buf->len)
 		die(_("no tag message?"));
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..5d15e2387d 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
 	strbuf_complete_line(tagbuf);
 	if (sig->len) {
 		strbuf_addch(tagbuf, '\n');
-		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len);
+		strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
+					   comment_line_char);
 	}
 }
 
@@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 				strbuf_addch(&tagline, '\n');
 				strbuf_add_commented_lines(&tagline,
 						origins.items[first_tag].string,
-						strlen(origins.items[first_tag].string));
+						strlen(origins.items[first_tag].string),
+						comment_line_char);
 				strbuf_insert(&tagbuf, 0, tagline.buf,
 					      tagline.len);
 				strbuf_release(&tagline);
@@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 			strbuf_addch(&tagbuf, '\n');
 			strbuf_add_commented_lines(&tagbuf,
 					origins.items[i].string,
-					strlen(origins.items[i].string));
+					strlen(origins.items[i].string),
+					comment_line_char);
 			fmt_tag_signature(&tagbuf, &sig, buf, len);
 		}
 		strbuf_release(&payload);
diff --git a/gpg-interface.c b/gpg-interface.c
index 19a3471a0b..6a3817bbca 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -13,6 +13,7 @@
 #include "tempfile.h"
 #include "alias.h"
 #include "wrapper.h"
+#include "environment.h"
 
 static int git_gpg_config(const char *, const char *, void *);
 
@@ -586,8 +587,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
 		}
 	}
 
-	strbuf_stripspace(&ssh_keygen_out, 0);
-	strbuf_stripspace(&ssh_keygen_err, 0);
+	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);
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 852a331318..f286404d4b 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -72,13 +72,14 @@ void append_todo_help(int command_count,
 
 	if (!edit_todo) {
 		strbuf_addch(buf, '\n');
-		strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
-					      "Rebase %s onto %s (%d commands)",
-					      command_count),
+		strbuf_commented_addf(buf, comment_line_char,
+				      Q_("Rebase %s onto %s (%d command)",
+					 "Rebase %s onto %s (%d commands)",
+					 command_count),
 				      shortrevisions, shortonto, command_count);
 	}
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
 		msg = _("\nDo not remove any line. Use 'drop' "
@@ -87,7 +88,7 @@ void append_todo_help(int command_count,
 		msg = _("\nIf you remove a line here "
 			 "THAT COMMIT WILL BE LOST.\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 
 	if (edit_todo)
 		msg = _("\nYou are editing the todo file "
@@ -98,7 +99,7 @@ void append_todo_help(int command_count,
 		msg = _("\nHowever, if you remove everything, "
 			"the rebase will be aborted.\n\n");
 
-	strbuf_add_commented_lines(buf, msg, strlen(msg));
+	strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
 }
 
 int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@@ -130,7 +131,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
 	if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
 		return -2;
 
-	strbuf_stripspace(&new_todo->buf, 1);
+	strbuf_stripspace(&new_todo->buf, comment_line_char);
 	if (initial && new_todo->buf.len == 0)
 		return -3;
 
diff --git a/sequencer.c b/sequencer.c
index bceb6abcb6..a8a70ab235 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -660,11 +660,12 @@ void append_conflicts_hint(struct index_state *istate,
 	}
 
 	strbuf_addch(msgbuf, '\n');
-	strbuf_commented_addf(msgbuf, "Conflicts:\n");
+	strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
 	for (i = 0; i < istate->cache_nr;) {
 		const struct cache_entry *ce = istate->cache[i++];
 		if (ce_stage(ce)) {
-			strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
+			strbuf_commented_addf(msgbuf, comment_line_char,
+					      "\t%s\n", ce->name);
 			while (i < istate->cache_nr &&
 			       !strcmp(ce->name, istate->cache[i]->name))
 				i++;
@@ -1143,7 +1144,8 @@ void cleanup_message(struct strbuf *msgbuf,
 	    cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
 		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 : '\0');
 }
 
 /*
@@ -1174,7 +1176,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
 	if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 		return 0;
 
-	strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
+	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);
@@ -1546,7 +1549,8 @@ static int try_to_commit(struct repository *r,
 		cleanup = opts->default_msg_cleanup;
 
 	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 : '\0');
 	if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
 		res = 1; /* run 'git commit' to display error message */
 		goto out;
@@ -1840,7 +1844,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
 		s += count;
 		len -= count;
 	}
-	strbuf_add_commented_lines(buf, s, len);
+	strbuf_add_commented_lines(buf, s, len, comment_line_char);
 }
 
 /* Does the current fixup chain contain a squash command? */
@@ -1939,7 +1943,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
 	strbuf_addf(buf, _(nth_commit_msg_fmt),
 		    ++opts->current_fixup_count + 1);
 	strbuf_addstr(buf, "\n\n");
-	strbuf_add_commented_lines(buf, body, commented_len);
+	strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
 	/* buf->buf may be reallocated so store an offset into the buffer */
 	fixup_off = buf->len;
 	strbuf_addstr(buf, body + commented_len);
@@ -2029,7 +2033,8 @@ static int update_squash_messages(struct repository *r,
 			      _(first_commit_msg_str));
 		strbuf_addstr(&buf, "\n\n");
 		if (is_fixup_flag(command, flag))
-			strbuf_add_commented_lines(&buf, body, strlen(body));
+			strbuf_add_commented_lines(&buf, body, strlen(body),
+						   comment_line_char);
 		else
 			strbuf_addstr(&buf, body);
 
@@ -2048,7 +2053,8 @@ static int update_squash_messages(struct repository *r,
 		strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
 			    ++opts->current_fixup_count + 1);
 		strbuf_addstr(&buf, "\n\n");
-		strbuf_add_commented_lines(&buf, body, strlen(body));
+		strbuf_add_commented_lines(&buf, body, strlen(body),
+					   comment_line_char);
 	} else
 		return error(_("unknown command: %d"), command);
 	repo_unuse_commit_buffer(r, commit, message);
diff --git a/strbuf.c b/strbuf.c
index d5978fee4e..67e399b60a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
 #include "git-compat-util.h"
 #include "alloc.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "strbuf.h"
@@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
 	strbuf_complete_line(out);
 }
 
-void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
+void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
+				size_t size, char comment_line_char)
 {
 	static char prefix1[3];
 	static char prefix2[2];
@@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
 	add_lines(out, prefix1, prefix2, buf, size);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
+			   const char *fmt, ...)
 {
 	va_list params;
 	struct strbuf buf = STRBUF_INIT;
@@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_vaddf(&buf, fmt, params);
 	va_end(params);
 
-	strbuf_add_commented_lines(sb, buf.buf, buf.len);
+	strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
 	if (incomplete_line)
 		sb->buf[--sb->len] = '\0';
 
@@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
  *
  * 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-NUL 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, char comment_line_char)
 {
 	size_t empties = 0;
 	size_t i, j, len, newlen;
@@ -1067,7 +1068,8 @@ 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 && len &&
+		    sb->buf[i] == comment_line_char) {
 			newlen = 0;
 			continue;
 		}
diff --git a/strbuf.h b/strbuf.h
index 5d97e27b99..da91c17257 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -291,7 +291,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  * by a comment character and a blank.
  */
 void strbuf_add_commented_lines(struct strbuf *out,
-				const char *buf, size_t size);
+				const char *buf, size_t size,
+				char comment_line_char);
 
 
 /**
@@ -420,8 +421,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
  */
-__attribute__((format (printf, 2, 3)))
-void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
 
 __attribute__((format (printf,2,0)))
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@@ -543,10 +544,11 @@ int strbuf_getcwd(struct strbuf *sb);
 int strbuf_normalize_path(struct strbuf *sb);
 
 /**
- * Strip whitespace from a buffer. The second parameter controls if
- * comments are considered contents to be removed or not.
+ * Strip whitespace from a buffer. If comment_line_char is non-NUL,
+ * then lines beginning with that character are considered comments,
+ * thus removed.
  */
-void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+void strbuf_stripspace(struct strbuf *buf, char comment_line_char);
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {
diff --git a/wt-status.c b/wt-status.c
index 068b76ef6d..677465b10c 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1024,7 +1024,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
 	if (s->display_comment_prefix) {
 		size_t len;
 		summary_content = strbuf_detach(&summary, &len);
-		strbuf_add_commented_lines(&summary, summary_content, len);
+		strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
 		free(summary_content);
 	}
 
@@ -1099,8 +1099,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
 {
 	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
 
-	strbuf_commented_addf(buf, "%s", cut_line);
-	strbuf_add_commented_lines(buf, explanation, strlen(explanation));
+	strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
+	strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
 void wt_status_add_cut_line(FILE *fp)
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 85+ messages in thread

end of thread, other threads:[~2023-06-06 19:49 UTC | newest]

Thread overview: 85+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
2023-05-02 21:42   ` Junio C Hamano
2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
2023-05-02 21:52   ` Junio C Hamano
2023-05-03 16:28   ` Jeff King
2023-05-03 16:34     ` Jeff King
2023-05-03 18:38       ` Calvin Wan
2023-05-02 21:14 ` [PATCH 3/6] object-name: move related functions to object-name Calvin Wan
2023-05-02 21:14 ` [PATCH 4/6] path: move related function to path Calvin Wan
2023-05-02 21:14 ` [PATCH 5/6] strbuf: clarify dependency Calvin Wan
2023-05-03  1:56   ` Elijah Newren
2023-05-02 21:14 ` [PATCH 6/6] strbuf: remove environment variables Calvin Wan
2023-05-03  2:15   ` Elijah Newren
2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
2023-05-02 22:31   ` Junio C Hamano
2023-05-02 23:51     ` Felipe Contreras
2023-05-02 22:36   ` Calvin Wan
2023-05-03  2:37 ` Elijah Newren
2023-05-03 18:00   ` Calvin Wan
2023-05-07  0:14     ` Elijah Newren
2023-05-07 13:14       ` Jeff King
2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
2023-05-03 18:50   ` [PATCH v2 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-03 18:50   ` [PATCH v2 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-03 18:50   ` [PATCH v2 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-03 18:50   ` [PATCH v2 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-03 18:50   ` [PATCH v2 5/7] path: move related function to path Calvin Wan
2023-05-03 18:50   ` [PATCH v2 6/7] strbuf: clarify dependency Calvin Wan
2023-05-03 19:26     ` Junio C Hamano
2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
2023-05-03 19:24     ` Junio C Hamano
2023-05-03 19:41       ` Calvin Wan
2023-05-03 19:45         ` Junio C Hamano
2023-05-03 19:42     ` [PATCH v3 7/7] strbuf: remove environment variable Calvin Wan
2023-05-05 22:33   ` [PATCH v2 0/7] strbuf cleanups Junio C Hamano
2023-05-08 16:38     ` Calvin Wan
2023-05-07  0:40   ` Elijah Newren
2023-05-07 21:47     ` Felipe Contreras
2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-08 17:22       ` Eric Sunshine
2023-05-10 22:51         ` Junio C Hamano
2023-05-08 16:59     ` [PATCH v4 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-08 16:59     ` [PATCH v4 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-08 16:59     ` [PATCH v4 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-08 16:59     ` [PATCH v4 5/7] path: move related function to path Calvin Wan
2023-05-08 16:59     ` [PATCH v4 6/7] strbuf: clarify dependency Calvin Wan
2023-05-08 16:59     ` [PATCH v4 7/7] strbuf: remove global variable Calvin Wan
2023-05-10  8:12       ` Phillip Wood
2023-05-09  1:57     ` [PATCH v4 0/7] strbuf cleanups Elijah Newren
2023-05-09  2:13       ` Felipe Contreras
2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-11 19:57         ` Eric Sunshine
2023-05-11 20:03           ` Calvin Wan
2023-05-11 19:48       ` [PATCH v5 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-11 19:48       ` [PATCH v5 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-11 19:48       ` [PATCH v5 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-11 19:48       ` [PATCH v5 5/7] path: move related function to path Calvin Wan
2023-05-11 19:48       ` [PATCH v5 6/7] strbuf: clarify dependency Calvin Wan
2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
2023-05-11 20:24         ` Eric Sunshine
2023-05-11 21:42         ` Junio C Hamano
2023-05-12 14:54           ` Phillip Wood
2023-05-12 14:53         ` Phillip Wood
2023-05-12 19:31           ` Junio C Hamano
2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
2023-05-12 17:15         ` [PATCH v6 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-12 17:15         ` [PATCH v6 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-12 17:15         ` [PATCH v6 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-12 17:15         ` [PATCH v6 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-12 17:15         ` [PATCH v6 5/7] path: move related function to path Calvin Wan
2023-05-12 17:15         ` [PATCH v6 6/7] strbuf: clarify dependency Calvin Wan
2023-05-12 17:15         ` [PATCH v6 7/7] strbuf: remove global variable Calvin Wan
2023-05-12 20:24         ` [PATCH v6 0/7] strbuf cleanups Junio C Hamano
2023-05-13  5:54           ` Eric Sunshine
2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
2023-06-06 19:48           ` [PATCH v7 1/7] strbuf: clarify API boundary Calvin Wan
2023-06-06 19:48           ` [PATCH v7 2/7] strbuf: clarify dependency Calvin Wan
2023-06-06 19:48           ` [PATCH v7 3/7] abspath: move related functions to abspath Calvin Wan
2023-06-06 19:48           ` [PATCH v7 4/7] credential-store: move related functions to credential-store file Calvin Wan
2023-06-06 19:48           ` [PATCH v7 5/7] object-name: move related functions to object-name Calvin Wan
2023-06-06 19:48           ` [PATCH v7 6/7] path: move related function to path Calvin Wan
2023-06-06 19:48           ` [PATCH v7 7/7] strbuf: remove global variable Calvin Wan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).