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 echo "$2" >"$GIT_DIR/$1" ;;
116 # NEEDSWORK: use the same cmpxchg protocol here.
117 echo "$2" >"$GIT_DIR/$1.lock"
118 if test -f "$GIT_DIR/$1"
120 local=$(git-rev-parse --verify "$1^0") &&
121 mb=$(git-merge-base "$local" "$2") &&
124 echo >&2 "* $1: same as $3"
127 echo >&2 "* $1: fast forward to $3"
133 echo >&2 "* $1: does not fast forward to $3;"
134 case "$force,$single_force" in
136 echo >&2 " forcing update."
139 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
140 echo >&2 " leaving it in '$1.remote'"
145 echo >&2 "* $1: storing $3"
147 test -f "$GIT_DIR/$1.lock" &&
148 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
153 case "$update_head_ok" in
155 orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
159 for ref in $(get_remote_refs_for_fetch "$@")
163 # These are relative path from $GIT_DIR, typically starting at refs/
165 if expr "$ref" : '\.' >/dev/null
168 ref=$(expr "$ref" : '\.\(.*\)')
172 if expr "$ref" : '\+' >/dev/null
175 ref=$(expr "$ref" : '\+\(.*\)')
179 remote_name=$(expr "$ref" : '\([^:]*\):')
180 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
182 rref="$rref $remote_name"
184 # There are transports that can fetch only one head at a time...
186 http://* | https://*)
187 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
190 head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
191 expr "$head" : "$_x40\$" >/dev/null ||
192 die "Failed to fetch $remote_name from $remote"
193 echo >&2 Fetching "$remote_name from $remote" using http
194 git-http-fetch -v -a "$head" "$remote/" || exit
197 TMP_HEAD="$GIT_DIR/TMP_HEAD"
198 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
199 head=$(git-rev-parse TMP_HEAD)
201 test "$rsync_slurped_objects" || {
202 rsync -av --ignore-existing --exclude info \
203 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
205 # Look at objects/info/alternates for rsync -- http will
206 # support it natively and git native ones will do it on the remote
207 # end. Not having that file is not a crime.
208 rsync -q "$remote/objects/info/alternates" \
209 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
210 rm -f "$GIT_DIR/TMP_ALT"
211 if test -f "$GIT_DIR/TMP_ALT"
213 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
216 case "$alt" in 'bad alternate: '*) die "$alt";; esac
217 echo >&2 "Getting alternate: $alt"
218 rsync -av --ignore-existing --exclude info \
219 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
221 rm -f "$GIT_DIR/TMP_ALT"
223 rsync_slurped_objects=t
227 # We will do git native transport with just one call later.
231 append_fetch_head "$head" "$remote" \
232 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
237 http://* | https://* | rsync://* )
238 ;; # we are already done.
241 git-fetch-pack "$remote" $rref || echo failed "$remote"
243 while read sha1 remote_name
247 echo >&2 "Fetch failure: $remote"
275 local_name=$(expr "$found" : '[^:]*:\(.*\)')
276 append_fetch_head "$sha1" "$remote" \
277 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
282 # If the original head was empty (i.e. no "master" yet), or
283 # if we were told not to worry, we do not have to check.
284 case ",$update_head_ok,$orig_head," in
288 curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
289 if test "$curr_head" != "$orig_head"
291 echo "$orig_head" >$GIT_DIR/HEAD
292 die "Cannot fetch into the current branch."