sequencer: comment `--reference` subject line properly

`git revert --reference <commit>` leaves behind a comment in the first line:[1] # *** SAY WHY WE ARE REVERTING ON THE TITLE LINE *** Meaning that the commit will just consist of the next line if the user exits the editor directly: This reverts commit <--format=reference commit> But the comment char here is hardcoded (#). Which means that the comment line will inadvertently be included in the commit message if `core.commentChar`/`core.commentString` is in use. † 1: See 43966ab3156 (revert: optionally refer to commit in the "reference" format, 2022-05-26) Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristoffer Haugsbakk committed Nov 25, 2024 at 21:13 UTC 515d034f8d922602b9d417cdc02768757aa6f6c1
2 files changed +19 -4
sequencer.c
+5 -4
@@ -2341,8 +2341,8 @@ static int do_pick_commit(struct repository *r,
2341 next = parent;
2342 next_label = msg.parent_label;
2343 if (opts->commit_use_reference) {
2344 - strbuf_addstr(&ctx->message,
2345 - "# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
2344 + strbuf_commented_addf(&ctx->message, comment_line_str,
2345 + "*** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
2346 } else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) &&
2347 /*
2348 * We don't touch pre-existing repeated reverts, because
@@ -2352,12 +2352,13 @@ static int do_pick_commit(struct repository *r,
2352 !starts_with(orig_subject, "Revert \"")) {
2353 strbuf_addstr(&ctx->message, "Reapply \"");
2354 strbuf_addstr(&ctx->message, orig_subject);
2355 + strbuf_addstr(&ctx->message, "\n");
2356 } else {
2357 strbuf_addstr(&ctx->message, "Revert \"");
2358 strbuf_addstr(&ctx->message, msg.subject);
2358 - strbuf_addstr(&ctx->message, "\"");
2359 + strbuf_addstr(&ctx->message, "\"\n");
2360 }
2360 - strbuf_addstr(&ctx->message, "\n\nThis reverts commit ");
2361 + strbuf_addstr(&ctx->message, "\nThis reverts commit ");
2362 refer_to_commit(opts, &ctx->message, commit);
2363
2364 if (commit->parents && commit->parents->next) {
t/t3501-revert-cherry-pick.sh
+14
@@ -228,6 +228,20 @@ test_expect_success 'identification of reverted commit (--reference)' '
228 test_cmp expect actual
229 '
230
231 +test_expect_success 'git revert --reference with core.commentChar' '
232 + test_when_finished "git reset --hard to-ident" &&
233 + git checkout --detach to-ident &&
234 + GIT_EDITOR="head -n4 >actual" git -c core.commentChar=% revert \
235 + --edit --reference HEAD &&
236 + cat <<-EOF >expect &&
237 + % *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***
238 +
239 + This reverts commit $(git show -s --pretty=reference HEAD^).
240 +
241 + EOF
242 + test_cmp expect actual
243 +'
244 +
245 test_expect_success 'identification of reverted commit (revert.reference)' '
246 git checkout --detach to-ident &&
247 git -c revert.reference=true revert --no-edit HEAD &&