Skip to content

Commit 97b1b4f

Browse files
peffgitster
authored andcommitted
merge: merge unborn index before setting ref
When we merge into an unborn branch, there are basically two steps: 1. Write the sha1 of the new commit into the ref pointed to by HEAD. 2. Update the index with the new content, and check it out to the working tree. We currently do them in this order. However, (2) is the step that is much more likely to fail, since it can be blocked by things like untracked working tree files. When it does, the merge fails and we are left with an empty index but an updated HEAD. This patch switches the order, so that a failure in updating the index leaves us unchanged. Of course, a failure in updating the ref now leaves us with an updated index and mis-matched HEAD. That is arguably not much better, but it is probably less likely to actually happen. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6d9e76 commit 97b1b4f

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,9 +1063,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10631063
remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
10641064
if (!remote_head)
10651065
die("%s - not something we can merge", argv[0]);
1066+
read_empty(remote_head->sha1, 0);
10661067
update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
10671068
DIE_ON_ERR);
1068-
read_empty(remote_head->sha1, 0);
10691069
return 0;
10701070
} else {
10711071
struct strbuf merge_names = STRBUF_INIT;

t/t7607-merge-overwrite.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ test_expect_success 'will not overwrite untracked file on unborn branch' '
157157
test_cmp important c0.c
158158
'
159159

160+
test_expect_success 'failed merge leaves unborn branch in the womb' '
161+
test_must_fail git rev-parse --verify HEAD
162+
'
163+
160164
test_expect_success 'set up unborn branch and content' '
161165
git symbolic-ref HEAD refs/heads/unborn &&
162166
rm -f .git/index &&

0 commit comments

Comments
 (0)