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