]>
Commit | Line | Data |
---|---|---|
3f571e0b | 1 | #!/bin/sh |
e95ab1ed JH |
2 | # |
3 | # Copyright (c) 2005, Linus Torvalds | |
4 | # Copyright (c) 2005, Junio C Hamano | |
a6080a0a | 5 | # |
e95ab1ed JH |
6 | # Clone a repository into a different directory that does not yet exist. |
7 | ||
365527ad JH |
8 | # See git-sh-setup why. |
9 | unset CDPATH | |
10 | ||
87b787ac DL |
11 | die() { |
12 | echo >&2 "$@" | |
e95ab1ed JH |
13 | exit 1 |
14 | } | |
15 | ||
87b787ac | 16 | usage() { |
37818d7d | 17 | die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]" |
87b787ac DL |
18 | } |
19 | ||
ba375acf | 20 | get_repo_base() { |
c2f599e0 JH |
21 | ( |
22 | cd "`/bin/pwd`" && | |
defe13a2 | 23 | cd "$1" || cd "$1.git" && |
c2f599e0 | 24 | { |
223fa327 | 25 | cd .git |
c2f599e0 JH |
26 | pwd |
27 | } | |
223fa327 | 28 | ) 2>/dev/null |
ba375acf LT |
29 | } |
30 | ||
0516de30 JH |
31 | if [ -n "$GIT_SSL_NO_VERIFY" ]; then |
32 | curl_extra_args="-k" | |
33 | fi | |
34 | ||
35 | http_fetch () { | |
36 | # $1 = Remote, $2 = Local | |
f28dd477 GP |
37 | curl -nsfL $curl_extra_args "$1" >"$2" || |
38 | case $? in | |
39 | 126|127) exit ;; | |
40 | *) return $? ;; | |
41 | esac | |
0516de30 JH |
42 | } |
43 | ||
44 | clone_dumb_http () { | |
45 | # $1 - remote, $2 - local | |
46 | cd "$2" && | |
5e3a620c | 47 | clone_tmp="$GIT_DIR/clone-tmp" && |
0516de30 | 48 | mkdir -p "$clone_tmp" || exit 1 |
3ea099d4 | 49 | if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \ |
5be60078 | 50 | "`git config --bool http.noEPSV`" = true ]; then |
3ea099d4 SK |
51 | curl_extra_args="${curl_extra_args} --disable-epsv" |
52 | fi | |
87b787ac DL |
53 | http_fetch "$1/info/refs" "$clone_tmp/refs" || |
54 | die "Cannot get remote repository information. | |
0516de30 | 55 | Perhaps git-update-server-info needs to be run there?" |
7e8c8255 | 56 | test "z$quiet" = z && v=-v || v= |
0516de30 JH |
57 | while read sha1 refname |
58 | do | |
f327dbce | 59 | name=`expr "z$refname" : 'zrefs/\(.*\)'` && |
cdb39508 | 60 | case "$name" in |
dfeff66e | 61 | *^*) continue;; |
cdb39508 | 62 | esac |
983d2ee2 JH |
63 | case "$bare,$name" in |
64 | yes,* | ,heads/* | ,tags/*) ;; | |
65 | *) continue ;; | |
66 | esac | |
dfeff66e | 67 | if test -n "$use_separate_remote" && |
f327dbce | 68 | branch_name=`expr "z$name" : 'zheads/\(.*\)'` |
dfeff66e | 69 | then |
5ceb05f8 | 70 | tname="remotes/$origin/$branch_name" |
dfeff66e JH |
71 | else |
72 | tname=$name | |
73 | fi | |
928c210a | 74 | git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1 |
0516de30 JH |
75 | done <"$clone_tmp/refs" |
76 | rm -fr "$clone_tmp" | |
c72112e6 JH |
77 | http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" || |
78 | rm -f "$GIT_DIR/REMOTE_HEAD" | |
59c93929 SV |
79 | if test -f "$GIT_DIR/REMOTE_HEAD"; then |
80 | head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"` | |
81 | case "$head_sha1" in | |
82 | 'ref: refs/'*) | |
83 | ;; | |
84 | *) | |
85 | git-http-fetch $v -a "$head_sha1" "$1" || | |
86 | rm -f "$GIT_DIR/REMOTE_HEAD" | |
87 | ;; | |
88 | esac | |
89 | fi | |
dfeff66e JH |
90 | } |
91 | ||
167a4a33 | 92 | quiet= |
ef5b4eab | 93 | local=no |
3d5c418f | 94 | use_local_hardlink=yes |
aae4f42c | 95 | local_shared=no |
a57c8bac | 96 | unset template |
036a72d8 | 97 | no_checkout= |
6ec311da | 98 | upload_pack= |
87e80c4b | 99 | bare= |
dfeff66e JH |
100 | reference= |
101 | origin= | |
e6489a1b | 102 | origin_override= |
71821351 | 103 | use_separate_remote=t |
016e6ccb | 104 | depth= |
83a5ad61 | 105 | no_progress= |
23d53358 | 106 | local_explicitly_asked_for= |
83a5ad61 | 107 | test -t 1 || no_progress=--no-progress |
e95ab1ed JH |
108 | while |
109 | case "$#,$1" in | |
110 | 0,*) break ;; | |
8a1a120c JH |
111 | *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\ |
112 | *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout) | |
113 | no_checkout=yes ;; | |
87e80c4b JH |
114 | *,--na|*,--nak|*,--nake|*,--naked|\ |
115 | *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;; | |
3d5c418f | 116 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) |
23d53358 | 117 | local_explicitly_asked_for=yes |
3d5c418f JH |
118 | use_local_hardlink=yes ;; |
119 | *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\ | |
120 | *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks) | |
121 | use_local_hardlink=no ;; | |
a6080a0a | 122 | *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) |
3d5c418f | 123 | local_shared=yes; ;; |
a57c8bac JH |
124 | 1,--template) usage ;; |
125 | *,--template) | |
126 | shift; template="--template=$1" ;; | |
127 | *,--template=*) | |
128 | template="$1" ;; | |
167a4a33 | 129 | *,-q|*,--quiet) quiet=-q ;; |
63085fab | 130 | *,--use-separate-remote) ;; |
b360cca0 | 131 | *,--no-separate-remote) |
63085fab | 132 | die "clones are always made with separate-remote layout" ;; |
dfeff66e JH |
133 | 1,--reference) usage ;; |
134 | *,--reference) | |
135 | shift; reference="$1" ;; | |
136 | *,--reference=*) | |
8096fae7 | 137 | reference=`expr "z$1" : 'z--reference=\(.*\)'` ;; |
98a4fef3 | 138 | *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin) |
47874d6d | 139 | case "$2" in |
98a4fef3 YS |
140 | '') |
141 | usage ;; | |
47874d6d | 142 | */*) |
87b787ac | 143 | die "'$2' is not suitable for an origin name" |
47874d6d | 144 | esac |
5be60078 | 145 | git check-ref-format "heads/$2" || |
87b787ac DL |
146 | die "'$2' is not suitable for a branch name" |
147 | test -z "$origin_override" || | |
148 | die "Do not give more than one --origin options." | |
e6489a1b | 149 | origin_override=yes |
e6c310fd JS |
150 | origin="$2"; shift |
151 | ;; | |
1cadb5a2 | 152 | 1,-u|1,--upload-pack) usage ;; |
6ec311da JH |
153 | *,-u|*,--upload-pack) |
154 | shift | |
ae1dffcb JH |
155 | upload_pack="--upload-pack=$1" ;; |
156 | *,--upload-pack=*) | |
4a91a1f3 | 157 | upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;; |
016e6ccb JS |
158 | 1,--depth) usage;; |
159 | *,--depth) | |
160 | shift | |
161 | depth="--depth=$1";; | |
e95ab1ed JH |
162 | *,-*) usage ;; |
163 | *) break ;; | |
164 | esac | |
165 | do | |
166 | shift | |
167 | done | |
168 | ||
ef5b4eab | 169 | repo="$1" |
87b787ac DL |
170 | test -n "$repo" || |
171 | die 'you must specify a repository to clone.' | |
ef5b4eab | 172 | |
b360cca0 | 173 | # --bare implies --no-checkout and --no-separate-remote |
e6489a1b JH |
174 | if test yes = "$bare" |
175 | then | |
176 | if test yes = "$origin_override" | |
177 | then | |
87b787ac | 178 | die '--bare and --origin $origin options are incompatible.' |
e6489a1b JH |
179 | fi |
180 | no_checkout=yes | |
71821351 | 181 | use_separate_remote= |
e6489a1b | 182 | fi |
8a1a120c | 183 | |
47874d6d | 184 | if test -z "$origin" |
dfeff66e | 185 | then |
47874d6d | 186 | origin=origin |
dfeff66e JH |
187 | fi |
188 | ||
ba375acf LT |
189 | # Turn the source into an absolute path if |
190 | # it is local | |
ba375acf LT |
191 | if base=$(get_repo_base "$repo"); then |
192 | repo="$base" | |
193 | local=yes | |
194 | fi | |
195 | ||
3f571e0b | 196 | dir="$2" |
0879aa28 | 197 | # Try using "humanish" part of source repo if user didn't specify one |
e7555785 | 198 | [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') |
87b787ac | 199 | [ -e "$dir" ] && die "destination directory '$dir' already exists." |
20ccef49 ML |
200 | [ yes = "$bare" ] && unset GIT_WORK_TREE |
201 | [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] && | |
202 | die "working tree '$GIT_WORK_TREE' already exists." | |
68ad8910 | 203 | D= |
20ccef49 | 204 | W= |
68ad8910 ML |
205 | cleanup() { |
206 | err=$? | |
207 | test -z "$D" && rm -rf "$dir" | |
20ccef49 | 208 | test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE" |
68ad8910 ML |
209 | cd .. |
210 | test -n "$D" && rm -rf "$D" | |
20ccef49 | 211 | test -n "$W" && rm -rf "$W" |
68ad8910 ML |
212 | exit $err |
213 | } | |
214 | trap cleanup 0 | |
215 | mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage | |
20ccef49 ML |
216 | test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" && |
217 | W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W" | |
218 | if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then | |
219 | GIT_DIR="$D" | |
220 | else | |
221 | GIT_DIR="$D/.git" | |
222 | fi && | |
68ad8910 | 223 | export GIT_DIR && |
312efe9b JH |
224 | GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage |
225 | ||
226 | if test -n "$bare" | |
227 | then | |
228 | GIT_CONFIG="$GIT_DIR/config" git config core.bare true | |
229 | fi | |
e95ab1ed | 230 | |
dfeff66e JH |
231 | if test -n "$reference" |
232 | then | |
099c7837 | 233 | ref_git= |
dfeff66e JH |
234 | if test -d "$reference" |
235 | then | |
236 | if test -d "$reference/.git/objects" | |
237 | then | |
099c7837 JH |
238 | ref_git="$reference/.git" |
239 | elif test -d "$reference/objects" | |
240 | then | |
241 | ref_git="$reference" | |
dfeff66e | 242 | fi |
099c7837 JH |
243 | fi |
244 | if test -n "$ref_git" | |
245 | then | |
246 | ref_git=$(cd "$ref_git" && pwd) | |
247 | echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates" | |
248 | ( | |
249 | GIT_DIR="$ref_git" git for-each-ref \ | |
250 | --format='%(objectname) %(*objectname)' | |
251 | ) | | |
252 | while read a b | |
253 | do | |
254 | test -z "$a" || | |
255 | git update-ref "refs/reference-tmp/$a" "$a" | |
256 | test -z "$b" || | |
257 | git update-ref "refs/reference-tmp/$b" "$b" | |
258 | done | |
dfeff66e | 259 | else |
87b787ac | 260 | die "reference repository '$reference' is not a local directory." |
dfeff66e JH |
261 | fi |
262 | fi | |
263 | ||
264 | rm -f "$GIT_DIR/CLONE_HEAD" | |
265 | ||
e95ab1ed | 266 | # We do local magic only when the user tells us to. |
3d5c418f JH |
267 | case "$local" in |
268 | yes) | |
87b787ac | 269 | ( cd "$repo/objects" ) || |
3d5c418f | 270 | die "cannot chdir to local '$repo/objects'." |
e95ab1ed | 271 | |
3d5c418f JH |
272 | if test "$local_shared" = yes |
273 | then | |
274 | mkdir -p "$GIT_DIR/objects/info" | |
275 | echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates" | |
276 | else | |
277 | l= && | |
278 | if test "$use_local_hardlink" = yes | |
279 | then | |
280 | # See if we can hardlink and drop "l" if not. | |
281 | sample_file=$(cd "$repo" && \ | |
282 | find objects -type f -print | sed -e 1q) | |
283 | # objects directory should not be empty because | |
284 | # we are cloning! | |
285 | test -f "$repo/$sample_file" || exit | |
286 | if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null | |
287 | then | |
288 | rm -f "$GIT_DIR/objects/sample" | |
289 | l=l | |
23d53358 JH |
290 | elif test -n "$local_explicitly_asked_for" |
291 | then | |
3d5c418f JH |
292 | echo >&2 "Warning: -l asked but cannot hardlink to $repo" |
293 | fi | |
294 | fi && | |
295 | cd "$repo" && | |
296 | find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1 | |
297 | fi | |
57a39690 | 298 | git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1 |
7558ef89 LT |
299 | ;; |
300 | *) | |
1cadb5a2 JH |
301 | case "$repo" in |
302 | rsync://*) | |
016e6ccb JS |
303 | case "$depth" in |
304 | "") ;; | |
305 | *) die "shallow over rsync not supported" ;; | |
306 | esac | |
4447badc | 307 | rsync $quiet -av --ignore-existing \ |
dfeff66e JH |
308 | --exclude info "$repo/objects/" "$GIT_DIR/objects/" || |
309 | exit | |
4447badc JH |
310 | # Look at objects/info/alternates for rsync -- http will |
311 | # support it natively and git native ones will do it on the | |
312 | # remote end. Not having that file is not a crime. | |
89d844d0 | 313 | rsync -q "$repo/objects/info/alternates" \ |
8a1a120c JH |
314 | "$GIT_DIR/TMP_ALT" 2>/dev/null || |
315 | rm -f "$GIT_DIR/TMP_ALT" | |
316 | if test -f "$GIT_DIR/TMP_ALT" | |
4447badc | 317 | then |
0e9ab02d | 318 | ( cd "$D" && |
4447badc | 319 | . git-parse-remote && |
8a1a120c | 320 | resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) | |
4447badc JH |
321 | while read alt |
322 | do | |
323 | case "$alt" in 'bad alternate: '*) die "$alt";; esac | |
324 | case "$quiet" in | |
325 | '') echo >&2 "Getting alternate: $alt" ;; | |
326 | esac | |
327 | rsync $quiet -av --ignore-existing \ | |
8a1a120c | 328 | --exclude info "$alt" "$GIT_DIR/objects" || exit |
4447badc | 329 | done |
8a1a120c | 330 | rm -f "$GIT_DIR/TMP_ALT" |
4447badc | 331 | fi |
57a39690 | 332 | git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1 |
1cadb5a2 | 333 | ;; |
38529e28 | 334 | https://*|http://*|ftp://*) |
016e6ccb JS |
335 | case "$depth" in |
336 | "") ;; | |
337 | *) die "shallow over http or ftp not supported" ;; | |
338 | esac | |
6c5c62f3 FP |
339 | if test -z "@@NO_CURL@@" |
340 | then | |
341 | clone_dumb_http "$repo" "$D" | |
342 | else | |
87b787ac | 343 | die "http transport not supported, rebuild Git with curl support" |
6c5c62f3 | 344 | fi |
1cadb5a2 JH |
345 | ;; |
346 | *) | |
ced78b39 | 347 | case "$upload_pack" in |
83a5ad61 JS |
348 | '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";; |
349 | *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;; | |
87b787ac DL |
350 | esac >"$GIT_DIR/CLONE_HEAD" || |
351 | die "fetch-pack from '$repo' failed." | |
1cadb5a2 | 352 | ;; |
6ec311da | 353 | esac |
7558ef89 LT |
354 | ;; |
355 | esac | |
dfeff66e JH |
356 | test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp" |
357 | ||
358 | if test -f "$GIT_DIR/CLONE_HEAD" | |
359 | then | |
c72112e6 | 360 | # Read git-fetch-pack -k output and store the remote branches. |
def2747d SS |
361 | if [ -n "$use_separate_remote" ] |
362 | then | |
363 | branch_top="remotes/$origin" | |
364 | else | |
365 | branch_top="heads" | |
366 | fi | |
367 | tag_top="tags" | |
368 | while read sha1 name | |
369 | do | |
370 | case "$name" in | |
371 | *'^{}') | |
372 | continue ;; | |
373 | HEAD) | |
374 | destname="REMOTE_HEAD" ;; | |
375 | refs/heads/*) | |
376 | destname="refs/$branch_top/${name#refs/heads/}" ;; | |
377 | refs/tags/*) | |
378 | destname="refs/$tag_top/${name#refs/tags/}" ;; | |
379 | *) | |
380 | continue ;; | |
381 | esac | |
5be60078 | 382 | git update-ref -m "clone: from $repo" "$destname" "$sha1" "" |
def2747d | 383 | done < "$GIT_DIR/CLONE_HEAD" |
dfeff66e | 384 | fi |
1cadb5a2 | 385 | |
20ccef49 ML |
386 | if test -n "$W"; then |
387 | cd "$W" || exit | |
388 | else | |
389 | cd "$D" || exit | |
390 | fi | |
036a72d8 | 391 | |
dfeff66e | 392 | if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD" |
036a72d8 | 393 | then |
63085fab JH |
394 | # a non-bare repository is always in separate-remote layout |
395 | remote_top="refs/remotes/$origin" | |
c72112e6 JH |
396 | head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"` |
397 | case "$head_sha1" in | |
398 | 'ref: refs/'*) | |
399 | # Uh-oh, the remote told us (http transport done against | |
400 | # new style repository with a symref HEAD). | |
401 | # Ideally we should skip the guesswork but for now | |
402 | # opt for minimum change. | |
f327dbce | 403 | head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'` |
c72112e6 JH |
404 | head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"` |
405 | ;; | |
406 | esac | |
47874d6d | 407 | |
c72112e6 | 408 | # The name under $remote_top the remote HEAD seems to point at. |
dfeff66e JH |
409 | head_points_at=$( |
410 | ( | |
3fe71f3a | 411 | test -f "$GIT_DIR/$remote_top/master" && echo "master" |
dfeff66e JH |
412 | cd "$GIT_DIR/$remote_top" && |
413 | find . -type f -print | sed -e 's/^\.\///' | |
414 | ) | ( | |
415 | done=f | |
416 | while read name | |
417 | do | |
418 | test t = $done && continue | |
419 | branch_tip=`cat "$GIT_DIR/$remote_top/$name"` | |
420 | if test "$head_sha1" = "$branch_tip" | |
421 | then | |
422 | echo "$name" | |
423 | done=t | |
424 | fi | |
425 | done | |
426 | ) | |
427 | ) | |
47874d6d | 428 | |
6762079a | 429 | # Upstream URL |
5be60078 | 430 | git config remote."$origin".url "$repo" && |
6762079a NS |
431 | |
432 | # Set up the mappings to track the remote branches. | |
5be60078 | 433 | git config remote."$origin".fetch \ |
6762079a NS |
434 | "+refs/heads/*:$remote_top/*" '^$' && |
435 | ||
61dde8f9 | 436 | # Write out remote.$origin config, and update our "$head_points_at". |
e125c1a7 | 437 | case "$head_points_at" in |
dfeff66e | 438 | ?*) |
61dde8f9 | 439 | # Local default branch |
5be60078 | 440 | git symbolic-ref HEAD "refs/heads/$head_points_at" && |
61dde8f9 JH |
441 | |
442 | # Tracking branch for the primary branch at the remote. | |
5be60078 | 443 | git update-ref HEAD "$head_sha1" && |
61dde8f9 | 444 | |
63085fab | 445 | rm -f "refs/remotes/$origin/HEAD" |
5be60078 | 446 | git symbolic-ref "refs/remotes/$origin/HEAD" \ |
63085fab | 447 | "refs/remotes/$origin/$head_points_at" && |
61dde8f9 | 448 | |
5be60078 JH |
449 | git config branch."$head_points_at".remote "$origin" && |
450 | git config branch."$head_points_at".merge "refs/heads/$head_points_at" | |
6762079a NS |
451 | ;; |
452 | '') | |
453 | # Source had detached HEAD pointing nowhere | |
5be60078 | 454 | git update-ref --no-deref HEAD "$head_sha1" && |
6762079a NS |
455 | rm -f "refs/remotes/$origin/HEAD" |
456 | ;; | |
e125c1a7 JH |
457 | esac |
458 | ||
036a72d8 JH |
459 | case "$no_checkout" in |
460 | '') | |
b0e90897 | 461 | test "z$quiet" = z -a "z$no_progress" = z && v=-v || v= |
5be60078 | 462 | git read-tree -m -u $v HEAD HEAD |
036a72d8 JH |
463 | esac |
464 | fi | |
dfeff66e | 465 | rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD" |
41ff7a10 | 466 | |
f803eec5 | 467 | trap - 0 |