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 while case "$#" in 0) break ;; esac
31 -a|--a|--ap|--app|--appe|--appen|--append)
34 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
35 --upload-pa|--upload-pac|--upload-pack)
40 -f|--f|--fo|--for|--forc|--force)
43 -t|--t|--ta|--tag|--tags)
46 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
49 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
50 --update-he|--update-hea|--update-head|--update-head-|\
51 --update-head-o|--update-head-ok)
57 -k|--k|--ke|--kee|--keep)
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 if test "" = "$append"
93 : >"$GIT_DIR/FETCH_HEAD"
96 # Global that is reused later
97 ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
98 die "Cannot get the repository state from $remote"
100 append_fetch_head () {
107 t) not_for_merge_='not-for-merge' ;;
108 '') not_for_merge_= ;;
111 # remote-nick is the URL given on the command line (or a shorthand)
112 # remote-name is the $GIT_DIR relative refs/ path we computed
115 # the $note_ variable will be fed to git-fmt-merge-msg for further
117 case "$remote_name_" in
121 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
122 note_="branch '$note_' of " ;;
124 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
125 note_="tag '$note_' of " ;;
127 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
128 note_="remote branch '$note_' of " ;;
130 note_="$remote_name of " ;;
132 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
134 note_="$note_$remote_"
136 # 2.6.11-tree tag would not be happy to be fed to resolve.
137 if git-cat-file commit "$head_" >/dev/null 2>&1
139 headc_=$(git-rev-parse --verify "$head_^0") || exit
140 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
142 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
145 update_local_ref "$local_name_" "$head_" "$note_"
148 update_local_ref () {
149 # If we are storing the head locally make sure that it is
150 # a fast forward (aka "reverse push").
152 label_=$(git-cat-file -t $2)
153 newshort_=$(git-rev-parse --short $2)
154 if test -z "$1" ; then
155 [ "$verbose" ] && echo >&2 "* fetched $3"
156 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
159 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
163 # Tags need not be pointing at commits so there
164 # is no way to guarantee "fast-forward" anyway.
165 if test -n "$oldshort_"
167 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
169 [ "$verbose" ] && echo >&2 "* $1: same as $3"
170 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
172 echo >&2 "* $1: updating with $3"
173 echo >&2 " $label_: $newshort_"
174 git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
177 echo >&2 "* $1: storing $3"
178 echo >&2 " $label_: $newshort_"
179 git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
183 refs/heads/* | refs/remotes/*)
184 # $1 is the ref being updated.
185 # $2 is the new value for the ref.
186 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
189 # Require fast-forward.
190 mb=$(git-merge-base "$local" "$2") &&
193 if test -n "$verbose"
195 echo >&2 "* $1: same as $3"
196 echo >&2 " $label_: $newshort_"
200 echo >&2 "* $1: fast forward to $3"
201 echo >&2 " old..new: $oldshort_..$newshort_"
202 git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
208 case ",$force,$single_force," in
210 echo >&2 "* $1: forcing update to non-fast forward $3"
211 echo >&2 " old...new: $oldshort_...$newshort_"
212 git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
215 echo >&2 "* $1: not updating to non-fast forward $3"
216 echo >&2 " old...new: $oldshort_...$newshort_"
222 echo >&2 "* $1: storing $3"
223 echo >&2 " $label_: $newshort_"
224 git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
230 # updating the current HEAD with git-fetch in a bare
231 # repository is always fine.
232 if test -z "$update_head_ok" && test $(is_bare_repository) = false
234 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
237 # If --tags (and later --heads or --all) is specified, then we are
238 # not talking about defaults stored in Pull: line of remotes or
239 # branches file, and just fetch those and refspecs explicitly given.
240 # Otherwise we do what we always did.
242 reflist=$(get_remote_refs_for_fetch "$@")
246 echo "$ls_remote_result" |
258 if git-check-ref-format "$name"
260 echo ".${name}:${name}"
262 echo >&2 "warning: tag ${name} ignored"
267 # remote URL plus explicit refspecs; we need to merge them.
268 reflist="$reflist$LF$taglist"
270 # No explicit refspecs; fetch tags only.
284 # These are relative path from $GIT_DIR, typically starting at refs/
286 if expr "z$ref" : 'z\.' >/dev/null
289 ref=$(expr "z$ref" : 'z\.\(.*\)')
293 if expr "z$ref" : 'z+' >/dev/null
296 ref=$(expr "z$ref" : 'z+\(.*\)')
300 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
301 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
303 rref="$rref$LF$remote_name"
305 # There are transports that can fetch only one head at a time...
307 http://* | https://* | ftp://*)
308 test -n "$shallow_depth" &&
309 die "shallow clone with http not supported"
310 proto=`expr "$remote" : '\([^:]*\):'`
311 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
314 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
315 "`git-repo-config --bool http.noEPSV`" = true ]; then
316 noepsv_opt="--disable-epsv"
319 # Find $remote_name from ls-remote output.
322 echo "$ls_remote_result" |
325 test "z$name" = "z$remote_name" || continue
330 expr "z$head" : "z$_x40\$" >/dev/null ||
331 die "No such ref $remote_name at $remote"
332 echo >&2 "Fetching $remote_name from $remote using $proto"
333 git-http-fetch -v -a "$head" "$remote/" || exit
336 test -n "$shallow_depth" &&
337 die "shallow clone with rsync not supported"
338 TMP_HEAD="$GIT_DIR/TMP_HEAD"
339 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
340 head=$(git-rev-parse --verify TMP_HEAD)
342 test "$rsync_slurped_objects" || {
343 rsync -av --ignore-existing --exclude info \
344 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
346 # Look at objects/info/alternates for rsync -- http will
347 # support it natively and git native ones will do it on
348 # the remote end. Not having that file is not a crime.
349 rsync -q "$remote/objects/info/alternates" \
350 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
351 rm -f "$GIT_DIR/TMP_ALT"
352 if test -f "$GIT_DIR/TMP_ALT"
354 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
357 case "$alt" in 'bad alternate: '*) die "$alt";; esac
358 echo >&2 "Getting alternate: $alt"
359 rsync -av --ignore-existing --exclude info \
360 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
362 rm -f "$GIT_DIR/TMP_ALT"
364 rsync_slurped_objects=t
368 # We will do git native transport with just one call later.
372 append_fetch_head "$head" "$remote" \
373 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
378 http://* | https://* | ftp://* | rsync://* )
379 ;; # we are already done.
381 ( : subshell because we muck with IFS
384 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
385 echo failed "$remote"
389 if test -n "$keepfile" && test -f "$keepfile"
396 while read sha1 remote_name
400 echo >&2 "Fetch failure: $remote"
402 # special line coming from index-pack with the pack name
406 keepfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
434 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
435 append_fetch_head "$sha1" "$remote" \
436 "$remote_name" "$remote_nick" "$local_name" \
437 "$not_for_merge" || exit
445 fetch_main "$reflist" || exit
447 # automated tag following
448 case "$no_tags$tags" in
452 # effective only when we are following remote branch
453 # using local tracking branch.
455 echo "$ls_remote_result" |
456 git-show-ref --exclude-existing=refs/tags/ |
459 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
460 echo >&2 "Auto-following $name"
461 echo ".${name}:${name}"
467 # do not deepen a shallow tree when following tags
469 fetch_main "$taglist" || exit ;;
473 # If the original head was empty (i.e. no "master" yet), or
474 # if we were told not to worry, we do not have to check.
479 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
480 if test "$curr_head" != "$orig_head"
483 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
485 die "Cannot fetch into the current branch."