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