Skip to content

Commit c573851

Browse files
edith007gitster
authored andcommitted
sequencer: extract revert message formatting into shared function
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 <siddharthasthana31@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 864f55e commit c573851

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

sequencer.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,31 +2356,17 @@ static int do_pick_commit(struct repository *r,
23562356
*/
23572357

23582358
if (command == TODO_REVERT) {
2359-
const char *orig_subject;
2360-
23612359
base = commit;
23622360
base_label = msg.label;
23632361
next = parent;
23642362
next_label = msg.parent_label;
23652363
if (opts->commit_use_reference) {
23662364
strbuf_commented_addf(&ctx->message, comment_line_str,
23672365
"*** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
2368-
} else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) &&
2369-
/*
2370-
* We don't touch pre-existing repeated reverts, because
2371-
* theoretically these can be nested arbitrarily deeply,
2372-
* thus requiring excessive complexity to deal with.
2373-
*/
2374-
!starts_with(orig_subject, "Revert \"")) {
2375-
strbuf_addstr(&ctx->message, "Reapply \"");
2376-
strbuf_addstr(&ctx->message, orig_subject);
2377-
strbuf_addstr(&ctx->message, "\n");
2366+
strbuf_addstr(&ctx->message, "\nThis reverts commit ");
23782367
} else {
2379-
strbuf_addstr(&ctx->message, "Revert \"");
2380-
strbuf_addstr(&ctx->message, msg.subject);
2381-
strbuf_addstr(&ctx->message, "\"\n");
2368+
sequencer_format_revert_header(&ctx->message, msg.subject, NULL);
23822369
}
2383-
strbuf_addstr(&ctx->message, "\nThis reverts commit ");
23842370
refer_to_commit(opts, &ctx->message, commit);
23852371

23862372
if (commit->parents && commit->parents->next) {
@@ -5572,6 +5558,35 @@ int sequencer_pick_revisions(struct repository *r,
55725558
return res;
55735559
}
55745560

5561+
void sequencer_format_revert_header(struct strbuf *out,
5562+
const char *orig_subject,
5563+
const struct object_id *oid)
5564+
{
5565+
const char *revert_subject;
5566+
5567+
if (skip_prefix(orig_subject, "Revert \"", &revert_subject) &&
5568+
/*
5569+
* We don't touch pre-existing repeated reverts, because
5570+
* theoretically these can be nested arbitrarily deeply,
5571+
* thus requiring excessive complexity to deal with.
5572+
*/
5573+
!starts_with(revert_subject, "Revert \"")) {
5574+
strbuf_addstr(out, "Reapply \"");
5575+
strbuf_addstr(out, revert_subject);
5576+
strbuf_addch(out, '\n');
5577+
} else {
5578+
strbuf_addstr(out, "Revert \"");
5579+
strbuf_addstr(out, orig_subject);
5580+
strbuf_addstr(out, "\"\n");
5581+
}
5582+
5583+
strbuf_addstr(out, "\nThis reverts commit ");
5584+
if (oid) {
5585+
strbuf_addstr(out, oid_to_hex(oid));
5586+
strbuf_addstr(out, ".\n");
5587+
}
5588+
}
5589+
55755590
void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
55765591
{
55775592
unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;

sequencer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,15 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
271271
*/
272272
int sequencer_get_update_refs_state(const char *wt_dir, struct string_list *refs);
273273

274+
/*
275+
* Formats a revert commit message following standard Git conventions.
276+
* Handles both regular reverts ("Revert \"<subject>\"") and revert of revert
277+
* cases ("Reapply \"<subject>\""). Adds "This reverts commit <oid>." if oid
278+
* is provided, otherwise just adds "This reverts commit " and the caller
279+
* should append the commit reference.
280+
*/
281+
void sequencer_format_revert_header(struct strbuf *out,
282+
const char *orig_subject,
283+
const struct object_id *oid);
284+
274285
#endif /* SEQUENCER_H */

0 commit comments

Comments
 (0)