4 USAGE='<fetch-options> <repository> <refspec>...'
7 TOP=$(git-rev-parse --show-cdup)
13 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
14 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
30 while case "$#" in 0) break ;; esac
33 -a|--a|--ap|--app|--appe|--appen|--append)
36 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
37 --upload-pa|--upload-pac|--upload-pack)
42 -f|--f|--fo|--for|--forc|--force)
45 -t|--t|--ta|--tag|--tags)
48 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
51 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
52 --update-he|--update-hea|--update-head|--update-head-|\
53 --update-head-o|--update-head-ok)
59 -k|--k|--ke|--kee|--keep)
63 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
77 origin=$(get_default_remote)
78 test -n "$(get_remote_url ${origin})" ||
79 die "Where do you want to fetch from today?"
80 set x $origin ; shift ;;
84 remote=$(get_remote_url "$@")
87 rsync_slurped_objects=
89 rloga="$rloga $remote_nick"
90 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
92 if test "" = "$append"
94 : >"$GIT_DIR/FETCH_HEAD"
97 # Global that is reused later
98 ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
99 die "Cannot get the repository state from $remote"
101 append_fetch_head () {
108 t) not_for_merge_='not-for-merge' ;;
109 '') not_for_merge_= ;;
112 # remote-nick is the URL given on the command line (or a shorthand)
113 # remote-name is the $GIT_DIR relative refs/ path we computed
116 # the $note_ variable will be fed to git-fmt-merge-msg for further
118 case "$remote_name_" in
122 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
123 note_="branch '$note_' of " ;;
125 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
126 note_="tag '$note_' of " ;;
128 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
129 note_="remote branch '$note_' of " ;;
131 note_="$remote_name of " ;;
133 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
135 note_="$note_$remote_"
137 # 2.6.11-tree tag would not be happy to be fed to resolve.
138 if git-cat-file commit "$head_" >/dev/null 2>&1
140 headc_=$(git-rev-parse --verify "$head_^0") || exit
141 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
143 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
146 update_local_ref "$local_name_" "$head_" "$note_"
149 update_local_ref () {
150 # If we are storing the head locally make sure that it is
151 # a fast forward (aka "reverse push").
153 label_=$(git-cat-file -t $2)
154 newshort_=$(git-rev-parse --short $2)
155 if test -z "$1" ; then
156 [ "$verbose" ] && echo >&2 "* fetched $3"
157 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
160 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
164 # Tags need not be pointing at commits so there
165 # is no way to guarantee "fast-forward" anyway.
166 if test -n "$oldshort_"
168 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
170 [ "$verbose" ] && echo >&2 "* $1: same as $3"
171 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
173 echo >&2 "* $1: updating with $3"
174 echo >&2 " $label_: $newshort_"
175 git-update-ref -m "$rloga: updating tag" "$1" "$2"
178 echo >&2 "* $1: storing $3"
179 echo >&2 " $label_: $newshort_"
180 git-update-ref -m "$rloga: storing tag" "$1" "$2"
184 refs/heads/* | refs/remotes/*)
185 # $1 is the ref being updated.
186 # $2 is the new value for the ref.
187 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
190 # Require fast-forward.
191 mb=$(git-merge-base "$local" "$2") &&
194 if test -n "$verbose"
196 echo >&2 "* $1: same as $3"
197 echo >&2 " $label_: $newshort_"
201 echo >&2 "* $1: fast forward to $3"
202 echo >&2 " old..new: $oldshort_..$newshort_"
203 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
209 case ",$force,$single_force," in
211 echo >&2 "* $1: forcing update to non-fast forward $3"
212 echo >&2 " old...new: $oldshort_...$newshort_"
213 git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
216 echo >&2 "* $1: not updating to non-fast forward $3"
217 echo >&2 " old...new: $oldshort_...$newshort_"
223 echo >&2 "* $1: storing $3"
224 echo >&2 " $label_: $newshort_"
225 git-update-ref -m "$rloga: storing head" "$1" "$2"
231 case "$update_head_ok" in
233 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 proto=`expr "$remote" : '\([^:]*\):'`
309 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
312 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
313 "`git-repo-config --bool http.noEPSV`" = true ]; then
314 noepsv_opt="--disable-epsv"
317 # Find $remote_name from ls-remote output.
320 echo "$ls_remote_result" |
323 test "z$name" = "z$remote_name" || continue
328 expr "z$head" : "z$_x40\$" >/dev/null ||
329 die "No such ref $remote_name at $remote"
330 echo >&2 "Fetching $remote_name from $remote using $proto"
331 git-http-fetch -v -a "$head" "$remote/" || exit
334 TMP_HEAD="$GIT_DIR/TMP_HEAD"
335 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
336 head=$(git-rev-parse --verify TMP_HEAD)
338 test "$rsync_slurped_objects" || {
339 rsync -av --ignore-existing --exclude info \
340 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
342 # Look at objects/info/alternates for rsync -- http will
343 # support it natively and git native ones will do it on
344 # the remote end. Not having that file is not a crime.
345 rsync -q "$remote/objects/info/alternates" \
346 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
347 rm -f "$GIT_DIR/TMP_ALT"
348 if test -f "$GIT_DIR/TMP_ALT"
350 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
353 case "$alt" in 'bad alternate: '*) die "$alt";; esac
354 echo >&2 "Getting alternate: $alt"
355 rsync -av --ignore-existing --exclude info \
356 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
358 rm -f "$GIT_DIR/TMP_ALT"
360 rsync_slurped_objects=t
364 # We will do git native transport with just one call later.
368 append_fetch_head "$head" "$remote" \
369 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
374 http://* | https://* | ftp://* | rsync://* )
375 ;; # we are already done.
377 ( : subshell because we muck with IFS
381 git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
383 while read sha1 remote_name
387 echo >&2 "Fetch failure: $remote"
389 # special line coming from index-pack with the pack name
393 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
421 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
422 append_fetch_head "$sha1" "$remote" \
423 "$remote_name" "$remote_nick" "$local_name" \
424 "$not_for_merge" || exit
426 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
432 fetch_main "$reflist" || exit
434 # automated tag following
435 case "$no_tags$tags" in
439 # effective only when we are following remote branch
440 # using local tracking branch.
442 echo "$ls_remote_result" |
443 git-show-ref --exclude-existing=refs/tags/ |
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."