1#!/bin/sh
2#
3
4USAGE='<fetch-options> <repository> <refspec>...'
5SUBDIRECTORY_OK=Yes
6. git-sh-setup
7set_reflog_action "fetch $*"
8cd_to_toplevel ;# probably unnecessary...
9
10. git-parse-remote
11_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
12_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
13
14LF='
15'
16IFS="$LF"
17
18no_tags=
19tags=
20append=
21force=
22verbose=
23update_head_ok=
24exec=
25keep=
26shallow_depth=
27while case "$#" in 0) break ;; esac
28do
29 case "$1" in
30 -a|--a|--ap|--app|--appe|--appen|--append)
31 append=t
32 ;;
33 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
34 --upload-pa|--upload-pac|--upload-pack)
35 shift
36 exec="--upload-pack=$1"
37 ;;
38 --upl=*|--uplo=*|--uploa=*|--upload=*|\
39 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
40 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
41 shift
42 ;;
43 -f|--f|--fo|--for|--forc|--force)
44 force=t
45 ;;
46 -t|--t|--ta|--tag|--tags)
47 tags=t
48 ;;
49 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
50 no_tags=t
51 ;;
52 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
53 --update-he|--update-hea|--update-head|--update-head-|\
54 --update-head-o|--update-head-ok)
55 update_head_ok=t
56 ;;
57 -v|--verbose)
58 verbose=Yes
59 ;;
60 -k|--k|--ke|--kee|--keep)
61 keep='-k -k'
62 ;;
63 --depth=*)
64 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
65 ;;
66 --depth)
67 shift
68 shallow_depth="--depth=$1"
69 ;;
70 -*)
71 usage
72 ;;
73 *)
74 break
75 ;;
76 esac
77 shift
78done
79
80case "$#" in
810)
82 origin=$(get_default_remote)
83 test -n "$(get_remote_url ${origin})" ||
84 die "Where do you want to fetch from today?"
85 set x $origin ; shift ;;
86esac
87
88if test -z "$exec"
89then
90 # No command line override and we have configuration for the remote.
91 exec="--upload-pack=$(get_uploadpack $1)"
92fi
93
94remote_nick="$1"
95remote=$(get_remote_url "$@")
96refs=
97rref=
98rsync_slurped_objects=
99
100if test "" = "$append"
101then
102 : >"$GIT_DIR/FETCH_HEAD"
103fi
104
105# Global that is reused later
106ls_remote_result=$(git ls-remote $exec "$remote") ||
107 die "Cannot get the repository state from $remote"
108
109append_fetch_head () {
110 flags=
111 test -n "$verbose" && flags="$flags -v"
112 test -n "$force" && flags="$flags -f"
113 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
114 git-fetch--tool append-fetch-head $flags "$@"
115}
116
117update_local_ref () {
118 flags=
119 test -n "$verbose" && flags="$flags -v"
120 test -n "$force" && flags="$flags -f"
121 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
122 git-fetch--tool update-local-ref $flags "$@"
123}
124
125# updating the current HEAD with git-fetch in a bare
126# repository is always fine.
127if test -z "$update_head_ok" && test $(is_bare_repository) = false
128then
129 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
130fi
131
132# If --tags (and later --heads or --all) is specified, then we are
133# not talking about defaults stored in Pull: line of remotes or
134# branches file, and just fetch those and refspecs explicitly given.
135# Otherwise we do what we always did.
136
137reflist=$(get_remote_refs_for_fetch "$@")
138if test "$tags"
139then
140 taglist=`IFS=' ' &&
141 echo "$ls_remote_result" |
142 git-show-ref --exclude-existing=refs/tags/ |
143 while read sha1 name
144 do
145 echo ".${name}:${name}"
146 done` || exit
147 if test "$#" -gt 1
148 then
149 # remote URL plus explicit refspecs; we need to merge them.
150 reflist="$reflist$LF$taglist"
151 else
152 # No explicit refspecs; fetch tags only.
153 reflist=$taglist
154 fi
155fi
156
157fetch_native () {
158
159 eval=$(git-fetch--tool parse-reflist "$1")
160 eval "$eval"
161
162 ( : subshell because we muck with IFS
163 IFS=" $LF"
164 (
165 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
166 echo failed "$remote"
167 ) |
168 (
169 flags=
170 test -n "$verbose" && flags="$flags -v"
171 test -n "$force" && flags="$flags -f"
172 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
173 git-fetch--tool native-store \
174 $flags "$remote" "$remote_nick" "$refs"
175 )
176 ) || exit
177
178}
179
180fetch_dumb () {
181 reflist="$1"
182 refs=
183 rref=
184
185 for ref in $reflist
186 do
187 refs="$refs$LF$ref"
188
189 # These are relative path from $GIT_DIR, typically starting at refs/
190 # but may be HEAD
191 if expr "z$ref" : 'z\.' >/dev/null
192 then
193 not_for_merge=t
194 ref=$(expr "z$ref" : 'z\.\(.*\)')
195 else
196 not_for_merge=
197 fi
198 if expr "z$ref" : 'z+' >/dev/null
199 then
200 single_force=t
201 ref=$(expr "z$ref" : 'z+\(.*\)')
202 else
203 single_force=
204 fi
205 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
206 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
207
208 rref="$rref$LF$remote_name"
209
210 # There are transports that can fetch only one head at a time...
211 case "$remote" in
212 http://* | https://* | ftp://*)
213 test -n "$shallow_depth" &&
214 die "shallow clone with http not supported"
215 proto=`expr "$remote" : '\([^:]*\):'`
216 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
217 curl_extra_args="-k"
218 fi
219 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
220 "`git-config --bool http.noEPSV`" = true ]; then
221 noepsv_opt="--disable-epsv"
222 fi
223
224 # Find $remote_name from ls-remote output.
225 head=$(
226 IFS=' '
227 echo "$ls_remote_result" |
228 while read sha1 name
229 do
230 test "z$name" = "z$remote_name" || continue
231 echo "$sha1"
232 break
233 done
234 )
235 expr "z$head" : "z$_x40\$" >/dev/null ||
236 die "No such ref $remote_name at $remote"
237 echo >&2 "Fetching $remote_name from $remote using $proto"
238 git-http-fetch -v -a "$head" "$remote/" || exit
239 ;;
240 rsync://*)
241 test -n "$shallow_depth" &&
242 die "shallow clone with rsync not supported"
243 TMP_HEAD="$GIT_DIR/TMP_HEAD"
244 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
245 head=$(git-rev-parse --verify TMP_HEAD)
246 rm -f "$TMP_HEAD"
247 test "$rsync_slurped_objects" || {
248 rsync -av --ignore-existing --exclude info \
249 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
250
251 # Look at objects/info/alternates for rsync -- http will
252 # support it natively and git native ones will do it on
253 # the remote end. Not having that file is not a crime.
254 rsync -q "$remote/objects/info/alternates" \
255 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
256 rm -f "$GIT_DIR/TMP_ALT"
257 if test -f "$GIT_DIR/TMP_ALT"
258 then
259 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
260 while read alt
261 do
262 case "$alt" in 'bad alternate: '*) die "$alt";; esac
263 echo >&2 "Getting alternate: $alt"
264 rsync -av --ignore-existing --exclude info \
265 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
266 done
267 rm -f "$GIT_DIR/TMP_ALT"
268 fi
269 rsync_slurped_objects=t
270 }
271 ;;
272 esac
273
274 append_fetch_head "$head" "$remote" \
275 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
276
277 done
278
279}
280
281fetch_main () {
282 case "$remote" in
283 http://* | https://* | ftp://* | rsync://* )
284 fetch_dumb "$@"
285 ;;
286 *)
287 fetch_native "$@"
288 ;;
289 esac
290}
291
292fetch_main "$reflist" || exit
293
294# automated tag following
295case "$no_tags$tags" in
296'')
297 case "$reflist" in
298 *:refs/*)
299 # effective only when we are following remote branch
300 # using local tracking branch.
301 taglist=$(IFS=' ' &&
302 echo "$ls_remote_result" |
303 git-show-ref --exclude-existing=refs/tags/ |
304 while read sha1 name
305 do
306 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
307 echo >&2 "Auto-following $name"
308 echo ".${name}:${name}"
309 done)
310 esac
311 case "$taglist" in
312 '') ;;
313 ?*)
314 # do not deepen a shallow tree when following tags
315 shallow_depth=
316 fetch_main "$taglist" || exit ;;
317 esac
318esac
319
320# If the original head was empty (i.e. no "master" yet), or
321# if we were told not to worry, we do not have to check.
322case "$orig_head" in
323'')
324 ;;
325?*)
326 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
327 if test "$curr_head" != "$orig_head"
328 then
329 git-update-ref \
330 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
331 HEAD "$orig_head"
332 die "Cannot fetch into the current branch."
333 fi
334 ;;
335esac