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"
24 while case "$#" in 0) break ;; esac
27 -a|--a|--ap|--app|--appe|--appen|--append)
30 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
31 --upload-pa|--upload-pac|--upload-pack)
36 -f|--f|--fo|--for|--forc|--force)
39 -t|--t|--ta|--tag|--tags)
42 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
45 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
46 --update-he|--update-hea|--update-head|--update-head-|\
47 --update-head-o|--update-head-ok)
53 -k|--k|--ke|--kee|--keep)
57 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
71 origin=$(get_default_remote)
72 test -n "$(get_remote_url ${origin})" ||
73 die "Where do you want to fetch from today?"
74 set x $origin ; shift ;;
78 remote=$(get_remote_url "$@")
81 rsync_slurped_objects=
83 rloga="$rloga $remote_nick"
84 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
86 if test "" = "$append"
88 : >"$GIT_DIR/FETCH_HEAD"
91 # Global that is reused later
92 ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
93 die "Cannot find the reflist at $remote"
95 append_fetch_head () {
102 t) not_for_merge_='not-for-merge' ;;
103 '') not_for_merge_= ;;
106 # remote-nick is the URL given on the command line (or a shorthand)
107 # remote-name is the $GIT_DIR relative refs/ path we computed
110 # the $note_ variable will be fed to git-fmt-merge-msg for further
112 case "$remote_name_" in
116 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
117 note_="branch '$note_' of " ;;
119 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
120 note_="tag '$note_' of " ;;
122 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
123 note_="remote branch '$note_' of " ;;
125 note_="$remote_name of " ;;
127 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
129 note_="$note_$remote_"
131 # 2.6.11-tree tag would not be happy to be fed to resolve.
132 if git-cat-file commit "$head_" >/dev/null 2>&1
134 headc_=$(git-rev-parse --verify "$head_^0") || exit
135 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
137 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
140 update_local_ref "$local_name_" "$head_" "$note_"
143 update_local_ref () {
144 # If we are storing the head locally make sure that it is
145 # a fast forward (aka "reverse push").
147 label_=$(git-cat-file -t $2)
148 newshort_=$(git-rev-parse --short $2)
149 if test -z "$1" ; then
150 [ "$verbose" ] && echo >&2 "* fetched $3"
151 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
154 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
158 # Tags need not be pointing at commits so there
159 # is no way to guarantee "fast-forward" anyway.
160 if test -n "$oldshort_"
162 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
164 [ "$verbose" ] && echo >&2 "* $1: same as $3"
165 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
167 echo >&2 "* $1: updating with $3"
168 echo >&2 " $label_: $newshort_"
169 git-update-ref -m "$rloga: updating tag" "$1" "$2"
172 echo >&2 "* $1: storing $3"
173 echo >&2 " $label_: $newshort_"
174 git-update-ref -m "$rloga: storing tag" "$1" "$2"
178 refs/heads/* | refs/remotes/*)
179 # $1 is the ref being updated.
180 # $2 is the new value for the ref.
181 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
184 # Require fast-forward.
185 mb=$(git-merge-base "$local" "$2") &&
188 if test -n "$verbose"
190 echo >&2 "* $1: same as $3"
191 echo >&2 " $label_: $newshort_"
195 echo >&2 "* $1: fast forward to $3"
196 echo >&2 " old..new: $oldshort_..$newshort_"
197 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
203 case ",$force,$single_force," in
205 echo >&2 "* $1: forcing update to non-fast forward $3"
206 echo >&2 " old...new: $oldshort_...$newshort_"
207 git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
210 echo >&2 "* $1: not updating to non-fast forward $3"
211 echo >&2 " old...new: $oldshort_...$newshort_"
217 echo >&2 "* $1: storing $3"
218 echo >&2 " $label_: $newshort_"
219 git-update-ref -m "$rloga: storing head" "$1" "$2"
225 case "$update_head_ok" in
227 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
231 # If --tags (and later --heads or --all) is specified, then we are
232 # not talking about defaults stored in Pull: line of remotes or
233 # branches file, and just fetch those and refspecs explicitly given.
234 # Otherwise we do what we always did.
236 reflist=$(get_remote_refs_for_fetch "$@")
240 echo "$ls_remote_result" |
252 if git-check-ref-format "$name"
254 echo ".${name}:${name}"
256 echo >&2 "warning: tag ${name} ignored"
261 # remote URL plus explicit refspecs; we need to merge them.
262 reflist="$reflist$LF$taglist"
264 # No explicit refspecs; fetch tags only.
278 # These are relative path from $GIT_DIR, typically starting at refs/
280 if expr "z$ref" : 'z\.' >/dev/null
283 ref=$(expr "z$ref" : 'z\.\(.*\)')
287 if expr "z$ref" : 'z+' >/dev/null
290 ref=$(expr "z$ref" : 'z+\(.*\)')
294 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
295 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
297 rref="$rref$LF$remote_name"
299 # There are transports that can fetch only one head at a time...
301 http://* | https://* | ftp://*)
302 proto=`expr "$remote" : '\([^:]*\):'`
303 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
306 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
307 "`git-repo-config --bool http.noEPSV`" = true ]; then
308 noepsv_opt="--disable-epsv"
311 # Find $remote_name from ls-remote output.
314 echo "$ls_remote_result" |
317 test "z$name" = "z$remote_name" || continue
322 expr "z$head" : "z$_x40\$" >/dev/null ||
323 die "No such ref $remote_name at $remote"
324 echo >&2 "Fetching $remote_name from $remote using $proto"
325 git-http-fetch -v -a "$head" "$remote/" || exit
328 TMP_HEAD="$GIT_DIR/TMP_HEAD"
329 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
330 head=$(git-rev-parse --verify TMP_HEAD)
332 test "$rsync_slurped_objects" || {
333 rsync -av --ignore-existing --exclude info \
334 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
336 # Look at objects/info/alternates for rsync -- http will
337 # support it natively and git native ones will do it on
338 # the remote end. Not having that file is not a crime.
339 rsync -q "$remote/objects/info/alternates" \
340 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
341 rm -f "$GIT_DIR/TMP_ALT"
342 if test -f "$GIT_DIR/TMP_ALT"
344 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
347 case "$alt" in 'bad alternate: '*) die "$alt";; esac
348 echo >&2 "Getting alternate: $alt"
349 rsync -av --ignore-existing --exclude info \
350 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
352 rm -f "$GIT_DIR/TMP_ALT"
354 rsync_slurped_objects=t
358 # We will do git native transport with just one call later.
362 append_fetch_head "$head" "$remote" \
363 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
368 http://* | https://* | ftp://* | rsync://* )
369 ;; # we are already done.
371 ( : subshell because we muck with IFS
375 git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
377 while read sha1 remote_name
381 echo >&2 "Fetch failure: $remote"
383 # special line coming from index-pack with the pack name
387 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
415 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
416 append_fetch_head "$sha1" "$remote" \
417 "$remote_name" "$remote_nick" "$local_name" \
418 "$not_for_merge" || exit
420 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
426 fetch_main "$reflist" || exit
428 # automated tag following
429 case "$no_tags$tags" in
433 # effective only when we are following remote branch
434 # using local tracking branch.
436 echo "$ls_remote_result" |
437 sed -n -e 's|^\('"$_x40"'\) \(refs/tags/.*\)^{}$|\1 \2|p' \
438 -e 's|^\('"$_x40"'\) \(refs/tags/.*\)$|\1 \2|p' |
441 git-show-ref --verify --quiet -- "$name" && continue
442 git-check-ref-format "$name" || {
443 echo >&2 "warning: tag ${name} ignored"
446 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
447 echo >&2 "Auto-following $name"
448 echo ".${name}:${name}"
454 fetch_main "$taglist" || exit ;;
458 # If the original head was empty (i.e. no "master" yet), or
459 # if we were told not to worry, we do not have to check.
464 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
465 if test "$curr_head" != "$orig_head"
468 -m "$rloga: Undoing incorrectly fetched HEAD." \
470 die "Cannot fetch into the current branch."