commit: avoid scanning trailing comments when 'core.commentChar' is "auto"

When core.commentChar is set to "auto", Git selects a comment character by scanning the commit message contents and avoiding any character already present in the message. If the message still contains old conflict comments (starting with a comment character), Git assumes that character is in use and chooses a different one. As a result, those existing comment lines are no longer recognized as comments and end up being included in the final commit message. To avoid this, skip scanning the trailing comment block when selecting the comment character. This allows Git to safely reuse the original character when appropriate, keeping the commit message clean and free of leftover conflict information. Background: The "auto" value for core.commentchar was introduced in the commit 84c9dc2c5a (commit: allow core.commentChar=auto for character auto selection, 2014-05-17) but did not exhibit this issue at that time. The bug was introduced in commit a6c2654f83 (rebase -m: fix --signoff with conflicts, 2024-04-18) where Git started writing conflict comments to the file at 'rebase_path_message()'. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com> Signed-off-by: Ayush Chandekar <ayu.chandekar@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ayush Chandekar committed Jul 16, 2025 at 17:13 UTC e69bbfa2947ee733a2b76f1dc28ceaf415368f2a
2 files changed +18 -1
builtin/commit.c
+5 -1
@@ -688,6 +688,10 @@ static void adjust_comment_line_char(const struct strbuf *sb)
688 char candidates[] = "#;@!$%^&|:";
689 char *candidate;
690 const char *p;
691 + size_t cutoff;
692 +
693 + /* Ignore comment chars in trailing comments (e.g., Conflicts:) */
694 + cutoff = sb->len - ignored_log_message_bytes(sb->buf, sb->len);
695
696 if (!memchr(sb->buf, candidates[0], sb->len)) {
697 free(comment_line_str_to_free);
@@ -700,7 +704,7 @@ static void adjust_comment_line_char(const struct strbuf *sb)
704 candidate = strchr(candidates, *p);
705 if (candidate)
706 *candidate = ' ';
703 - for (p = sb->buf; *p; p++) {
707 + for (p = sb->buf; p + 1 < sb->buf + cutoff; p++) {
708 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
709 candidate = strchr(candidates, p[1]);
710 if (candidate)
t/t3418-rebase-continue.sh
+13
@@ -328,6 +328,19 @@ test_expect_success 'there is no --no-reschedule-failed-exec in an ongoing rebas
328 test_expect_code 129 git rebase --edit-todo --no-reschedule-failed-exec
329 '
330
331 +test_expect_success 'no change in comment character due to conflicts markers with core.commentChar=auto' '
332 + git checkout -b branch-a &&
333 + test_commit A F1 &&
334 + git checkout -b branch-b HEAD^ &&
335 + test_commit B F1 &&
336 + test_must_fail git rebase branch-a &&
337 + printf "B\nA\n" >F1 &&
338 + git add F1 &&
339 + GIT_EDITOR="cat >actual" git -c core.commentChar=auto rebase --continue &&
340 + # Check that "#" is still the comment character.
341 + test_grep "^# Changes to be committed" actual
342 +'
343 +
344 test_orig_head_helper () {
345 test_when_finished 'git rebase --abort &&
346 git checkout topic &&