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