Skip to content

Commit b4dc085

Browse files
Thomas Rastgitster
authored andcommitted
pull: merge into unborn by fast-forwarding from empty tree
The logic for pulling into an unborn branch was originally designed to be used on a newly-initialized repository (d09e79c, git-pull: allow pulling into an empty repository, 2006-11-16). It thus did not initially deal with uncommitted changes in the unborn branch. The case of an _unstaged_ untracked file was fixed by 4b3ffe5 (pull: do not clobber untracked files on initial pull, 2011-03-25). However, it still clobbered existing staged files, both when the file exists in the merged commit (it will be overwritten), and when it does not (it will be deleted). We fix this by doing a two-way merge, where the "current" side of the merge is an empty tree, and the "target" side is HEAD (already updated to FETCH_HEAD at this point). This amounts to claiming that all work in the index was done vs. an empty tree, and thus all content of the index is precious. Note that this use of read-tree just gives us protection against overwriting index and working tree changes. It will not actually result in a 3-way merge conflict in the index. This is fine, as this is a rare situation, and the conflict would not be interesting anyway (it must, by definition, be an add/add conflict with the whole content conflicting). And it makes it simpler for the user to recover, as they have no HEAD to "git reset" back to. Reported-by: Stefan Schüßler <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9f48f2b commit b4dc085

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

git-pull.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,16 @@ case "$merge_head" in
260260
;;
261261
esac
262262

263+
# Pulling into unborn branch: a shorthand for branching off
264+
# FETCH_HEAD, for lazy typers.
263265
if test -z "$orig_head"
264266
then
265-
git read-tree -m -u $merge_head &&
267+
# Two-way merge: we claim the index is based on an empty tree,
268+
# and try to fast-forward to HEAD. This ensures we will not
269+
# lose index/worktree changes that the user already made on
270+
# the unborn branch.
271+
empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
272+
git read-tree -m -u $empty_tree $merge_head &&
266273
git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
267274
exit
268275
fi

t/t5520-pull.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ test_expect_success 'pulling into void does not overwrite untracked files' '
5757
)
5858
'
5959

60+
test_expect_success 'pulling into void does not overwrite staged files' '
61+
git init cloned-staged-colliding &&
62+
(
63+
cd cloned-staged-colliding &&
64+
echo "alternate content" >file &&
65+
git add file &&
66+
test_must_fail git pull .. master &&
67+
echo "alternate content" >expect &&
68+
test_cmp expect file &&
69+
git cat-file blob :file >file.index &&
70+
test_cmp expect file.index
71+
)
72+
'
73+
74+
75+
test_expect_success 'pulling into void does not remove new staged files' '
76+
git init cloned-staged-new &&
77+
(
78+
cd cloned-staged-new &&
79+
echo "new tracked file" >newfile &&
80+
git add newfile &&
81+
git pull .. master &&
82+
echo "new tracked file" >expect &&
83+
test_cmp expect newfile &&
84+
git cat-file blob :newfile >newfile.index &&
85+
test_cmp expect newfile.index
86+
)
87+
'
88+
6089
test_expect_success 'test . as a remote' '
6190
6291
git branch copy master &&

0 commit comments

Comments
 (0)