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