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