fetch-pack: Do not fetch tags for shallow clones.
[alt-git.git] / git-fetch.sh
blobf0645d97c91e1151354b737bb3259313dd07fed8
1 #!/bin/sh
4 USAGE='<fetch-options> <repository> <refspec>...'
5 . git-sh-setup
6 . git-parse-remote
7 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
8 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
10 LF='
12 IFS="$LF"
14 rloga=fetch
15 no_tags=
16 tags=
17 append=
18 force=
19 verbose=
20 update_head_ok=
21 exec=
22 upload_pack=
23 keep=
24 shallow_depth=
25 while case "$#" in 0) break ;; esac
27 case "$1" in
28 -a|--a|--ap|--app|--appe|--appen|--append)
29 append=t
31 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
32 --upload-pa|--upload-pac|--upload-pack)
33 shift
34 exec="--exec=$1"
35 upload_pack="-u $1"
37 -f|--f|--fo|--for|--forc|--force)
38 force=t
40 -t|--t|--ta|--tag|--tags)
41 tags=t
43 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
44 no_tags=t
46 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
47 --update-he|--update-hea|--update-head|--update-head-|\
48 --update-head-o|--update-head-ok)
49 update_head_ok=t
51 -v|--verbose)
52 verbose=Yes
54 -k|--k|--ke|--kee|--keep)
55 keep='-k -k'
57 --reflog-action=*)
58 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
60 --depth=*)
61 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
63 --depth)
64 shift
65 shallow_depth="--depth=$1"
67 -*)
68 usage
71 break
73 esac
74 shift
75 done
77 case "$#" in
79 origin=$(get_default_remote)
80 test -n "$(get_remote_url ${origin})" ||
81 die "Where do you want to fetch from today?"
82 set x $origin ; shift ;;
83 esac
85 remote_nick="$1"
86 remote=$(get_remote_url "$@")
87 refs=
88 rref=
89 rsync_slurped_objects=
91 rloga="$rloga $remote_nick"
92 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
94 if test "" = "$append"
95 then
96 : >"$GIT_DIR/FETCH_HEAD"
99 append_fetch_head () {
100 head_="$1"
101 remote_="$2"
102 remote_name_="$3"
103 remote_nick_="$4"
104 local_name_="$5"
105 case "$6" in
106 t) not_for_merge_='not-for-merge' ;;
107 '') not_for_merge_= ;;
108 esac
110 # remote-nick is the URL given on the command line (or a shorthand)
111 # remote-name is the $GIT_DIR relative refs/ path we computed
112 # for this refspec.
114 # the $note_ variable will be fed to git-fmt-merge-msg for further
115 # processing.
116 case "$remote_name_" in
117 HEAD)
118 note_= ;;
119 refs/heads/*)
120 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
121 note_="branch '$note_' of " ;;
122 refs/tags/*)
123 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
124 note_="tag '$note_' of " ;;
125 refs/remotes/*)
126 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
127 note_="remote branch '$note_' of " ;;
129 note_="$remote_name of " ;;
130 esac
131 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
132 remote_="$remote_1_"
133 note_="$note_$remote_"
135 # 2.6.11-tree tag would not be happy to be fed to resolve.
136 if git-cat-file commit "$head_" >/dev/null 2>&1
137 then
138 headc_=$(git-rev-parse --verify "$head_^0") || exit
139 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
140 else
141 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
144 update_local_ref "$local_name_" "$head_" "$note_"
147 update_local_ref () {
148 # If we are storing the head locally make sure that it is
149 # a fast forward (aka "reverse push").
151 label_=$(git-cat-file -t $2)
152 newshort_=$(git-rev-parse --short $2)
153 if test -z "$1" ; then
154 [ "$verbose" ] && echo >&2 "* fetched $3"
155 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
156 return 0
158 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
160 case "$1" in
161 refs/tags/*)
162 # Tags need not be pointing at commits so there
163 # is no way to guarantee "fast-forward" anyway.
164 if test -n "$oldshort_"
165 then
166 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
167 then
168 [ "$verbose" ] && echo >&2 "* $1: same as $3"
169 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
170 else
171 echo >&2 "* $1: updating with $3"
172 echo >&2 " $label_: $newshort_"
173 git-update-ref -m "$rloga: updating tag" "$1" "$2"
175 else
176 echo >&2 "* $1: storing $3"
177 echo >&2 " $label_: $newshort_"
178 git-update-ref -m "$rloga: storing tag" "$1" "$2"
182 refs/heads/* | refs/remotes/*)
183 # $1 is the ref being updated.
184 # $2 is the new value for the ref.
185 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
186 if test "$local"
187 then
188 # Require fast-forward.
189 mb=$(git-merge-base "$local" "$2") &&
190 case "$2,$mb" in
191 $local,*)
192 if test -n "$verbose"
193 then
194 echo >&2 "* $1: same as $3"
195 echo >&2 " $label_: $newshort_"
198 *,$local)
199 echo >&2 "* $1: fast forward to $3"
200 echo >&2 " old..new: $oldshort_..$newshort_"
201 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
204 false
206 esac || {
207 case ",$force,$single_force," in
208 *,t,*)
209 echo >&2 "* $1: forcing update to non-fast forward $3"
210 echo >&2 " old...new: $oldshort_...$newshort_"
211 git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
214 echo >&2 "* $1: not updating to non-fast forward $3"
215 echo >&2 " old...new: $oldshort_...$newshort_"
216 exit 1
218 esac
220 else
221 echo >&2 "* $1: storing $3"
222 echo >&2 " $label_: $newshort_"
223 git-update-ref -m "$rloga: storing head" "$1" "$2"
226 esac
229 case "$update_head_ok" in
231 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
233 esac
235 # If --tags (and later --heads or --all) is specified, then we are
236 # not talking about defaults stored in Pull: line of remotes or
237 # branches file, and just fetch those and refspecs explicitly given.
238 # Otherwise we do what we always did.
240 reflist=$(get_remote_refs_for_fetch "$@")
241 if test "$tags"
242 then
243 taglist=`IFS=" " &&
245 git-ls-remote $upload_pack --tags "$remote" ||
246 echo fail ouch
248 while read sha1 name
250 case "$sha1" in
251 fail)
252 exit 1
253 esac
254 case "$name" in
255 *^*) continue ;;
256 esac
257 if git-check-ref-format "$name"
258 then
259 echo ".${name}:${name}"
260 else
261 echo >&2 "warning: tag ${name} ignored"
263 done` || exit
264 if test "$#" -gt 1
265 then
266 # remote URL plus explicit refspecs; we need to merge them.
267 reflist="$reflist$LF$taglist"
268 else
269 # No explicit refspecs; fetch tags only.
270 reflist=$taglist
274 fetch_main () {
275 reflist="$1"
276 refs=
277 rref=
279 for ref in $reflist
281 refs="$refs$LF$ref"
283 # These are relative path from $GIT_DIR, typically starting at refs/
284 # but may be HEAD
285 if expr "z$ref" : 'z\.' >/dev/null
286 then
287 not_for_merge=t
288 ref=$(expr "z$ref" : 'z\.\(.*\)')
289 else
290 not_for_merge=
292 if expr "z$ref" : 'z+' >/dev/null
293 then
294 single_force=t
295 ref=$(expr "z$ref" : 'z+\(.*\)')
296 else
297 single_force=
299 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
300 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
302 rref="$rref$LF$remote_name"
304 # There are transports that can fetch only one head at a time...
305 case "$remote" in
306 http://* | https://* | ftp://*)
307 test -n "$shallow_depth" &&
308 die "shallow clone with http not supported"
309 proto=`expr "$remote" : '\([^:]*\):'`
310 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
311 curl_extra_args="-k"
313 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
314 "`git-repo-config --bool http.noEPSV`" = true ]; then
315 noepsv_opt="--disable-epsv"
317 max_depth=5
318 depth=0
319 head="ref: $remote_name"
320 while (expr "z$head" : "zref:" && expr $depth \< $max_depth) >/dev/null
322 remote_name_quoted=$(@@PERL@@ -e '
323 my $u = $ARGV[0];
324 $u =~ s/^ref:\s*//;
325 $u =~ s{([^-a-zA-Z0-9/.])}{sprintf"%%%02x",ord($1)}eg;
326 print "$u";
327 ' "$head")
328 head=$(curl -nsfL $curl_extra_args $noepsv_opt "$remote/$remote_name_quoted")
329 depth=$( expr \( $depth + 1 \) )
330 done
331 expr "z$head" : "z$_x40\$" >/dev/null ||
332 die "Failed to fetch $remote_name from $remote"
333 echo >&2 "Fetching $remote_name from $remote using $proto"
334 git-http-fetch -v -a "$head" "$remote/" || exit
336 rsync://*)
337 test -n "$shallow_depth" &&
338 die "shallow clone with rsync not supported"
339 TMP_HEAD="$GIT_DIR/TMP_HEAD"
340 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
341 head=$(git-rev-parse --verify TMP_HEAD)
342 rm -f "$TMP_HEAD"
343 test "$rsync_slurped_objects" || {
344 rsync -av --ignore-existing --exclude info \
345 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
347 # Look at objects/info/alternates for rsync -- http will
348 # support it natively and git native ones will do it on
349 # the remote end. Not having that file is not a crime.
350 rsync -q "$remote/objects/info/alternates" \
351 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
352 rm -f "$GIT_DIR/TMP_ALT"
353 if test -f "$GIT_DIR/TMP_ALT"
354 then
355 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
356 while read alt
358 case "$alt" in 'bad alternate: '*) die "$alt";; esac
359 echo >&2 "Getting alternate: $alt"
360 rsync -av --ignore-existing --exclude info \
361 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
362 done
363 rm -f "$GIT_DIR/TMP_ALT"
365 rsync_slurped_objects=t
369 # We will do git native transport with just one call later.
370 continue ;;
371 esac
373 append_fetch_head "$head" "$remote" \
374 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
376 done
378 case "$remote" in
379 http://* | https://* | ftp://* | rsync://* )
380 ;; # we are already done.
382 ( : subshell because we muck with IFS
383 pack_lockfile=
384 IFS=" $LF"
386 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
388 while read sha1 remote_name
390 case "$sha1" in
391 failed)
392 echo >&2 "Fetch failure: $remote"
393 exit 1 ;;
394 # special line coming from index-pack with the pack name
395 pack)
396 continue ;;
397 keep)
398 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
399 continue ;;
400 esac
401 found=
402 single_force=
403 for ref in $refs
405 case "$ref" in
406 +$remote_name:*)
407 single_force=t
408 not_for_merge=
409 found="$ref"
410 break ;;
411 .+$remote_name:*)
412 single_force=t
413 not_for_merge=t
414 found="$ref"
415 break ;;
416 .$remote_name:*)
417 not_for_merge=t
418 found="$ref"
419 break ;;
420 $remote_name:*)
421 not_for_merge=
422 found="$ref"
423 break ;;
424 esac
425 done
426 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
427 append_fetch_head "$sha1" "$remote" \
428 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
429 done
430 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
431 ) || exit ;;
432 esac
436 fetch_main "$reflist"
438 # automated tag following
439 case "$no_tags$tags" in
441 case "$reflist" in
442 *:refs/*)
443 # effective only when we are following remote branch
444 # using local tracking branch.
445 taglist=$(IFS=" " &&
446 git-ls-remote $upload_pack --tags "$remote" |
447 sed -n -e 's|^\('"$_x40"'\) \(refs/tags/.*\)^{}$|\1 \2|p' \
448 -e 's|^\('"$_x40"'\) \(refs/tags/.*\)$|\1 \2|p' |
449 while read sha1 name
451 git-show-ref --verify --quiet -- "$name" && continue
452 git-check-ref-format "$name" || {
453 echo >&2 "warning: tag ${name} ignored"
454 continue
456 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
457 echo >&2 "Auto-following $name"
458 echo ".${name}:${name}"
459 done)
460 esac
461 case "$taglist" in
462 '') ;;
464 # do not deepen a shallow tree when following tags
465 shallow_depth=
466 fetch_main "$taglist" ;;
467 esac
468 esac
470 # If the original head was empty (i.e. no "master" yet), or
471 # if we were told not to worry, we do not have to check.
472 case "$orig_head" in
476 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
477 if test "$curr_head" != "$orig_head"
478 then
479 git-update-ref \
480 -m "$rloga: Undoing incorrectly fetched HEAD." \
481 HEAD "$orig_head"
482 die "Cannot fetch into the current branch."
485 esac