4 USAGE='<fetch-options> <repository> <refspec>...'
7 set_reflog_action "fetch $*"
8 cd_to_toplevel ;# probably unnecessary...
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"
28 test -t 1 || no_progress=--no-progress
29 while case "$#" in 0) break ;; esac
32 -a|--a|--ap|--app|--appe|--appen|--append)
35 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
36 --upload-pa|--upload-pac|--upload-pack)
38 exec="--upload-pack=$1"
40 --upl=*|--uplo=*|--uploa=*|--upload=*|\
41 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
42 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
45 -f|--f|--fo|--for|--forc|--force)
48 -t|--t|--ta|--tag|--tags)
51 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
54 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
55 --update-he|--update-hea|--update-head|--update-head-|\
56 --update-head-o|--update-head-ok)
62 -k|--k|--ke|--kee|--keep)
66 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
70 shallow_depth="--depth=$1"
84 origin=$(get_default_remote)
85 test -n "$(get_remote_url ${origin})" ||
86 die "Where do you want to fetch from today?"
87 set x $origin ; shift ;;
92 # No command line override and we have configuration for the remote.
93 exec="--upload-pack=$(get_uploadpack $1)"
97 remote=$(get_remote_url "$@")
100 rsync_slurped_objects=
102 if test "" = "$append"
104 : >"$GIT_DIR/FETCH_HEAD"
107 # Global that is reused later
108 ls_remote_result=$(git ls-remote $exec "$remote") ||
109 die "Cannot get the repository state from $remote"
111 append_fetch_head () {
118 t) not_for_merge_='not-for-merge' ;;
119 '') not_for_merge_= ;;
122 # remote-nick is the URL given on the command line (or a shorthand)
123 # remote-name is the $GIT_DIR relative refs/ path we computed
126 # the $note_ variable will be fed to git-fmt-merge-msg for further
128 case "$remote_name_" in
132 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
133 note_="branch '$note_' of " ;;
135 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
136 note_="tag '$note_' of " ;;
138 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
139 note_="remote branch '$note_' of " ;;
141 note_="$remote_name of " ;;
143 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
145 note_="$note_$remote_"
147 # 2.6.11-tree tag would not be happy to be fed to resolve.
148 if git-cat-file commit "$head_" >/dev/null 2>&1
150 headc_=$(git-rev-parse --verify "$head_^0") || exit
151 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
153 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
156 update_local_ref "$local_name_" "$head_" "$note_"
159 update_local_ref () {
160 # If we are storing the head locally make sure that it is
161 # a fast forward (aka "reverse push").
163 label_=$(git-cat-file -t $2)
164 newshort_=$(git-rev-parse --short $2)
165 if test -z "$1" ; then
166 [ "$verbose" ] && echo >&2 "* fetched $3"
167 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
170 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
174 # Tags need not be pointing at commits so there
175 # is no way to guarantee "fast-forward" anyway.
176 if test -n "$oldshort_"
178 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
180 [ "$verbose" ] && echo >&2 "* $1: same as $3"
181 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
183 echo >&2 "* $1: updating with $3"
184 echo >&2 " $label_: $newshort_"
185 git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
188 echo >&2 "* $1: storing $3"
189 echo >&2 " $label_: $newshort_"
190 git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
194 refs/heads/* | refs/remotes/*)
195 # $1 is the ref being updated.
196 # $2 is the new value for the ref.
197 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
200 # Require fast-forward.
201 mb=$(git-merge-base "$local" "$2") &&
204 if test -n "$verbose"
206 echo >&2 "* $1: same as $3"
207 echo >&2 " $label_: $newshort_"
211 echo >&2 "* $1: fast forward to $3"
212 echo >&2 " old..new: $oldshort_..$newshort_"
213 git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
219 case ",$force,$single_force," in
221 echo >&2 "* $1: forcing update to non-fast forward $3"
222 echo >&2 " old...new: $oldshort_...$newshort_"
223 git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
226 echo >&2 "* $1: not updating to non-fast forward $3"
227 echo >&2 " old...new: $oldshort_...$newshort_"
233 echo >&2 "* $1: storing $3"
234 echo >&2 " $label_: $newshort_"
235 git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
241 # updating the current HEAD with git-fetch in a bare
242 # repository is always fine.
243 if test -z "$update_head_ok" && test $(is_bare_repository) = false
245 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
248 # Allow --notags from remote.$1.tagopt
249 case "$tags$no_tags" in
251 case "$(git-config --get "remote.$1.tagopt")" in
257 # If --tags (and later --heads or --all) is specified, then we are
258 # not talking about defaults stored in Pull: line of remotes or
259 # branches file, and just fetch those and refspecs explicitly given.
260 # Otherwise we do what we always did.
262 reflist=$(get_remote_refs_for_fetch "$@")
266 echo "$ls_remote_result" |
267 git-show-ref --exclude-existing=refs/tags/ |
270 echo ".${name}:${name}"
274 # remote URL plus explicit refspecs; we need to merge them.
275 reflist="$reflist$LF$taglist"
277 # No explicit refspecs; fetch tags only.
291 # These are relative path from $GIT_DIR, typically starting at refs/
293 if expr "z$ref" : 'z\.' >/dev/null
296 ref=$(expr "z$ref" : 'z\.\(.*\)')
300 if expr "z$ref" : 'z+' >/dev/null
303 ref=$(expr "z$ref" : 'z+\(.*\)')
307 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
308 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
310 rref="$rref$LF$remote_name"
312 # There are transports that can fetch only one head at a time...
314 http://* | https://* | ftp://*)
315 test -n "$shallow_depth" &&
316 die "shallow clone with http not supported"
317 proto=`expr "$remote" : '\([^:]*\):'`
318 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
321 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
322 "`git-config --bool http.noEPSV`" = true ]; then
323 noepsv_opt="--disable-epsv"
326 # Find $remote_name from ls-remote output.
329 echo "$ls_remote_result" |
332 test "z$name" = "z$remote_name" || continue
337 expr "z$head" : "z$_x40\$" >/dev/null ||
338 die "No such ref $remote_name at $remote"
339 echo >&2 "Fetching $remote_name from $remote using $proto"
340 git-http-fetch -v -a "$head" "$remote/" || exit
343 test -n "$shallow_depth" &&
344 die "shallow clone with rsync not supported"
345 TMP_HEAD="$GIT_DIR/TMP_HEAD"
346 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
347 head=$(git-rev-parse --verify TMP_HEAD)
349 test "$rsync_slurped_objects" || {
350 rsync -av --ignore-existing --exclude info \
351 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
353 # Look at objects/info/alternates for rsync -- http will
354 # support it natively and git native ones will do it on
355 # the remote end. Not having that file is not a crime.
356 rsync -q "$remote/objects/info/alternates" \
357 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
358 rm -f "$GIT_DIR/TMP_ALT"
359 if test -f "$GIT_DIR/TMP_ALT"
361 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
364 case "$alt" in 'bad alternate: '*) die "$alt";; esac
365 echo >&2 "Getting alternate: $alt"
366 rsync -av --ignore-existing --exclude info \
367 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
369 rm -f "$GIT_DIR/TMP_ALT"
371 rsync_slurped_objects=t
375 # We will do git native transport with just one call later.
379 append_fetch_head "$head" "$remote" \
380 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
385 http://* | https://* | ftp://* | rsync://* )
386 ;; # we are already done.
388 ( : subshell because we muck with IFS
391 if test -f "$remote" ; then
392 test -n "$shallow_depth" &&
393 die "shallow clone with bundle is not supported"
394 git-bundle unbundle "$remote" $rref ||
395 echo failed "$remote"
397 git-fetch-pack --thin $exec $keep $shallow_depth $no_progress \
399 echo failed "$remote"
404 if test -n "$keepfile" && test -f "$keepfile"
411 while read sha1 remote_name
415 echo >&2 "Fetch failure: $remote"
417 # special line coming from index-pack with the pack name
421 keepfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
449 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
450 append_fetch_head "$sha1" "$remote" \
451 "$remote_name" "$remote_nick" "$local_name" \
452 "$not_for_merge" || exit
460 fetch_main "$reflist" || exit
462 # automated tag following
463 case "$no_tags$tags" in
467 # effective only when we are following remote branch
468 # using local tracking branch.
470 echo "$ls_remote_result" |
471 git-show-ref --exclude-existing=refs/tags/ |
474 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
475 echo >&2 "Auto-following $name"
476 echo ".${name}:${name}"
482 # do not deepen a shallow tree when following tags
484 fetch_main "$taglist" || exit ;;
488 # If the original head was empty (i.e. no "master" yet), or
489 # if we were told not to worry, we do not have to check.
494 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
495 if test "$curr_head" != "$orig_head"
498 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
500 die "Cannot fetch into the current branch."