Skip to content

Commit b995e78

Browse files
committed
Merge branch 'pw/rebase-i-after-failure'
Various fixes to the behaviour of "rebase -i" when the command got interrupted by conflicting changes. * pw/rebase-i-after-failure: rebase -i: fix adding failed command to the todo list rebase --continue: refuse to commit after failed command rebase: fix rewritten list for failed pick sequencer: factor out part of pick_commits() sequencer: use rebase_path_message() rebase -i: remove patch file after conflict resolution rebase -i: move unlink() calls
2 parents f73604f + 203573b commit b995e78

File tree

5 files changed

+228
-103
lines changed

5 files changed

+228
-103
lines changed

sequencer.c

Lines changed: 99 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
147147
* the commit object name of the corresponding patch.
148148
*/
149149
static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
150+
/*
151+
* When we stop for the user to resolve conflicts this file contains
152+
* the patch of the commit that is being picked.
153+
*/
154+
static GIT_PATH_FUNC(rebase_path_patch, "rebase-merge/patch")
150155
/*
151156
* For the post-rewrite hook, we make a list of rewritten commits and
152157
* their new sha1s. The rewritten-pending list keeps the sha1s of
@@ -3412,7 +3417,8 @@ int sequencer_skip(struct repository *r, struct replay_opts *opts)
34123417
return -1;
34133418
}
34143419

3415-
static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
3420+
static int save_todo(struct todo_list *todo_list, struct replay_opts *opts,
3421+
int reschedule)
34163422
{
34173423
struct lock_file todo_lock = LOCK_INIT;
34183424
const char *todo_path = get_todo_path(opts);
@@ -3422,7 +3428,7 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
34223428
* rebase -i writes "git-rebase-todo" without the currently executing
34233429
* command, appending it to "done" instead.
34243430
*/
3425-
if (is_rebase_i(opts))
3431+
if (is_rebase_i(opts) && !reschedule)
34263432
next++;
34273433

34283434
fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
@@ -3435,7 +3441,7 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
34353441
if (commit_lock_file(&todo_lock) < 0)
34363442
return error(_("failed to finalize '%s'"), todo_path);
34373443

3438-
if (is_rebase_i(opts) && next > 0) {
3444+
if (is_rebase_i(opts) && !reschedule && next > 0) {
34393445
const char *done = rebase_path_done();
34403446
int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666);
34413447
int ret = 0;
@@ -3516,47 +3522,46 @@ static int make_patch(struct repository *r,
35163522
struct commit *commit,
35173523
struct replay_opts *opts)
35183524
{
3519-
struct strbuf buf = STRBUF_INIT;
35203525
struct rev_info log_tree_opt;
35213526
const char *subject;
35223527
char hex[GIT_MAX_HEXSZ + 1];
35233528
int res = 0;
35243529

3530+
if (!is_rebase_i(opts))
3531+
BUG("make_patch should only be called when rebasing");
3532+
35253533
oid_to_hex_r(hex, &commit->object.oid);
35263534
if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0)
35273535
return -1;
35283536
res |= write_rebase_head(&commit->object.oid);
35293537

3530-
strbuf_addf(&buf, "%s/patch", get_dir(opts));
35313538
memset(&log_tree_opt, 0, sizeof(log_tree_opt));
35323539
repo_init_revisions(r, &log_tree_opt, NULL);
35333540
log_tree_opt.abbrev = 0;
35343541
log_tree_opt.diff = 1;
35353542
log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH;
35363543
log_tree_opt.disable_stdin = 1;
35373544
log_tree_opt.no_commit_id = 1;
3538-
log_tree_opt.diffopt.file = fopen(buf.buf, "w");
3545+
log_tree_opt.diffopt.file = fopen(rebase_path_patch(), "w");
35393546
log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER;
35403547
if (!log_tree_opt.diffopt.file)
3541-
res |= error_errno(_("could not open '%s'"), buf.buf);
3548+
res |= error_errno(_("could not open '%s'"),
3549+
rebase_path_patch());
35423550
else {
35433551
res |= log_tree_commit(&log_tree_opt, commit);
35443552
fclose(log_tree_opt.diffopt.file);
35453553
}
3546-
strbuf_reset(&buf);
35473554

3548-
strbuf_addf(&buf, "%s/message", get_dir(opts));
3549-
if (!file_exists(buf.buf)) {
3555+
if (!file_exists(rebase_path_message())) {
35503556
const char *encoding = get_commit_output_encoding();
35513557
const char *commit_buffer = repo_logmsg_reencode(r,
35523558
commit, NULL,
35533559
encoding);
35543560
find_commit_subject(commit_buffer, &subject);
3555-
res |= write_message(subject, strlen(subject), buf.buf, 1);
3561+
res |= write_message(subject, strlen(subject), rebase_path_message(), 1);
35563562
repo_unuse_commit_buffer(r, commit,
35573563
commit_buffer);
35583564
}
3559-
strbuf_release(&buf);
35603565
release_revisions(&log_tree_opt);
35613566

35623567
return res;
@@ -4174,6 +4179,7 @@ static int do_merge(struct repository *r,
41744179
if (ret < 0) {
41754180
error(_("could not even attempt to merge '%.*s'"),
41764181
merge_arg_len, arg);
4182+
unlink(git_path_merge_msg(r));
41774183
goto leave_merge;
41784184
}
41794185
/*
@@ -4661,6 +4667,68 @@ N_("Could not execute the todo command\n"
46614667
" git rebase --edit-todo\n"
46624668
" git rebase --continue\n");
46634669

4670+
static int pick_one_commit(struct repository *r,
4671+
struct todo_list *todo_list,
4672+
struct replay_opts *opts,
4673+
int *check_todo, int* reschedule)
4674+
{
4675+
int res;
4676+
struct todo_item *item = todo_list->items + todo_list->current;
4677+
const char *arg = todo_item_get_arg(todo_list, item);
4678+
if (is_rebase_i(opts))
4679+
opts->reflog_message = reflog_message(
4680+
opts, command_to_string(item->command), NULL);
4681+
4682+
res = do_pick_commit(r, item, opts, is_final_fixup(todo_list),
4683+
check_todo);
4684+
if (is_rebase_i(opts) && res < 0) {
4685+
/* Reschedule */
4686+
*reschedule = 1;
4687+
return -1;
4688+
}
4689+
if (item->command == TODO_EDIT) {
4690+
struct commit *commit = item->commit;
4691+
if (!res) {
4692+
if (!opts->verbose)
4693+
term_clear_line();
4694+
fprintf(stderr, _("Stopped at %s... %.*s\n"),
4695+
short_commit_name(r, commit), item->arg_len, arg);
4696+
}
4697+
return error_with_patch(r, commit,
4698+
arg, item->arg_len, opts, res, !res);
4699+
}
4700+
if (is_rebase_i(opts) && !res)
4701+
record_in_rewritten(&item->commit->object.oid,
4702+
peek_command(todo_list, 1));
4703+
if (res && is_fixup(item->command)) {
4704+
if (res == 1)
4705+
intend_to_amend();
4706+
return error_failed_squash(r, item->commit, opts,
4707+
item->arg_len, arg);
4708+
} else if (res && is_rebase_i(opts) && item->commit) {
4709+
int to_amend = 0;
4710+
struct object_id oid;
4711+
4712+
/*
4713+
* If we are rewording and have either
4714+
* fast-forwarded already, or are about to
4715+
* create a new root commit, we want to amend,
4716+
* otherwise we do not.
4717+
*/
4718+
if (item->command == TODO_REWORD &&
4719+
!repo_get_oid(r, "HEAD", &oid) &&
4720+
(oideq(&item->commit->object.oid, &oid) ||
4721+
(opts->have_squash_onto &&
4722+
oideq(&opts->squash_onto, &oid))))
4723+
to_amend = 1;
4724+
4725+
return res | error_with_patch(r, item->commit,
4726+
arg, item->arg_len, opts,
4727+
res, to_amend);
4728+
}
4729+
return res;
4730+
}
4731+
46644732
static int pick_commits(struct repository *r,
46654733
struct todo_list *todo_list,
46664734
struct replay_opts *opts)
@@ -4676,12 +4744,17 @@ static int pick_commits(struct repository *r,
46764744
if (read_and_refresh_cache(r, opts))
46774745
return -1;
46784746

4747+
unlink(rebase_path_message());
4748+
unlink(rebase_path_stopped_sha());
4749+
unlink(rebase_path_amend());
4750+
unlink(rebase_path_patch());
4751+
46794752
while (todo_list->current < todo_list->nr) {
46804753
struct todo_item *item = todo_list->items + todo_list->current;
46814754
const char *arg = todo_item_get_arg(todo_list, item);
46824755
int check_todo = 0;
46834756

4684-
if (save_todo(todo_list, opts))
4757+
if (save_todo(todo_list, opts, reschedule))
46854758
return -1;
46864759
if (is_rebase_i(opts)) {
46874760
if (item->command != TODO_COMMENT) {
@@ -4699,10 +4772,7 @@ static int pick_commits(struct repository *r,
46994772
todo_list->total_nr,
47004773
opts->verbose ? "\n" : "\r");
47014774
}
4702-
unlink(rebase_path_message());
47034775
unlink(rebase_path_author_script());
4704-
unlink(rebase_path_stopped_sha());
4705-
unlink(rebase_path_amend());
47064776
unlink(git_path_merge_head(r));
47074777
unlink(git_path_auto_merge(r));
47084778
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
@@ -4714,66 +4784,10 @@ static int pick_commits(struct repository *r,
47144784
}
47154785
}
47164786
if (item->command <= TODO_SQUASH) {
4717-
if (is_rebase_i(opts))
4718-
opts->reflog_message = reflog_message(opts,
4719-
command_to_string(item->command), NULL);
4720-
4721-
res = do_pick_commit(r, item, opts,
4722-
is_final_fixup(todo_list),
4723-
&check_todo);
4724-
if (is_rebase_i(opts) && res < 0) {
4725-
/* Reschedule */
4726-
advise(_(rescheduled_advice),
4727-
get_item_line_length(todo_list,
4728-
todo_list->current),
4729-
get_item_line(todo_list,
4730-
todo_list->current));
4731-
todo_list->current--;
4732-
if (save_todo(todo_list, opts))
4733-
return -1;
4734-
}
4735-
if (item->command == TODO_EDIT) {
4736-
struct commit *commit = item->commit;
4737-
if (!res) {
4738-
if (!opts->verbose)
4739-
term_clear_line();
4740-
fprintf(stderr,
4741-
_("Stopped at %s... %.*s\n"),
4742-
short_commit_name(r, commit),
4743-
item->arg_len, arg);
4744-
}
4745-
return error_with_patch(r, commit,
4746-
arg, item->arg_len, opts, res, !res);
4747-
}
4748-
if (is_rebase_i(opts) && !res)
4749-
record_in_rewritten(&item->commit->object.oid,
4750-
peek_command(todo_list, 1));
4751-
if (res && is_fixup(item->command)) {
4752-
if (res == 1)
4753-
intend_to_amend();
4754-
return error_failed_squash(r, item->commit, opts,
4755-
item->arg_len, arg);
4756-
} else if (res && is_rebase_i(opts) && item->commit) {
4757-
int to_amend = 0;
4758-
struct object_id oid;
4759-
4760-
/*
4761-
* If we are rewording and have either
4762-
* fast-forwarded already, or are about to
4763-
* create a new root commit, we want to amend,
4764-
* otherwise we do not.
4765-
*/
4766-
if (item->command == TODO_REWORD &&
4767-
!repo_get_oid(r, "HEAD", &oid) &&
4768-
(oideq(&item->commit->object.oid, &oid) ||
4769-
(opts->have_squash_onto &&
4770-
oideq(&opts->squash_onto, &oid))))
4771-
to_amend = 1;
4772-
4773-
return res | error_with_patch(r, item->commit,
4774-
arg, item->arg_len, opts,
4775-
res, to_amend);
4776-
}
4787+
res = pick_one_commit(r, todo_list, opts, &check_todo,
4788+
&reschedule);
4789+
if (!res && item->command == TODO_EDIT)
4790+
return 0;
47774791
} else if (item->command == TODO_EXEC) {
47784792
char *end_of_arg = (char *)(arg + item->arg_len);
47794793
int saved = *end_of_arg;
@@ -4821,22 +4835,19 @@ static int pick_commits(struct repository *r,
48214835
get_item_line_length(todo_list,
48224836
todo_list->current),
48234837
get_item_line(todo_list, todo_list->current));
4824-
todo_list->current--;
4825-
if (save_todo(todo_list, opts))
4838+
if (save_todo(todo_list, opts, reschedule))
48264839
return -1;
48274840
if (item->commit)
4828-
return error_with_patch(r,
4829-
item->commit,
4830-
arg, item->arg_len,
4831-
opts, res, 0);
4841+
write_rebase_head(&item->commit->object.oid);
48324842
} else if (is_rebase_i(opts) && check_todo && !res &&
48334843
reread_todo_if_changed(r, todo_list, opts)) {
48344844
return -1;
48354845
}
48364846

4837-
todo_list->current++;
48384847
if (res)
48394848
return res;
4849+
4850+
todo_list->current++;
48404851
}
48414852

48424853
if (is_rebase_i(opts)) {
@@ -4989,6 +5000,11 @@ static int commit_staged_changes(struct repository *r,
49895000

49905001
is_clean = !has_uncommitted_changes(r, 0);
49915002

5003+
if (!is_clean && !file_exists(rebase_path_message())) {
5004+
const char *gpg_opt = gpg_sign_opt_quoted(opts);
5005+
5006+
return error(_(staged_changes_advice), gpg_opt, gpg_opt);
5007+
}
49925008
if (file_exists(rebase_path_amend())) {
49935009
struct strbuf rev = STRBUF_INIT;
49945010
struct object_id head, to_amend;

t/t3404-rebase-interactive.sh

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ test_expect_success 'clean error after failed "exec"' '
604604
echo "edited again" > file7 &&
605605
git add file7 &&
606606
test_must_fail git rebase --continue 2>error &&
607-
test_i18ngrep "you have staged changes in your working tree" error
607+
test_i18ngrep "you have staged changes in your working tree" error &&
608+
test_i18ngrep ! "could not open.*for reading" error
608609
'
609610

610611
test_expect_success 'rebase a detached HEAD' '
@@ -1276,20 +1277,34 @@ test_expect_success 'todo count' '
12761277
'
12771278

12781279
test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1279-
git checkout --force branch2 &&
1280+
git checkout --force A &&
12801281
git clean -f &&
1282+
cat >todo <<-EOF &&
1283+
exec >file2
1284+
pick $(git rev-parse B) B
1285+
pick $(git rev-parse C) C
1286+
pick $(git rev-parse D) D
1287+
exec cat .git/rebase-merge/done >actual
1288+
EOF
12811289
(
1282-
set_fake_editor &&
1283-
FAKE_LINES="edit 1 2" git rebase -i A
1284-
) &&
1285-
test_cmp_rev HEAD F &&
1286-
test_path_is_missing file6 &&
1287-
>file6 &&
1288-
test_must_fail git rebase --continue &&
1289-
test_cmp_rev HEAD F &&
1290-
rm file6 &&
1290+
set_replace_editor todo &&
1291+
test_must_fail git rebase -i A
1292+
) &&
1293+
test_cmp_rev HEAD B &&
1294+
test_cmp_rev REBASE_HEAD C &&
1295+
head -n3 todo >expect &&
1296+
test_cmp expect .git/rebase-merge/done &&
1297+
rm file2 &&
1298+
test_path_is_missing .git/rebase-merge/patch &&
1299+
echo changed >file1 &&
1300+
git add file1 &&
1301+
test_must_fail git rebase --continue 2>err &&
1302+
grep "error: you have staged changes in your working tree" err &&
1303+
git reset --hard HEAD &&
12911304
git rebase --continue &&
1292-
test_cmp_rev HEAD I
1305+
test_cmp_rev HEAD D &&
1306+
tail -n3 todo >>expect &&
1307+
test_cmp expect actual
12931308
'
12941309

12951310
test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
@@ -1305,7 +1320,14 @@ test_expect_success 'rebase -i commits that overwrite untracked files (squash)'
13051320
>file6 &&
13061321
test_must_fail git rebase --continue &&
13071322
test_cmp_rev HEAD F &&
1323+
test_cmp_rev REBASE_HEAD I &&
13081324
rm file6 &&
1325+
test_path_is_missing .git/rebase-merge/patch &&
1326+
echo changed >file1 &&
1327+
git add file1 &&
1328+
test_must_fail git rebase --continue 2>err &&
1329+
grep "error: you have staged changes in your working tree" err &&
1330+
git reset --hard HEAD &&
13091331
git rebase --continue &&
13101332
test $(git cat-file commit HEAD | sed -ne \$p) = I &&
13111333
git reset --hard original-branch2
@@ -1323,7 +1345,14 @@ test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
13231345
>file6 &&
13241346
test_must_fail git rebase --continue &&
13251347
test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1348+
test_cmp_rev REBASE_HEAD I &&
13261349
rm file6 &&
1350+
test_path_is_missing .git/rebase-merge/patch &&
1351+
echo changed >file1 &&
1352+
git add file1 &&
1353+
test_must_fail git rebase --continue 2>err &&
1354+
grep "error: you have staged changes in your working tree" err &&
1355+
git reset --hard HEAD &&
13271356
git rebase --continue &&
13281357
test $(git cat-file commit HEAD | sed -ne \$p) = I
13291358
'

0 commit comments

Comments
 (0)