Skip to content

Commit d09e79c

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
git-pull: allow pulling into an empty repository
We used to complain that we cannot merge anything we fetched with a local branch that does not exist yet. Just treat the case as a natural extension of fast forwarding and make the local branch'es tip point at the same commit we just fetched. After all an empty repository without an initial commit is an ancestor of any commit. [jc: I added a trivial test. We've become sloppy but we should stick to the discipline of covering new behaviour with new tests. ] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 73fbd33 commit d09e79c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

git-pull.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ do
4444
shift
4545
done
4646

47-
orig_head=$(git-rev-parse --verify HEAD) || die "Pulling into a black hole?"
47+
orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
4848
git-fetch --update-head-ok --reflog-action=pull "$@" || exit 1
4949

50-
curr_head=$(git-rev-parse --verify HEAD)
50+
curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
5151
if test "$curr_head" != "$orig_head"
5252
then
5353
# The fetch involved updating the current branch.
@@ -80,6 +80,11 @@ case "$merge_head" in
8080
exit 0
8181
;;
8282
?*' '?*)
83+
if test -z "$orig_head"
84+
then
85+
echo >&2 "Cannot merge multiple branches into empty head"
86+
exit 1
87+
fi
8388
var=`git-repo-config --get pull.octopus`
8489
if test -n "$var"
8590
then
@@ -95,6 +100,13 @@ case "$merge_head" in
95100
;;
96101
esac
97102

103+
if test -z "$orig_head"
104+
then
105+
git-update-ref -m "initial pull" HEAD $merge_head "" &&
106+
git-read-tree --reset -u HEAD || exit 1
107+
exit
108+
fi
109+
98110
case "$strategy_args" in
99111
'')
100112
strategy_args=$strategy_default_args

t/t5520-pull.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
test_description='pulling into void'
4+
5+
. ./test-lib.sh
6+
7+
D=`pwd`
8+
9+
test_expect_success setup '
10+
11+
echo file >file &&
12+
git add file &&
13+
git commit -a -m original
14+
15+
'
16+
17+
test_expect_success 'pulling into void' '
18+
mkdir cloned &&
19+
cd cloned &&
20+
git init-db &&
21+
git pull ..
22+
'
23+
24+
cd "$D"
25+
26+
test_expect_success 'checking the results' '
27+
test -f file &&
28+
test -f cloned/file &&
29+
diff file cloned/file
30+
'
31+
32+
test_done
33+

0 commit comments

Comments
 (0)