Skip to content

Commit d559f50

Browse files
phillipwoodgitster
authored andcommitted
rebase --abort/--quit: cleanup refs/rewritten
When `rebase -r` finishes it removes any refs under refs/rewritten that it has created. However if the user aborts or quits the rebase refs are not removed. This can cause problems for future rebases. For example I recently wanted to merge a updated version of a topic branch into an integration branch so ran `rebase -ir` and removed the picks and label for the topic branch from the todo list so that merge -C <old-merge> topic would pick up the new version of topic. Unfortunately refs/rewritten/topic already existed from a previous rebase that had been aborted so the rebase just used the old topic, not the new one. The logic for the non-interactive quit case is changed to ensure `buf` is always freed. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 37e9ee5 commit d559f50

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

builtin/rebase.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,18 @@ static int finish_rebase(struct rebase_options *opts)
770770
* user should see them.
771771
*/
772772
run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
773-
strbuf_addstr(&dir, opts->state_dir);
774-
if (remove_dir_recursively(&dir, 0))
775-
ret = error(_("could not remove '%s'"), opts->state_dir);
776-
strbuf_release(&dir);
773+
if (opts->type == REBASE_INTERACTIVE) {
774+
struct replay_opts replay = REPLAY_OPTS_INIT;
775+
776+
replay.action = REPLAY_INTERACTIVE_REBASE;
777+
ret = sequencer_remove_state(&replay);
778+
} else {
779+
strbuf_addstr(&dir, opts->state_dir);
780+
if (remove_dir_recursively(&dir, 0))
781+
ret = error(_("could not remove '%s'"),
782+
opts->state_dir);
783+
strbuf_release(&dir);
784+
}
777785

778786
return ret;
779787
}
@@ -1651,11 +1659,19 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
16511659
goto cleanup;
16521660
}
16531661
case ACTION_QUIT: {
1654-
strbuf_reset(&buf);
1655-
strbuf_addstr(&buf, options.state_dir);
1656-
ret = !!remove_dir_recursively(&buf, 0);
1657-
if (ret)
1658-
die(_("could not remove '%s'"), options.state_dir);
1662+
if (options.type == REBASE_INTERACTIVE) {
1663+
struct replay_opts replay = REPLAY_OPTS_INIT;
1664+
1665+
replay.action = REPLAY_INTERACTIVE_REBASE;
1666+
ret = !!sequencer_remove_state(&replay);
1667+
} else {
1668+
strbuf_reset(&buf);
1669+
strbuf_addstr(&buf, options.state_dir);
1670+
ret = !!remove_dir_recursively(&buf, 0);
1671+
if (ret)
1672+
error(_("could not remove '%s'"),
1673+
options.state_dir);
1674+
}
16591675
goto cleanup;
16601676
}
16611677
case ACTION_EDIT_TODO:

t/t3430-rebase-merges.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,24 @@ test_expect_success 'refs/rewritten/* is worktree-local' '
224224
test_cmp_rev HEAD "$(cat wt/b)"
225225
'
226226

227+
test_expect_success '--abort cleans up refs/rewritten' '
228+
git checkout -b abort-cleans-refs-rewritten H &&
229+
GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
230+
git rev-parse --verify refs/rewritten/onto &&
231+
git rebase --abort &&
232+
test_must_fail git rev-parse --verify refs/rewritten/onto
233+
'
234+
235+
test_expect_success '--quit cleans up refs/rewritten' '
236+
git checkout -b quit-cleans-refs-rewritten H &&
237+
GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
238+
git rev-parse --verify refs/rewritten/onto &&
239+
git rebase --quit &&
240+
test_must_fail git rev-parse --verify refs/rewritten/onto
241+
'
242+
227243
test_expect_success 'post-rewrite hook and fixups work for merges' '
228-
git checkout -b post-rewrite &&
244+
git checkout -b post-rewrite H &&
229245
test_commit same1 &&
230246
git reset --hard HEAD^ &&
231247
test_commit same2 &&

0 commit comments

Comments
 (0)