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=
27no_progress=
28test -t 1 || no_progress=--no-progress
29quiet=
30while case "$#" in 0) break ;; esac
31do
32 case "$1" in
33 -a|--a|--ap|--app|--appe|--appen|--append)
34 append=t
35 ;;
36 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
37 --upload-pa|--upload-pac|--upload-pack)
38 shift
39 exec="--upload-pack=$1"
40 ;;
41 --upl=*|--uplo=*|--uploa=*|--upload=*|\
42 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
43 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
44 shift
45 ;;
46 -f|--f|--fo|--for|--forc|--force)
47 force=t
48 ;;
49 -t|--t|--ta|--tag|--tags)
50 tags=t
51 ;;
52 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
53 no_tags=t
54 ;;
55 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
56 --update-he|--update-hea|--update-head|--update-head-|\
57 --update-head-o|--update-head-ok)
58 update_head_ok=t
59 ;;
60 -q|--q|--qu|--qui|--quie|--quiet)
61 quiet=--quiet
62 ;;
63 -v|--verbose)
64 verbose=Yes
65 ;;
66 -k|--k|--ke|--kee|--keep)
67 keep='-k -k'
68 ;;
69 --depth=*)
70 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
71 ;;
72 --depth)
73 shift
74 shallow_depth="--depth=$1"
75 ;;
76 -*)
77 usage
78 ;;
79 *)
80 break
81 ;;
82 esac
83 shift
84done
85
86case "$#" in
870)
88 origin=$(get_default_remote)
89 test -n "$(get_remote_url ${origin})" ||
90 die "Where do you want to fetch from today?"
91 set x $origin ; shift ;;
92esac
93
94if test -z "$exec"
95then
96 # No command line override and we have configuration for the remote.
97 exec="--upload-pack=$(get_uploadpack $1)"
98fi
99
100remote_nick="$1"
101remote=$(get_remote_url "$@")
102refs=
103rref=
104rsync_slurped_objects=
105
106if test "" = "$append"
107then
108 : >"$GIT_DIR/FETCH_HEAD"
109fi
110
111# Global that is reused later
112ls_remote_result=$(git ls-remote $exec "$remote") ||
113 die "Cannot get the repository state from $remote"
114
115append_fetch_head () {
116 head_="$1"
117 remote_="$2"
118 remote_name_="$3"
119 remote_nick_="$4"
120 local_name_="$5"
121 case "$6" in
122 t) not_for_merge_='not-for-merge' ;;
123 '') not_for_merge_= ;;
124 esac
125
126 # remote-nick is the URL given on the command line (or a shorthand)
127 # remote-name is the $GIT_DIR relative refs/ path we computed
128 # for this refspec.
129
130 # the $note_ variable will be fed to git-fmt-merge-msg for further
131 # processing.
132 case "$remote_name_" in
133 HEAD)
134 note_= ;;
135 refs/heads/*)
136 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
137 note_="branch '$note_' of " ;;
138 refs/tags/*)
139 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
140 note_="tag '$note_' of " ;;
141 refs/remotes/*)
142 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
143 note_="remote branch '$note_' of " ;;
144 *)
145 note_="$remote_name of " ;;
146 esac
147 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
148 remote_="$remote_1_"
149 note_="$note_$remote_"
150
151 # 2.6.11-tree tag would not be happy to be fed to resolve.
152 if git-cat-file commit "$head_" >/dev/null 2>&1
153 then
154 headc_=$(git-rev-parse --verify "$head_^0") || exit
155 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
156 else
157 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
158 fi
159
160 update_local_ref "$local_name_" "$head_" "$note_"
161}
162
163update_local_ref () {
164 # If we are storing the head locally make sure that it is
165 # a fast forward (aka "reverse push").
166
167 label_=$(git-cat-file -t $2)
168 newshort_=$(git-rev-parse --short $2)
169 if test -z "$1" ; then
170 [ "$verbose" ] && echo >&2 "* fetched $3"
171 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
172 return 0
173 fi
174 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
175
176 case "$1" in
177 refs/tags/*)
178 # Tags need not be pointing at commits so there
179 # is no way to guarantee "fast-forward" anyway.
180 if test -n "$oldshort_"
181 then
182 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
183 then
184 [ "$verbose" ] && echo >&2 "* $1: same as $3"
185 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
186 else
187 echo >&2 "* $1: updating with $3"
188 echo >&2 " $label_: $newshort_"
189 git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
190 fi
191 else
192 echo >&2 "* $1: storing $3"
193 echo >&2 " $label_: $newshort_"
194 git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
195 fi
196 ;;
197
198 refs/heads/* | refs/remotes/*)
199 # $1 is the ref being updated.
200 # $2 is the new value for the ref.
201 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
202 if test "$local"
203 then
204 # Require fast-forward.
205 mb=$(git-merge-base "$local" "$2") &&
206 case "$2,$mb" in
207 $local,*)
208 if test -n "$verbose"
209 then
210 echo >&2 "* $1: same as $3"
211 echo >&2 " $label_: $newshort_"
212 fi
213 ;;
214 *,$local)
215 echo >&2 "* $1: fast forward to $3"
216 echo >&2 " old..new: $oldshort_..$newshort_"
217 git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
218 ;;
219 *)
220 false
221 ;;
222 esac || {
223 case ",$force,$single_force," in
224 *,t,*)
225 echo >&2 "* $1: forcing update to non-fast forward $3"
226 echo >&2 " old...new: $oldshort_...$newshort_"
227 git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
228 ;;
229 *)
230 echo >&2 "* $1: not updating to non-fast forward $3"
231 echo >&2 " old...new: $oldshort_...$newshort_"
232 exit 1
233 ;;
234 esac
235 }
236 else
237 echo >&2 "* $1: storing $3"
238 echo >&2 " $label_: $newshort_"
239 git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
240 fi
241 ;;
242 esac
243}
244
245# updating the current HEAD with git-fetch in a bare
246# repository is always fine.
247if test -z "$update_head_ok" && test $(is_bare_repository) = false
248then
249 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
250fi
251
252# Allow --notags from remote.$1.tagopt
253case "$tags$no_tags" in
254'')
255 case "$(git-config --get "remote.$1.tagopt")" in
256 --no-tags)
257 no_tags=t ;;
258 esac
259esac
260
261# If --tags (and later --heads or --all) is specified, then we are
262# not talking about defaults stored in Pull: line of remotes or
263# branches file, and just fetch those and refspecs explicitly given.
264# Otherwise we do what we always did.
265
266reflist=$(get_remote_refs_for_fetch "$@")
267if test "$tags"
268then
269 taglist=`IFS=' ' &&
270 echo "$ls_remote_result" |
271 git-show-ref --exclude-existing=refs/tags/ |
272 while read sha1 name
273 do
274 echo ".${name}:${name}"
275 done` || exit
276 if test "$#" -gt 1
277 then
278 # remote URL plus explicit refspecs; we need to merge them.
279 reflist="$reflist$LF$taglist"
280 else
281 # No explicit refspecs; fetch tags only.
282 reflist=$taglist
283 fi
284fi
285
286fetch_main () {
287 reflist="$1"
288 refs=
289 rref=
290
291 for ref in $reflist
292 do
293 refs="$refs$LF$ref"
294
295 # These are relative path from $GIT_DIR, typically starting at refs/
296 # but may be HEAD
297 if expr "z$ref" : 'z\.' >/dev/null
298 then
299 not_for_merge=t
300 ref=$(expr "z$ref" : 'z\.\(.*\)')
301 else
302 not_for_merge=
303 fi
304 if expr "z$ref" : 'z+' >/dev/null
305 then
306 single_force=t
307 ref=$(expr "z$ref" : 'z+\(.*\)')
308 else
309 single_force=
310 fi
311 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
312 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
313
314 rref="$rref$LF$remote_name"
315
316 # There are transports that can fetch only one head at a time...
317 case "$remote" in
318 http://* | https://* | ftp://*)
319 test -n "$shallow_depth" &&
320 die "shallow clone with http not supported"
321 proto=`expr "$remote" : '\([^:]*\):'`
322 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
323 curl_extra_args="-k"
324 fi
325 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
326 "`git-config --bool http.noEPSV`" = true ]; then
327 noepsv_opt="--disable-epsv"
328 fi
329
330 # Find $remote_name from ls-remote output.
331 head=$(
332 IFS=' '
333 echo "$ls_remote_result" |
334 while read sha1 name
335 do
336 test "z$name" = "z$remote_name" || continue
337 echo "$sha1"
338 break
339 done
340 )
341 expr "z$head" : "z$_x40\$" >/dev/null ||
342 die "No such ref $remote_name at $remote"
343 echo >&2 "Fetching $remote_name from $remote using $proto"
344 case "$quiet" in '') v=-v ;; *) v= ;; esac
345 git-http-fetch $v -a "$head" "$remote/" || exit
346 ;;
347 rsync://*)
348 test -n "$shallow_depth" &&
349 die "shallow clone with rsync not supported"
350 TMP_HEAD="$GIT_DIR/TMP_HEAD"
351 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
352 head=$(git-rev-parse --verify TMP_HEAD)
353 rm -f "$TMP_HEAD"
354 case "$quiet" in '') v=-v ;; *) v= ;; esac
355 test "$rsync_slurped_objects" || {
356 rsync -a $v --ignore-existing --exclude info \
357 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
358
359 # Look at objects/info/alternates for rsync -- http will
360 # support it natively and git native ones will do it on
361 # the remote end. Not having that file is not a crime.
362 rsync -q "$remote/objects/info/alternates" \
363 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
364 rm -f "$GIT_DIR/TMP_ALT"
365 if test -f "$GIT_DIR/TMP_ALT"
366 then
367 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
368 while read alt
369 do
370 case "$alt" in 'bad alternate: '*) die "$alt";; esac
371 echo >&2 "Getting alternate: $alt"
372 rsync -av --ignore-existing --exclude info \
373 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
374 done
375 rm -f "$GIT_DIR/TMP_ALT"
376 fi
377 rsync_slurped_objects=t
378 }
379 ;;
380 *)
381 # We will do git native transport with just one call later.
382 continue ;;
383 esac
384
385 append_fetch_head "$head" "$remote" \
386 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
387
388 done
389
390 case "$remote" in
391 http://* | https://* | ftp://* | rsync://* )
392 ;; # we are already done.
393 *)
394 ( : subshell because we muck with IFS
395 IFS=" $LF"
396 (
397 if test -f "$remote" ; then
398 test -n "$shallow_depth" &&
399 die "shallow clone with bundle is not supported"
400 git-bundle unbundle "$remote" $rref ||
401 echo failed "$remote"
402 else
403 git-fetch-pack --thin $exec $keep $shallow_depth \
404 $quiet $no_progress "$remote" $rref ||
405 echo failed "$remote"
406 fi
407 ) |
408 (
409 trap '
410 if test -n "$keepfile" && test -f "$keepfile"
411 then
412 rm -f "$keepfile"
413 fi
414 ' 0
415
416 keepfile=
417 while read sha1 remote_name
418 do
419 case "$sha1" in
420 failed)
421 echo >&2 "Fetch failure: $remote"
422 exit 1 ;;
423 # special line coming from index-pack with the pack name
424 pack)
425 continue ;;
426 keep)
427 keepfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
428 continue ;;
429 esac
430 found=
431 single_force=
432 for ref in $refs
433 do
434 case "$ref" in
435 +$remote_name:*)
436 single_force=t
437 not_for_merge=
438 found="$ref"
439 break ;;
440 .+$remote_name:*)
441 single_force=t
442 not_for_merge=t
443 found="$ref"
444 break ;;
445 .$remote_name:*)
446 not_for_merge=t
447 found="$ref"
448 break ;;
449 $remote_name:*)
450 not_for_merge=
451 found="$ref"
452 break ;;
453 esac
454 done
455 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
456 append_fetch_head "$sha1" "$remote" \
457 "$remote_name" "$remote_nick" "$local_name" \
458 "$not_for_merge" || exit
459 done
460 )
461 ) || exit ;;
462 esac
463
464}
465
466fetch_main "$reflist" || exit
467
468# automated tag following
469case "$no_tags$tags" in
470'')
471 case "$reflist" in
472 *:refs/*)
473 # effective only when we are following remote branch
474 # using local tracking branch.
475 taglist=$(IFS=' ' &&
476 echo "$ls_remote_result" |
477 git-show-ref --exclude-existing=refs/tags/ |
478 while read sha1 name
479 do
480 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
481 echo >&2 "Auto-following $name"
482 echo ".${name}:${name}"
483 done)
484 esac
485 case "$taglist" in
486 '') ;;
487 ?*)
488 # do not deepen a shallow tree when following tags
489 shallow_depth=
490 fetch_main "$taglist" || exit ;;
491 esac
492esac
493
494# If the original head was empty (i.e. no "master" yet), or
495# if we were told not to worry, we do not have to check.
496case "$orig_head" in
497'')
498 ;;
499?*)
500 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
501 if test "$curr_head" != "$orig_head"
502 then
503 git-update-ref \
504 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
505 HEAD "$orig_head"
506 die "Cannot fetch into the current branch."
507 fi
508 ;;
509esac