1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-10 18:56:10 +02:00

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>
This commit is contained in:
Phillip Wood 2024-04-18 14:14:07 +01:00 committed by Junio C Hamano
parent a3152edc97
commit 497a01a2d3
2 changed files with 57 additions and 37 deletions

View File

@ -211,17 +211,29 @@ static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redu
* A 'struct replay_ctx' represents the private state of the sequencer.
*/
struct replay_ctx {
/*
* The list of completed fixup and squash commands in the
* current chain.
*/
struct strbuf current_fixups;
/*
* Stores the reflog message that will be used when creating a
* commit. Points to a static buffer and should not be free()'d.
*/
const char *reflog_message;
/*
* The number of completed fixup and squash commands in the
* current chain.
*/
int current_fixup_count;
};
struct replay_ctx* replay_ctx_new(void)
{
struct replay_ctx *ctx = xcalloc(1, sizeof(*ctx));
strbuf_init(&ctx->current_fixups, 0);
return ctx;
}
@ -384,17 +396,24 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
return buf.buf;
}
static void replay_ctx_release(struct replay_ctx *ctx)
{
strbuf_release(&ctx->current_fixups);
}
void replay_opts_release(struct replay_opts *opts)
{
struct replay_ctx *ctx = opts->ctx;
free(opts->gpg_sign);
free(opts->reflog_action);
free(opts->default_strategy);
free(opts->strategy);
strvec_clear (&opts->xopts);
strbuf_release(&opts->current_fixups);
if (opts->revs)
release_revisions(opts->revs);
free(opts->revs);
replay_ctx_release(ctx);
free(opts->ctx);
}
@ -1876,10 +1895,10 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
}
/* Does the current fixup chain contain a squash command? */
static int seen_squash(struct replay_opts *opts)
static int seen_squash(struct replay_ctx *ctx)
{
return starts_with(opts->current_fixups.buf, "squash") ||
strstr(opts->current_fixups.buf, "\nsquash");
return starts_with(ctx->current_fixups.buf, "squash") ||
strstr(ctx->current_fixups.buf, "\nsquash");
}
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,
enum todo_command command, struct replay_opts *opts,
unsigned flag)
{
struct replay_ctx *ctx = opts->ctx;
const char *fixup_msg;
size_t commented_len = 0, fixup_off;
/*
@ -1963,13 +1983,13 @@ static int append_squash_message(struct strbuf *buf, const char *body,
* squashing commit messages.
*/
if (starts_with(body, "amend!") ||
((command == TODO_SQUASH || seen_squash(opts)) &&
((command == TODO_SQUASH || seen_squash(ctx)) &&
(starts_with(body, "squash!") || starts_with(body, "fixup!"))))
commented_len = commit_subject_length(body);
strbuf_addf(buf, "\n%c ", comment_line_char);
strbuf_addf(buf, _(nth_commit_msg_fmt),
++opts->current_fixup_count + 1);
++ctx->current_fixup_count + 1);
strbuf_addstr(buf, "\n\n");
strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
/* 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,
strbuf_addstr(buf, body + commented_len);
/* fixup -C after squash behaves like squash */
if (is_fixup_flag(command, flag) && !seen_squash(opts)) {
if (is_fixup_flag(command, flag) && !seen_squash(ctx)) {
/*
* We're replacing the commit message so we need to
* append the Signed-off-by: trailer if the user
@ -2011,12 +2031,13 @@ static int update_squash_messages(struct repository *r,
struct replay_opts *opts,
unsigned flag)
{
struct replay_ctx *ctx = opts->ctx;
struct strbuf buf = STRBUF_INIT;
int res = 0;
const char *message, *body;
const char *encoding = get_commit_output_encoding();
if (opts->current_fixup_count > 0) {
if (ctx->current_fixup_count > 0) {
struct strbuf header = STRBUF_INIT;
char *eol;
@ -2029,10 +2050,10 @@ static int update_squash_messages(struct repository *r,
strbuf_addf(&header, "%c ", comment_line_char);
strbuf_addf(&header, _(combined_commit_msg_fmt),
opts->current_fixup_count + 2);
ctx->current_fixup_count + 2);
strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len);
strbuf_release(&header);
if (is_fixup_flag(command, flag) && !seen_squash(opts))
if (is_fixup_flag(command, flag) && !seen_squash(ctx))
update_squash_message_for_fixup(&buf);
} else {
struct object_id head;
@ -2079,7 +2100,7 @@ static int update_squash_messages(struct repository *r,
} else if (command == TODO_FIXUP) {
strbuf_addf(&buf, "\n%c ", comment_line_char);
strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
++opts->current_fixup_count + 1);
++ctx->current_fixup_count + 1);
strbuf_addstr(&buf, "\n\n");
strbuf_add_commented_lines(&buf, body, strlen(body),
comment_line_char);
@ -2093,12 +2114,12 @@ static int update_squash_messages(struct repository *r,
strbuf_release(&buf);
if (!res) {
strbuf_addf(&opts->current_fixups, "%s%s %s",
opts->current_fixups.len ? "\n" : "",
strbuf_addf(&ctx->current_fixups, "%s%s %s",
ctx->current_fixups.len ? "\n" : "",
command_to_string(command),
oid_to_hex(&commit->object.oid));
res = write_message(opts->current_fixups.buf,
opts->current_fixups.len,
res = write_message(ctx->current_fixups.buf,
ctx->current_fixups.len,
rebase_path_current_fixups(), 0);
}
@ -2176,6 +2197,7 @@ static int do_pick_commit(struct repository *r,
struct replay_opts *opts,
int final_fixup, int *check_todo)
{
struct replay_ctx *ctx = opts->ctx;
unsigned int flags = should_edit(opts) ? EDIT_MSG : 0;
const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
struct object_id head;
@ -2456,8 +2478,8 @@ static int do_pick_commit(struct repository *r,
unlink(rebase_path_fixup_msg());
unlink(rebase_path_squash_msg());
unlink(rebase_path_current_fixups());
strbuf_reset(&opts->current_fixups);
opts->current_fixup_count = 0;
strbuf_reset(&ctx->current_fixups);
ctx->current_fixup_count = 0;
}
leave:
@ -3019,6 +3041,8 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
static int read_populate_opts(struct replay_opts *opts)
{
struct replay_ctx *ctx = opts->ctx;
if (is_rebase_i(opts)) {
struct strbuf buf = STRBUF_INIT;
int ret = 0;
@ -3078,13 +3102,13 @@ static int read_populate_opts(struct replay_opts *opts)
read_strategy_opts(opts, &buf);
strbuf_reset(&buf);
if (read_oneliner(&opts->current_fixups,
if (read_oneliner(&ctx->current_fixups,
rebase_path_current_fixups(),
READ_ONELINER_SKIP_IF_EMPTY)) {
const char *p = opts->current_fixups.buf;
opts->current_fixup_count = 1;
const char *p = ctx->current_fixups.buf;
ctx->current_fixup_count = 1;
while ((p = strchr(p, '\n'))) {
opts->current_fixup_count++;
ctx->current_fixup_count++;
p++;
}
}
@ -5066,6 +5090,7 @@ static int commit_staged_changes(struct repository *r,
struct replay_opts *opts,
struct todo_list *todo_list)
{
struct replay_ctx *ctx = opts->ctx;
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
unsigned int final_fixup = 0, is_clean;
@ -5102,7 +5127,7 @@ static int commit_staged_changes(struct repository *r,
* the commit message and if there was a squash, let the user
* edit it.
*/
if (!is_clean || !opts->current_fixup_count)
if (!is_clean || !ctx->current_fixup_count)
; /* this is not the final fixup */
else if (!oideq(&head, &to_amend) ||
!file_exists(rebase_path_stopped_sha())) {
@ -5111,20 +5136,20 @@ static int commit_staged_changes(struct repository *r,
unlink(rebase_path_fixup_msg());
unlink(rebase_path_squash_msg());
unlink(rebase_path_current_fixups());
strbuf_reset(&opts->current_fixups);
opts->current_fixup_count = 0;
strbuf_reset(&ctx->current_fixups);
ctx->current_fixup_count = 0;
}
} else {
/* we are in a fixup/squash chain */
const char *p = opts->current_fixups.buf;
int len = opts->current_fixups.len;
const char *p = ctx->current_fixups.buf;
int len = ctx->current_fixups.len;
opts->current_fixup_count--;
ctx->current_fixup_count--;
if (!len)
BUG("Incorrect current_fixups:\n%s", p);
while (len && p[len - 1] != '\n')
len--;
strbuf_setlen(&opts->current_fixups, len);
strbuf_setlen(&ctx->current_fixups, len);
if (write_message(p, len, rebase_path_current_fixups(),
0) < 0)
return error(_("could not write file: '%s'"),
@ -5141,7 +5166,7 @@ static int commit_staged_changes(struct repository *r,
* actually need to re-commit with a cleaned up commit
* message.
*/
if (opts->current_fixup_count > 0 &&
if (ctx->current_fixup_count > 0 &&
!is_fixup(peek_command(todo_list, 0))) {
final_fixup = 1;
/*
@ -5214,14 +5239,14 @@ static int commit_staged_changes(struct repository *r,
unlink(rebase_path_fixup_msg());
unlink(rebase_path_squash_msg());
}
if (opts->current_fixup_count > 0) {
if (ctx->current_fixup_count > 0) {
/*
* Whether final fixup or not, we just cleaned up the commit
* message...
*/
unlink(rebase_path_current_fixups());
strbuf_reset(&opts->current_fixups);
opts->current_fixup_count = 0;
strbuf_reset(&ctx->current_fixups);
ctx->current_fixup_count = 0;
}
return 0;
}

View File

@ -69,10 +69,6 @@ struct replay_opts {
/* Reflog */
char *reflog_action;
/* Used by fixup/squash */
struct strbuf current_fixups;
int current_fixup_count;
/* placeholder commit for -i --root */
struct object_id squash_onto;
int have_squash_onto;
@ -86,7 +82,6 @@ struct replay_opts {
#define REPLAY_OPTS_INIT { \
.edit = -1, \
.action = -1, \
.current_fixups = STRBUF_INIT, \
.xopts = STRVEC_INIT, \
.ctx = replay_ctx_new(), \
}