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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Apr 18, 2024 at 14:14 UTC 497a01a2d3d2bef45dd76d76c769a0a2a3c80b57
2 files changed +57 -37
sequencer.c
+57 -32
@@ -211,17 +211,29 @@ static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redu
211 * A 'struct replay_ctx' represents the private state of the sequencer.
212 */
213 struct replay_ctx {
214 + /*
215 + * The list of completed fixup and squash commands in the
216 + * current chain.
217 + */
218 + struct strbuf current_fixups;
219 /*
220 * Stores the reflog message that will be used when creating a
221 * commit. Points to a static buffer and should not be free()'d.
222 */
223 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;
229 };
230
231 struct replay_ctx* replay_ctx_new(void)
232 {
233 struct replay_ctx *ctx = xcalloc(1, sizeof(*ctx));
234
235 + strbuf_init(&ctx->current_fixups, 0);
236 +
237 return ctx;
238 }
239
@@ -384,17 +396,24 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
396 return buf.buf;
397 }
398
399 +static void replay_ctx_release(struct replay_ctx *ctx)
400 +{
401 + strbuf_release(&ctx->current_fixups);
402 +}
403 +
404 void replay_opts_release(struct replay_opts *opts)
405 {
406 + struct replay_ctx *ctx = opts->ctx;
407 +
408 free(opts->gpg_sign);
409 free(opts->reflog_action);
410 free(opts->default_strategy);
411 free(opts->strategy);
412 strvec_clear (&opts->xopts);
394 - strbuf_release(&opts->current_fixups);
413 if (opts->revs)
414 release_revisions(opts->revs);
415 free(opts->revs);
416 + replay_ctx_release(ctx);
417 free(opts->ctx);
418 }
419
@@ -1876,10 +1895,10 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
1895 }
1896
1897 /* 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)
1899 {
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");
1902 }
1903
1904 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,
1974 enum todo_command command, struct replay_opts *opts,
1975 unsigned flag)
1976 {
1977 + struct replay_ctx *ctx = opts->ctx;
1978 const char *fixup_msg;
1979 size_t commented_len = 0, fixup_off;
1980 /*
@@ -1963,13 +1983,13 @@ static int append_squash_message(struct strbuf *buf, const char *body,
1983 * squashing commit messages.
1984 */
1985 if (starts_with(body, "amend!") ||
1966 - ((command == TODO_SQUASH || seen_squash(opts)) &&
1986 + ((command == TODO_SQUASH || seen_squash(ctx)) &&
1987 (starts_with(body, "squash!") || starts_with(body, "fixup!"))))
1988 commented_len = commit_subject_length(body);
1989
1990 strbuf_addf(buf, "\n%c ", comment_line_char);
1991 strbuf_addf(buf, _(nth_commit_msg_fmt),
1972 - ++opts->current_fixup_count + 1);
1992 + ++ctx->current_fixup_count + 1);
1993 strbuf_addstr(buf, "\n\n");
1994 strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
1995 /* buf->buf may be reallocated so store an offset into the buffer */
@@ -1977,7 +1997,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
1997 strbuf_addstr(buf, body + commented_len);
1998
1999 /* 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)) {
2001 /*
2002 * We're replacing the commit message so we need to
2003 * append the Signed-off-by: trailer if the user
@@ -2011,12 +2031,13 @@ static int update_squash_messages(struct repository *r,
2031 struct replay_opts *opts,
2032 unsigned flag)
2033 {
2034 + struct replay_ctx *ctx = opts->ctx;
2035 struct strbuf buf = STRBUF_INIT;
2036 int res = 0;
2037 const char *message, *body;
2038 const char *encoding = get_commit_output_encoding();
2039
2019 - if (opts->current_fixup_count > 0) {
2040 + if (ctx->current_fixup_count > 0) {
2041 struct strbuf header = STRBUF_INIT;
2042 char *eol;
2043
@@ -2029,10 +2050,10 @@ static int update_squash_messages(struct repository *r,
2050
2051 strbuf_addf(&header, "%c ", comment_line_char);
2052 strbuf_addf(&header, _(combined_commit_msg_fmt),
2032 - opts->current_fixup_count + 2);
2053 + ctx->current_fixup_count + 2);
2054 strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len);
2055 strbuf_release(&header);
2035 - if (is_fixup_flag(command, flag) && !seen_squash(opts))
2056 + if (is_fixup_flag(command, flag) && !seen_squash(ctx))
2057 update_squash_message_for_fixup(&buf);
2058 } else {
2059 struct object_id head;
@@ -2079,7 +2100,7 @@ static int update_squash_messages(struct repository *r,
2100 } else if (command == TODO_FIXUP) {
2101 strbuf_addf(&buf, "\n%c ", comment_line_char);
2102 strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
2082 - ++opts->current_fixup_count + 1);
2103 + ++ctx->current_fixup_count + 1);
2104 strbuf_addstr(&buf, "\n\n");
2105 strbuf_add_commented_lines(&buf, body, strlen(body),
2106 comment_line_char);
@@ -2093,12 +2114,12 @@ static int update_squash_messages(struct repository *r,
2114 strbuf_release(&buf);
2115
2116 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" : "",
2119 command_to_string(command),
2120 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,
2123 rebase_path_current_fixups(), 0);
2124 }
2125
@@ -2176,6 +2197,7 @@ static int do_pick_commit(struct repository *r,
2197 struct replay_opts *opts,
2198 int final_fixup, int *check_todo)
2199 {
2200 + struct replay_ctx *ctx = opts->ctx;
2201 unsigned int flags = should_edit(opts) ? EDIT_MSG : 0;
2202 const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
2203 struct object_id head;
@@ -2456,8 +2478,8 @@ fast_forward_edit:
2478 unlink(rebase_path_fixup_msg());
2479 unlink(rebase_path_squash_msg());
2480 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;
2483 }
2484
2485 leave:
@@ -3019,6 +3041,8 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
3041
3042 static int read_populate_opts(struct replay_opts *opts)
3043 {
3044 + struct replay_ctx *ctx = opts->ctx;
3045 +
3046 if (is_rebase_i(opts)) {
3047 struct strbuf buf = STRBUF_INIT;
3048 int ret = 0;
@@ -3078,13 +3102,13 @@ static int read_populate_opts(struct replay_opts *opts)
3102 read_strategy_opts(opts, &buf);
3103 strbuf_reset(&buf);
3104
3081 - if (read_oneliner(&opts->current_fixups,
3105 + if (read_oneliner(&ctx->current_fixups,
3106 rebase_path_current_fixups(),
3107 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;
3110 while ((p = strchr(p, '\n'))) {
3087 - opts->current_fixup_count++;
3111 + ctx->current_fixup_count++;
3112 p++;
3113 }
3114 }
@@ -5066,6 +5090,7 @@ static int commit_staged_changes(struct repository *r,
5090 struct replay_opts *opts,
5091 struct todo_list *todo_list)
5092 {
5093 + struct replay_ctx *ctx = opts->ctx;
5094 unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
5095 unsigned int final_fixup = 0, is_clean;
5096
@@ -5102,7 +5127,7 @@ static int commit_staged_changes(struct repository *r,
5127 * the commit message and if there was a squash, let the user
5128 * edit it.
5129 */
5105 - if (!is_clean || !opts->current_fixup_count)
5130 + if (!is_clean || !ctx->current_fixup_count)
5131 ; /* this is not the final fixup */
5132 else if (!oideq(&head, &to_amend) ||
5133 !file_exists(rebase_path_stopped_sha())) {
@@ -5111,20 +5136,20 @@ static int commit_staged_changes(struct repository *r,
5136 unlink(rebase_path_fixup_msg());
5137 unlink(rebase_path_squash_msg());
5138 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;
5141 }
5142 } else {
5143 /* 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;
5146
5122 - opts->current_fixup_count--;
5147 + ctx->current_fixup_count--;
5148 if (!len)
5149 BUG("Incorrect current_fixups:\n%s", p);
5150 while (len && p[len - 1] != '\n')
5151 len--;
5127 - strbuf_setlen(&opts->current_fixups, len);
5152 + strbuf_setlen(&ctx->current_fixups, len);
5153 if (write_message(p, len, rebase_path_current_fixups(),
5154 0) < 0)
5155 return error(_("could not write file: '%s'"),
@@ -5141,7 +5166,7 @@ static int commit_staged_changes(struct repository *r,
5166 * actually need to re-commit with a cleaned up commit
5167 * message.
5168 */
5144 - if (opts->current_fixup_count > 0 &&
5169 + if (ctx->current_fixup_count > 0 &&
5170 !is_fixup(peek_command(todo_list, 0))) {
5171 final_fixup = 1;
5172 /*
@@ -5214,14 +5239,14 @@ static int commit_staged_changes(struct repository *r,
5239 unlink(rebase_path_fixup_msg());
5240 unlink(rebase_path_squash_msg());
5241 }
5217 - if (opts->current_fixup_count > 0) {
5242 + if (ctx->current_fixup_count > 0) {
5243 /*
5244 * Whether final fixup or not, we just cleaned up the commit
5245 * message...
5246 */
5247 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;
5250 }
5251 return 0;
5252 }
sequencer.h
-5
@@ -69,10 +69,6 @@ struct replay_opts {
69 /* Reflog */
70 char *reflog_action;
71
72 - /* Used by fixup/squash */
73 - struct strbuf current_fixups;
74 - int current_fixup_count;
75 -
72 /* placeholder commit for -i --root */
73 struct object_id squash_onto;
74 int have_squash_onto;
@@ -86,7 +82,6 @@ struct replay_opts {
82 #define REPLAY_OPTS_INIT { \
83 .edit = -1, \
84 .action = -1, \
89 - .current_fixups = STRBUF_INIT, \
85 .xopts = STRVEC_INIT, \
86 .ctx = replay_ctx_new(), \
87 }