allow deepening of a shallow repository
[git/git.git] / git-fetch.sh
CommitLineData
7ef76925
LT
1#!/bin/sh
2#
87358b7a
FK
3
4USAGE='<fetch-options> <repository> <refspec>...'
ae2b0f15 5. git-sh-setup
215a7ad1 6. git-parse-remote
853a3697
JH
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"
9
221e743c
JH
10LF='
11'
12IFS="$LF"
13
55b7835e 14rloga=fetch
03febf99 15no_tags=
ed1aadf1 16tags=
853a3697 17append=
ae2da406 18force=
583122cd 19verbose=
b10ac50f 20update_head_ok=
2c620a1a 21exec=
96b086d6 22upload_pack=
920ccbfc 23keep=
f53514bc 24shallow_depth=
ae2da406
JH
25while case "$#" in 0) break ;; esac
26do
27 case "$1" in
28 -a|--a|--ap|--app|--appe|--appen|--append)
29 append=t
ae2da406 30 ;;
2796a9de
JH
31 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
32 --upload-pa|--upload-pac|--upload-pack)
2c620a1a
MO
33 shift
34 exec="--exec=$1"
96b086d6 35 upload_pack="-u $1"
2c620a1a 36 ;;
ae2da406
JH
37 -f|--f|--fo|--for|--forc|--force)
38 force=t
b10ac50f 39 ;;
ed1aadf1
LT
40 -t|--t|--ta|--tag|--tags)
41 tags=t
42 ;;
03febf99
JH
43 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
44 no_tags=t
45 ;;
b10ac50f
JH
46 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
47 --update-he|--update-hea|--update-head|--update-head-|\
48 --update-head-o|--update-head-ok)
49 update_head_ok=t
ae2da406 50 ;;
583122cd
LT
51 -v|--verbose)
52 verbose=Yes
53 ;;
0f76f526 54 -k|--k|--ke|--kee|--keep)
576162a4 55 keep='-k -k'
0f76f526 56 ;;
55b7835e
SP
57 --reflog-action=*)
58 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
59 ;;
f53514bc
JS
60 --depth=*)
61 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
62 ;;
63 --depth)
64 shift
65 shallow_depth="--depth=$1"
66 ;;
87358b7a
FK
67 -*)
68 usage
69 ;;
ae2da406
JH
70 *)
71 break
72 ;;
73 esac
b10ac50f 74 shift
ae2da406
JH
75done
76
853a3697
JH
77case "$#" in
780)
648ad18f
SB
79 origin=$(get_default_remote)
80 test -n "$(get_remote_url ${origin})" ||
81 die "Where do you want to fetch from today?"
82 set x $origin ; shift ;;
853a3697 83esac
ae2da406 84
853a3697
JH
85remote_nick="$1"
86remote=$(get_remote_url "$@")
87refs=
88rref=
89rsync_slurped_objects=
90
55b7835e 91rloga="$rloga $remote_nick"
ed0e078f 92test "$remote_nick" = "$remote" || rloga="$rloga $remote"
55b7835e 93
853a3697
JH
94if test "" = "$append"
95then
df34297a 96 : >"$GIT_DIR/FETCH_HEAD"
853a3697
JH
97fi
98
99append_fetch_head () {
100 head_="$1"
101 remote_="$2"
102 remote_name_="$3"
103 remote_nick_="$4"
104 local_name_="$5"
05dd8e2e
JH
105 case "$6" in
106 t) not_for_merge_='not-for-merge' ;;
107 '') not_for_merge_= ;;
108 esac
853a3697 109
06ec2b4b
JH
110 # remote-nick is the URL given on the command line (or a shorthand)
111 # remote-name is the $GIT_DIR relative refs/ path we computed
112 # for this refspec.
687b8be8
EW
113
114 # the $note_ variable will be fed to git-fmt-merge-msg for further
115 # processing.
06ec2b4b
JH
116 case "$remote_name_" in
117 HEAD)
118 note_= ;;
119 refs/heads/*)
120 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
b91fb518 121 note_="branch '$note_' of " ;;
06ec2b4b
JH
122 refs/tags/*)
123 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
b91fb518 124 note_="tag '$note_' of " ;;
687b8be8
EW
125 refs/remotes/*)
126 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
127 note_="remote branch '$note_' of " ;;
06ec2b4b 128 *)
b91fb518 129 note_="$remote_name of " ;;
06ec2b4b 130 esac
f327dbce 131 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
b91fb518
JH
132 remote_="$remote_1_"
133 note_="$note_$remote_"
06ec2b4b 134
853a3697
JH
135 # 2.6.11-tree tag would not be happy to be fed to resolve.
136 if git-cat-file commit "$head_" >/dev/null 2>&1
137 then
8572aa85 138 headc_=$(git-rev-parse --verify "$head_^0") || exit
df34297a 139 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
853a3697 140 else
df34297a 141 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
853a3697 142 fi
20a3847d
SB
143
144 update_local_ref "$local_name_" "$head_" "$note_"
853a3697
JH
145}
146
20a3847d
SB
147update_local_ref () {
148 # If we are storing the head locally make sure that it is
149 # a fast forward (aka "reverse push").
150
151 label_=$(git-cat-file -t $2)
152 newshort_=$(git-rev-parse --short $2)
153 if test -z "$1" ; then
154 [ "$verbose" ] && echo >&2 "* fetched $3"
155 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
156 return 0
157 fi
fbc72799
JH
158 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
159
853a3697
JH
160 case "$1" in
161 refs/tags/*)
162 # Tags need not be pointing at commits so there
163 # is no way to guarantee "fast-forward" anyway.
fbc72799 164 if test -n "$oldshort_"
ae2da406 165 then
fbc72799 166 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
f8765797 167 then
20a3847d
SB
168 [ "$verbose" ] && echo >&2 "* $1: same as $3"
169 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
f8765797
JH
170 else
171 echo >&2 "* $1: updating with $3"
20a3847d 172 echo >&2 " $label_: $newshort_"
55b7835e 173 git-update-ref -m "$rloga: updating tag" "$1" "$2"
f8765797 174 fi
ae2da406 175 else
06ec2b4b 176 echo >&2 "* $1: storing $3"
20a3847d 177 echo >&2 " $label_: $newshort_"
55b7835e 178 git-update-ref -m "$rloga: storing tag" "$1" "$2"
ae2da406 179 fi
bf7960eb 180 ;;
ae2da406 181
687b8be8 182 refs/heads/* | refs/remotes/*)
bf7960eb
JH
183 # $1 is the ref being updated.
184 # $2 is the new value for the ref.
185 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
186 if test "$local"
33b83034 187 then
bf7960eb 188 # Require fast-forward.
853a3697
JH
189 mb=$(git-merge-base "$local" "$2") &&
190 case "$2,$mb" in
191 $local,*)
ea5aeb07
JH
192 if test -n "$verbose"
193 then
194 echo >&2 "* $1: same as $3"
20a3847d 195 echo >&2 " $label_: $newshort_"
ea5aeb07 196 fi
853a3697
JH
197 ;;
198 *,$local)
06ec2b4b 199 echo >&2 "* $1: fast forward to $3"
20a3847d 200 echo >&2 " old..new: $oldshort_..$newshort_"
55b7835e 201 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
853a3697
JH
202 ;;
203 *)
204 false
205 ;;
206 esac || {
bf7960eb
JH
207 case ",$force,$single_force," in
208 *,t,*)
20a3847d
SB
209 echo >&2 "* $1: forcing update to non-fast forward $3"
210 echo >&2 " old...new: $oldshort_...$newshort_"
55b7835e 211 git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
ae2da406
JH
212 ;;
213 *)
20a3847d
SB
214 echo >&2 "* $1: not updating to non-fast forward $3"
215 echo >&2 " old...new: $oldshort_...$newshort_"
a9698bb2 216 exit 1
ae2da406
JH
217 ;;
218 esac
853a3697
JH
219 }
220 else
bf7960eb 221 echo >&2 "* $1: storing $3"
20a3847d 222 echo >&2 " $label_: $newshort_"
55b7835e 223 git-update-ref -m "$rloga: storing head" "$1" "$2"
33b83034 224 fi
0a623e7c 225 ;;
853a3697
JH
226 esac
227}
228
b10ac50f
JH
229case "$update_head_ok" in
230'')
bf7960eb 231 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
232 ;;
233esac
234
ed1aadf1
LT
235# If --tags (and later --heads or --all) is specified, then we are
236# not talking about defaults stored in Pull: line of remotes or
237# branches file, and just fetch those and refspecs explicitly given.
238# Otherwise we do what we always did.
239
240reflist=$(get_remote_refs_for_fetch "$@")
241if test "$tags"
242then
878ccb26 243 taglist=`IFS=" " &&
57a39690
JH
244 (
245 git-ls-remote $upload_pack --tags "$remote" ||
246 echo fail ouch
247 ) |
81214e4d
JH
248 while read sha1 name
249 do
57a39690
JH
250 case "$sha1" in
251 fail)
252 exit 1
253 esac
81214e4d 254 case "$name" in
878ccb26 255 *^*) continue ;;
81214e4d
JH
256 esac
257 if git-check-ref-format "$name"
258 then
259 echo ".${name}:${name}"
260 else
261 echo >&2 "warning: tag ${name} ignored"
262 fi
57a39690 263 done` || exit
ed1aadf1
LT
264 if test "$#" -gt 1
265 then
266 # remote URL plus explicit refspecs; we need to merge them.
221e743c 267 reflist="$reflist$LF$taglist"
ed1aadf1
LT
268 else
269 # No explicit refspecs; fetch tags only.
270 reflist=$taglist
271 fi
272fi
273
03febf99
JH
274fetch_main () {
275 reflist="$1"
276 refs=
4839bd8a 277 rref=
f170e4b3 278
03febf99
JH
279 for ref in $reflist
280 do
281 refs="$refs$LF$ref"
f170e4b3 282
03febf99
JH
283 # These are relative path from $GIT_DIR, typically starting at refs/
284 # but may be HEAD
f327dbce 285 if expr "z$ref" : 'z\.' >/dev/null
03febf99
JH
286 then
287 not_for_merge=t
f327dbce 288 ref=$(expr "z$ref" : 'z\.\(.*\)')
03febf99
JH
289 else
290 not_for_merge=
291 fi
dfdcb558 292 if expr "z$ref" : 'z+' >/dev/null
03febf99
JH
293 then
294 single_force=t
dfdcb558 295 ref=$(expr "z$ref" : 'z+\(.*\)')
03febf99
JH
296 else
297 single_force=
298 fi
f327dbce
MW
299 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
300 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
853a3697 301
03febf99 302 rref="$rref$LF$remote_name"
4447badc 303
03febf99
JH
304 # There are transports that can fetch only one head at a time...
305 case "$remote" in
38529e28 306 http://* | https://* | ftp://*)
f53514bc
JS
307 test -n "$shallow_depth" &&
308 die "shallow clone with http not supported"
ddaf7314 309 proto=`expr "$remote" : '\([^:]*\):'`
03febf99
JH
310 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
311 curl_extra_args="-k"
312 fi
3ea099d4
SK
313 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
314 "`git-repo-config --bool http.noEPSV`" = true ]; then
315 noepsv_opt="--disable-epsv"
316 fi
093b0688
NH
317 max_depth=5
318 depth=0
319 head="ref: $remote_name"
320 while (expr "z$head" : "zref:" && expr $depth \< $max_depth) >/dev/null
321 do
d9bffc08 322 remote_name_quoted=$(@@PERL@@ -e '
03febf99 323 my $u = $ARGV[0];
093b0688 324 $u =~ s/^ref:\s*//;
03febf99
JH
325 $u =~ s{([^-a-zA-Z0-9/.])}{sprintf"%%%02x",ord($1)}eg;
326 print "$u";
093b0688 327 ' "$head")
3ea099d4 328 head=$(curl -nsfL $curl_extra_args $noepsv_opt "$remote/$remote_name_quoted")
093b0688
NH
329 depth=$( expr \( $depth + 1 \) )
330 done
f327dbce 331 expr "z$head" : "z$_x40\$" >/dev/null ||
093b0688 332 die "Failed to fetch $remote_name from $remote"
ddaf7314 333 echo >&2 "Fetching $remote_name from $remote using $proto"
03febf99
JH
334 git-http-fetch -v -a "$head" "$remote/" || exit
335 ;;
336 rsync://*)
f53514bc
JS
337 test -n "$shallow_depth" &&
338 die "shallow clone with rsync not supported"
03febf99
JH
339 TMP_HEAD="$GIT_DIR/TMP_HEAD"
340 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
341 head=$(git-rev-parse --verify TMP_HEAD)
342 rm -f "$TMP_HEAD"
343 test "$rsync_slurped_objects" || {
344 rsync -av --ignore-existing --exclude info \
345 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
853a3697 346
03febf99
JH
347 # Look at objects/info/alternates for rsync -- http will
348 # support it natively and git native ones will do it on
349 # the remote end. Not having that file is not a crime.
350 rsync -q "$remote/objects/info/alternates" \
351 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
352 rm -f "$GIT_DIR/TMP_ALT"
353 if test -f "$GIT_DIR/TMP_ALT"
354 then
355 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
356 while read alt
357 do
358 case "$alt" in 'bad alternate: '*) die "$alt";; esac
359 echo >&2 "Getting alternate: $alt"
360 rsync -av --ignore-existing --exclude info \
361 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
362 done
363 rm -f "$GIT_DIR/TMP_ALT"
364 fi
365 rsync_slurped_objects=t
366 }
367 ;;
368 *)
369 # We will do git native transport with just one call later.
370 continue ;;
371 esac
f170e4b3 372
03febf99
JH
373 append_fetch_head "$head" "$remote" \
374 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
853a3697 375
03febf99
JH
376 done
377
378 case "$remote" in
38529e28 379 http://* | https://* | ftp://* | rsync://* )
03febf99
JH
380 ;; # we are already done.
381 *)
382 ( : subshell because we muck with IFS
576162a4 383 pack_lockfile=
03febf99
JH
384 IFS=" $LF"
385 (
f53514bc 386 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
03febf99
JH
387 ) |
388 while read sha1 remote_name
389 do
390 case "$sha1" in
391 failed)
392 echo >&2 "Fetch failure: $remote"
393 exit 1 ;;
576162a4
NP
394 # special line coming from index-pack with the pack name
395 pack)
396 continue ;;
397 keep)
398 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
399 continue ;;
03febf99
JH
400 esac
401 found=
402 single_force=
403 for ref in $refs
404 do
405 case "$ref" in
406 +$remote_name:*)
407 single_force=t
408 not_for_merge=
409 found="$ref"
410 break ;;
411 .+$remote_name:*)
412 single_force=t
413 not_for_merge=t
414 found="$ref"
415 break ;;
416 .$remote_name:*)
417 not_for_merge=t
418 found="$ref"
419 break ;;
420 $remote_name:*)
421 not_for_merge=
422 found="$ref"
423 break ;;
424 esac
425 done
f327dbce 426 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
03febf99
JH
427 append_fetch_head "$sha1" "$remote" \
428 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
429 done
576162a4 430 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
03febf99
JH
431 ) || exit ;;
432 esac
433
434}
435
436fetch_main "$reflist"
437
438# automated tag following
439case "$no_tags$tags" in
440'')
6dc78e69
JH
441 case "$reflist" in
442 *:refs/*)
443 # effective only when we are following remote branch
444 # using local tracking branch.
445 taglist=$(IFS=" " &&
446 git-ls-remote $upload_pack --tags "$remote" |
6c96c0f1
JH
447 sed -n -e 's|^\('"$_x40"'\) \(refs/tags/.*\)^{}$|\1 \2|p' \
448 -e 's|^\('"$_x40"'\) \(refs/tags/.*\)$|\1 \2|p' |
6dc78e69
JH
449 while read sha1 name
450 do
6c96c0f1 451 git-show-ref --verify --quiet -- "$name" && continue
6dc78e69
JH
452 git-check-ref-format "$name" || {
453 echo >&2 "warning: tag ${name} ignored"
454 continue
455 }
456 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
457 echo >&2 "Auto-following $name"
458 echo ".${name}:${name}"
459 done)
460 esac
03febf99
JH
461 case "$taglist" in
462 '') ;;
463 ?*)
464 fetch_main "$taglist" ;;
465 esac
853a3697 466esac
b10ac50f
JH
467
468# If the original head was empty (i.e. no "master" yet), or
469# if we were told not to worry, we do not have to check.
9861718b
JH
470case "$orig_head" in
471'')
b10ac50f 472 ;;
9861718b 473?*)
bf7960eb 474 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
475 if test "$curr_head" != "$orig_head"
476 then
55b7835e
SP
477 git-update-ref \
478 -m "$rloga: Undoing incorrectly fetched HEAD." \
479 HEAD "$orig_head"
b10ac50f
JH
480 die "Cannot fetch into the current branch."
481 fi
482 ;;
483esac