3 . git-sh-setup || die "Not a git archive"
5 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
6 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
11 while case "$#" in 0) break ;; esac
14 -a|--a|--ap|--app|--appe|--appen|--append)
17 -f|--f|--fo|--for|--forc|--force)
20 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
21 --update-he|--update-hea|--update-head|--update-head-|\
22 --update-head-o|--update-head-ok)
34 test -f "$GIT_DIR/branches/origin" ||
35 test -f "$GIT_DIR/remotes/origin" ||
36 die "Where do you want to fetch from today?"
41 remote=$(get_remote_url "$@")
44 rsync_slurped_objects=
46 if test "" = "$append"
48 : >$GIT_DIR/FETCH_HEAD
51 append_fetch_head () {
58 t) not_for_merge_='not-for-merge' ;;
59 '') not_for_merge_= ;;
62 # remote-nick is the URL given on the command line (or a shorthand)
63 # remote-name is the $GIT_DIR relative refs/ path we computed
65 case "$remote_name_" in
69 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
70 note_="branch '$note_' of " ;;
72 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
73 note_="tag '$note_' of " ;;
75 note_="$remote_name of " ;;
77 remote_1_=$(expr "$remote_" : '\(.*\)\.git/*$') &&
79 note_="$note_$remote_"
81 # 2.6.11-tree tag would not be happy to be fed to resolve.
82 if git-cat-file commit "$head_" >/dev/null 2>&1
84 headc_=$(git-rev-parse --verify "$head_^0") || exit
85 echo "$headc_ $not_for_merge_ $note_" >>$GIT_DIR/FETCH_HEAD
86 echo >&2 "* committish: $head_"
89 echo "$head_ not-for-merge $note_" >>$GIT_DIR/FETCH_HEAD
90 echo >&2 "* non-commit: $head_"
93 if test "$local_name_" != ""
95 # We are storing the head locally. Make sure that it is
96 # a fast forward (aka "reverse push").
97 fast_forward_local "$local_name_" "$head_" "$note_"
101 fast_forward_local () {
102 mkdir -p "$(dirname "$GIT_DIR/$1")"
105 # Tags need not be pointing at commits so there
106 # is no way to guarantee "fast-forward" anyway.
107 if test -f "$GIT_DIR/$1"
109 echo >&2 "* $1: updating with $3"
111 echo >&2 "* $1: storing $3"
113 git-update-ref "$1" "$2"
117 # $1 is the ref being updated.
118 # $2 is the new value for the ref.
119 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
122 # Require fast-forward.
123 mb=$(git-merge-base "$local" "$2") &&
126 echo >&2 "* $1: same as $3"
129 echo >&2 "* $1: fast forward to $3"
130 git-update-ref "$1" "$2" "$local"
136 echo >&2 "* $1: does not fast forward to $3;"
137 case ",$force,$single_force," in
139 echo >&2 " forcing update."
140 git-update-ref "$1" "$2" "$local"
143 echo >&2 " not updating."
148 echo >&2 "* $1: storing $3"
149 git-update-ref "$1" "$2"
155 case "$update_head_ok" in
157 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
161 for ref in $(get_remote_refs_for_fetch "$@")
165 # These are relative path from $GIT_DIR, typically starting at refs/
167 if expr "$ref" : '\.' >/dev/null
170 ref=$(expr "$ref" : '\.\(.*\)')
174 if expr "$ref" : '\+' >/dev/null
177 ref=$(expr "$ref" : '\+\(.*\)')
181 remote_name=$(expr "$ref" : '\([^:]*\):')
182 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
184 rref="$rref $remote_name"
186 # There are transports that can fetch only one head at a time...
188 http://* | https://*)
189 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
192 head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
193 expr "$head" : "$_x40\$" >/dev/null ||
194 die "Failed to fetch $remote_name from $remote"
195 echo >&2 Fetching "$remote_name from $remote" using http
196 git-http-fetch -v -a "$head" "$remote/" || exit
199 TMP_HEAD="$GIT_DIR/TMP_HEAD"
200 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
201 head=$(git-rev-parse --verify TMP_HEAD)
203 test "$rsync_slurped_objects" || {
204 rsync -av --ignore-existing --exclude info \
205 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
207 # Look at objects/info/alternates for rsync -- http will
208 # support it natively and git native ones will do it on the remote
209 # end. Not having that file is not a crime.
210 rsync -q "$remote/objects/info/alternates" \
211 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
212 rm -f "$GIT_DIR/TMP_ALT"
213 if test -f "$GIT_DIR/TMP_ALT"
215 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
218 case "$alt" in 'bad alternate: '*) die "$alt";; esac
219 echo >&2 "Getting alternate: $alt"
220 rsync -av --ignore-existing --exclude info \
221 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
223 rm -f "$GIT_DIR/TMP_ALT"
225 rsync_slurped_objects=t
229 # We will do git native transport with just one call later.
233 append_fetch_head "$head" "$remote" \
234 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
239 http://* | https://* | rsync://* )
240 ;; # we are already done.
243 git-fetch-pack "$remote" $rref || echo failed "$remote"
245 while read sha1 remote_name
249 echo >&2 "Fetch failure: $remote"
277 local_name=$(expr "$found" : '[^:]*:\(.*\)')
278 append_fetch_head "$sha1" "$remote" \
279 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
284 # If the original head was empty (i.e. no "master" yet), or
285 # if we were told not to worry, we do not have to check.
286 case ",$update_head_ok,$orig_head," in
290 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
291 if test "$curr_head" != "$orig_head"
293 git-update-ref HEAD "$orig_head"
294 die "Cannot fetch into the current branch."