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