Skip to content

Commit 53f6746

Browse files
phillipwoodgitster
authored andcommitted
sequencer: store commit message in private context
Add an strbuf to "struct replay_ctx" to hold the current commit message. This does not change the behavior but it will allow us to fix a bug with "git rebase --signoff" in the next commit. A future patch series will use the changes here to avoid writing the commit message to disc unless there are conflicts or the commit is being reworded. The changes in do_pick_commit() are a mechanical replacement of "msgbuf" with "ctx->message". In do_merge() the code to write commit message to disc is factored out of the conditional now that both branches store the message in the same buffer. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 497a01a commit 53f6746

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

sequencer.c

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redu
211211
* A 'struct replay_ctx' represents the private state of the sequencer.
212212
*/
213213
struct replay_ctx {
214+
/*
215+
* The commit message that will be used except at the end of a
216+
* chain of fixup and squash commands.
217+
*/
218+
struct strbuf message;
214219
/*
215220
* The list of completed fixup and squash commands in the
216221
* current chain.
@@ -226,13 +231,18 @@ struct replay_ctx {
226231
* current chain.
227232
*/
228233
int current_fixup_count;
234+
/*
235+
* Whether message contains a commit message.
236+
*/
237+
unsigned have_message :1;
229238
};
230239

231240
struct replay_ctx* replay_ctx_new(void)
232241
{
233242
struct replay_ctx *ctx = xcalloc(1, sizeof(*ctx));
234243

235244
strbuf_init(&ctx->current_fixups, 0);
245+
strbuf_init(&ctx->message, 0);
236246

237247
return ctx;
238248
}
@@ -399,6 +409,7 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
399409
static void replay_ctx_release(struct replay_ctx *ctx)
400410
{
401411
strbuf_release(&ctx->current_fixups);
412+
strbuf_release(&ctx->message);
402413
}
403414

404415
void replay_opts_release(struct replay_opts *opts)
@@ -2205,7 +2216,6 @@ static int do_pick_commit(struct repository *r,
22052216
const char *base_label, *next_label;
22062217
char *author = NULL;
22072218
struct commit_message msg = { NULL, NULL, NULL, NULL };
2208-
struct strbuf msgbuf = STRBUF_INIT;
22092219
int res, unborn = 0, reword = 0, allow, drop_commit;
22102220
enum todo_command command = item->command;
22112221
struct commit *commit = item->commit;
@@ -2304,7 +2314,7 @@ static int do_pick_commit(struct repository *r,
23042314
next = parent;
23052315
next_label = msg.parent_label;
23062316
if (opts->commit_use_reference) {
2307-
strbuf_addstr(&msgbuf,
2317+
strbuf_addstr(&ctx->message,
23082318
"# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
23092319
} else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) &&
23102320
/*
@@ -2313,21 +2323,21 @@ static int do_pick_commit(struct repository *r,
23132323
* thus requiring excessive complexity to deal with.
23142324
*/
23152325
!starts_with(orig_subject, "Revert \"")) {
2316-
strbuf_addstr(&msgbuf, "Reapply \"");
2317-
strbuf_addstr(&msgbuf, orig_subject);
2326+
strbuf_addstr(&ctx->message, "Reapply \"");
2327+
strbuf_addstr(&ctx->message, orig_subject);
23182328
} else {
2319-
strbuf_addstr(&msgbuf, "Revert \"");
2320-
strbuf_addstr(&msgbuf, msg.subject);
2321-
strbuf_addstr(&msgbuf, "\"");
2329+
strbuf_addstr(&ctx->message, "Revert \"");
2330+
strbuf_addstr(&ctx->message, msg.subject);
2331+
strbuf_addstr(&ctx->message, "\"");
23222332
}
2323-
strbuf_addstr(&msgbuf, "\n\nThis reverts commit ");
2324-
refer_to_commit(opts, &msgbuf, commit);
2333+
strbuf_addstr(&ctx->message, "\n\nThis reverts commit ");
2334+
refer_to_commit(opts, &ctx->message, commit);
23252335

23262336
if (commit->parents && commit->parents->next) {
2327-
strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
2328-
refer_to_commit(opts, &msgbuf, parent);
2337+
strbuf_addstr(&ctx->message, ", reversing\nchanges made to ");
2338+
refer_to_commit(opts, &ctx->message, parent);
23292339
}
2330-
strbuf_addstr(&msgbuf, ".\n");
2340+
strbuf_addstr(&ctx->message, ".\n");
23312341
} else {
23322342
const char *p;
23332343

@@ -2336,21 +2346,22 @@ static int do_pick_commit(struct repository *r,
23362346
next = commit;
23372347
next_label = msg.label;
23382348

2339-
/* Append the commit log message to msgbuf. */
2349+
/* Append the commit log message to ctx->message. */
23402350
if (find_commit_subject(msg.message, &p))
2341-
strbuf_addstr(&msgbuf, p);
2351+
strbuf_addstr(&ctx->message, p);
23422352

23432353
if (opts->record_origin) {
2344-
strbuf_complete_line(&msgbuf);
2345-
if (!has_conforming_footer(&msgbuf, NULL, 0))
2346-
strbuf_addch(&msgbuf, '\n');
2347-
strbuf_addstr(&msgbuf, cherry_picked_prefix);
2348-
strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid));
2349-
strbuf_addstr(&msgbuf, ")\n");
2354+
strbuf_complete_line(&ctx->message);
2355+
if (!has_conforming_footer(&ctx->message, NULL, 0))
2356+
strbuf_addch(&ctx->message, '\n');
2357+
strbuf_addstr(&ctx->message, cherry_picked_prefix);
2358+
strbuf_addstr(&ctx->message, oid_to_hex(&commit->object.oid));
2359+
strbuf_addstr(&ctx->message, ")\n");
23502360
}
23512361
if (!is_fixup(command))
23522362
author = get_author(msg.message);
23532363
}
2364+
ctx->have_message = 1;
23542365

23552366
if (command == TODO_REWORD)
23562367
reword = 1;
@@ -2381,7 +2392,7 @@ static int do_pick_commit(struct repository *r,
23812392
}
23822393

23832394
if (opts->signoff && !is_fixup(command))
2384-
append_signoff(&msgbuf, 0, 0);
2395+
append_signoff(&ctx->message, 0, 0);
23852396

23862397
if (is_rebase_i(opts) && write_author_script(msg.message) < 0)
23872398
res = -1;
@@ -2390,17 +2401,17 @@ static int do_pick_commit(struct repository *r,
23902401
!strcmp(opts->strategy, "ort") ||
23912402
command == TODO_REVERT) {
23922403
res = do_recursive_merge(r, base, next, base_label, next_label,
2393-
&head, &msgbuf, opts);
2404+
&head, &ctx->message, opts);
23942405
if (res < 0)
23952406
goto leave;
23962407

2397-
res |= write_message(msgbuf.buf, msgbuf.len,
2408+
res |= write_message(ctx->message.buf, ctx->message.len,
23982409
git_path_merge_msg(r), 0);
23992410
} else {
24002411
struct commit_list *common = NULL;
24012412
struct commit_list *remotes = NULL;
24022413

2403-
res = write_message(msgbuf.buf, msgbuf.len,
2414+
res = write_message(ctx->message.buf, ctx->message.len,
24042415
git_path_merge_msg(r), 0);
24052416

24062417
commit_list_insert(base, &common);
@@ -2485,7 +2496,6 @@ static int do_pick_commit(struct repository *r,
24852496
leave:
24862497
free_message(commit, &msg);
24872498
free(author);
2488-
strbuf_release(&msgbuf);
24892499
update_abort_safety_file();
24902500

24912501
return res;
@@ -3952,6 +3962,7 @@ static int do_merge(struct repository *r,
39523962
const char *arg, int arg_len,
39533963
int flags, int *check_todo, struct replay_opts *opts)
39543964
{
3965+
struct replay_ctx *ctx = opts->ctx;
39553966
int run_commit_flags = 0;
39563967
struct strbuf ref_name = STRBUF_INIT;
39573968
struct commit *head_commit, *merge_commit, *i;
@@ -4080,40 +4091,31 @@ static int do_merge(struct repository *r,
40804091
write_author_script(message);
40814092
find_commit_subject(message, &body);
40824093
len = strlen(body);
4083-
ret = write_message(body, len, git_path_merge_msg(r), 0);
4094+
strbuf_add(&ctx->message, body, len);
40844095
repo_unuse_commit_buffer(r, commit, message);
4085-
if (ret) {
4086-
error_errno(_("could not write '%s'"),
4087-
git_path_merge_msg(r));
4088-
goto leave_merge;
4089-
}
40904096
} else {
40914097
struct strbuf buf = STRBUF_INIT;
4092-
int len;
40934098

40944099
strbuf_addf(&buf, "author %s", git_author_info(0));
40954100
write_author_script(buf.buf);
4096-
strbuf_reset(&buf);
4101+
strbuf_release(&buf);
40974102

40984103
if (oneline_offset < arg_len) {
4099-
p = arg + oneline_offset;
4100-
len = arg_len - oneline_offset;
4104+
strbuf_add(&ctx->message, arg + oneline_offset,
4105+
arg_len - oneline_offset);
41014106
} else {
4102-
strbuf_addf(&buf, "Merge %s '%.*s'",
4107+
strbuf_addf(&ctx->message, "Merge %s '%.*s'",
41034108
to_merge->next ? "branches" : "branch",
41044109
merge_arg_len, arg);
4105-
p = buf.buf;
4106-
len = buf.len;
4107-
}
4108-
4109-
ret = write_message(p, len, git_path_merge_msg(r), 0);
4110-
strbuf_release(&buf);
4111-
if (ret) {
4112-
error_errno(_("could not write '%s'"),
4113-
git_path_merge_msg(r));
4114-
goto leave_merge;
41154110
}
41164111
}
4112+
ctx->have_message = 1;
4113+
if (write_message(ctx->message.buf, ctx->message.len,
4114+
git_path_merge_msg(r), 0)) {
4115+
ret = error_errno(_("could not write '%s'"),
4116+
git_path_merge_msg(r));
4117+
goto leave_merge;
4118+
}
41174119

41184120
if (strategy || to_merge->next) {
41194121
/* Octopus merge */
@@ -4885,6 +4887,8 @@ static int pick_commits(struct repository *r,
48854887
return stopped_at_head(r);
48864888
}
48874889
}
4890+
strbuf_reset(&ctx->message);
4891+
ctx->have_message = 0;
48884892
if (item->command <= TODO_SQUASH) {
48894893
res = pick_one_commit(r, todo_list, opts, &check_todo,
48904894
&reschedule);

0 commit comments

Comments
 (0)