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"
21 while case "$#" in 0) break ;; esac
24 -a|--a|--ap|--app|--appe|--appen|--append)
27 -u|--u|--up|--upl|--uploa|--upload|--upload-|--upload-p|--upload-pa|\
28 --upload-pac|--upload-pack)
32 -f|--f|--fo|--for|--forc|--force)
35 -t|--t|--ta|--tag|--tags)
38 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
41 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
42 --update-he|--update-hea|--update-head|--update-head-|\
43 --update-head-o|--update-head-ok)
49 -k|--k|--ke|--kee|--keep)
64 test -f "$GIT_DIR/branches/origin" ||
65 test -f "$GIT_DIR/remotes/origin" ||
66 die "Where do you want to fetch from today?"
71 remote=$(get_remote_url "$@")
74 rsync_slurped_objects=
76 if test "" = "$append"
78 : >"$GIT_DIR/FETCH_HEAD"
81 append_fetch_head () {
88 t) not_for_merge_='not-for-merge' ;;
89 '') not_for_merge_= ;;
92 # remote-nick is the URL given on the command line (or a shorthand)
93 # remote-name is the $GIT_DIR relative refs/ path we computed
95 case "$remote_name_" in
99 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
100 note_="branch '$note_' of " ;;
102 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
103 note_="tag '$note_' of " ;;
105 note_="$remote_name of " ;;
107 remote_1_=$(expr "$remote_" : '\(.*\)\.git/*$') &&
109 note_="$note_$remote_"
111 # 2.6.11-tree tag would not be happy to be fed to resolve.
112 if git-cat-file commit "$head_" >/dev/null 2>&1
114 headc_=$(git-rev-parse --verify "$head_^0") || exit
115 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
116 [ "$verbose" ] && echo >&2 "* committish: $head_"
117 [ "$verbose" ] && echo >&2 " $note_"
119 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
120 [ "$verbose" ] && echo >&2 "* non-commit: $head_"
121 [ "$verbose" ] && echo >&2 " $note_"
123 if test "$local_name_" != ""
125 # We are storing the head locally. Make sure that it is
126 # a fast forward (aka "reverse push").
127 fast_forward_local "$local_name_" "$head_" "$note_"
131 fast_forward_local () {
132 mkdir -p "$(dirname "$GIT_DIR/$1")"
135 # Tags need not be pointing at commits so there
136 # is no way to guarantee "fast-forward" anyway.
137 if test -f "$GIT_DIR/$1"
139 if now_=$(cat "$GIT_DIR/$1") && test "$now_" = "$2"
141 [ "$verbose" ] && echo >&2 "* $1: same as $3"
143 echo >&2 "* $1: updating with $3"
146 echo >&2 "* $1: storing $3"
148 git-update-ref "$1" "$2"
152 # $1 is the ref being updated.
153 # $2 is the new value for the ref.
154 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
157 # Require fast-forward.
158 mb=$(git-merge-base "$local" "$2") &&
161 echo >&2 "* $1: same as $3"
164 echo >&2 "* $1: fast forward to $3"
165 git-update-ref "$1" "$2" "$local"
171 echo >&2 "* $1: does not fast forward to $3;"
172 case ",$force,$single_force," in
174 echo >&2 " forcing update."
175 git-update-ref "$1" "$2" "$local"
178 echo >&2 " not updating."
183 echo >&2 "* $1: storing $3"
184 git-update-ref "$1" "$2"
190 case "$update_head_ok" in
192 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
196 # If --tags (and later --heads or --all) is specified, then we are
197 # not talking about defaults stored in Pull: line of remotes or
198 # branches file, and just fetch those and refspecs explicitly given.
199 # Otherwise we do what we always did.
201 reflist=$(get_remote_refs_for_fetch "$@")
205 git-ls-remote --tags "$remote" |
211 if git-check-ref-format "$name"
213 echo ".${name}:${name}"
215 echo >&2 "warning: tag ${name} ignored"
220 # remote URL plus explicit refspecs; we need to merge them.
221 reflist="$reflist$LF$taglist"
223 # No explicit refspecs; fetch tags only.
236 # These are relative path from $GIT_DIR, typically starting at refs/
238 if expr "$ref" : '\.' >/dev/null
241 ref=$(expr "$ref" : '\.\(.*\)')
245 if expr "$ref" : '\+' >/dev/null
248 ref=$(expr "$ref" : '\+\(.*\)')
252 remote_name=$(expr "$ref" : '\([^:]*\):')
253 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
255 rref="$rref$LF$remote_name"
257 # There are transports that can fetch only one head at a time...
259 http://* | https://*)
260 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
263 remote_name_quoted=$(perl -e '
265 $u =~ s{([^-a-zA-Z0-9/.])}{sprintf"%%%02x",ord($1)}eg;
268 head=$(curl -nsfL $curl_extra_args "$remote/$remote_name_quoted") &&
269 expr "$head" : "$_x40\$" >/dev/null ||
270 die "Failed to fetch $remote_name from $remote"
271 echo >&2 Fetching "$remote_name from $remote" using http
272 git-http-fetch -v -a "$head" "$remote/" || exit
275 TMP_HEAD="$GIT_DIR/TMP_HEAD"
276 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
277 head=$(git-rev-parse --verify TMP_HEAD)
279 test "$rsync_slurped_objects" || {
280 rsync -av --ignore-existing --exclude info \
281 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
283 # Look at objects/info/alternates for rsync -- http will
284 # support it natively and git native ones will do it on
285 # the remote end. Not having that file is not a crime.
286 rsync -q "$remote/objects/info/alternates" \
287 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
288 rm -f "$GIT_DIR/TMP_ALT"
289 if test -f "$GIT_DIR/TMP_ALT"
291 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
294 case "$alt" in 'bad alternate: '*) die "$alt";; esac
295 echo >&2 "Getting alternate: $alt"
296 rsync -av --ignore-existing --exclude info \
297 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
299 rm -f "$GIT_DIR/TMP_ALT"
301 rsync_slurped_objects=t
305 # We will do git native transport with just one call later.
309 append_fetch_head "$head" "$remote" \
310 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
315 http://* | https://* | rsync://* )
316 ;; # we are already done.
318 ( : subshell because we muck with IFS
321 git-fetch-pack $exec $keep "$remote" $rref || echo failed "$remote"
323 while read sha1 remote_name
327 echo >&2 "Fetch failure: $remote"
355 local_name=$(expr "$found" : '[^:]*:\(.*\)')
356 append_fetch_head "$sha1" "$remote" \
357 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
364 fetch_main "$reflist"
366 # automated tag following
367 case "$no_tags$tags" in
370 git-ls-remote --tags "$remote" |
371 sed -ne 's|^\([0-9a-f]*\)[ ]\(refs/tags/.*\)^{}$|\1 \2|p' |
374 test -f "$GIT_DIR/$name" && continue
375 git-check-ref-format "$name" || {
376 echo >&2 "warning: tag ${name} ignored"
379 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
380 echo >&2 "Auto-following $name"
381 echo ".${name}:${name}"
386 fetch_main "$taglist" ;;
390 # If the original head was empty (i.e. no "master" yet), or
391 # if we were told not to worry, we do not have to check.
392 case ",$update_head_ok,$orig_head," in
396 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
397 if test "$curr_head" != "$orig_head"
399 git-update-ref HEAD "$orig_head"
400 die "Cannot fetch into the current branch."