4 USAGE='<fetch-options> <repository> <refspec>...'
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"
25 while case "$#" in 0) break ;; esac
28 -a|--a|--ap|--app|--appe|--appen|--append)
31 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
32 --upload-pa|--upload-pac|--upload-pack)
37 -f|--f|--fo|--for|--forc|--force)
40 -t|--t|--ta|--tag|--tags)
43 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
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)
54 -k|--k|--ke|--kee|--keep)
58 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
61 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
65 shallow_depth="--depth=$1"
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 ;;
86 remote=$(get_remote_url "$@")
89 rsync_slurped_objects=
91 rloga="$rloga $remote_nick"
92 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
94 if test "" = "$append"
96 : >"$GIT_DIR/FETCH_HEAD"
99 append_fetch_head () {
106 t) not_for_merge_='not-for-merge' ;;
107 '') not_for_merge_= ;;
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
114 # the $note_ variable will be fed to git-fmt-merge-msg for further
116 case "$remote_name_" in
120 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
121 note_="branch '$note_' of " ;;
123 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
124 note_="tag '$note_' of " ;;
126 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
127 note_="remote branch '$note_' of " ;;
129 note_="$remote_name of " ;;
131 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
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
138 headc_=$(git-rev-parse --verify "$head_^0") || exit
139 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
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_"
158 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
162 # Tags need not be pointing at commits so there
163 # is no way to guarantee "fast-forward" anyway.
164 if test -n "$oldshort_"
166 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
168 [ "$verbose" ] && echo >&2 "* $1: same as $3"
169 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
171 echo >&2 "* $1: updating with $3"
172 echo >&2 " $label_: $newshort_"
173 git-update-ref -m "$rloga: updating tag" "$1" "$2"
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)
188 # Require fast-forward.
189 mb=$(git-merge-base "$local" "$2") &&
192 if test -n "$verbose"
194 echo >&2 "* $1: same as $3"
195 echo >&2 " $label_: $newshort_"
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"
207 case ",$force,$single_force," in
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_"
221 echo >&2 "* $1: storing $3"
222 echo >&2 " $label_: $newshort_"
223 git-update-ref -m "$rloga: storing head" "$1" "$2"
229 case "$update_head_ok" in
231 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
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 "$@")
245 git-ls-remote $upload_pack --tags "$remote" ||
257 if git-check-ref-format "$name"
259 echo ".${name}:${name}"
261 echo >&2 "warning: tag ${name} ignored"
266 # remote URL plus explicit refspecs; we need to merge them.
267 reflist="$reflist$LF$taglist"
269 # No explicit refspecs; fetch tags only.
283 # These are relative path from $GIT_DIR, typically starting at refs/
285 if expr "z$ref" : 'z\.' >/dev/null
288 ref=$(expr "z$ref" : 'z\.\(.*\)')
292 if expr "z$ref" : 'z+' >/dev/null
295 ref=$(expr "z$ref" : 'z+\(.*\)')
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...
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
313 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
314 "`git-repo-config --bool http.noEPSV`" = true ]; then
315 noepsv_opt="--disable-epsv"
319 head="ref: $remote_name"
320 while (expr "z$head" : "zref:" && expr $depth \< $max_depth) >/dev/null
322 remote_name_quoted=$(@@PERL@@ -e '
325 $u =~ s{([^-a-zA-Z0-9/.])}{sprintf"%%%02x",ord($1)}eg;
328 head=$(curl -nsfL $curl_extra_args $noepsv_opt "$remote/$remote_name_quoted")
329 depth=$( expr \( $depth + 1 \) )
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
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)
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"
355 resolve_alternates "$remote" <"$GIT_DIR/TMP_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
363 rm -f "$GIT_DIR/TMP_ALT"
365 rsync_slurped_objects=t
369 # We will do git native transport with just one call later.
373 append_fetch_head "$head" "$remote" \
374 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
379 http://* | https://* | ftp://* | rsync://* )
380 ;; # we are already done.
382 ( : subshell because we muck with IFS
386 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
388 while read sha1 remote_name
392 echo >&2 "Fetch failure: $remote"
394 # special line coming from index-pack with the pack name
398 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
426 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
427 append_fetch_head "$sha1" "$remote" \
428 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
430 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
436 fetch_main "$reflist"
438 # automated tag following
439 case "$no_tags$tags" in
443 # effective only when we are following remote branch
444 # using local tracking branch.
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' |
451 git-show-ref --verify --quiet -- "$name" && continue
452 git-check-ref-format "$name" || {
453 echo >&2 "warning: tag ${name} ignored"
456 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
457 echo >&2 "Auto-following $name"
458 echo ".${name}:${name}"
464 fetch_main "$taglist" ;;
468 # If the original head was empty (i.e. no "master" yet), or
469 # if we were told not to worry, we do not have to check.
474 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
475 if test "$curr_head" != "$orig_head"
478 -m "$rloga: Undoing incorrectly fetched HEAD." \
480 die "Cannot fetch into the current branch."