# [PATCH] var: add GIT_COMMENT_STRING

3 messages from 2026-11-24 to 2026-11-24. Participants: Phillip Wood, Kristoffer Haugsbakk, Junio C Hamano.
Thread: https://gitlist.dev/t/66926

## Phillip Wood, 2026-11-24 10:37

Subject: [PATCH] var: add GIT_COMMENT_STRING
Message-ID: <fa8b80af26481e0dc96fd8f80f3c7ba84f60f9d4.1795516651.git.phillip.wood@dunelm.org.uk>
URL: https://gitlist.dev/e/fa8b80af26481e0dc96fd8f80f3c7ba84f60f9d4.1795516651.git.phillip.wood%40dunelm.org.uk

```
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Since commit 9ccf3e9b22 (config: add core.commentString, 2024-03-27) we
have two separate config keys that set the comment string used by git
when creating template commit messages etc. As that commit notes, when
core.commentString is set to a multi-byte string, it can be useful to
keep core.commentChar set to a single chararcter for compatibilty with
older versions of git. That makes it awkward for scripts to determine
what the comment chararcter is because they need to do something like

    {
        echo ' #'
        git config --get-regex '^core.comment(char|string)$'
    } | sed -n '$s/^[^ ] //p'

Add GIT_COMMENT_STRING to "git var" to make it easier for scripts to
determine the comment character.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
base-commit: f7de81456940dd6f3274e461149bba12965de609
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fvar-comment-string%2Fv1
View-Changes-At: https://github.com/phillipwood/git/compare/f7de81456...fa8b80af2
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/var-comment-string/v1

 Documentation/git-var.adoc |  4 ++++
 builtin/var.c              |  9 +++++++++
 t/t0007-git-var.sh         | 10 ++++++++++
 3 files changed, 23 insertions(+)

diff --git a/Documentation/git-var.adoc b/Documentation/git-var.adoc
index 5f240eb56e3..04aeccd7aa6 100644
--- a/Documentation/git-var.adoc
+++ b/Documentation/git-var.adoc
@@ -130,6 +130,10 @@ endif::git-default-pager[]
     The path to the global (per-user) configuration files, if any.
     This variable can have multiple values.
 
+`GIT_COMMENT_STRING`::
+    The comment string used in template commit messages and rebase todo
+    lists etc.
+
 `GIT_HOSTNAME`::
     The hostname of the machine, as reported by the operating system.
     This is the hostname matched by `includeIf "hostname:..."` (see
diff --git a/builtin/var.c b/builtin/var.c
index eb85a985b15..6dc8ec4ba9f 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -188,6 +188,11 @@ static char *hostname(int ident_flag UNUSED)
 	return xstrdup(buf);
 }
 
+static char *comment_string(int ident_flag UNUSED)
+{
+	return xstrdup(comment_line_str);
+}
+
 struct git_var {
 	const char *name;
 	char *(*read)(int);
@@ -262,6 +267,10 @@ static struct git_var git_vars[] = {
 		.name = "GIT_CONFIG_GLOBAL",
 		.multiread = git_config_val_global,
 	},
+	{
+		.name = "GIT_COMMENT_STRING",
+		.read = comment_string,
+	},
 	{
 		.name = "GIT_HOSTNAME",
 		.read = hostname,
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 883a45399e2..f228cb5bb6f 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -434,4 +434,14 @@ test_expect_success 'git var -l lists the identity components' '
 	test_grep "^GIT_COMMITTER_DATE=" actual
 '
 
+test_expect_success 'GIT_COMMENT_STRING' '
+	git var GIT_COMMENT_STRING >actual &&
+	echo "#" >expect &&
+	test_cmp expect actual &&
+	git -c core.commentChar=: -c core.commentString=%% \
+		var GIT_COMMENT_STRING >actual &&
+	echo %%>expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.56.1.576.g0cfbda1d5c7.dirty



```

## Kristoffer Haugsbakk, 2026-11-24 15:02

Subject: Re: [PATCH] var: add GIT_COMMENT_STRING
Message-ID: <b8c40805-90f2-414c-ae70-e2f79a19170e@app.fastmail.com>
URL: https://gitlist.dev/e/b8c40805-90f2-414c-ae70-e2f79a19170e%40app.fastmail.com
In-Reply-To: <fa8b80af26481e0dc96fd8f80f3c7ba84f60f9d4.1795516651.git.phillip.wood@dunelm.org.uk>

```
On Tue, Nov 24, 2026, at 11:37, Phillip Wood wrote:
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> Since commit 9ccf3e9b22 (config: add core.commentString, 2024-03-27) we
> have two separate config keys that set the comment string used by git
> when creating template commit messages etc. As that commit notes, when
> core.commentString is set to a multi-byte string, it can be useful to
> keep core.commentChar set to a single chararcter for compatibilty with
> older versions of git. That makes it awkward for scripts to determine
> what the comment chararcter is because they need to do something like
>
>     {
>         echo ' #'
>         git config --get-regex '^core.comment(char|string)$'
>     } | sed -n '$s/^[^ ] //p'

Yeah that’s inconvenient.

>
> Add GIT_COMMENT_STRING to "git var" to make it easier for scripts to
> determine the comment character.

s/determine the comment character/determine the comment string/ ?

>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
> base-commit: f7de81456940dd6f3274e461149bba12965de609
> Published-As:
> https://github.com/phillipwood/git/releases/tag/pw%2Fvar-comment-string%2Fv1
> View-Changes-At:
> https://github.com/phillipwood/git/compare/f7de81456...fa8b80af2
> Fetch-It-Via: git fetch https://github.com/phillipwood/git
> pw/var-comment-string/v1
>
>  Documentation/git-var.adoc |  4 ++++
>  builtin/var.c              |  9 +++++++++
>  t/t0007-git-var.sh         | 10 ++++++++++
>  3 files changed, 23 insertions(+)
>
> diff --git a/Documentation/git-var.adoc b/Documentation/git-var.adoc
> index 5f240eb56e3..04aeccd7aa6 100644
> --- a/Documentation/git-var.adoc
> +++ b/Documentation/git-var.adoc
> @@ -130,6 +130,10 @@ endif::git-default-pager[]
>      The path to the global (per-user) configuration files, if any.
>      This variable can have multiple values.
>
> +`GIT_COMMENT_STRING`::
> +    The comment string used in template commit messages and rebase todo
> +    lists etc.

This could also mention the config variables and git-config(1)?

> +
>  `GIT_HOSTNAME`::
>      The hostname of the machine, as reported by the operating system.
>      This is the hostname matched by `includeIf "hostname:..."` (see
> diff --git a/builtin/var.c b/builtin/var.c
> index eb85a985b15..6dc8ec4ba9f 100644
> --- a/builtin/var.c
> +++ b/builtin/var.c
> @@ -188,6 +188,11 @@ static char *hostname(int ident_flag UNUSED)
>  	return xstrdup(buf);
>  }
>
> +static char *comment_string(int ident_flag UNUSED)
> +{
> +	return xstrdup(comment_line_str);
> +}
> +
>  struct git_var {
>  	const char *name;
>  	char *(*read)(int);
> @@ -262,6 +267,10 @@ static struct git_var git_vars[] = {
>  		.name = "GIT_CONFIG_GLOBAL",
>  		.multiread = git_config_val_global,
>  	},
> +	{
> +		.name = "GIT_COMMENT_STRING",
> +		.read = comment_string,
> +	},
>  	{
>  		.name = "GIT_HOSTNAME",
>  		.read = hostname,
> diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
> index 883a45399e2..f228cb5bb6f 100755
> --- a/t/t0007-git-var.sh
> +++ b/t/t0007-git-var.sh
> @@ -434,4 +434,14 @@ test_expect_success 'git var -l lists the identity
> components' '
>  	test_grep "^GIT_COMMITTER_DATE=" actual
>  '
>
> +test_expect_success 'GIT_COMMENT_STRING' '
> +	git var GIT_COMMENT_STRING >actual &&
> +	echo "#" >expect &&
> +	test_cmp expect actual &&

This tests the default.

> +	git -c core.commentChar=: -c core.commentString=%% \
> +		var GIT_COMMENT_STRING >actual &&
> +	echo %%>expect &&
> +	test_cmp expect actual

Then it tests that `commentString` overriding `*Char` works as it
should. That makes sense and seems to cover all testing bases.

This happens if you have an invalid comment string:

    $ bin-wrappers/git -c core.commentString= var GIT_COMMENT_STRING
    error: core.commentstring must have at least one character
    fatal: unable to parse 'core.commentstring' from command-line config

With non-zero exit status.

> +'
> +
>  test_done
> --
> 2.56.1.576.g0cfbda1d5c7.dirty


```

## Junio C Hamano, 2026-11-24 20:24

Subject: Re: [PATCH] var: add GIT_COMMENT_STRING
Message-ID: <xmqqh5h6ou4q.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqh5h6ou4q.fsf%40gitster.g
In-Reply-To: <fa8b80af26481e0dc96fd8f80f3c7ba84f60f9d4.1795516651.git.phillip.wood@dunelm.org.uk>

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

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> Since commit 9ccf3e9b22 (config: add core.commentString, 2024-03-27) we
> have two separate config keys that set the comment string used by git
> when creating template commit messages etc. As that commit notes, when
> core.commentString is set to a multi-byte string, it can be useful to
> keep core.commentChar set to a single chararcter for compatibilty with
> older versions of git. That makes it awkward for scripts to determine
> what the comment chararcter is because they need to do something like
>
>     {
>         echo ' #'
>         git config --get-regex '^core.comment(char|string)$'
>     } | sed -n '$s/^[^ ] //p'
>
> Add GIT_COMMENT_STRING to "git var" to make it easier for scripts to
> determine the comment character.

It is somewhat unfortunate that we honor last-one-wins across two
variables (otherwise it would have been just the matter of using
core.commentString, falling back to core.commentChar).

But the motivation is good.


```
