Skip to content

Commit 497a01a

Browse files
phillipwoodgitster
authored andcommitted
sequencer: move current fixups to private context
The list of current fixups is an implementation detail of the sequencer and so it should not be stored in the public options struct. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3152ed commit 497a01a

File tree

2 files changed

+57
-37
lines changed

2 files changed

+57
-37
lines changed

sequencer.c

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,29 @@ 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 list of completed fixup and squash commands in the
216+
* current chain.
217+
*/
218+
struct strbuf current_fixups;
214219
/*
215220
* Stores the reflog message that will be used when creating a
216221
* commit. Points to a static buffer and should not be free()'d.
217222
*/
218223
const char *reflog_message;
224+
/*
225+
* The number of completed fixup and squash commands in the
226+
* current chain.
227+
*/
228+
int current_fixup_count;
219229
};
220230

221231
struct replay_ctx* replay_ctx_new(void)
222232
{
223233
struct replay_ctx *ctx = xcalloc(1, sizeof(*ctx));
224234

235+
strbuf_init(&ctx->current_fixups, 0);
236+
225237
return ctx;
226238
}
227239

@@ -384,17 +396,24 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
384396
return buf.buf;
385397
}
386398

399+
static void replay_ctx_release(struct replay_ctx *ctx)
400+
{
401+
strbuf_release(&ctx->current_fixups);
402+
}
403+
387404
void replay_opts_release(struct replay_opts *opts)
388405
{
406+
struct replay_ctx *ctx = opts->ctx;
407+
389408
free(opts->gpg_sign);
390409
free(opts->reflog_action);
391410
free(opts->default_strategy);
392411
free(opts->strategy);
393412
strvec_clear (&opts->xopts);
394-
strbuf_release(&opts->current_fixups);
395413
if (opts->revs)
396414
release_revisions(opts->revs);
397415
free(opts->revs);
416+
replay_ctx_release(ctx);
398417
free(opts->ctx);
399418
}
400419

@@ -1876,10 +1895,10 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
18761895
}
18771896

18781897
/* Does the current fixup chain contain a squash command? */
1879-
static int seen_squash(struct replay_opts *opts)
1898+
static int seen_squash(struct replay_ctx *ctx)
18801899
{
1881-
return starts_with(opts->current_fixups.buf, "squash") ||
1882-
strstr(opts->current_fixups.buf, "\nsquash");
1900+
return starts_with(ctx->current_fixups.buf, "squash") ||
1901+
strstr(ctx->current_fixups.buf, "\nsquash");
18831902
}
18841903

18851904
static void update_comment_bufs(struct strbuf *buf1, struct strbuf *buf2, int n)
@@ -1955,6 +1974,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
19551974
enum todo_command command, struct replay_opts *opts,
19561975
unsigned flag)
19571976
{
1977+
struct replay_ctx *ctx = opts->ctx;
19581978
const char *fixup_msg;
19591979
size_t commented_len = 0, fixup_off;
19601980
/*
@@ -1963,21 +1983,21 @@ static int append_squash_message(struct strbuf *buf, const char *body,
19631983
* squashing commit messages.
19641984
*/
19651985
if (starts_with(body, "amend!") ||
1966-
((command == TODO_SQUASH || seen_squash(opts)) &&
1986+
((command == TODO_SQUASH || seen_squash(ctx)) &&
19671987
(starts_with(body, "squash!") || starts_with(body, "fixup!"))))
19681988
commented_len = commit_subject_length(body);
19691989

19701990
strbuf_addf(buf, "\n%c ", comment_line_char);
19711991
strbuf_addf(buf, _(nth_commit_msg_fmt),
1972-
++opts->current_fixup_count + 1);
1992+
++ctx->current_fixup_count + 1);
19731993
strbuf_addstr(buf, "\n\n");
19741994
strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
19751995
/* buf->buf may be reallocated so store an offset into the buffer */
19761996
fixup_off = buf->len;
19771997
strbuf_addstr(buf, body + commented_len);
19781998

19791999
/* fixup -C after squash behaves like squash */
1980-
if (is_fixup_flag(command, flag) && !seen_squash(opts)) {
2000+
if (is_fixup_flag(command, flag) && !seen_squash(ctx)) {
19812001
/*
19822002
* We're replacing the commit message so we need to
19832003
* append the Signed-off-by: trailer if the user
@@ -2011,12 +2031,13 @@ static int update_squash_messages(struct repository *r,
20112031
struct replay_opts *opts,
20122032
unsigned flag)
20132033
{
2034+
struct replay_ctx *ctx = opts->ctx;
20142035
struct strbuf buf = STRBUF_INIT;
20152036
int res = 0;
20162037
const char *message, *body;
20172038
const char *encoding = get_commit_output_encoding();
20182039

2019-
if (opts->current_fixup_count > 0) {
2040+
if (ctx->current_fixup_count > 0) {
20202041
struct strbuf header = STRBUF_INIT;
20212042
char *eol;
20222043

@@ -2029,10 +2050,10 @@ static int update_squash_messages(struct repository *r,
20292050

20302051
strbuf_addf(&header, "%c ", comment_line_char);
20312052
strbuf_addf(&header, _(combined_commit_msg_fmt),
2032-
opts->current_fixup_count + 2);
2053+
ctx->current_fixup_count + 2);
20332054
strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len);
20342055
strbuf_release(&header);
2035-
if (is_fixup_flag(command, flag) && !seen_squash(opts))
2056+
if (is_fixup_flag(command, flag) && !seen_squash(ctx))
20362057
update_squash_message_for_fixup(&buf);
20372058
} else {
20382059
struct object_id head;
@@ -2079,7 +2100,7 @@ static int update_squash_messages(struct repository *r,
20792100
} else if (command == TODO_FIXUP) {
20802101
strbuf_addf(&buf, "\n%c ", comment_line_char);
20812102
strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
2082-
++opts->current_fixup_count + 1);
2103+
++ctx->current_fixup_count + 1);
20832104
strbuf_addstr(&buf, "\n\n");
20842105
strbuf_add_commented_lines(&buf, body, strlen(body),
20852106
comment_line_char);
@@ -2093,12 +2114,12 @@ static int update_squash_messages(struct repository *r,
20932114
strbuf_release(&buf);
20942115

20952116
if (!res) {
2096-
strbuf_addf(&opts->current_fixups, "%s%s %s",
2097-
opts->current_fixups.len ? "\n" : "",
2117+
strbuf_addf(&ctx->current_fixups, "%s%s %s",
2118+
ctx->current_fixups.len ? "\n" : "",
20982119
command_to_string(command),
20992120
oid_to_hex(&commit->object.oid));
2100-
res = write_message(opts->current_fixups.buf,
2101-
opts->current_fixups.len,
2121+
res = write_message(ctx->current_fixups.buf,
2122+
ctx->current_fixups.len,
21022123
rebase_path_current_fixups(), 0);
21032124
}
21042125

@@ -2176,6 +2197,7 @@ static int do_pick_commit(struct repository *r,
21762197
struct replay_opts *opts,
21772198
int final_fixup, int *check_todo)
21782199
{
2200+
struct replay_ctx *ctx = opts->ctx;
21792201
unsigned int flags = should_edit(opts) ? EDIT_MSG : 0;
21802202
const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
21812203
struct object_id head;
@@ -2456,8 +2478,8 @@ static int do_pick_commit(struct repository *r,
24562478
unlink(rebase_path_fixup_msg());
24572479
unlink(rebase_path_squash_msg());
24582480
unlink(rebase_path_current_fixups());
2459-
strbuf_reset(&opts->current_fixups);
2460-
opts->current_fixup_count = 0;
2481+
strbuf_reset(&ctx->current_fixups);
2482+
ctx->current_fixup_count = 0;
24612483
}
24622484

24632485
leave:
@@ -3019,6 +3041,8 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
30193041

30203042
static int read_populate_opts(struct replay_opts *opts)
30213043
{
3044+
struct replay_ctx *ctx = opts->ctx;
3045+
30223046
if (is_rebase_i(opts)) {
30233047
struct strbuf buf = STRBUF_INIT;
30243048
int ret = 0;
@@ -3078,13 +3102,13 @@ static int read_populate_opts(struct replay_opts *opts)
30783102
read_strategy_opts(opts, &buf);
30793103
strbuf_reset(&buf);
30803104

3081-
if (read_oneliner(&opts->current_fixups,
3105+
if (read_oneliner(&ctx->current_fixups,
30823106
rebase_path_current_fixups(),
30833107
READ_ONELINER_SKIP_IF_EMPTY)) {
3084-
const char *p = opts->current_fixups.buf;
3085-
opts->current_fixup_count = 1;
3108+
const char *p = ctx->current_fixups.buf;
3109+
ctx->current_fixup_count = 1;
30863110
while ((p = strchr(p, '\n'))) {
3087-
opts->current_fixup_count++;
3111+
ctx->current_fixup_count++;
30883112
p++;
30893113
}
30903114
}
@@ -5066,6 +5090,7 @@ static int commit_staged_changes(struct repository *r,
50665090
struct replay_opts *opts,
50675091
struct todo_list *todo_list)
50685092
{
5093+
struct replay_ctx *ctx = opts->ctx;
50695094
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
50705095
unsigned int final_fixup = 0, is_clean;
50715096

@@ -5102,7 +5127,7 @@ static int commit_staged_changes(struct repository *r,
51025127
* the commit message and if there was a squash, let the user
51035128
* edit it.
51045129
*/
5105-
if (!is_clean || !opts->current_fixup_count)
5130+
if (!is_clean || !ctx->current_fixup_count)
51065131
; /* this is not the final fixup */
51075132
else if (!oideq(&head, &to_amend) ||
51085133
!file_exists(rebase_path_stopped_sha())) {
@@ -5111,20 +5136,20 @@ static int commit_staged_changes(struct repository *r,
51115136
unlink(rebase_path_fixup_msg());
51125137
unlink(rebase_path_squash_msg());
51135138
unlink(rebase_path_current_fixups());
5114-
strbuf_reset(&opts->current_fixups);
5115-
opts->current_fixup_count = 0;
5139+
strbuf_reset(&ctx->current_fixups);
5140+
ctx->current_fixup_count = 0;
51165141
}
51175142
} else {
51185143
/* we are in a fixup/squash chain */
5119-
const char *p = opts->current_fixups.buf;
5120-
int len = opts->current_fixups.len;
5144+
const char *p = ctx->current_fixups.buf;
5145+
int len = ctx->current_fixups.len;
51215146

5122-
opts->current_fixup_count--;
5147+
ctx->current_fixup_count--;
51235148
if (!len)
51245149
BUG("Incorrect current_fixups:\n%s", p);
51255150
while (len && p[len - 1] != '\n')
51265151
len--;
5127-
strbuf_setlen(&opts->current_fixups, len);
5152+
strbuf_setlen(&ctx->current_fixups, len);
51285153
if (write_message(p, len, rebase_path_current_fixups(),
51295154
0) < 0)
51305155
return error(_("could not write file: '%s'"),
@@ -5141,7 +5166,7 @@ static int commit_staged_changes(struct repository *r,
51415166
* actually need to re-commit with a cleaned up commit
51425167
* message.
51435168
*/
5144-
if (opts->current_fixup_count > 0 &&
5169+
if (ctx->current_fixup_count > 0 &&
51455170
!is_fixup(peek_command(todo_list, 0))) {
51465171
final_fixup = 1;
51475172
/*
@@ -5214,14 +5239,14 @@ static int commit_staged_changes(struct repository *r,
52145239
unlink(rebase_path_fixup_msg());
52155240
unlink(rebase_path_squash_msg());
52165241
}
5217-
if (opts->current_fixup_count > 0) {
5242+
if (ctx->current_fixup_count > 0) {
52185243
/*
52195244
* Whether final fixup or not, we just cleaned up the commit
52205245
* message...
52215246
*/
52225247
unlink(rebase_path_current_fixups());
5223-
strbuf_reset(&opts->current_fixups);
5224-
opts->current_fixup_count = 0;
5248+
strbuf_reset(&ctx->current_fixups);
5249+
ctx->current_fixup_count = 0;
52255250
}
52265251
return 0;
52275252
}

sequencer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ struct replay_opts {
6969
/* Reflog */
7070
char *reflog_action;
7171

72-
/* Used by fixup/squash */
73-
struct strbuf current_fixups;
74-
int current_fixup_count;
75-
7672
/* placeholder commit for -i --root */
7773
struct object_id squash_onto;
7874
int have_squash_onto;
@@ -86,7 +82,6 @@ struct replay_opts {
8682
#define REPLAY_OPTS_INIT { \
8783
.edit = -1, \
8884
.action = -1, \
89-
.current_fixups = STRBUF_INIT, \
9085
.xopts = STRVEC_INIT, \
9186
.ctx = replay_ctx_new(), \
9287
}

0 commit comments

Comments
 (0)