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