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