Sample update hook: typofix and modernization to use "git log"
[git/kusma.git] / git-fetch.sh
blob59bee5db0f0738cd6d43772e7279edf726a0b7d1
1 #!/bin/sh
4 USAGE='<fetch-options> <repository> <refspec>...'
5 SUBDIRECTORY_OK=Yes
6 . git-sh-setup
7 set_reflog_action "fetch $*"
8 cd_to_toplevel ;# probably unnecessary...
10 . git-parse-remote
11 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
12 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
14 LF='
16 IFS="$LF"
18 no_tags=
19 tags=
20 append=
21 force=
22 verbose=
23 update_head_ok=
24 exec=
25 keep=
26 shallow_depth=
27 while case "$#" in 0) break ;; esac
29 case "$1" in
30 -a|--a|--ap|--app|--appe|--appen|--append)
31 append=t
33 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
34 --upload-pa|--upload-pac|--upload-pack)
35 shift
36 exec="--upload-pack=$1"
38 --upl=*|--uplo=*|--uploa=*|--upload=*|\
39 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
40 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
41 shift
43 -f|--f|--fo|--for|--forc|--force)
44 force=t
46 -t|--t|--ta|--tag|--tags)
47 tags=t
49 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
50 no_tags=t
52 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
53 --update-he|--update-hea|--update-head|--update-head-|\
54 --update-head-o|--update-head-ok)
55 update_head_ok=t
57 -v|--verbose)
58 verbose=Yes
60 -k|--k|--ke|--kee|--keep)
61 keep='-k -k'
63 --depth=*)
64 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
66 --depth)
67 shift
68 shallow_depth="--depth=$1"
70 -*)
71 usage
74 break
76 esac
77 shift
78 done
80 case "$#" in
82 origin=$(get_default_remote)
83 test -n "$(get_remote_url ${origin})" ||
84 die "Where do you want to fetch from today?"
85 set x $origin ; shift ;;
86 esac
88 if test -z "$exec"
89 then
90 # No command line override and we have configuration for the remote.
91 exec="--upload-pack=$(get_uploadpack $1)"
94 remote_nick="$1"
95 remote=$(get_remote_url "$@")
96 refs=
97 rref=
98 rsync_slurped_objects=
100 if test "" = "$append"
101 then
102 : >"$GIT_DIR/FETCH_HEAD"
105 # Global that is reused later
106 ls_remote_result=$(git ls-remote $exec "$remote") ||
107 die "Cannot get the repository state from $remote"
109 append_fetch_head () {
110 head_="$1"
111 remote_="$2"
112 remote_name_="$3"
113 remote_nick_="$4"
114 local_name_="$5"
115 case "$6" in
116 t) not_for_merge_='not-for-merge' ;;
117 '') not_for_merge_= ;;
118 esac
120 # remote-nick is the URL given on the command line (or a shorthand)
121 # remote-name is the $GIT_DIR relative refs/ path we computed
122 # for this refspec.
124 # the $note_ variable will be fed to git-fmt-merge-msg for further
125 # processing.
126 case "$remote_name_" in
127 HEAD)
128 note_= ;;
129 refs/heads/*)
130 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
131 note_="branch '$note_' of " ;;
132 refs/tags/*)
133 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
134 note_="tag '$note_' of " ;;
135 refs/remotes/*)
136 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
137 note_="remote branch '$note_' of " ;;
139 note_="$remote_name of " ;;
140 esac
141 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
142 remote_="$remote_1_"
143 note_="$note_$remote_"
145 # 2.6.11-tree tag would not be happy to be fed to resolve.
146 if git-cat-file commit "$head_" >/dev/null 2>&1
147 then
148 headc_=$(git-rev-parse --verify "$head_^0") || exit
149 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
150 else
151 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
154 update_local_ref "$local_name_" "$head_" "$note_"
157 update_local_ref () {
158 # If we are storing the head locally make sure that it is
159 # a fast forward (aka "reverse push").
161 label_=$(git-cat-file -t $2)
162 newshort_=$(git-rev-parse --short $2)
163 if test -z "$1" ; then
164 [ "$verbose" ] && echo >&2 "* fetched $3"
165 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
166 return 0
168 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
170 case "$1" in
171 refs/tags/*)
172 # Tags need not be pointing at commits so there
173 # is no way to guarantee "fast-forward" anyway.
174 if test -n "$oldshort_"
175 then
176 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
177 then
178 [ "$verbose" ] && echo >&2 "* $1: same as $3"
179 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
180 else
181 echo >&2 "* $1: updating with $3"
182 echo >&2 " $label_: $newshort_"
183 git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
185 else
186 echo >&2 "* $1: storing $3"
187 echo >&2 " $label_: $newshort_"
188 git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
192 refs/heads/* | refs/remotes/*)
193 # $1 is the ref being updated.
194 # $2 is the new value for the ref.
195 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
196 if test "$local"
197 then
198 # Require fast-forward.
199 mb=$(git-merge-base "$local" "$2") &&
200 case "$2,$mb" in
201 $local,*)
202 if test -n "$verbose"
203 then
204 echo >&2 "* $1: same as $3"
205 echo >&2 " $label_: $newshort_"
208 *,$local)
209 echo >&2 "* $1: fast forward to $3"
210 echo >&2 " old..new: $oldshort_..$newshort_"
211 git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
214 false
216 esac || {
217 case ",$force,$single_force," in
218 *,t,*)
219 echo >&2 "* $1: forcing update to non-fast forward $3"
220 echo >&2 " old...new: $oldshort_...$newshort_"
221 git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
224 echo >&2 "* $1: not updating to non-fast forward $3"
225 echo >&2 " old...new: $oldshort_...$newshort_"
226 exit 1
228 esac
230 else
231 echo >&2 "* $1: storing $3"
232 echo >&2 " $label_: $newshort_"
233 git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
236 esac
239 # updating the current HEAD with git-fetch in a bare
240 # repository is always fine.
241 if test -z "$update_head_ok" && test $(is_bare_repository) = false
242 then
243 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
246 # Allow --notags from remote.$1.tagopt
247 case "$tags$no_tags" in
249 case "$(git-config --get "remote.$1.tagopt")" in
250 --no-tags)
251 no_tags=t ;;
252 esac
253 esac
255 # If --tags (and later --heads or --all) is specified, then we are
256 # not talking about defaults stored in Pull: line of remotes or
257 # branches file, and just fetch those and refspecs explicitly given.
258 # Otherwise we do what we always did.
260 reflist=$(get_remote_refs_for_fetch "$@")
261 if test "$tags"
262 then
263 taglist=`IFS=' ' &&
264 echo "$ls_remote_result" |
265 git-show-ref --exclude-existing=refs/tags/ |
266 while read sha1 name
268 echo ".${name}:${name}"
269 done` || exit
270 if test "$#" -gt 1
271 then
272 # remote URL plus explicit refspecs; we need to merge them.
273 reflist="$reflist$LF$taglist"
274 else
275 # No explicit refspecs; fetch tags only.
276 reflist=$taglist
280 fetch_main () {
281 reflist="$1"
282 refs=
283 rref=
285 for ref in $reflist
287 refs="$refs$LF$ref"
289 # These are relative path from $GIT_DIR, typically starting at refs/
290 # but may be HEAD
291 if expr "z$ref" : 'z\.' >/dev/null
292 then
293 not_for_merge=t
294 ref=$(expr "z$ref" : 'z\.\(.*\)')
295 else
296 not_for_merge=
298 if expr "z$ref" : 'z+' >/dev/null
299 then
300 single_force=t
301 ref=$(expr "z$ref" : 'z+\(.*\)')
302 else
303 single_force=
305 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
306 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
308 rref="$rref$LF$remote_name"
310 # There are transports that can fetch only one head at a time...
311 case "$remote" in
312 http://* | https://* | ftp://*)
313 test -n "$shallow_depth" &&
314 die "shallow clone with http not supported"
315 proto=`expr "$remote" : '\([^:]*\):'`
316 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
317 curl_extra_args="-k"
319 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
320 "`git-config --bool http.noEPSV`" = true ]; then
321 noepsv_opt="--disable-epsv"
324 # Find $remote_name from ls-remote output.
325 head=$(
326 IFS=' '
327 echo "$ls_remote_result" |
328 while read sha1 name
330 test "z$name" = "z$remote_name" || continue
331 echo "$sha1"
332 break
333 done
335 expr "z$head" : "z$_x40\$" >/dev/null ||
336 die "No such ref $remote_name at $remote"
337 echo >&2 "Fetching $remote_name from $remote using $proto"
338 git-http-fetch -v -a "$head" "$remote/" || exit
340 rsync://*)
341 test -n "$shallow_depth" &&
342 die "shallow clone with rsync not supported"
343 TMP_HEAD="$GIT_DIR/TMP_HEAD"
344 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
345 head=$(git-rev-parse --verify TMP_HEAD)
346 rm -f "$TMP_HEAD"
347 test "$rsync_slurped_objects" || {
348 rsync -av --ignore-existing --exclude info \
349 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
351 # Look at objects/info/alternates for rsync -- http will
352 # support it natively and git native ones will do it on
353 # the remote end. Not having that file is not a crime.
354 rsync -q "$remote/objects/info/alternates" \
355 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
356 rm -f "$GIT_DIR/TMP_ALT"
357 if test -f "$GIT_DIR/TMP_ALT"
358 then
359 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
360 while read alt
362 case "$alt" in 'bad alternate: '*) die "$alt";; esac
363 echo >&2 "Getting alternate: $alt"
364 rsync -av --ignore-existing --exclude info \
365 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
366 done
367 rm -f "$GIT_DIR/TMP_ALT"
369 rsync_slurped_objects=t
373 # We will do git native transport with just one call later.
374 continue ;;
375 esac
377 append_fetch_head "$head" "$remote" \
378 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
380 done
382 case "$remote" in
383 http://* | https://* | ftp://* | rsync://* )
384 ;; # we are already done.
386 ( : subshell because we muck with IFS
387 IFS=" $LF"
389 if test -f "$remote" ; then
390 test -n "$shallow_depth" &&
391 die "shallow clone with bundle is not supported"
392 git-bundle unbundle "$remote" $rref ||
393 echo failed "$remote"
394 else
395 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
396 echo failed "$remote"
400 trap '
401 if test -n "$keepfile" && test -f "$keepfile"
402 then
403 rm -f "$keepfile"
407 keepfile=
408 while read sha1 remote_name
410 case "$sha1" in
411 failed)
412 echo >&2 "Fetch failure: $remote"
413 exit 1 ;;
414 # special line coming from index-pack with the pack name
415 pack)
416 continue ;;
417 keep)
418 keepfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
419 continue ;;
420 esac
421 found=
422 single_force=
423 for ref in $refs
425 case "$ref" in
426 +$remote_name:*)
427 single_force=t
428 not_for_merge=
429 found="$ref"
430 break ;;
431 .+$remote_name:*)
432 single_force=t
433 not_for_merge=t
434 found="$ref"
435 break ;;
436 .$remote_name:*)
437 not_for_merge=t
438 found="$ref"
439 break ;;
440 $remote_name:*)
441 not_for_merge=
442 found="$ref"
443 break ;;
444 esac
445 done
446 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
447 append_fetch_head "$sha1" "$remote" \
448 "$remote_name" "$remote_nick" "$local_name" \
449 "$not_for_merge" || exit
450 done
452 ) || exit ;;
453 esac
457 fetch_main "$reflist" || exit
459 # automated tag following
460 case "$no_tags$tags" in
462 case "$reflist" in
463 *:refs/*)
464 # effective only when we are following remote branch
465 # using local tracking branch.
466 taglist=$(IFS=' ' &&
467 echo "$ls_remote_result" |
468 git-show-ref --exclude-existing=refs/tags/ |
469 while read sha1 name
471 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
472 echo >&2 "Auto-following $name"
473 echo ".${name}:${name}"
474 done)
475 esac
476 case "$taglist" in
477 '') ;;
479 # do not deepen a shallow tree when following tags
480 shallow_depth=
481 fetch_main "$taglist" || exit ;;
482 esac
483 esac
485 # If the original head was empty (i.e. no "master" yet), or
486 # if we were told not to worry, we do not have to check.
487 case "$orig_head" in
491 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
492 if test "$curr_head" != "$orig_head"
493 then
494 git-update-ref \
495 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
496 HEAD "$orig_head"
497 die "Cannot fetch into the current branch."
500 esac