summaryrefslogtreecommitdiff
path: root/sequencer.c
diff options
context:
space:
mode:
Diffstat (limited to 'sequencer.c')
-rw-r--r--sequencer.c346
1 files changed, 294 insertions, 52 deletions
diff --git a/sequencer.c b/sequencer.c
index 848204d..0bec01c 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -164,6 +164,7 @@ static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec")
+static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-reschedule-failed-exec")
static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits")
static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits")
@@ -524,7 +525,7 @@ static int fast_forward_to(struct repository *r,
if (!transaction ||
ref_transaction_update(transaction, "HEAD",
to, unborn && !is_rebase_i(opts) ?
- &null_oid : from,
+ null_oid() : from,
0, sb.buf, &err) ||
ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction);
@@ -1131,7 +1132,7 @@ int update_head_with_reflog(const struct commit *old_head,
transaction = ref_transaction_begin(err);
if (!transaction ||
ref_transaction_update(transaction, "HEAD", new_head,
- old_head ? &old_head->object.oid : &null_oid,
+ old_head ? &old_head->object.oid : null_oid(),
0, sb.buf, err) ||
ref_transaction_commit(transaction, err)) {
ret = -1;
@@ -1726,13 +1727,183 @@ static int is_pick_or_similar(enum todo_command command)
}
}
+enum todo_item_flags {
+ TODO_EDIT_MERGE_MSG = (1 << 0),
+ TODO_REPLACE_FIXUP_MSG = (1 << 1),
+ TODO_EDIT_FIXUP_MSG = (1 << 2),
+};
+
+static const char first_commit_msg_str[] = N_("This is the 1st commit message:");
+static const char nth_commit_msg_fmt[] = N_("This is the commit message #%d:");
+static const char skip_first_commit_msg_str[] = N_("The 1st commit message will be skipped:");
+static const char skip_nth_commit_msg_fmt[] = N_("The commit message #%d will be skipped:");
+static const char combined_commit_msg_fmt[] = N_("This is a combination of %d commits.");
+
+static int is_fixup_flag(enum todo_command command, unsigned flag)
+{
+ return command == TODO_FIXUP && ((flag & TODO_REPLACE_FIXUP_MSG) ||
+ (flag & TODO_EDIT_FIXUP_MSG));
+}
+
+/*
+ * Wrapper around strbuf_add_commented_lines() which avoids double
+ * commenting commit subjects.
+ */
+static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
+{
+ const char *s = str;
+ while (len > 0 && s[0] == comment_line_char) {
+ size_t count;
+ const char *n = memchr(s, '\n', len);
+ if (!n)
+ count = len;
+ else
+ count = n - s + 1;
+ strbuf_add(buf, s, count);
+ s += count;
+ len -= count;
+ }
+ strbuf_add_commented_lines(buf, s, len);
+}
+
+/* Does the current fixup chain contain a squash command? */
+static int seen_squash(struct replay_opts *opts)
+{
+ return starts_with(opts->current_fixups.buf, "squash") ||
+ strstr(opts->current_fixups.buf, "\nsquash");
+}
+
+static void update_comment_bufs(struct strbuf *buf1, struct strbuf *buf2, int n)
+{
+ strbuf_setlen(buf1, 2);
+ strbuf_addf(buf1, _(nth_commit_msg_fmt), n);
+ strbuf_addch(buf1, '\n');
+ strbuf_setlen(buf2, 2);
+ strbuf_addf(buf2, _(skip_nth_commit_msg_fmt), n);
+ strbuf_addch(buf2, '\n');
+}
+
+/*
+ * Comment out any un-commented commit messages, updating the message comments
+ * to say they will be skipped but do not comment out the empty lines that
+ * surround commit messages and their comments.
+ */
+static void update_squash_message_for_fixup(struct strbuf *msg)
+{
+ void (*copy_lines)(struct strbuf *, const void *, size_t) = strbuf_add;
+ struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
+ const char *s, *start;
+ char *orig_msg;
+ size_t orig_msg_len;
+ int i = 1;
+
+ strbuf_addf(&buf1, "# %s\n", _(first_commit_msg_str));
+ strbuf_addf(&buf2, "# %s\n", _(skip_first_commit_msg_str));
+ s = start = orig_msg = strbuf_detach(msg, &orig_msg_len);
+ while (s) {
+ const char *next;
+ size_t off;
+ if (skip_prefix(s, buf1.buf, &next)) {
+ /*
+ * Copy the last message, preserving the blank line
+ * preceding the current line
+ */
+ off = (s > start + 1 && s[-2] == '\n') ? 1 : 0;
+ copy_lines(msg, start, s - start - off);
+ if (off)
+ strbuf_addch(msg, '\n');
+ /*
+ * The next message needs to be commented out but the
+ * message header is already commented out so just copy
+ * it and the blank line that follows it.
+ */
+ strbuf_addbuf(msg, &buf2);
+ if (*next == '\n')
+ strbuf_addch(msg, *next++);
+ start = s = next;
+ copy_lines = add_commented_lines;
+ update_comment_bufs(&buf1, &buf2, ++i);
+ } else if (skip_prefix(s, buf2.buf, &next)) {
+ off = (s > start + 1 && s[-2] == '\n') ? 1 : 0;
+ copy_lines(msg, start, s - start - off);
+ start = s - off;
+ s = next;
+ copy_lines = strbuf_add;
+ update_comment_bufs(&buf1, &buf2, ++i);
+ } else {
+ s = strchr(s, '\n');
+ if (s)
+ s++;
+ }
+ }
+ copy_lines(msg, start, orig_msg_len - (start - orig_msg));
+ free(orig_msg);
+ strbuf_release(&buf1);
+ strbuf_release(&buf2);
+}
+
+static int append_squash_message(struct strbuf *buf, const char *body,
+ enum todo_command command, struct replay_opts *opts,
+ unsigned flag)
+{
+ const char *fixup_msg;
+ size_t commented_len = 0, fixup_off;
+ /*
+ * amend is non-interactive and not normally used with fixup!
+ * or squash! commits, so only comment out those subjects when
+ * squashing commit messages.
+ */
+ if (starts_with(body, "amend!") ||
+ ((command == TODO_SQUASH || seen_squash(opts)) &&
+ (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);
+ strbuf_addstr(buf, "\n\n");
+ strbuf_add_commented_lines(buf, body, commented_len);
+ /* buf->buf may be reallocated so store an offset into the buffer */
+ fixup_off = buf->len;
+ strbuf_addstr(buf, body + commented_len);
+
+ /* fixup -C after squash behaves like squash */
+ if (is_fixup_flag(command, flag) && !seen_squash(opts)) {
+ /*
+ * We're replacing the commit message so we need to
+ * append the Signed-off-by: trailer if the user
+ * requested '--signoff'.
+ */
+ if (opts->signoff)
+ append_signoff(buf, 0, 0);
+
+ if ((command == TODO_FIXUP) &&
+ (flag & TODO_REPLACE_FIXUP_MSG) &&
+ (file_exists(rebase_path_fixup_msg()) ||
+ !file_exists(rebase_path_squash_msg()))) {
+ fixup_msg = skip_blank_lines(buf->buf + fixup_off);
+ if (write_message(fixup_msg, strlen(fixup_msg),
+ rebase_path_fixup_msg(), 0) < 0)
+ return error(_("cannot write '%s'"),
+ rebase_path_fixup_msg());
+ } else {
+ unlink(rebase_path_fixup_msg());
+ }
+ } else {
+ unlink(rebase_path_fixup_msg());
+ }
+
+ return 0;
+}
+
static int update_squash_messages(struct repository *r,
enum todo_command command,
struct commit *commit,
- struct replay_opts *opts)
+ struct replay_opts *opts,
+ unsigned flag)
{
struct strbuf buf = STRBUF_INIT;
- int res;
+ int res = 0;
const char *message, *body;
const char *encoding = get_commit_output_encoding();
@@ -1748,10 +1919,12 @@ static int update_squash_messages(struct repository *r,
buf.buf : strchrnul(buf.buf, '\n');
strbuf_addf(&header, "%c ", comment_line_char);
- strbuf_addf(&header, _("This is a combination of %d commits."),
+ strbuf_addf(&header, _(combined_commit_msg_fmt),
opts->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))
+ update_squash_message_for_fixup(&buf);
} else {
struct object_id head;
struct commit *head_commit;
@@ -1765,19 +1938,22 @@ static int update_squash_messages(struct repository *r,
return error(_("could not read HEAD's commit message"));
find_commit_subject(head_message, &body);
- if (write_message(body, strlen(body),
- rebase_path_fixup_msg(), 0)) {
+ if (command == TODO_FIXUP && !flag && write_message(body, strlen(body),
+ rebase_path_fixup_msg(), 0) < 0) {
unuse_commit_buffer(head_commit, head_message);
- return error(_("cannot write '%s'"),
- rebase_path_fixup_msg());
+ return error(_("cannot write '%s'"), rebase_path_fixup_msg());
}
-
strbuf_addf(&buf, "%c ", comment_line_char);
- strbuf_addf(&buf, _("This is a combination of %d commits."), 2);
+ strbuf_addf(&buf, _(combined_commit_msg_fmt), 2);
strbuf_addf(&buf, "\n%c ", comment_line_char);
- strbuf_addstr(&buf, _("This is the 1st commit message:"));
+ strbuf_addstr(&buf, is_fixup_flag(command, flag) ?
+ _(skip_first_commit_msg_str) :
+ _(first_commit_msg_str));
strbuf_addstr(&buf, "\n\n");
- strbuf_addstr(&buf, body);
+ if (is_fixup_flag(command, flag))
+ strbuf_add_commented_lines(&buf, body, strlen(body));
+ else
+ strbuf_addstr(&buf, body);
unuse_commit_buffer(head_commit, head_message);
}
@@ -1787,16 +1963,11 @@ static int update_squash_messages(struct repository *r,
oid_to_hex(&commit->object.oid));
find_commit_subject(message, &body);
- if (command == TODO_SQUASH) {
- unlink(rebase_path_fixup_msg());
- strbuf_addf(&buf, "\n%c ", comment_line_char);
- strbuf_addf(&buf, _("This is the commit message #%d:"),
- ++opts->current_fixup_count + 1);
- strbuf_addstr(&buf, "\n\n");
- strbuf_addstr(&buf, body);
+ if (command == TODO_SQUASH || is_fixup_flag(command, flag)) {
+ res = append_squash_message(&buf, body, command, opts, flag);
} else if (command == TODO_FIXUP) {
strbuf_addf(&buf, "\n%c ", comment_line_char);
- strbuf_addf(&buf, _("The commit message #%d will be skipped:"),
+ strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
++opts->current_fixup_count + 1);
strbuf_addstr(&buf, "\n\n");
strbuf_add_commented_lines(&buf, body, strlen(body));
@@ -1804,7 +1975,9 @@ static int update_squash_messages(struct repository *r,
return error(_("unknown command: %d"), command);
unuse_commit_buffer(commit, message);
- res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0);
+ if (!res)
+ res = write_message(buf.buf, buf.len, rebase_path_squash_msg(),
+ 0);
strbuf_release(&buf);
if (!res) {
@@ -1860,14 +2033,24 @@ static void record_in_rewritten(struct object_id *oid,
flush_rewritten_pending();
}
+static int should_edit(struct replay_opts *opts) {
+ if (opts->edit < 0)
+ /*
+ * Note that we only handle the case of non-conflicted
+ * commits; continue_single_pick() handles the conflicted
+ * commits itself instead of calling this function.
+ */
+ return (opts->action == REPLAY_REVERT && isatty(0)) ? 1 : 0;
+ return opts->edit;
+}
+
static int do_pick_commit(struct repository *r,
- enum todo_command command,
- struct commit *commit,
+ struct todo_item *item,
struct replay_opts *opts,
int final_fixup, int *check_todo)
{
- unsigned int flags = opts->edit ? EDIT_MSG : 0;
- const char *msg_file = opts->edit ? NULL : git_path_merge_msg(r);
+ 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;
struct commit *base, *next, *parent;
const char *base_label, *next_label;
@@ -1875,6 +2058,8 @@ static int do_pick_commit(struct repository *r,
struct commit_message msg = { NULL, NULL, NULL, NULL };
struct strbuf msgbuf = STRBUF_INIT;
int res, unborn = 0, reword = 0, allow, drop_commit;
+ enum todo_command command = item->command;
+ struct commit *commit = item->commit;
if (opts->no_commit) {
/*
@@ -2004,7 +2189,8 @@ static int do_pick_commit(struct repository *r,
if (command == TODO_REWORD)
reword = 1;
else if (is_fixup(command)) {
- if (update_squash_messages(r, command, commit, opts))
+ if (update_squash_messages(r, command, commit,
+ opts, item->flags))
return -1;
flags |= AMEND_MSG;
if (!final_fixup)
@@ -2096,6 +2282,7 @@ static int do_pick_commit(struct repository *r,
refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
NULL, 0);
unlink(git_path_merge_msg(r));
+ unlink(git_path_auto_merge(r));
fprintf(stderr,
_("dropping %s %s -- patch contents already upstream\n"),
oid_to_hex(&commit->object.oid), msg.subject);
@@ -2169,10 +2356,6 @@ static int read_and_refresh_cache(struct repository *r,
return 0;
}
-enum todo_item_flags {
- TODO_EDIT_MERGE_MSG = 1
-};
-
void todo_list_release(struct todo_list *todo_list)
{
strbuf_release(&todo_list->buf);
@@ -2259,6 +2442,18 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
return 0;
}
+ if (item->command == TODO_FIXUP) {
+ if (skip_prefix(bol, "-C", &bol) &&
+ (*bol == ' ' || *bol == '\t')) {
+ bol += strspn(bol, " \t");
+ item->flags |= TODO_REPLACE_FIXUP_MSG;
+ } else if (skip_prefix(bol, "-c", &bol) &&
+ (*bol == ' ' || *bol == '\t')) {
+ bol += strspn(bol, " \t");
+ item->flags |= TODO_EDIT_FIXUP_MSG;
+ }
+ }
+
if (item->command == TODO_MERGE) {
if (skip_prefix(bol, "-C", &bol))
bol += strspn(bol, " \t");
@@ -2451,6 +2646,8 @@ void sequencer_post_commit_cleanup(struct repository *r, int verbose)
need_cleanup = 1;
}
+ unlink(git_path_auto_merge(r));
+
if (!need_cleanup)
return;
@@ -2672,6 +2869,8 @@ static int read_populate_opts(struct replay_opts *opts)
if (file_exists(rebase_path_reschedule_failed_exec()))
opts->reschedule_failed_exec = 1;
+ else if (file_exists(rebase_path_no_reschedule_failed_exec()))
+ opts->reschedule_failed_exec = 0;
if (file_exists(rebase_path_drop_redundant_commits()))
opts->drop_redundant_commits = 1;
@@ -2772,6 +2971,8 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
write_file(rebase_path_ignore_date(), "%s", "");
if (opts->reschedule_failed_exec)
write_file(rebase_path_reschedule_failed_exec(), "%s", "");
+ else
+ write_file(rebase_path_no_reschedule_failed_exec(), "%s", "");
return 0;
}
@@ -3101,9 +3302,9 @@ static int save_opts(struct replay_opts *opts)
if (opts->no_commit)
res |= git_config_set_in_file_gently(opts_file,
"options.no-commit", "true");
- if (opts->edit)
- res |= git_config_set_in_file_gently(opts_file,
- "options.edit", "true");
+ if (opts->edit >= 0)
+ res |= git_config_set_in_file_gently(opts_file, "options.edit",
+ opts->edit ? "true" : "false");
if (opts->allow_empty)
res |= git_config_set_in_file_gently(opts_file,
"options.allow-empty", "true");
@@ -4077,7 +4278,7 @@ static int pick_commits(struct repository *r,
prev_reflog_action = xstrdup(getenv(GIT_REFLOG_ACTION));
if (opts->allow_ff)
assert(!(opts->signoff || opts->no_commit ||
- opts->record_origin || opts->edit ||
+ opts->record_origin || should_edit(opts) ||
opts->committer_date_is_author_date ||
opts->ignore_date));
if (read_and_refresh_cache(r, opts))
@@ -4111,6 +4312,7 @@ static int pick_commits(struct repository *r,
unlink(rebase_path_stopped_sha());
unlink(rebase_path_amend());
unlink(git_path_merge_head(r));
+ unlink(git_path_auto_merge(r));
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
if (item->command == TODO_BREAK) {
@@ -4124,8 +4326,8 @@ static int pick_commits(struct repository *r,
setenv(GIT_REFLOG_ACTION, reflog_message(opts,
command_to_string(item->command), NULL),
1);
- res = do_pick_commit(r, item->command, item->commit,
- opts, is_final_fixup(todo_list),
+ res = do_pick_commit(r, item, opts,
+ is_final_fixup(todo_list),
&check_todo);
if (is_rebase_i(opts))
setenv(GIT_REFLOG_ACTION, prev_reflog_action, 1);
@@ -4370,14 +4572,33 @@ cleanup_head_ref:
return sequencer_remove_state(opts);
}
-static int continue_single_pick(struct repository *r)
+static int continue_single_pick(struct repository *r, struct replay_opts *opts)
{
- const char *argv[] = { "commit", NULL };
+ struct strvec argv = STRVEC_INIT;
+ int ret;
if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD"))
return error(_("no cherry-pick or revert in progress"));
- return run_command_v_opt(argv, RUN_GIT_CMD);
+
+ strvec_push(&argv, "commit");
+
+ /*
+ * continue_single_pick() handles the case of recovering from a
+ * conflict. should_edit() doesn't handle that case; for a conflict,
+ * we want to edit if the user asked for it, or if they didn't specify
+ * and stdin is a tty.
+ */
+ if (!opts->edit || (opts->edit < 0 && !isatty(0)))
+ /*
+ * Include --cleanup=strip as well because we don't want the
+ * "# Conflicts:" messages.
+ */
+ strvec_pushl(&argv, "--no-edit", "--cleanup=strip", NULL);
+
+ ret = run_command_v_opt(argv.v, RUN_GIT_CMD);
+ strvec_clear(&argv);
+ return ret;
}
static int commit_staged_changes(struct repository *r,
@@ -4505,6 +4726,7 @@ static int commit_staged_changes(struct repository *r,
return error(_("could not commit staged changes."));
unlink(rebase_path_amend());
unlink(git_path_merge_head(r));
+ unlink(git_path_auto_merge(r));
if (final_fixup) {
unlink(rebase_path_fixup_msg());
unlink(rebase_path_squash_msg());
@@ -4547,7 +4769,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
goto release_todo_list;
}
} else if (!file_exists(get_todo_path(opts)))
- return continue_single_pick(r);
+ return continue_single_pick(r, opts);
else if ((res = read_populate_todo(r, &todo_list, opts)))
goto release_todo_list;
@@ -4556,7 +4778,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
if (refs_ref_exists(get_main_ref_store(r),
"CHERRY_PICK_HEAD") ||
refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) {
- res = continue_single_pick(r);
+ res = continue_single_pick(r, opts);
if (res)
goto release_todo_list;
}
@@ -4587,11 +4809,14 @@ static int single_pick(struct repository *r,
struct replay_opts *opts)
{
int check_todo;
+ struct todo_item item;
+
+ item.command = opts->action == REPLAY_PICK ?
+ TODO_PICK : TODO_REVERT;
+ item.commit = cmit;
setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
- return do_pick_commit(r, opts->action == REPLAY_PICK ?
- TODO_PICK : TODO_REVERT, cmit, opts, 0,
- &check_todo);
+ return do_pick_commit(r, &item, opts, 0, &check_todo);
}
int sequencer_pick_revisions(struct repository *r,
@@ -5262,6 +5487,14 @@ static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_lis
short_commit_name(item->commit) :
oid_to_hex(&item->commit->object.oid);
+ if (item->command == TODO_FIXUP) {
+ if (item->flags & TODO_EDIT_FIXUP_MSG)
+ strbuf_addstr(buf, " -c");
+ else if (item->flags & TODO_REPLACE_FIXUP_MSG) {
+ strbuf_addstr(buf, " -C");
+ }
+ }
+
if (item->command == TODO_MERGE) {
if (item->flags & TODO_EDIT_MERGE_MSG)
strbuf_addstr(buf, " -c");
@@ -5462,6 +5695,12 @@ static int subject2item_cmp(const void *fndata,
define_commit_slab(commit_todo_item, struct todo_item *);
+static int skip_fixupish(const char *subject, const char **p) {
+ return skip_prefix(subject, "fixup! ", p) ||
+ skip_prefix(subject, "amend! ", p) ||
+ skip_prefix(subject, "squash! ", p);
+}
+
/*
* Rearrange the todo list that has both "pick commit-id msg" and "pick
* commit-id fixup!/squash! msg" in it so that the latter is put immediately
@@ -5520,15 +5759,13 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
format_subject(&buf, subject, " ");
subject = subjects[i] = strbuf_detach(&buf, &subject_len);
unuse_commit_buffer(item->commit, commit_buffer);
- if ((skip_prefix(subject, "fixup! ", &p) ||
- skip_prefix(subject, "squash! ", &p))) {
+ if (skip_fixupish(subject, &p)) {
struct commit *commit2;
for (;;) {
while (isspace(*p))
p++;
- if (!skip_prefix(p, "fixup! ", &p) &&
- !skip_prefix(p, "squash! ", &p))
+ if (!skip_fixupish(p, &p))
break;
}
@@ -5558,9 +5795,14 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
}
if (i2 >= 0) {
rearranged = 1;
- todo_list->items[i].command =
- starts_with(subject, "fixup!") ?
- TODO_FIXUP : TODO_SQUASH;
+ if (starts_with(subject, "fixup!")) {
+ todo_list->items[i].command = TODO_FIXUP;
+ } else if (starts_with(subject, "amend!")) {
+ todo_list->items[i].command = TODO_FIXUP;
+ todo_list->items[i].flags = TODO_REPLACE_FIXUP_MSG;
+ } else {
+ todo_list->items[i].command = TODO_SQUASH;
+ }
if (tail[i2] < 0) {
next[i] = next[i2];
next[i2] = i;