]> git.ipfire.org Git - thirdparty/git.git/blame - git-fetch.sh
Revert "Make left-right automatic."
[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
4da90285
JH
7TOP=$(git-rev-parse --show-cdup)
8if test ! -z "$TOP"
9then
10 cd "$TOP"
11fi
215a7ad1 12. git-parse-remote
853a3697
JH
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"
15
221e743c
JH
16LF='
17'
18IFS="$LF"
19
55b7835e 20rloga=fetch
03febf99 21no_tags=
ed1aadf1 22tags=
853a3697 23append=
ae2da406 24force=
583122cd 25verbose=
b10ac50f 26update_head_ok=
2c620a1a 27exec=
96b086d6 28upload_pack=
920ccbfc 29keep=
ae2da406
JH
30while case "$#" in 0) break ;; esac
31do
32 case "$1" in
33 -a|--a|--ap|--app|--appe|--appen|--append)
34 append=t
ae2da406 35 ;;
2796a9de
JH
36 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
37 --upload-pa|--upload-pac|--upload-pack)
2c620a1a
MO
38 shift
39 exec="--exec=$1"
96b086d6 40 upload_pack="-u $1"
2c620a1a 41 ;;
ae2da406
JH
42 -f|--f|--fo|--for|--forc|--force)
43 force=t
b10ac50f 44 ;;
ed1aadf1
LT
45 -t|--t|--ta|--tag|--tags)
46 tags=t
47 ;;
03febf99
JH
48 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
49 no_tags=t
50 ;;
b10ac50f
JH
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
ae2da406 55 ;;
583122cd
LT
56 -v|--verbose)
57 verbose=Yes
58 ;;
0f76f526 59 -k|--k|--ke|--kee|--keep)
576162a4 60 keep='-k -k'
0f76f526 61 ;;
55b7835e
SP
62 --reflog-action=*)
63 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
64 ;;
87358b7a
FK
65 -*)
66 usage
67 ;;
ae2da406
JH
68 *)
69 break
70 ;;
71 esac
b10ac50f 72 shift
ae2da406
JH
73done
74
853a3697
JH
75case "$#" in
760)
648ad18f
SB
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 ;;
853a3697 81esac
ae2da406 82
853a3697
JH
83remote_nick="$1"
84remote=$(get_remote_url "$@")
85refs=
86rref=
87rsync_slurped_objects=
88
55b7835e 89rloga="$rloga $remote_nick"
ed0e078f 90test "$remote_nick" = "$remote" || rloga="$rloga $remote"
55b7835e 91
853a3697
JH
92if test "" = "$append"
93then
df34297a 94 : >"$GIT_DIR/FETCH_HEAD"
853a3697
JH
95fi
96
28b8e61f
JH
97# Global that is reused later
98ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
99 die "Cannot find the reflist at $remote"
100
853a3697
JH
101append_fetch_head () {
102 head_="$1"
103 remote_="$2"
104 remote_name_="$3"
105 remote_nick_="$4"
106 local_name_="$5"
05dd8e2e
JH
107 case "$6" in
108 t) not_for_merge_='not-for-merge' ;;
109 '') not_for_merge_= ;;
110 esac
853a3697 111
06ec2b4b
JH
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.
687b8be8
EW
115
116 # the $note_ variable will be fed to git-fmt-merge-msg for further
117 # processing.
06ec2b4b
JH
118 case "$remote_name_" in
119 HEAD)
120 note_= ;;
121 refs/heads/*)
122 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
b91fb518 123 note_="branch '$note_' of " ;;
06ec2b4b
JH
124 refs/tags/*)
125 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
b91fb518 126 note_="tag '$note_' of " ;;
687b8be8
EW
127 refs/remotes/*)
128 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
129 note_="remote branch '$note_' of " ;;
06ec2b4b 130 *)
b91fb518 131 note_="$remote_name of " ;;
06ec2b4b 132 esac
f327dbce 133 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
b91fb518
JH
134 remote_="$remote_1_"
135 note_="$note_$remote_"
06ec2b4b 136
853a3697
JH
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
8572aa85 140 headc_=$(git-rev-parse --verify "$head_^0") || exit
df34297a 141 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
853a3697 142 else
df34297a 143 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
853a3697 144 fi
20a3847d
SB
145
146 update_local_ref "$local_name_" "$head_" "$note_"
853a3697
JH
147}
148
20a3847d
SB
149update_local_ref () {
150 # If we are storing the head locally make sure that it is
151 # a fast forward (aka "reverse push").
152
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
159 fi
fbc72799
JH
160 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
161
853a3697
JH
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.
fbc72799 166 if test -n "$oldshort_"
ae2da406 167 then
fbc72799 168 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
f8765797 169 then
20a3847d
SB
170 [ "$verbose" ] && echo >&2 "* $1: same as $3"
171 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
f8765797
JH
172 else
173 echo >&2 "* $1: updating with $3"
20a3847d 174 echo >&2 " $label_: $newshort_"
55b7835e 175 git-update-ref -m "$rloga: updating tag" "$1" "$2"
f8765797 176 fi
ae2da406 177 else
06ec2b4b 178 echo >&2 "* $1: storing $3"
20a3847d 179 echo >&2 " $label_: $newshort_"
55b7835e 180 git-update-ref -m "$rloga: storing tag" "$1" "$2"
ae2da406 181 fi
bf7960eb 182 ;;
ae2da406 183
687b8be8 184 refs/heads/* | refs/remotes/*)
bf7960eb
JH
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"
33b83034 189 then
bf7960eb 190 # Require fast-forward.
853a3697
JH
191 mb=$(git-merge-base "$local" "$2") &&
192 case "$2,$mb" in
193 $local,*)
ea5aeb07
JH
194 if test -n "$verbose"
195 then
196 echo >&2 "* $1: same as $3"
20a3847d 197 echo >&2 " $label_: $newshort_"
ea5aeb07 198 fi
853a3697
JH
199 ;;
200 *,$local)
06ec2b4b 201 echo >&2 "* $1: fast forward to $3"
20a3847d 202 echo >&2 " old..new: $oldshort_..$newshort_"
55b7835e 203 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
853a3697
JH
204 ;;
205 *)
206 false
207 ;;
208 esac || {
bf7960eb
JH
209 case ",$force,$single_force," in
210 *,t,*)
20a3847d
SB
211 echo >&2 "* $1: forcing update to non-fast forward $3"
212 echo >&2 " old...new: $oldshort_...$newshort_"
55b7835e 213 git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
ae2da406
JH
214 ;;
215 *)
20a3847d
SB
216 echo >&2 "* $1: not updating to non-fast forward $3"
217 echo >&2 " old...new: $oldshort_...$newshort_"
a9698bb2 218 exit 1
ae2da406
JH
219 ;;
220 esac
853a3697
JH
221 }
222 else
bf7960eb 223 echo >&2 "* $1: storing $3"
20a3847d 224 echo >&2 " $label_: $newshort_"
55b7835e 225 git-update-ref -m "$rloga: storing head" "$1" "$2"
33b83034 226 fi
0a623e7c 227 ;;
853a3697
JH
228 esac
229}
230
b10ac50f
JH
231case "$update_head_ok" in
232'')
bf7960eb 233 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
234 ;;
235esac
236
ed1aadf1
LT
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.
241
242reflist=$(get_remote_refs_for_fetch "$@")
243if test "$tags"
244then
878ccb26 245 taglist=`IFS=" " &&
28b8e61f 246 echo "$ls_remote_result" |
81214e4d
JH
247 while read sha1 name
248 do
57a39690
JH
249 case "$sha1" in
250 fail)
251 exit 1
252 esac
81214e4d 253 case "$name" in
878ccb26 254 *^*) continue ;;
28b8e61f
JH
255 refs/tags/*) ;;
256 *) continue ;;
81214e4d
JH
257 esac
258 if git-check-ref-format "$name"
259 then
260 echo ".${name}:${name}"
261 else
262 echo >&2 "warning: tag ${name} ignored"
263 fi
57a39690 264 done` || exit
ed1aadf1
LT
265 if test "$#" -gt 1
266 then
267 # remote URL plus explicit refspecs; we need to merge them.
221e743c 268 reflist="$reflist$LF$taglist"
ed1aadf1
LT
269 else
270 # No explicit refspecs; fetch tags only.
271 reflist=$taglist
272 fi
273fi
274
03febf99
JH
275fetch_main () {
276 reflist="$1"
277 refs=
4839bd8a 278 rref=
f170e4b3 279
03febf99
JH
280 for ref in $reflist
281 do
282 refs="$refs$LF$ref"
f170e4b3 283
03febf99
JH
284 # These are relative path from $GIT_DIR, typically starting at refs/
285 # but may be HEAD
f327dbce 286 if expr "z$ref" : 'z\.' >/dev/null
03febf99
JH
287 then
288 not_for_merge=t
f327dbce 289 ref=$(expr "z$ref" : 'z\.\(.*\)')
03febf99
JH
290 else
291 not_for_merge=
292 fi
dfdcb558 293 if expr "z$ref" : 'z+' >/dev/null
03febf99
JH
294 then
295 single_force=t
dfdcb558 296 ref=$(expr "z$ref" : 'z+\(.*\)')
03febf99
JH
297 else
298 single_force=
299 fi
f327dbce
MW
300 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
301 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
853a3697 302
03febf99 303 rref="$rref$LF$remote_name"
4447badc 304
03febf99
JH
305 # There are transports that can fetch only one head at a time...
306 case "$remote" in
38529e28 307 http://* | https://* | ftp://*)
ddaf7314 308 proto=`expr "$remote" : '\([^:]*\):'`
03febf99
JH
309 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
310 curl_extra_args="-k"
311 fi
3ea099d4
SK
312 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
313 "`git-repo-config --bool http.noEPSV`" = true ]; then
314 noepsv_opt="--disable-epsv"
315 fi
2986c022
JH
316
317 # Find $remote_name from ls-remote output.
318 head=$(
319 IFS=' '
320 echo "$ls_remote_result" |
321 while read sha1 name
322 do
323 test "z$name" = "z$remote_name" || continue
324 echo "$sha1"
325 break
326 done
327 )
f327dbce 328 expr "z$head" : "z$_x40\$" >/dev/null ||
2986c022 329 die "No such ref $remote_name at $remote"
ddaf7314 330 echo >&2 "Fetching $remote_name from $remote using $proto"
03febf99
JH
331 git-http-fetch -v -a "$head" "$remote/" || exit
332 ;;
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
853a3697 341
03febf99
JH
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
352 do
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"
359 fi
360 rsync_slurped_objects=t
361 }
362 ;;
363 *)
364 # We will do git native transport with just one call later.
365 continue ;;
366 esac
f170e4b3 367
03febf99 368 append_fetch_head "$head" "$remote" \
f64d7fd2 369 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
853a3697 370
03febf99
JH
371 done
372
373 case "$remote" in
38529e28 374 http://* | https://* | ftp://* | rsync://* )
03febf99
JH
375 ;; # we are already done.
376 *)
377 ( : subshell because we muck with IFS
576162a4 378 pack_lockfile=
03febf99
JH
379 IFS=" $LF"
380 (
920ccbfc 381 git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
03febf99
JH
382 ) |
383 while read sha1 remote_name
384 do
385 case "$sha1" in
386 failed)
387 echo >&2 "Fetch failure: $remote"
388 exit 1 ;;
576162a4
NP
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 ;;
03febf99
JH
395 esac
396 found=
397 single_force=
398 for ref in $refs
399 do
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
f327dbce 421 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
03febf99 422 append_fetch_head "$sha1" "$remote" \
f64d7fd2
JH
423 "$remote_name" "$remote_nick" "$local_name" \
424 "$not_for_merge" || exit
425 done &&
576162a4 426 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
03febf99
JH
427 ) || exit ;;
428 esac
429
430}
431
f64d7fd2 432fetch_main "$reflist" || exit
03febf99
JH
433
434# automated tag following
435case "$no_tags$tags" in
436'')
6dc78e69
JH
437 case "$reflist" in
438 *:refs/*)
439 # effective only when we are following remote branch
440 # using local tracking branch.
441 taglist=$(IFS=" " &&
28b8e61f 442 echo "$ls_remote_result" |
6c96c0f1
JH
443 sed -n -e 's|^\('"$_x40"'\) \(refs/tags/.*\)^{}$|\1 \2|p' \
444 -e 's|^\('"$_x40"'\) \(refs/tags/.*\)$|\1 \2|p' |
6dc78e69
JH
445 while read sha1 name
446 do
6c96c0f1 447 git-show-ref --verify --quiet -- "$name" && continue
6dc78e69
JH
448 git-check-ref-format "$name" || {
449 echo >&2 "warning: tag ${name} ignored"
450 continue
451 }
452 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
453 echo >&2 "Auto-following $name"
454 echo ".${name}:${name}"
455 done)
456 esac
03febf99
JH
457 case "$taglist" in
458 '') ;;
459 ?*)
f64d7fd2 460 fetch_main "$taglist" || exit ;;
03febf99 461 esac
853a3697 462esac
b10ac50f
JH
463
464# If the original head was empty (i.e. no "master" yet), or
465# if we were told not to worry, we do not have to check.
9861718b
JH
466case "$orig_head" in
467'')
b10ac50f 468 ;;
9861718b 469?*)
bf7960eb 470 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
471 if test "$curr_head" != "$orig_head"
472 then
55b7835e
SP
473 git-update-ref \
474 -m "$rloga: Undoing incorrectly fetched HEAD." \
475 HEAD "$orig_head"
b10ac50f
JH
476 die "Cannot fetch into the current branch."
477 fi
478 ;;
479esac