aboutsummaryrefslogtreecommitdiffstats
path: root/config.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2024-03-15 12:43:58 -0700
committerJunio C Hamano <gitster@pobox.com>2024-03-15 16:07:37 -0700
commitfbad334db9cff30cdf1fe3d498dec737bfae38df (patch)
treeaf08f0a5f5c0a7397344084df6a9a1d7106ee66d /config.c
parent42d5c033945e4fc41d7268bfe4284d37651986b8 (diff)
downloadgit-fbad334db9cff30cdf1fe3d498dec737bfae38df.tar.gz
config: fix --comment formatting
When git adds comments itself (like "rebase -i" todo list and "commit -e" log message editor), it always gives a comment introducer "#" followed by a Space before the message, except for the recently introduced "git config --comment", where the users are forced to say " this is my comment" if they want to add their comment in this usual format; otherwise their comment string will end up without a space after the "#". Make it more ergonomic, while keeping it possible to also use this unusual style, by massaging the comment string at the UI layer with a set of simple rules: * If the given comment string begins with '#', it is passed intact. * Otherwise, "# " is prefixed. * A string with LF in it cannot be used as a comment string. Right now there is only one "front-end" that accepts end-user comment string and calls the underlying machinery to add or modify configuration file with comments, but to make sure that the future callers perform similar massaging as they see fit, add a sanity check logic in git_config_set_multivar_in_file_gently(), which is the single choke point in the codepaths that consumes the comment string. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'config.c')
-rw-r--r--config.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/config.c b/config.c
index 2f3f6d123c..15019cb9a5 100644
--- a/config.c
+++ b/config.c
@@ -3043,12 +3043,9 @@ static ssize_t write_pair(int fd, const char *key, const char *value,
break;
}
- if (comment) {
- if (strchr(comment, '\n'))
- die(_("multi-line comments are not permitted: '%s'"), comment);
- else
- strbuf_addf(&sb, "%s #%s\n", quote, comment);
- } else
+ if (comment)
+ strbuf_addf(&sb, "%s %s\n", quote, comment);
+ else
strbuf_addf(&sb, "%s\n", quote);
ret = write_in_full(fd, sb.buf, sb.len);
@@ -3214,6 +3211,17 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
size_t contents_sz;
struct config_store_data store = CONFIG_STORE_INIT;
+ if (comment) {
+ /*
+ * The front-end must have massaged the comment string
+ * properly before calling us.
+ */
+ if (strchr(comment, '\n'))
+ BUG("multi-line comments are not permitted: '%s'", comment);
+ if (comment[0] != '#')
+ BUG("comment should begin with '#': '%s'", comment);
+ }
+
/* parse-key returns negative; flip the sign to feed exit(3) */
ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
if (ret)