Support +<src>:<dst> format in push as well.
[git/jnareb-git.git] / git-fetch-script
blobd55cc85620e5952d5172ce1e9128a0168f19b921
1 #!/bin/sh
3 . git-sh-setup-script || die "Not a git archive"
4 . git-parse-remote-script
5 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
6 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
8 append=
9 force=
10 while case "$#" in 0) break ;; esac
12 case "$1" in
13 -a|--a|--ap|--app|--appe|--appen|--append)
14 append=t
15 shift
17 -f|--f|--fo|--for|--forc|--force)
18 force=t
19 shift
22 break
24 esac
25 done
27 case "$#" in
29 test -f "$GIT_DIR/branches/origin" ||
30 test -f "$GIT_DIR/remotes/origin" ||
31 die "Where do you want to fetch from?"
32 set origin ;;
33 esac
35 remote_nick="$1"
36 remote=$(get_remote_url "$@")
37 refs=
38 rref=
39 rsync_slurped_objects=
41 if test "" = "$append"
42 then
43 : >$GIT_DIR/FETCH_HEAD
46 append_fetch_head () {
47 head_="$1"
48 remote_="$2"
49 remote_name_="$3"
50 remote_nick_="$4"
51 local_name_="$5"
53 # 2.6.11-tree tag would not be happy to be fed to resolve.
54 if git-cat-file commit "$head_" >/dev/null 2>&1
55 then
56 head_=$(git-rev-parse --verify "$head_^0") || exit
57 note_="$head_ $remote_name_ from $remote_nick_"
58 echo "$note_" >>$GIT_DIR/FETCH_HEAD
59 echo >&2 "* committish: $note_"
60 else
61 echo >&2 "* non-commit: $note_"
63 if test "$local_name_" != ""
64 then
65 # We are storing the head locally. Make sure that it is
66 # a fast forward (aka "reverse push").
67 fast_forward_local "$local_name_" "$head_" "$remote_" "$remote_name_"
71 fast_forward_local () {
72 case "$1" in
73 refs/tags/*)
74 # Tags need not be pointing at commits so there
75 # is no way to guarantee "fast-forward" anyway.
76 if test -f "$GIT_DIR/$1"
77 then
78 echo >&2 "* $1: updating with $4"
79 echo >&2 " from $3."
80 else
81 echo >&2 "* $1: storing $4"
82 echo >&2 " from $3."
84 echo "$2" >"$GIT_DIR/$1" ;;
86 refs/heads/*)
87 # NEEDSWORK: use the same cmpxchg protocol here.
88 echo "$2" >"$GIT_DIR/$1.lock"
89 if test -f "$GIT_DIR/$1"
90 then
91 local=$(git-rev-parse --verify "$1^0") &&
92 mb=$(git-merge-base "$local" "$2") &&
93 case "$2,$mb" in
94 $local,*)
95 echo >&2 "* $1: same as $4"
96 echo >&2 " from $3"
98 *,$local)
99 echo >&2 "* $1: fast forward to $4"
100 echo >&2 " from $3"
103 false
105 esac || {
106 echo >&2 "* $1: does not fast forward to $4"
107 case "$force,$single_force" in
108 t,* | *,t)
109 echo >&2 " from $3; forcing update."
112 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
113 echo >&2 " from $3; leaving it in '$1.remote'"
115 esac
117 else
118 echo >&2 "* $1: storing $4"
119 echo >&2 " from $3."
121 test -f "$GIT_DIR/$1.lock" &&
122 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
124 esac
127 for ref in $(get_remote_refs_for_fetch "$@")
129 refs="$refs $ref"
131 # These are relative path from $GIT_DIR, typically starting at refs/
132 # but may be HEAD
133 if expr "$ref" : '\+' >/dev/null
134 then
135 single_force=t
136 ref=$(expr "$ref" : '\+\(.*\)')
137 else
138 single_force=
140 remote_name=$(expr "$ref" : '\([^:]*\):')
141 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
143 rref="$rref $remote_name"
145 # There are transports that can fetch only one head at a time...
146 case "$remote" in
147 http://* | https://*)
148 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
149 curl_extra_args="-k"
151 head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
152 expr "$head" : "$_x40\$" >/dev/null ||
153 die "Failed to fetch $remote_name from $remote"
154 echo Fetching "$remote_name from $remote" using http
155 git-http-pull -v -a "$head" "$remote/" || exit
157 rsync://*)
158 TMP_HEAD="$GIT_DIR/TMP_HEAD"
159 rsync -L "$remote/$remote_name" "$TMP_HEAD" || exit 1
160 head=$(git-rev-parse TMP_HEAD)
161 rm -f "$TMP_HEAD"
162 test "$rsync_slurped_objects" || {
163 rsync -avz --ignore-existing "$remote/objects/" \
164 "$GIT_OBJECT_DIRECTORY/" || exit
165 rsync_slurped_objects=t
169 # We will do git native transport with just one call later.
170 continue ;;
171 esac
173 append_fetch_head "$head" "$remote" "$remote_name" "$remote_nick" "$local_name"
175 done
177 case "$remote" in
178 http://* | https://* | rsync://* )
179 ;; # we are already done.
181 git-fetch-pack "$remote" $rref |
182 while read sha1 remote_name
184 found=
185 single_force=
186 for ref in $refs
188 case "$ref" in
189 +$remote_name:*)
190 single_force=t
191 found="$ref"
192 break ;;
193 $remote_name:*)
194 found="$ref"
195 break ;;
196 esac
197 done
199 local_name=$(expr "$found" : '[^:]*:\(.*\)')
200 append_fetch_head "$sha1" "$remote" "$remote_name" "$remote_nick" "$local_name"
201 done
203 esac