Skip to content

Commit 206a78d

Browse files
phillipwoodgitster
authored andcommitted
rebase -i: remove patch file after conflict resolution
When a rebase stops for the user to resolve conflicts it writes a patch for the conflicting commit to .git/rebase-merge/patch. This file has been written since the introduction of "git-rebase-interactive.sh" in 1b1dce4 (Teach rebase an interactive mode, 2007-06-25). I assume the idea was to enable the user inspect the conflicting commit in the same way as they could for the patch based rebase. This file should be deleted when the rebase continues as if the rebase stops for a failed "exec" command or a "break" command it is confusing to the user if there is a stale patch lying around from an unrelated command. As the path is now used in two different places rebase_path_patch() is added and used to obtain the path for the patch. To construct the path write_patch() previously used get_dir() which returns different paths depending on whether we're rebasing or cherry-picking/reverting. As this function is only called when rebasing it is safe to use a hard coded string for the directory instead. An assertion is added to make sure we don't starting calling this function when cherry-picking in the future. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 36ac861 commit 206a78d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

sequencer.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
132132
* the commit object name of the corresponding patch.
133133
*/
134134
static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
135+
/*
136+
* When we stop for the user to resolve conflicts this file contains
137+
* the patch of the commit that is being picked.
138+
*/
139+
static GIT_PATH_FUNC(rebase_path_patch, "rebase-merge/patch")
135140
/*
136141
* For the post-rewrite hook, we make a list of rewritten commits and
137142
* their new sha1s. The rewritten-pending list keeps the sha1s of
@@ -3485,28 +3490,30 @@ static int make_patch(struct repository *r,
34853490
char hex[GIT_MAX_HEXSZ + 1];
34863491
int res = 0;
34873492

3493+
if (!is_rebase_i(opts))
3494+
BUG("make_patch should only be called when rebasing");
3495+
34883496
oid_to_hex_r(hex, &commit->object.oid);
34893497
if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0)
34903498
return -1;
34913499
res |= write_rebase_head(&commit->object.oid);
34923500

3493-
strbuf_addf(&buf, "%s/patch", get_dir(opts));
34943501
memset(&log_tree_opt, 0, sizeof(log_tree_opt));
34953502
repo_init_revisions(r, &log_tree_opt, NULL);
34963503
log_tree_opt.abbrev = 0;
34973504
log_tree_opt.diff = 1;
34983505
log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH;
34993506
log_tree_opt.disable_stdin = 1;
35003507
log_tree_opt.no_commit_id = 1;
3501-
log_tree_opt.diffopt.file = fopen(buf.buf, "w");
3508+
log_tree_opt.diffopt.file = fopen(rebase_path_patch(), "w");
35023509
log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER;
35033510
if (!log_tree_opt.diffopt.file)
3504-
res |= error_errno(_("could not open '%s'"), buf.buf);
3511+
res |= error_errno(_("could not open '%s'"),
3512+
rebase_path_patch());
35053513
else {
35063514
res |= log_tree_commit(&log_tree_opt, commit);
35073515
fclose(log_tree_opt.diffopt.file);
35083516
}
3509-
strbuf_reset(&buf);
35103517

35113518
strbuf_addf(&buf, "%s/message", get_dir(opts));
35123519
if (!file_exists(buf.buf)) {
@@ -4642,6 +4649,7 @@ static int pick_commits(struct repository *r,
46424649
unlink(rebase_path_message());
46434650
unlink(rebase_path_stopped_sha());
46444651
unlink(rebase_path_amend());
4652+
unlink(rebase_path_patch());
46454653

46464654
while (todo_list->current < todo_list->nr) {
46474655
struct todo_item *item = todo_list->items + todo_list->current;

t/t3418-rebase-continue.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ test_expect_success 'the todo command "break" works' '
244244
test_path_is_file execed
245245
'
246246

247+
test_expect_success 'patch file is removed before break command' '
248+
test_when_finished "git rebase --abort" &&
249+
cat >todo <<-\EOF &&
250+
pick commit-new-file-F2-on-topic-branch
251+
break
252+
EOF
253+
254+
(
255+
set_replace_editor todo &&
256+
test_must_fail git rebase -i --onto commit-new-file-F2 HEAD
257+
) &&
258+
test_path_is_file .git/rebase-merge/patch &&
259+
echo 22>F2 &&
260+
git add F2 &&
261+
git rebase --continue &&
262+
test_path_is_missing .git/rebase-merge/patch
263+
'
264+
247265
test_expect_success '--reschedule-failed-exec' '
248266
test_when_finished "git rebase --abort" &&
249267
test_must_fail git rebase -x false --reschedule-failed-exec HEAD^ &&

0 commit comments

Comments
 (0)