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