]> git.ipfire.org Git - thirdparty/git.git/blame - git-fetch.sh
diff-lib: use ce_mode_from_stat() rather than messing with modes manually
[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=
83a5ad61
JS
27no_progress=
28test -t 1 || no_progress=--no-progress
a858c006 29quiet=
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 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
LT
63 -v|--verbose)
64 verbose=Yes
65 ;;
0f76f526 66 -k|--k|--ke|--kee|--keep)
576162a4 67 keep='-k -k'
0f76f526 68 ;;
f53514bc
JS
69 --depth=*)
70 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
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" \
fee7c2c7 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
bf7960eb 127 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
4b441f47 128fi
b10ac50f 129
24424fc2
JH
130# Allow --notags from remote.$1.tagopt
131case "$tags$no_tags" in
132'')
133 case "$(git-config --get "remote.$1.tagopt")" in
134 --no-tags)
135 no_tags=t ;;
136 esac
137esac
138
ed1aadf1
LT
139# If --tags (and later --heads or --all) is specified, then we are
140# not talking about defaults stored in Pull: line of remotes or
141# branches file, and just fetch those and refspecs explicitly given.
142# Otherwise we do what we always did.
143
144reflist=$(get_remote_refs_for_fetch "$@")
145if test "$tags"
146then
ed9f7c95 147 taglist=`IFS=' ' &&
28b8e61f 148 echo "$ls_remote_result" |
85b1f988 149 git-show-ref --exclude-existing=refs/tags/ |
81214e4d
JH
150 while read sha1 name
151 do
85b1f988 152 echo ".${name}:${name}"
57a39690 153 done` || exit
ed1aadf1
LT
154 if test "$#" -gt 1
155 then
156 # remote URL plus explicit refspecs; we need to merge them.
221e743c 157 reflist="$reflist$LF$taglist"
ed1aadf1
LT
158 else
159 # No explicit refspecs; fetch tags only.
160 reflist=$taglist
161 fi
162fi
163
9debc324 164fetch_all_at_once () {
d1e0ef6c 165
617669da 166 eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
d1e0ef6c 167 eval "$eval"
b74e8cbd
JH
168
169 ( : subshell because we muck with IFS
170 IFS=" $LF"
171 (
9debc324
PB
172 if test "$remote" = . ; then
173 git-show-ref $rref || echo failed "$remote"
174 elif test -f "$remote" ; then
c1f5086e
JH
175 test -n "$shallow_depth" &&
176 die "shallow clone with bundle is not supported"
177 git-bundle unbundle "$remote" $rref ||
178 echo failed "$remote"
179 else
27be481f
JH
180 git-fetch-pack --thin $exec $keep $shallow_depth \
181 $quiet $no_progress "$remote" $rref ||
b74e8cbd 182 echo failed "$remote"
c1f5086e 183 fi
b74e8cbd
JH
184 ) |
185 (
fbe2687e
JH
186 flags=
187 test -n "$verbose" && flags="$flags -v"
188 test -n "$force" && flags="$flags -f"
189 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
fee7c2c7
JH
190 git-fetch--tool $flags native-store \
191 "$remote" "$remote_nick" "$refs"
b74e8cbd
JH
192 )
193 ) || exit
194
195}
196
9debc324 197fetch_per_ref () {
03febf99
JH
198 reflist="$1"
199 refs=
4839bd8a 200 rref=
f170e4b3 201
03febf99
JH
202 for ref in $reflist
203 do
204 refs="$refs$LF$ref"
f170e4b3 205
03febf99
JH
206 # These are relative path from $GIT_DIR, typically starting at refs/
207 # but may be HEAD
f327dbce 208 if expr "z$ref" : 'z\.' >/dev/null
03febf99
JH
209 then
210 not_for_merge=t
f327dbce 211 ref=$(expr "z$ref" : 'z\.\(.*\)')
03febf99
JH
212 else
213 not_for_merge=
214 fi
dfdcb558 215 if expr "z$ref" : 'z+' >/dev/null
03febf99
JH
216 then
217 single_force=t
dfdcb558 218 ref=$(expr "z$ref" : 'z+\(.*\)')
03febf99
JH
219 else
220 single_force=
221 fi
f327dbce
MW
222 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
223 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
853a3697 224
03febf99 225 rref="$rref$LF$remote_name"
4447badc 226
03febf99
JH
227 # There are transports that can fetch only one head at a time...
228 case "$remote" in
38529e28 229 http://* | https://* | ftp://*)
f53514bc
JS
230 test -n "$shallow_depth" &&
231 die "shallow clone with http not supported"
ddaf7314 232 proto=`expr "$remote" : '\([^:]*\):'`
03febf99
JH
233 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
234 curl_extra_args="-k"
235 fi
3ea099d4 236 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
e0d10e1c 237 "`git-config --bool http.noEPSV`" = true ]; then
3ea099d4
SK
238 noepsv_opt="--disable-epsv"
239 fi
2986c022
JH
240
241 # Find $remote_name from ls-remote output.
242 head=$(
243 IFS=' '
244 echo "$ls_remote_result" |
245 while read sha1 name
246 do
247 test "z$name" = "z$remote_name" || continue
248 echo "$sha1"
249 break
250 done
251 )
f327dbce 252 expr "z$head" : "z$_x40\$" >/dev/null ||
2986c022 253 die "No such ref $remote_name at $remote"
ddaf7314 254 echo >&2 "Fetching $remote_name from $remote using $proto"
a858c006 255 case "$quiet" in '') v=-v ;; *) v= ;; esac
27be481f 256 git-http-fetch $v -a "$head" "$remote" || exit
03febf99
JH
257 ;;
258 rsync://*)
f53514bc
JS
259 test -n "$shallow_depth" &&
260 die "shallow clone with rsync not supported"
03febf99
JH
261 TMP_HEAD="$GIT_DIR/TMP_HEAD"
262 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
263 head=$(git-rev-parse --verify TMP_HEAD)
264 rm -f "$TMP_HEAD"
a858c006 265 case "$quiet" in '') v=-v ;; *) v= ;; esac
03febf99 266 test "$rsync_slurped_objects" || {
a858c006 267 rsync -a $v --ignore-existing --exclude info \
03febf99 268 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
853a3697 269
03febf99
JH
270 # Look at objects/info/alternates for rsync -- http will
271 # support it natively and git native ones will do it on
272 # the remote end. Not having that file is not a crime.
273 rsync -q "$remote/objects/info/alternates" \
274 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
275 rm -f "$GIT_DIR/TMP_ALT"
276 if test -f "$GIT_DIR/TMP_ALT"
277 then
278 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
279 while read alt
280 do
281 case "$alt" in 'bad alternate: '*) die "$alt";; esac
282 echo >&2 "Getting alternate: $alt"
283 rsync -av --ignore-existing --exclude info \
284 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
285 done
286 rm -f "$GIT_DIR/TMP_ALT"
287 fi
288 rsync_slurped_objects=t
289 }
290 ;;
03febf99 291 esac
f170e4b3 292
03febf99 293 append_fetch_head "$head" "$remote" \
f64d7fd2 294 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
853a3697 295
03febf99
JH
296 done
297
b74e8cbd 298}
03febf99 299
b74e8cbd
JH
300fetch_main () {
301 case "$remote" in
302 http://* | https://* | ftp://* | rsync://* )
9debc324 303 fetch_per_ref "$@"
b74e8cbd
JH
304 ;;
305 *)
9debc324 306 fetch_all_at_once "$@"
b74e8cbd
JH
307 ;;
308 esac
03febf99
JH
309}
310
f64d7fd2 311fetch_main "$reflist" || exit
03febf99
JH
312
313# automated tag following
314case "$no_tags$tags" in
315'')
6dc78e69
JH
316 case "$reflist" in
317 *:refs/*)
318 # effective only when we are following remote branch
319 # using local tracking branch.
ed9f7c95 320 taglist=$(IFS=' ' &&
28b8e61f 321 echo "$ls_remote_result" |
ed9f7c95 322 git-show-ref --exclude-existing=refs/tags/ |
6dc78e69
JH
323 while read sha1 name
324 do
6dc78e69
JH
325 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
326 echo >&2 "Auto-following $name"
327 echo ".${name}:${name}"
328 done)
329 esac
03febf99
JH
330 case "$taglist" in
331 '') ;;
332 ?*)
d1586315
AJ
333 # do not deepen a shallow tree when following tags
334 shallow_depth=
f64d7fd2 335 fetch_main "$taglist" || exit ;;
03febf99 336 esac
853a3697 337esac
b10ac50f
JH
338
339# If the original head was empty (i.e. no "master" yet), or
340# if we were told not to worry, we do not have to check.
9861718b
JH
341case "$orig_head" in
342'')
b10ac50f 343 ;;
9861718b 344?*)
bf7960eb 345 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
346 if test "$curr_head" != "$orig_head"
347 then
55b7835e 348 git-update-ref \
f9474132 349 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
55b7835e 350 HEAD "$orig_head"
b10ac50f
JH
351 die "Cannot fetch into the current branch."
352 fi
353 ;;
354esac