From: Phillip Wood <phil…@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 character for compatibility with
older versions of git. That makes it awkward for scripts to determine
what the comment string 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 string.
Signed-off-by: Phillip Wood <phil…@dunelm.org.uk>
---
Thanks to Junio and Kristoffer for their comments
Changes since V2:
- Fixed sed expression in commit message
- Remove quotes around "git var" command in the documentation
Changes since V1:
- Fixed typos in commit message
- Added a reference to `git var GIT_COMMENT_STRING` to the
documentation for core.commentString
- Added a reference to the relevant config keys to the documentation
for GIT_COMMENT_STRING
base-commit: f7de81456940dd6f3274e461149bba12965de609
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fvar-comment-string%2Fv3
View-Changes-At: https://github.com/phillipwood/git/compare/f7de81456...e3dcd5207
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/var-comment-string/v3
Documentation/config/core.adoc | 3 ++-
Documentation/git-var.adoc | 4 ++++
builtin/var.c | 9 +++++++++
t/t0007-git-var.sh | 10 ++++++++++
4 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc
index bdc74d291cc..796131006ac 100644
--- a/Documentation/config/core.adoc
+++ b/Documentation/config/core.adoc
@@ -549,7 +549,8 @@ core.commentString::
Commands such as `commit` and `tag` that let you edit
messages consider a line that begins with this character
commented, and removes them after the editor returns
- (default '#').
+ (default '#'). Scripts can query the comment string with
+ `git var GIT_COMMENT_STRING`.
+
ifndef::with-breaking-changes[]
If set to "auto", `git-commit` will select a character that is not
diff --git a/Documentation/git-var.adoc b/Documentation/git-var.adoc
index 5f240eb56e3..9c3778b2535 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. as set by `core.commentString` or `core.commentChar`.
+
`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
Range-diff against v2:
1: 6e310e0af58 ! 1: e3dcd520771 var: add GIT_COMMENT_STRING
@@ Commit message
{
echo ' #'
git config --get-regex '^core.comment(char|string)$'
- } | sed -n '$s/^[^ ] //p'
+ } | sed -n '$s/^[^ ]* //p'
Add GIT_COMMENT_STRING to "git var" to make it easier for scripts to
determine the comment string.
@@ Documentation/config/core.adoc: core.commentString::
commented, and removes them after the editor returns
- (default '#').
+ (default '#'). Scripts can query the comment string with
-+ "`git var GIT_COMMENT_STRING`".
++ `git var GIT_COMMENT_STRING`.
+
ifndef::with-breaking-changes[]
If set to "auto", `git-commit` will select a character that is not
--
2.56.1.576.g0cfbda1d5c7.dirty