Move in_merge_bases() to commit.c
[git/repo.git] / git-fetch.sh
blobffbd44f0e1ab841e6f6a38295860220b01bb26ea
1 #!/bin/sh
4 USAGE='<fetch-options> <repository> <refspec>...'
5 SUBDIRECTORY_OK=Yes
6 . git-sh-setup
7 TOP=$(git-rev-parse --show-cdup)
8 if test ! -z "$TOP"
9 then
10 cd "$TOP"
12 . git-parse-remote
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"
16 LF='
18 IFS="$LF"
20 rloga=fetch
21 no_tags=
22 tags=
23 append=
24 force=
25 verbose=
26 update_head_ok=
27 exec=
28 upload_pack=
29 keep=
30 while case "$#" in 0) break ;; esac
32 case "$1" in
33 -a|--a|--ap|--app|--appe|--appen|--append)
34 append=t
36 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
37 --upload-pa|--upload-pac|--upload-pack)
38 shift
39 exec="--exec=$1"
40 upload_pack="-u $1"
42 -f|--f|--fo|--for|--forc|--force)
43 force=t
45 -t|--t|--ta|--tag|--tags)
46 tags=t
48 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
49 no_tags=t
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)
54 update_head_ok=t
56 -v|--verbose)
57 verbose=Yes
59 -k|--k|--ke|--kee|--keep)
60 keep='-k -k'
62 --reflog-action=*)
63 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
65 -*)
66 usage
69 break
71 esac
72 shift
73 done
75 case "$#" in
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 ;;
81 esac
83 remote_nick="$1"
84 remote=$(get_remote_url "$@")
85 refs=
86 rref=
87 rsync_slurped_objects=
89 rloga="$rloga $remote_nick"
90 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
92 if test "" = "$append"
93 then
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 () {
102 head_="$1"
103 remote_="$2"
104 remote_name_="$3"
105 remote_nick_="$4"
106 local_name_="$5"
107 case "$6" in
108 t) not_for_merge_='not-for-merge' ;;
109 '') not_for_merge_= ;;
110 esac
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
114 # for this refspec.
116 # the $note_ variable will be fed to git-fmt-merge-msg for further
117 # processing.
118 case "$remote_name_" in
119 HEAD)
120 note_= ;;
121 refs/heads/*)
122 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
123 note_="branch '$note_' of " ;;
124 refs/tags/*)
125 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
126 note_="tag '$note_' of " ;;
127 refs/remotes/*)
128 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
129 note_="remote branch '$note_' of " ;;
131 note_="$remote_name of " ;;
132 esac
133 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
134 remote_="$remote_1_"
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
139 then
140 headc_=$(git-rev-parse --verify "$head_^0") || exit
141 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
142 else
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_"
158 return 0
160 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
162 case "$1" in
163 refs/tags/*)
164 # Tags need not be pointing at commits so there
165 # is no way to guarantee "fast-forward" anyway.
166 if test -n "$oldshort_"
167 then
168 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
169 then
170 [ "$verbose" ] && echo >&2 "* $1: same as $3"
171 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
172 else
173 echo >&2 "* $1: updating with $3"
174 echo >&2 " $label_: $newshort_"
175 git-update-ref -m "$rloga: updating tag" "$1" "$2"
177 else
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)
188 if test "$local"
189 then
190 # Require fast-forward.
191 mb=$(git-merge-base "$local" "$2") &&
192 case "$2,$mb" in
193 $local,*)
194 if test -n "$verbose"
195 then
196 echo >&2 "* $1: same as $3"
197 echo >&2 " $label_: $newshort_"
200 *,$local)
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"
206 false
208 esac || {
209 case ",$force,$single_force," in
210 *,t,*)
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_"
218 exit 1
220 esac
222 else
223 echo >&2 "* $1: storing $3"
224 echo >&2 " $label_: $newshort_"
225 git-update-ref -m "$rloga: storing head" "$1" "$2"
228 esac
231 case "$update_head_ok" in
233 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
235 esac
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 "$@")
243 if test "$tags"
244 then
245 taglist=`IFS=' ' &&
246 echo "$ls_remote_result" |
247 while read sha1 name
249 case "$sha1" in
250 fail)
251 exit 1
252 esac
253 case "$name" in
254 *^*) continue ;;
255 refs/tags/*) ;;
256 *) continue ;;
257 esac
258 if git-check-ref-format "$name"
259 then
260 echo ".${name}:${name}"
261 else
262 echo >&2 "warning: tag ${name} ignored"
264 done` || exit
265 if test "$#" -gt 1
266 then
267 # remote URL plus explicit refspecs; we need to merge them.
268 reflist="$reflist$LF$taglist"
269 else
270 # No explicit refspecs; fetch tags only.
271 reflist=$taglist
275 fetch_main () {
276 reflist="$1"
277 refs=
278 rref=
280 for ref in $reflist
282 refs="$refs$LF$ref"
284 # These are relative path from $GIT_DIR, typically starting at refs/
285 # but may be HEAD
286 if expr "z$ref" : 'z\.' >/dev/null
287 then
288 not_for_merge=t
289 ref=$(expr "z$ref" : 'z\.\(.*\)')
290 else
291 not_for_merge=
293 if expr "z$ref" : 'z+' >/dev/null
294 then
295 single_force=t
296 ref=$(expr "z$ref" : 'z+\(.*\)')
297 else
298 single_force=
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...
306 case "$remote" in
307 http://* | https://* | ftp://*)
308 proto=`expr "$remote" : '\([^:]*\):'`
309 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
310 curl_extra_args="-k"
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.
318 head=$(
319 IFS=' '
320 echo "$ls_remote_result" |
321 while read sha1 name
323 test "z$name" = "z$remote_name" || continue
324 echo "$sha1"
325 break
326 done
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
333 rsync://*)
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)
337 rm -f "$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"
349 then
350 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
351 while read 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
357 done
358 rm -f "$GIT_DIR/TMP_ALT"
360 rsync_slurped_objects=t
364 # We will do git native transport with just one call later.
365 continue ;;
366 esac
368 append_fetch_head "$head" "$remote" \
369 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
371 done
373 case "$remote" in
374 http://* | https://* | ftp://* | rsync://* )
375 ;; # we are already done.
377 ( : subshell because we muck with IFS
378 pack_lockfile=
379 IFS=" $LF"
381 git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
383 while read sha1 remote_name
385 case "$sha1" in
386 failed)
387 echo >&2 "Fetch failure: $remote"
388 exit 1 ;;
389 # special line coming from index-pack with the pack name
390 pack)
391 continue ;;
392 keep)
393 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
394 continue ;;
395 esac
396 found=
397 single_force=
398 for ref in $refs
400 case "$ref" in
401 +$remote_name:*)
402 single_force=t
403 not_for_merge=
404 found="$ref"
405 break ;;
406 .+$remote_name:*)
407 single_force=t
408 not_for_merge=t
409 found="$ref"
410 break ;;
411 .$remote_name:*)
412 not_for_merge=t
413 found="$ref"
414 break ;;
415 $remote_name:*)
416 not_for_merge=
417 found="$ref"
418 break ;;
419 esac
420 done
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
425 done &&
426 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
427 ) || exit ;;
428 esac
432 fetch_main "$reflist" || exit
434 # automated tag following
435 case "$no_tags$tags" in
437 case "$reflist" in
438 *:refs/*)
439 # effective only when we are following remote branch
440 # using local tracking branch.
441 taglist=$(IFS=' ' &&
442 echo "$ls_remote_result" |
443 git-show-ref --exclude-existing=refs/tags/ |
444 while read sha1 name
446 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
447 echo >&2 "Auto-following $name"
448 echo ".${name}:${name}"
449 done)
450 esac
451 case "$taglist" in
452 '') ;;
454 fetch_main "$taglist" || exit ;;
455 esac
456 esac
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.
460 case "$orig_head" in
464 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
465 if test "$curr_head" != "$orig_head"
466 then
467 git-update-ref \
468 -m "$rloga: Undoing incorrectly fetched HEAD." \
469 HEAD "$orig_head"
470 die "Cannot fetch into the current branch."
473 esac