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