environment: store comment_line_char as a string

We'd like to eventually support multi-byte comment prefixes, but the comment_line_char variable is referenced in many spots, making the transition difficult. Let's start by storing the character in a NUL-terminated string. That will let us switch code over incrementally to the string format, and we can easily support the existing code with a macro wrapper (since we'll continue to allow only a single-byte prefix, this will behave identically). Once all references to the "char" variable have been converted, we can drop it and enable longer strings. We'll still have to touch all of the spots that create or set the variable in this patch, but there are only a few (reading the config, and the "auto" character selector). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 12, 2024 at 05:17 UTC 72a7d5d97fe0338719a45787994b04a4170719da
4 files changed +6 -5
builtin/commit.c
+2 -2
@@ -685,7 +685,7 @@ static void adjust_comment_line_char(const struct strbuf *sb)
685 const char *p;
686
687 if (!memchr(sb->buf, candidates[0], sb->len)) {
688 - comment_line_char = candidates[0];
688 + comment_line_str = xstrfmt("%c", candidates[0]);
689 return;
690 }
691
@@ -706,7 +706,7 @@ static void adjust_comment_line_char(const struct strbuf *sb)
706 if (!*p)
707 die(_("unable to select a comment character that is not used\n"
708 "in the current commit message"));
709 - comment_line_char = *p;
709 + comment_line_str = xstrfmt("%c", *p);
710 }
711
712 static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
config.c
+1 -1
@@ -1568,7 +1568,7 @@ static int git_default_core_config(const char *var, const char *value,
1568 else if (value[0] && !value[1]) {
1569 if (value[0] == '\n')
1570 return error(_("core.commentChar cannot be newline"));
1571 - comment_line_char = value[0];
1571 + comment_line_str = xstrfmt("%c", value[0]);
1572 auto_comment_line_char = 0;
1573 } else
1574 return error(_("core.commentChar should only be one ASCII character"));
environment.c
+1 -1
@@ -110,7 +110,7 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT;
110 * The character that begins a commented line in user-editable file
111 * that is subject to stripspace.
112 */
113 -char comment_line_char = '#';
113 +const char *comment_line_str = "#";
114 int auto_comment_line_char;
115
116 /* Parallel index stat data preload? */
environment.h
+2 -1
@@ -8,7 +8,8 @@ struct strvec;
8 * The character that begins a commented line in user-editable file
9 * that is subject to stripspace.
10 */
11 -extern char comment_line_char;
11 +#define comment_line_char (comment_line_str[0])
12 +extern const char *comment_line_str;
13 extern int auto_comment_line_char;
14
15 /*