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>
Linus Torvalds committed
Nov 16, 2006 at 11:47 UTC
d09e79cb1c474b3bb323356e6d1072922ab7ccb2
2 files changed
+47
-2
git-pull.sh
+14
-2
@@ -44,10 +44,10 @@ do
44
shift
45
done
46
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)
48
git-fetch --update-head-ok --reflog-action=pull "$@" || exit 1
49
50
-curr_head=$(git-rev-parse --verify HEAD)
50
+curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
51
if test "$curr_head" != "$orig_head"
52
then
53
# The fetch involved updating the current branch.
@@ -80,6 +80,11 @@ case "$merge_head" in
80
exit 0
81
;;
82
?*' '?*)
83
+ if test -z "$orig_head"
84
+ then
85
+ echo >&2 "Cannot merge multiple branches into empty head"
86
+ exit 1
87
+ fi
88
var=`git-repo-config --get pull.octopus`
89
if test -n "$var"
90
then
@@ -95,6 +100,13 @@ case "$merge_head" in
100
;;
101
esac
102
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
+
110
case "$strategy_args" in
111
'')
112
strategy_args=$strategy_default_args
t/t5520-pull.sh
new
+33
@@ -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
+