@matheusbsbs / matheus-bagliano / commits / 97b1b4f3a6
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 25, 2011 at 14:10 UTC 97b1b4f3a682377aa8f03348618e79bd1df1772e
2 files changed +5 -1
builtin/merge.c
+1 -1
@@ -1063,9 +1063,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1063 remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
1064 if (!remote_head)
1065 die("%s - not something we can merge", argv[0]);
1066 + read_empty(remote_head->sha1, 0);
1067 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
1068 DIE_ON_ERR);
1068 - read_empty(remote_head->sha1, 0);
1069 return 0;
1070 } else {
1071 struct strbuf merge_names = STRBUF_INIT;
t/t7607-merge-overwrite.sh
+4
@@ -157,6 +157,10 @@ test_expect_success 'will not overwrite untracked file on unborn branch' '
157 test_cmp important c0.c
158 '
159
160 +test_expect_success 'failed merge leaves unborn branch in the womb' '
161 + test_must_fail git rev-parse --verify HEAD
162 +'
163 +
164 test_expect_success 'set up unborn branch and content' '
165 git symbolic-ref HEAD refs/heads/unborn &&
166 rm -f .git/index &&