]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/examples/git-fetch.sh
refs: docstring typo
[thirdparty/git.git] / contrib / examples / 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
a858c006 29quiet=
822f7c73 30while test $# != 0
ae2da406
JH
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 38 shift
ae1dffcb
JH
39 exec="--upload-pack=$1"
40 ;;
41 --upl=*|--uplo=*|--uploa=*|--upload=*|\
42 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
4a91a1f3 43 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
ae1dffcb 44 shift
2c620a1a 45 ;;
ae2da406
JH
46 -f|--f|--fo|--for|--forc|--force)
47 force=t
b10ac50f 48 ;;
ed1aadf1
LT
49 -t|--t|--ta|--tag|--tags)
50 tags=t
51 ;;
03febf99
JH
52 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
53 no_tags=t
54 ;;
b10ac50f
JH
55 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
56 --update-he|--update-hea|--update-head|--update-head-|\
57 --update-head-o|--update-head-ok)
58 update_head_ok=t
ae2da406 59 ;;
a858c006
JH
60 -q|--q|--qu|--qui|--quie|--quiet)
61 quiet=--quiet
62 ;;
583122cd 63 -v|--verbose)
684f6742 64 verbose="$verbose"Yes
583122cd 65 ;;
0f76f526 66 -k|--k|--ke|--kee|--keep)
576162a4 67 keep='-k -k'
0f76f526 68 ;;
f53514bc 69 --depth=*)
3e867415 70 shallow_depth="--depth=$(expr "z$1" : 'z-[^=]*=\(.*\)')"
f53514bc
JS
71 ;;
72 --depth)
73 shift
74 shallow_depth="--depth=$1"
75 ;;
87358b7a
FK
76 -*)
77 usage
78 ;;
ae2da406
JH
79 *)
80 break
81 ;;
82 esac
b10ac50f 83 shift
ae2da406
JH
84done
85
853a3697
JH
86case "$#" in
870)
648ad18f
SB
88 origin=$(get_default_remote)
89 test -n "$(get_remote_url ${origin})" ||
90 die "Where do you want to fetch from today?"
91 set x $origin ; shift ;;
853a3697 92esac
ae2da406 93
5dee29ac
UKK
94if test -z "$exec"
95then
96 # No command line override and we have configuration for the remote.
97 exec="--upload-pack=$(get_uploadpack $1)"
98fi
99
853a3697
JH
100remote_nick="$1"
101remote=$(get_remote_url "$@")
102refs=
103rref=
104rsync_slurped_objects=
105
106if test "" = "$append"
107then
df34297a 108 : >"$GIT_DIR/FETCH_HEAD"
853a3697
JH
109fi
110
28b8e61f 111# Global that is reused later
ae1dffcb 112ls_remote_result=$(git ls-remote $exec "$remote") ||
b3d98993 113 die "Cannot get the repository state from $remote"
28b8e61f 114
853a3697 115append_fetch_head () {
d4289fff 116 flags=
08727ea8
SB
117 test -n "$verbose" && flags="$flags$LF-v"
118 test -n "$force$single_force" && flags="$flags$LF-f"
d4289fff 119 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
5be60078 120 git fetch--tool $flags append-fetch-head "$@"
853a3697
JH
121}
122
4b441f47
JH
123# updating the current HEAD with git-fetch in a bare
124# repository is always fine.
125if test -z "$update_head_ok" && test $(is_bare_repository) = false
126then
5be60078 127 orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
4b441f47 128fi
b10ac50f 129
944163a4 130# Allow --tags/--notags from remote.$1.tagopt
24424fc2
JH
131case "$tags$no_tags" in
132'')
5be60078 133 case "$(git config --get "remote.$1.tagopt")" in
944163a4
ST
134 --tags)
135 tags=t ;;
24424fc2
JH
136 --no-tags)
137 no_tags=t ;;
138 esac
139esac
140
ed1aadf1
LT
141# If --tags (and later --heads or --all) is specified, then we are
142# not talking about defaults stored in Pull: line of remotes or
143# branches file, and just fetch those and refspecs explicitly given.
144# Otherwise we do what we always did.
145
146reflist=$(get_remote_refs_for_fetch "$@")
147if test "$tags"
148then
cc301d7e 149 taglist=$(IFS=' ' &&
28b8e61f 150 echo "$ls_remote_result" |
5be60078 151 git show-ref --exclude-existing=refs/tags/ |
81214e4d
JH
152 while read sha1 name
153 do
85b1f988 154 echo ".${name}:${name}"
cc301d7e 155 done) || exit
ed1aadf1
LT
156 if test "$#" -gt 1
157 then
158 # remote URL plus explicit refspecs; we need to merge them.
221e743c 159 reflist="$reflist$LF$taglist"
ed1aadf1
LT
160 else
161 # No explicit refspecs; fetch tags only.
162 reflist=$taglist
163 fi
164fi
165
9debc324 166fetch_all_at_once () {
d1e0ef6c 167
5be60078 168 eval=$(echo "$1" | git fetch--tool parse-reflist "-")
d1e0ef6c 169 eval "$eval"
b74e8cbd
JH
170
171 ( : subshell because we muck with IFS
172 IFS=" $LF"
173 (
9debc324 174 if test "$remote" = . ; then
5be60078 175 git show-ref $rref || echo failed "$remote"
9debc324 176 elif test -f "$remote" ; then
c1f5086e
JH
177 test -n "$shallow_depth" &&
178 die "shallow clone with bundle is not supported"
5be60078 179 git bundle unbundle "$remote" $rref ||
c1f5086e
JH
180 echo failed "$remote"
181 else
e3c6f240
JH
182 if test -d "$remote" &&
183
184 # The remote might be our alternate. With
185 # this optimization we will bypass fetch-pack
186 # altogether, which means we cannot be doing
187 # the shallow stuff at all.
188 test ! -f "$GIT_DIR/shallow" &&
189 test -z "$shallow_depth" &&
190
191 # See if all of what we are going to fetch are
192 # connected to our repository's tips, in which
193 # case we do not have to do any fetch.
afb5f39e 194 theirs=$(echo "$ls_remote_result" | \
5be60078 195 git fetch--tool -s pick-rref "$rref" "-") &&
e3c6f240
JH
196
197 # This will barf when $theirs reach an object that
198 # we do not have in our repository. Otherwise,
199 # we already have everything the fetch would bring in.
5be60078 200 git rev-list --objects $theirs --not --all \
e3c6f240
JH
201 >/dev/null 2>/dev/null
202 then
afb5f39e 203 echo "$ls_remote_result" | \
5be60078 204 git fetch--tool pick-rref "$rref" "-"
e3c6f240 205 else
684f6742
AR
206 flags=
207 case $verbose in
208 YesYes*)
209 flags="-v"
210 ;;
211 esac
e3c6f240 212 git-fetch-pack --thin $exec $keep $shallow_depth \
684f6742 213 $quiet $no_progress $flags "$remote" $rref ||
e3c6f240
JH
214 echo failed "$remote"
215 fi
c1f5086e 216 fi
b74e8cbd
JH
217 ) |
218 (
fbe2687e
JH
219 flags=
220 test -n "$verbose" && flags="$flags -v"
221 test -n "$force" && flags="$flags -f"
222 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
5be60078 223 git fetch--tool $flags native-store \
fee7c2c7 224 "$remote" "$remote_nick" "$refs"
b74e8cbd
JH
225 )
226 ) || exit
227
228}
229
9debc324 230fetch_per_ref () {
03febf99
JH
231 reflist="$1"
232 refs=
4839bd8a 233 rref=
f170e4b3 234
03febf99
JH
235 for ref in $reflist
236 do
237 refs="$refs$LF$ref"
f170e4b3 238
03febf99
JH
239 # These are relative path from $GIT_DIR, typically starting at refs/
240 # but may be HEAD
f327dbce 241 if expr "z$ref" : 'z\.' >/dev/null
03febf99
JH
242 then
243 not_for_merge=t
f327dbce 244 ref=$(expr "z$ref" : 'z\.\(.*\)')
03febf99
JH
245 else
246 not_for_merge=
247 fi
dfdcb558 248 if expr "z$ref" : 'z+' >/dev/null
03febf99
JH
249 then
250 single_force=t
dfdcb558 251 ref=$(expr "z$ref" : 'z+\(.*\)')
03febf99
JH
252 else
253 single_force=
254 fi
f327dbce
MW
255 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
256 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
853a3697 257
03febf99 258 rref="$rref$LF$remote_name"
4447badc 259
03febf99
JH
260 # There are transports that can fetch only one head at a time...
261 case "$remote" in
38529e28 262 http://* | https://* | ftp://*)
f53514bc
JS
263 test -n "$shallow_depth" &&
264 die "shallow clone with http not supported"
3e867415 265 proto=$(expr "$remote" : '\([^:]*\):')
03febf99
JH
266 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
267 curl_extra_args="-k"
268 fi
3ea099d4 269 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
3e867415 270 "$(git config --bool http.noEPSV)" = true ]; then
3ea099d4
SK
271 noepsv_opt="--disable-epsv"
272 fi
2986c022
JH
273
274 # Find $remote_name from ls-remote output.
afb5f39e 275 head=$(echo "$ls_remote_result" | \
5be60078 276 git fetch--tool -s pick-rref "$remote_name" "-")
f327dbce 277 expr "z$head" : "z$_x40\$" >/dev/null ||
2986c022 278 die "No such ref $remote_name at $remote"
ddaf7314 279 echo >&2 "Fetching $remote_name from $remote using $proto"
a858c006 280 case "$quiet" in '') v=-v ;; *) v= ;; esac
27be481f 281 git-http-fetch $v -a "$head" "$remote" || exit
03febf99
JH
282 ;;
283 rsync://*)
f53514bc
JS
284 test -n "$shallow_depth" &&
285 die "shallow clone with rsync not supported"
03febf99
JH
286 TMP_HEAD="$GIT_DIR/TMP_HEAD"
287 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
5be60078 288 head=$(git rev-parse --verify TMP_HEAD)
03febf99 289 rm -f "$TMP_HEAD"
a858c006 290 case "$quiet" in '') v=-v ;; *) v= ;; esac
03febf99 291 test "$rsync_slurped_objects" || {
a858c006 292 rsync -a $v --ignore-existing --exclude info \
03febf99 293 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
853a3697 294
03febf99
JH
295 # Look at objects/info/alternates for rsync -- http will
296 # support it natively and git native ones will do it on
297 # the remote end. Not having that file is not a crime.
298 rsync -q "$remote/objects/info/alternates" \
299 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
300 rm -f "$GIT_DIR/TMP_ALT"
301 if test -f "$GIT_DIR/TMP_ALT"
302 then
303 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
304 while read alt
305 do
306 case "$alt" in 'bad alternate: '*) die "$alt";; esac
307 echo >&2 "Getting alternate: $alt"
308 rsync -av --ignore-existing --exclude info \
309 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
310 done
311 rm -f "$GIT_DIR/TMP_ALT"
312 fi
313 rsync_slurped_objects=t
314 }
315 ;;
03febf99 316 esac
f170e4b3 317
03febf99 318 append_fetch_head "$head" "$remote" \
f64d7fd2 319 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
853a3697 320
03febf99
JH
321 done
322
b74e8cbd 323}
03febf99 324
b74e8cbd
JH
325fetch_main () {
326 case "$remote" in
327 http://* | https://* | ftp://* | rsync://* )
9debc324 328 fetch_per_ref "$@"
b74e8cbd
JH
329 ;;
330 *)
9debc324 331 fetch_all_at_once "$@"
b74e8cbd
JH
332 ;;
333 esac
03febf99
JH
334}
335
f64d7fd2 336fetch_main "$reflist" || exit
03febf99
JH
337
338# automated tag following
339case "$no_tags$tags" in
340'')
6dc78e69
JH
341 case "$reflist" in
342 *:refs/*)
343 # effective only when we are following remote branch
344 # using local tracking branch.
ed9f7c95 345 taglist=$(IFS=' ' &&
28b8e61f 346 echo "$ls_remote_result" |
5be60078 347 git show-ref --exclude-existing=refs/tags/ |
6dc78e69
JH
348 while read sha1 name
349 do
5be60078 350 git cat-file -t "$sha1" >/dev/null 2>&1 || continue
6dc78e69
JH
351 echo >&2 "Auto-following $name"
352 echo ".${name}:${name}"
353 done)
354 esac
03febf99
JH
355 case "$taglist" in
356 '') ;;
357 ?*)
d1586315
AJ
358 # do not deepen a shallow tree when following tags
359 shallow_depth=
f64d7fd2 360 fetch_main "$taglist" || exit ;;
03febf99 361 esac
853a3697 362esac
b10ac50f
JH
363
364# If the original head was empty (i.e. no "master" yet), or
365# if we were told not to worry, we do not have to check.
9861718b
JH
366case "$orig_head" in
367'')
b10ac50f 368 ;;
9861718b 369?*)
5be60078 370 curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
371 if test "$curr_head" != "$orig_head"
372 then
5be60078 373 git update-ref \
f9474132 374 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
55b7835e 375 HEAD "$orig_head"
b10ac50f
JH
376 die "Cannot fetch into the current branch."
377 fi
378 ;;
379esac