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