Skip to content

Commit dce1e0b

Browse files
committed
Merge branch 'jk/core-comment-string'
core.commentChar used to be limited to a single byte, but has been updated to allow an arbitrary multi-byte sequence. * jk/core-comment-string: config: add core.commentString config: allow multi-byte core.commentChar environment: drop comment_line_char compatibility macro wt-status: drop custom comment-char stringification sequencer: handle multi-byte comment characters when writing todo list find multi-byte comment chars in unterminated buffers find multi-byte comment chars in NUL-terminated strings prefer comment_line_str to comment_line_char for printing strbuf: accept a comment string for strbuf_add_commented_lines() strbuf: accept a comment string for strbuf_commented_addf() strbuf: accept a comment string for strbuf_stripspace() environment: store comment_line_char as a string strbuf: avoid shadowing global comment_line_char name commit: refactor base-case of adjust_comment_line_char() strbuf: avoid static variables in strbuf_add_commented_lines() strbuf: simplify comment-handling in add_lines() helper config: forbid newline as core.commentChar
2 parents 3256584 + 9ccf3e9 commit dce1e0b

26 files changed

+182
-136
lines changed

Documentation/config/core.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,28 @@ core.editor::
520520
`GIT_EDITOR` is not set. See linkgit:git-var[1].
521521

522522
core.commentChar::
523+
core.commentString::
523524
Commands such as `commit` and `tag` that let you edit
524-
messages consider a line that begins with this ASCII character
525+
messages consider a line that begins with this character
525526
commented, and removes them after the editor returns
526527
(default '#').
527528
+
528529
If set to "auto", `git-commit` would select a character that is not
529530
the beginning character of any line in existing commit messages.
531+
+
532+
Note that these two variables are aliases of each other, and in modern
533+
versions of Git you are free to use a string (e.g., `//` or `⁑⁕⁑`) with
534+
`commentChar`. Versions of Git prior to v2.45.0 will ignore
535+
`commentString` but will reject a value of `commentChar` that consists
536+
of more than a single ASCII byte. If you plan to use your config with
537+
older and newer versions of Git, you may want to specify both:
538+
+
539+
[core]
540+
# single character for older versions
541+
commentChar = "#"
542+
# string for newer versions (which will override commentChar
543+
# because it comes later in the file)
544+
commentString = "//"
530545

531546
core.filesRefLockTimeout::
532547
The length of time, in milliseconds, to retry when trying to

add-patch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,26 +1105,26 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11051105
size_t i;
11061106

11071107
strbuf_reset(&s->buf);
1108-
strbuf_commented_addf(&s->buf, comment_line_char,
1108+
strbuf_commented_addf(&s->buf, comment_line_str,
11091109
_("Manual hunk edit mode -- see bottom for "
11101110
"a quick guide.\n"));
11111111
render_hunk(s, hunk, 0, 0, &s->buf);
1112-
strbuf_commented_addf(&s->buf, comment_line_char,
1112+
strbuf_commented_addf(&s->buf, comment_line_str,
11131113
_("---\n"
11141114
"To remove '%c' lines, make them ' ' lines "
11151115
"(context).\n"
11161116
"To remove '%c' lines, delete them.\n"
1117-
"Lines starting with %c will be removed.\n"),
1117+
"Lines starting with %s will be removed.\n"),
11181118
s->mode->is_reverse ? '+' : '-',
11191119
s->mode->is_reverse ? '-' : '+',
1120-
comment_line_char);
1121-
strbuf_commented_addf(&s->buf, comment_line_char, "%s",
1120+
comment_line_str);
1121+
strbuf_commented_addf(&s->buf, comment_line_str, "%s",
11221122
_(s->mode->edit_hunk_hint));
11231123
/*
11241124
* TRANSLATORS: 'it' refers to the patch mentioned in the previous
11251125
* messages.
11261126
*/
1127-
strbuf_commented_addf(&s->buf, comment_line_char,
1127+
strbuf_commented_addf(&s->buf, comment_line_str,
11281128
_("If it does not apply cleanly, you will be "
11291129
"given an opportunity to\n"
11301130
"edit again. If all lines of the hunk are "
@@ -1139,7 +1139,7 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11391139
for (i = 0; i < s->buf.len; ) {
11401140
size_t next = find_next_line(&s->buf, i);
11411141

1142-
if (s->buf.buf[i] != comment_line_char)
1142+
if (!starts_with(s->buf.buf + i, comment_line_str))
11431143
strbuf_add(&s->plain, s->buf.buf + i, next - i);
11441144
i = next;
11451145
}

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ static int parse_mail(struct am_state *state, const char *mail)
12901290

12911291
strbuf_addstr(&msg, "\n\n");
12921292
strbuf_addbuf(&msg, &mi.log_message);
1293-
strbuf_stripspace(&msg, '\0');
1293+
strbuf_stripspace(&msg, NULL);
12941294

12951295
assert(!state->author_name);
12961296
state->author_name = strbuf_detach(&author_name, NULL);

builtin/branch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,18 +677,18 @@ static int edit_branch_description(const char *branch_name)
677677
exists = !read_branch_desc(&buf, branch_name);
678678
if (!buf.len || buf.buf[buf.len-1] != '\n')
679679
strbuf_addch(&buf, '\n');
680-
strbuf_commented_addf(&buf, comment_line_char,
680+
strbuf_commented_addf(&buf, comment_line_str,
681681
_("Please edit the description for the branch\n"
682682
" %s\n"
683-
"Lines starting with '%c' will be stripped.\n"),
684-
branch_name, comment_line_char);
683+
"Lines starting with '%s' will be stripped.\n"),
684+
branch_name, comment_line_str);
685685
write_file_buf(edit_description(), buf.buf, buf.len);
686686
strbuf_reset(&buf);
687687
if (launch_editor(edit_description(), &buf, NULL)) {
688688
strbuf_release(&buf);
689689
return -1;
690690
}
691-
strbuf_stripspace(&buf, comment_line_char);
691+
strbuf_stripspace(&buf, comment_line_str);
692692

693693
strbuf_addf(&name, "branch.%s.description", branch_name);
694694
if (buf.len || exists)

builtin/commit.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,10 @@ static void adjust_comment_line_char(const struct strbuf *sb)
685685
char *candidate;
686686
const char *p;
687687

688-
comment_line_char = candidates[0];
689-
if (!memchr(sb->buf, comment_line_char, sb->len))
688+
if (!memchr(sb->buf, candidates[0], sb->len)) {
689+
comment_line_str = xstrfmt("%c", candidates[0]);
690690
return;
691+
}
691692

692693
p = sb->buf;
693694
candidate = strchr(candidates, *p);
@@ -706,7 +707,7 @@ static void adjust_comment_line_char(const struct strbuf *sb)
706707
if (!*p)
707708
die(_("unable to select a comment character that is not used\n"
708709
"in the current commit message"));
709-
comment_line_char = *p;
710+
comment_line_str = xstrfmt("%c", *p);
710711
}
711712

712713
static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
@@ -889,7 +890,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
889890
s->hints = 0;
890891

891892
if (clean_message_contents)
892-
strbuf_stripspace(&sb, '\0');
893+
strbuf_stripspace(&sb, NULL);
893894

894895
if (signoff)
895896
append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
@@ -909,18 +910,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
909910
struct ident_split ci, ai;
910911
const char *hint_cleanup_all = allow_empty_message ?
911912
_("Please enter the commit message for your changes."
912-
" Lines starting\nwith '%c' will be ignored.\n") :
913+
" Lines starting\nwith '%s' will be ignored.\n") :
913914
_("Please enter the commit message for your changes."
914-
" Lines starting\nwith '%c' will be ignored, and an empty"
915+
" Lines starting\nwith '%s' will be ignored, and an empty"
915916
" message aborts the commit.\n");
916917
const char *hint_cleanup_space = allow_empty_message ?
917918
_("Please enter the commit message for your changes."
918919
" Lines starting\n"
919-
"with '%c' will be kept; you may remove them"
920+
"with '%s' will be kept; you may remove them"
920921
" yourself if you want to.\n") :
921922
_("Please enter the commit message for your changes."
922923
" Lines starting\n"
923-
"with '%c' will be kept; you may remove them"
924+
"with '%s' will be kept; you may remove them"
924925
" yourself if you want to.\n"
925926
"An empty message aborts the commit.\n");
926927
if (whence != FROM_COMMIT) {
@@ -943,12 +944,12 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
943944

944945
fprintf(s->fp, "\n");
945946
if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
946-
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
947+
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
947948
else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
948949
if (whence == FROM_COMMIT)
949950
wt_status_add_cut_line(s);
950951
} else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
951-
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
952+
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
952953

953954
/*
954955
* These should never fail because they come from our own

builtin/merge.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ static const char scissors_editor_comment[] =
822822
N_("An empty message aborts the commit.\n");
823823

824824
static const char no_scissors_editor_comment[] =
825-
N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
825+
N_("Lines starting with '%s' will be ignored, and an empty message aborts\n"
826826
"the commit.\n");
827827

828828
static void write_merge_heads(struct commit_list *);
@@ -853,16 +853,16 @@ static void prepare_to_commit(struct commit_list *remoteheads)
853853
strbuf_addch(&msg, '\n');
854854
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
855855
wt_status_append_cut_line(&msg);
856-
strbuf_commented_addf(&msg, comment_line_char, "\n");
856+
strbuf_commented_addf(&msg, comment_line_str, "\n");
857857
}
858-
strbuf_commented_addf(&msg, comment_line_char,
858+
strbuf_commented_addf(&msg, comment_line_str,
859859
_(merge_editor_comment));
860860
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
861-
strbuf_commented_addf(&msg, comment_line_char,
861+
strbuf_commented_addf(&msg, comment_line_str,
862862
_(scissors_editor_comment));
863863
else
864-
strbuf_commented_addf(&msg, comment_line_char,
865-
_(no_scissors_editor_comment), comment_line_char);
864+
strbuf_commented_addf(&msg, comment_line_str,
865+
_(no_scissors_editor_comment), comment_line_str);
866866
}
867867
if (signoff)
868868
append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0);

builtin/notes.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static void write_commented_object(int fd, const struct object_id *object)
179179

180180
if (strbuf_read(&buf, show.out, 0) < 0)
181181
die_errno(_("could not read 'show' output"));
182-
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
182+
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
183183
write_or_die(fd, cbuf.buf, cbuf.len);
184184

185185
strbuf_release(&cbuf);
@@ -207,10 +207,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
207207
copy_obj_to_fd(fd, old_note);
208208

209209
strbuf_addch(&buf, '\n');
210-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
210+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
211211
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
212-
comment_line_char);
213-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
212+
comment_line_str);
213+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
214214
write_or_die(fd, buf.buf, buf.len);
215215

216216
write_commented_object(fd, object);
@@ -223,7 +223,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
223223
die(_("please supply the note contents using either -m or -F option"));
224224
}
225225
if (d->stripspace)
226-
strbuf_stripspace(&d->buf, comment_line_char);
226+
strbuf_stripspace(&d->buf, comment_line_str);
227227
}
228228
}
229229

@@ -264,7 +264,7 @@ static void concat_messages(struct note_data *d)
264264
if ((d->stripspace == UNSPECIFIED &&
265265
d->messages[i]->stripspace == STRIPSPACE) ||
266266
d->stripspace == STRIPSPACE)
267-
strbuf_stripspace(&d->buf, 0);
267+
strbuf_stripspace(&d->buf, NULL);
268268
strbuf_reset(&msg);
269269
}
270270
strbuf_release(&msg);

builtin/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static int edit_todo_file(unsigned flags)
204204
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
205205
return error_errno(_("could not read '%s'."), todo_file);
206206

207-
strbuf_stripspace(&todo_list.buf, comment_line_char);
207+
strbuf_stripspace(&todo_list.buf, comment_line_str);
208208
res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
209209
if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
210210
NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))

builtin/stripspace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void comment_lines(struct strbuf *buf)
1313
size_t len;
1414

1515
msg = strbuf_detach(buf, &len);
16-
strbuf_add_commented_lines(buf, msg, len, comment_line_char);
16+
strbuf_add_commented_lines(buf, msg, len, comment_line_str);
1717
free(msg);
1818
}
1919

@@ -59,7 +59,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
5959

6060
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
6161
strbuf_stripspace(&buf,
62-
mode == STRIP_COMMENTS ? comment_line_char : '\0');
62+
mode == STRIP_COMMENTS ? comment_line_str : NULL);
6363
else
6464
comment_lines(&buf);
6565

builtin/tag.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ static int do_sign(struct strbuf *buffer, struct object_id **compat_oid,
193193

194194
static const char tag_template[] =
195195
N_("\nWrite a message for tag:\n %s\n"
196-
"Lines starting with '%c' will be ignored.\n");
196+
"Lines starting with '%s' will be ignored.\n");
197197

198198
static const char tag_template_nocleanup[] =
199199
N_("\nWrite a message for tag:\n %s\n"
200-
"Lines starting with '%c' will be kept; you may remove them"
200+
"Lines starting with '%s' will be kept; you may remove them"
201201
" yourself if you want to.\n");
202202

203203
static int git_tag_config(const char *var, const char *value,
@@ -328,11 +328,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
328328
struct strbuf buf = STRBUF_INIT;
329329
strbuf_addch(&buf, '\n');
330330
if (opt->cleanup_mode == CLEANUP_ALL)
331-
strbuf_commented_addf(&buf, comment_line_char,
332-
_(tag_template), tag, comment_line_char);
331+
strbuf_commented_addf(&buf, comment_line_str,
332+
_(tag_template), tag, comment_line_str);
333333
else
334-
strbuf_commented_addf(&buf, comment_line_char,
335-
_(tag_template_nocleanup), tag, comment_line_char);
334+
strbuf_commented_addf(&buf, comment_line_str,
335+
_(tag_template_nocleanup), tag, comment_line_str);
336336
write_or_die(fd, buf.buf, buf.len);
337337
strbuf_release(&buf);
338338
}
@@ -347,7 +347,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
347347

348348
if (opt->cleanup_mode != CLEANUP_NONE)
349349
strbuf_stripspace(buf,
350-
opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
350+
opt->cleanup_mode == CLEANUP_ALL ? comment_line_str : NULL);
351351

352352
if (!opt->message_given && !buf->len)
353353
die(_("no tag message?"));

0 commit comments

Comments
 (0)