From 97b1b4f3a682377aa8f03348618e79bd1df1772e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 25 Mar 2011 14:10:38 -0400 Subject: 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 Signed-off-by: Junio C Hamano diff --git a/builtin/merge.c b/builtin/merge.c index aa3453c..c8d028c 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -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; diff --git a/t/t7607-merge-overwrite.sh b/t/t7607-merge-overwrite.sh index c86e298..b54e840 100755 --- a/t/t7607-merge-overwrite.sh +++ b/t/t7607-merge-overwrite.sh @@ -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 && -- cgit v0.10.2-6-g49f6 From 4b3ffe5184bd550dddacdd0b32b18ad0e73c7908 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 25 Mar 2011 14:13:31 -0400 Subject: pull: do not clobber untracked files on initial pull For a pull into an unborn branch, we do not use "git merge" at all. Instead, we call read-tree directly. However, we used the --reset parameter instead of "-m", which turns off the safety features. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/git-pull.sh b/git-pull.sh index 63b063a..e31226b 100755 --- a/git-pull.sh +++ b/git-pull.sh @@ -274,7 +274,7 @@ esac if test -z "$orig_head" then git update-ref -m "initial pull" HEAD $merge_head "$curr_head" && - git read-tree --reset -u HEAD || exit 1 + git read-tree -m -u HEAD || exit 1 exit fi diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh index 0470a81..0e5eb67 100755 --- a/t/t5520-pull.sh +++ b/t/t5520-pull.sh @@ -46,6 +46,17 @@ test_expect_success 'pulling into void using master:master' ' test_cmp file cloned-uho/file ' +test_expect_success 'pulling into void does not overwrite untracked files' ' + git init cloned-untracked && + ( + cd cloned-untracked && + echo untracked >file && + test_must_fail git pull .. master && + echo untracked >expect && + test_cmp expect file + ) +' + test_expect_success 'test . as a remote' ' git branch copy master && -- cgit v0.10.2-6-g49f6