3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
6 # Clone a repository into a different directory that does not yet exist.
8 # See git-sh-setup why.
12 git-clone [options] [--] <repo> [<dir>]
14 n,no-checkout don't create a checkout
15 bare create a bare repository
16 naked create a bare repository
17 l,local to clone from a local repository
18 no-hardlinks don't use local hardlinks, always copy
19 s,shared setup as a shared repository
20 template= path to the template directory
22 reference= reference repository
23 o,origin= use <name> instead of 'origin' to track upstream
24 u,upload-pack= path to git-upload-pack on the remote
25 depth= create a shallow clone of that depth
27 use-separate-remote compatibility, do not use
28 no-separate-remote compatibility, do not use"
35 # Fix some commands on Windows
38 # Windows has its own (incompatible) find
42 # need an emulation of cpio
47 *) die
"cpio $1 unexpected";;
49 # copy only files and empty directories
53 # here we assume that directories are listed after
54 # its files (aka 'find -depth'), hence, a directory
55 # that is not empty will be a leading sub-string
56 # of the preceding entry
66 xargs --no-run-if-empty \
67 cp $cp_arg --target-directory="$2" --parents
69 # pwd must return a path with a drive letter
71 # there are no symlinks to resolve: /bin/pwd is not needed
89 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@
" || echo exit $?)"
94 cd "$1" ||
cd "$1.git" &&
102 if [ -n "$GIT_SSL_NO_VERIFY" -o \
103 "`git config --bool http.sslVerify`" = false
]; then
108 # $1 = Remote, $2 = Local
109 curl
-nsfL $curl_extra_args "$1" >"$2"
111 case $curl_exit_status in
113 *) return $curl_exit_status ;;
118 # $1 - remote, $2 - local
120 clone_tmp
="$GIT_DIR/clone-tmp" &&
121 mkdir
-p "$clone_tmp" ||
exit 1
122 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
123 "`git config --bool http.noEPSV`" = true
]; then
124 curl_extra_args
="${curl_extra_args} --disable-epsv"
126 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
127 die
"Cannot get remote repository information.
128 Perhaps git-update-server-info needs to be run there?"
129 test "z$quiet" = z
&& v
=-v || v
=
130 while read sha1 refname
132 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
136 case "$bare,$name" in
137 yes,* |
,heads
/* |
,tags
/*) ;;
140 if test -n "$use_separate_remote" &&
141 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
143 tname
="remotes/$origin/$branch_name"
147 git-http-fetch
$v -a -w "$tname" "$sha1" "$1" ||
exit 1
148 done <"$clone_tmp/refs"
150 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
151 rm -f "$GIT_DIR/REMOTE_HEAD"
152 if test -f "$GIT_DIR/REMOTE_HEAD"; then
153 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
158 git-http-fetch
$v -a "$head_sha1" "$1" ||
159 rm -f "$GIT_DIR/REMOTE_HEAD"
167 use_local_hardlink
=yes
176 use_separate_remote
=t
179 local_explicitly_asked_for
=
180 test -t 1 || no_progress
=--no-progress
190 local_explicitly_asked_for
=yes
191 use_local_hardlink
=yes
194 use_local_hardlink
=no
;;
198 shift; template
="--template=$1" ;;
201 --use-separate-remote|
--no-separate-remote)
202 die
"clones are always made with separate-remote layout" ;;
204 shift; reference
="$1" ;;
211 die
"'$1' is not suitable for an origin name"
213 git check-ref-format
"heads/$1" ||
214 die
"'$1' is not suitable for a branch name"
215 test -z "$origin_override" ||
216 die
"Do not give more than one --origin options."
222 upload_pack
="--upload-pack=$1" ;;
225 depth
="--depth=$1" ;;
237 die
'you must specify a repository to clone.'
239 # --bare implies --no-checkout and --no-separate-remote
240 if test yes = "$bare"
242 if test yes = "$origin_override"
244 die
'--bare and --origin $origin options are incompatible.'
255 # Turn the source into an absolute path if
257 if base
=$
(get_repo_base
"$repo"); then
265 case "$repo" in /*) ;; *) repo
="$PWD/$repo" ;; esac
268 # Decide the directory name of the new repository
272 test $# = 2 || die
"excess parameter to git-clone"
274 # Derive one from the repository name
275 # Try using "humanish" part of source repo if user didn't specify one
278 # Cloning from a bundle
279 dir
=$
(echo "$repo" |
sed -e 's|/*\.bundle$||' -e 's|.*/||g')
282 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
286 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
287 [ yes = "$bare" ] && unset GIT_WORK_TREE
288 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
289 die
"working tree '$GIT_WORK_TREE' already exists."
294 test -z "$D" && rm -rf "$dir"
295 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
297 test -n "$D" && rm -rf "$D"
298 test -n "$W" && rm -rf "$W"
302 mkdir
-p "$dir" && D
=$
(cd "$dir" && pwd) || usage
303 test -n "$GIT_WORK_TREE" && mkdir
-p "$GIT_WORK_TREE" &&
304 W
=$
(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE
="$W" && export GIT_WORK_TREE
305 if test yes = "$bare" ||
test -n "$GIT_WORK_TREE"; then
311 GIT_CONFIG
="$GIT_DIR/config" git-init
$quiet ${template+"$template"} || usage
315 GIT_CONFIG
="$GIT_DIR/config" git config core.bare true
318 if test -n "$reference"
321 if test -d "$reference"
323 if test -d "$reference/.git/objects"
325 ref_git
="$reference/.git"
326 elif test -d "$reference/objects"
331 if test -n "$ref_git"
333 ref_git
=$
(cd "$ref_git" && pwd)
334 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
336 GIT_DIR
="$ref_git" git for-each-ref \
337 --format='%(objectname) %(*objectname)'
342 git update-ref
"refs/reference-tmp/$a" "$a"
344 git update-ref
"refs/reference-tmp/$b" "$b"
347 die
"reference repository '$reference' is not a local directory."
351 rm -f "$GIT_DIR/CLONE_HEAD"
353 # We do local magic only when the user tells us to.
356 ( cd "$repo/objects" ) ||
357 die
"cannot chdir to local '$repo/objects'."
359 if test "$local_shared" = yes
361 mkdir
-p "$GIT_DIR/objects/info"
362 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
365 cpio --help 2>&1 |
grep -- --quiet >/dev
/null
&& \
366 cpio_quiet_flag
=--quiet
368 if test "$use_local_hardlink" = yes
370 # See if we can hardlink and drop "l" if not.
371 sample_file
=$
(cd "$repo" && \
372 find objects
-type f
-print |
sed -e 1q
)
373 # objects directory should not be empty because
375 test -f "$repo/$sample_file" ||
376 die
"fatal: cannot clone empty repository"
377 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
379 rm -f "$GIT_DIR/objects/sample"
381 elif test -n "$local_explicitly_asked_for"
383 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
387 find objects
-depth -print |
cpio $cpio_quiet_flag -pumd$l "$GIT_DIR/" || \
390 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
397 *) die
"shallow over rsync not supported" ;;
399 rsync
$quiet -av --ignore-existing \
400 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
402 # Look at objects/info/alternates for rsync -- http will
403 # support it natively and git native ones will do it on the
404 # remote end. Not having that file is not a crime.
405 rsync
-q "$repo/objects/info/alternates" \
406 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
407 rm -f "$GIT_DIR/TMP_ALT"
408 if test -f "$GIT_DIR/TMP_ALT"
411 . git-parse-remote
&&
412 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
415 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
417 '') echo >&2 "Getting alternate: $alt" ;;
419 rsync
$quiet -av --ignore-existing \
420 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
422 rm -f "$GIT_DIR/TMP_ALT"
424 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
426 https
://*|http
://*|
ftp://*)
429 *) die
"shallow over http or ftp not supported" ;;
431 if test -z "@@NO_CURL@@"
433 clone_dumb_http
"$repo" "$D"
435 die
"http transport not supported, rebuild Git with curl support"
439 if [ -f "$repo" ] ; then
440 git bundle unbundle
"$repo" > "$GIT_DIR/CLONE_HEAD" ||
441 die
"unbundle from '$repo' failed."
443 case "$upload_pack" in
444 '') git-fetch-pack
--all -k $quiet $depth $no_progress "$repo";;
445 *) git-fetch-pack
--all -k \
446 $quiet "$upload_pack" $depth $no_progress "$repo" ;;
447 esac >"$GIT_DIR/CLONE_HEAD" ||
448 die
"fetch-pack from '$repo' failed."
454 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
456 if test -f "$GIT_DIR/CLONE_HEAD"
458 # Read git-fetch-pack -k output and store the remote branches.
459 if [ -n "$use_separate_remote" ]
461 branch_top
="remotes/$origin"
472 destname
="REMOTE_HEAD" ;;
474 destname
="refs/$branch_top/${name#refs/heads/}" ;;
476 destname
="refs/$tag_top/${name#refs/tags/}" ;;
480 git update-ref
-m "clone: from $repo" "$destname" "$sha1" ""
481 done < "$GIT_DIR/CLONE_HEAD"
484 if test -n "$W"; then
492 # a non-bare repository is always in separate-remote layout
493 remote_top
="refs/remotes/$origin"
495 test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
498 # Uh-oh, the remote told us (http transport done against
499 # new style repository with a symref HEAD).
500 # Ideally we should skip the guesswork but for now
501 # opt for minimum change.
502 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
503 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
507 # The name under $remote_top the remote HEAD seems to point at.
510 test -f "$GIT_DIR/$remote_top/master" && echo "master"
511 cd "$GIT_DIR/$remote_top" &&
512 find .
-type f
-print |
sed -e 's/^\.\///'
517 test t
= $done && continue
518 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
519 if test "$head_sha1" = "$branch_tip"
529 git config remote.
"$origin".url
"$repo" &&
531 # Set up the mappings to track the remote branches.
532 git config remote.
"$origin".fetch \
533 "+refs/heads/*:$remote_top/*" '^$' &&
535 # Write out remote.$origin config, and update our "$head_points_at".
536 case "$head_points_at" in
538 # Local default branch
539 git symbolic-ref HEAD
"refs/heads/$head_points_at" &&
541 # Tracking branch for the primary branch at the remote.
542 git update-ref HEAD
"$head_sha1" &&
544 rm -f "refs/remotes/$origin/HEAD"
545 git symbolic-ref
"refs/remotes/$origin/HEAD" \
546 "refs/remotes/$origin/$head_points_at" &&
548 git config branch.
"$head_points_at".remote
"$origin" &&
549 git config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
552 if test -z "$head_sha1"
554 # Source had nonexistent ref in HEAD
555 echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
558 # Source had detached HEAD pointing nowhere
559 git update-ref
--no-deref HEAD
"$head_sha1" &&
560 rm -f "refs/remotes/$origin/HEAD"
565 case "$no_checkout" in
567 test "z$quiet" = z
-a "z$no_progress" = z
&& v
=-v || v
=
568 git read-tree
-m -u $v HEAD HEAD
571 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"