[PATCH v3 1/2] sequencer: extract revert message formatting into shared function
From: Siddharth Asthana <hidden>
Date: 2026-02-18 23:42:31
Subsystem:
the rest · Maintainer:
Linus Torvalds
The logic for formatting revert commit messages (handling "Revert" and
"Reapply" cases) is currently duplicated between sequencer.c and will be
needed by builtin/replay.c.
Extract this logic into a new sequencer_format_revert_header() function
that can be shared. The function handles both regular reverts ("Revert
"<subject>"") and revert-of-revert cases ("Reapply "<subject>"").
When an oid is provided, the function appends the full commit hash and
period; otherwise the caller should append the commit reference.
Update do_pick_commit() to use the new helper, eliminating code
duplication while preserving the special handling for commit_use_reference.
Signed-off-by: Siddharth Asthana <redacted>
---
sequencer.c | 47 +++++++++++++++++++++++++++++++----------------
sequencer.h | 11 +++++++++++
2 files changed, 42 insertions(+), 16 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index 1f492f8460..b32347c853 100644
--- a/sequencer.c
+++ b/sequencer.c@@ -2356,8 +2356,6 @@ static int do_pick_commit(struct repository *r, */ if (command == TODO_REVERT) { - const char *orig_subject; - base = commit; base_label = msg.label; next = parent;
@@ -2365,22 +2363,10 @@ static int do_pick_commit(struct repository *r, if (opts->commit_use_reference) { strbuf_commented_addf(&ctx->message, comment_line_str, "*** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***"); - } else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) && - /* - * We don't touch pre-existing repeated reverts, because - * theoretically these can be nested arbitrarily deeply, - * thus requiring excessive complexity to deal with. - */ - !starts_with(orig_subject, "Revert \"")) { - strbuf_addstr(&ctx->message, "Reapply \""); - strbuf_addstr(&ctx->message, orig_subject); - strbuf_addstr(&ctx->message, "\n"); + strbuf_addstr(&ctx->message, "\nThis reverts commit "); } else { - strbuf_addstr(&ctx->message, "Revert \""); - strbuf_addstr(&ctx->message, msg.subject); - strbuf_addstr(&ctx->message, "\"\n"); + sequencer_format_revert_header(&ctx->message, msg.subject, NULL); } - strbuf_addstr(&ctx->message, "\nThis reverts commit "); refer_to_commit(opts, &ctx->message, commit); if (commit->parents && commit->parents->next) {
@@ -5572,6 +5558,35 @@ int sequencer_pick_revisions(struct repository *r, return res; } +void sequencer_format_revert_header(struct strbuf *out, + const char *orig_subject, + const struct object_id *oid) +{ + const char *revert_subject; + + if (skip_prefix(orig_subject, "Revert \"", &revert_subject) && + /* + * We don't touch pre-existing repeated reverts, because + * theoretically these can be nested arbitrarily deeply, + * thus requiring excessive complexity to deal with. + */ + !starts_with(revert_subject, "Revert \"")) { + strbuf_addstr(out, "Reapply \""); + strbuf_addstr(out, revert_subject); + strbuf_addch(out, '\n'); + } else { + strbuf_addstr(out, "Revert \""); + strbuf_addstr(out, orig_subject); + strbuf_addstr(out, "\"\n"); + } + + strbuf_addstr(out, "\nThis reverts commit "); + if (oid) { + strbuf_addstr(out, oid_to_hex(oid)); + strbuf_addstr(out, ".\n"); + } +} + void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag) { unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
diff --git a/sequencer.h b/sequencer.h
index 719684c8a9..b7291cc52d 100644
--- a/sequencer.h
+++ b/sequencer.h@@ -271,4 +271,15 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) */ int sequencer_get_update_refs_state(const char *wt_dir, struct string_list *refs); +/* + * Formats a revert commit message following standard Git conventions. + * Handles both regular reverts ("Revert \"<subject>\"") and revert of revert + * cases ("Reapply \"<subject>\""). Adds "This reverts commit <oid>." if oid + * is provided, otherwise just adds "This reverts commit " and the caller + * should append the commit reference. + */ +void sequencer_format_revert_header(struct strbuf *out, + const char *orig_subject, + const struct object_id *oid); + #endif /* SEQUENCER_H */
--
2.51.0