]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge: merge unborn index before setting ref
authorJeff King <peff@peff.net>
Fri, 25 Mar 2011 18:10:38 +0000 (14:10 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 25 Mar 2011 21:16:01 +0000 (14:16 -0700)
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>
builtin/merge.c
t/t7607-merge-overwrite.sh

index aa3453c5e1c99c02d802bc77632ecedec4451c43..c8d028cbccdc001efed2991e477b898606396114 100644 (file)
@@ -1063,9 +1063,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
                if (!remote_head)
                        die("%s - not something we can merge", argv[0]);
+               read_empty(remote_head->sha1, 0);
                update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
                                DIE_ON_ERR);
-               read_empty(remote_head->sha1, 0);
                return 0;
        } else {
                struct strbuf merge_names = STRBUF_INIT;
index c86e298d082316ff5a4de6f07c0faba85c65d4e1..b54e840ee2ab09a67afc663a2f475bfc9fcb9d8b 100755 (executable)
@@ -157,6 +157,10 @@ test_expect_success 'will not overwrite untracked file on unborn branch' '
        test_cmp important c0.c
 '
 
+test_expect_success 'failed merge leaves unborn branch in the womb' '
+       test_must_fail git rev-parse --verify HEAD
+'
+
 test_expect_success 'set up unborn branch and content' '
        git symbolic-ref HEAD refs/heads/unborn &&
        rm -f .git/index &&