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
76 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@
" || echo exit $?)"
81 cd "$1" ||
cd "$1.git" &&
89 if [ -n "$GIT_SSL_NO_VERIFY" -o \
90 "`git config --bool http.sslVerify`" = false
]; then
95 # $1 = Remote, $2 = Local
96 curl
-nsfL $curl_extra_args "$1" >"$2" ||
104 # $1 - remote, $2 - local
106 clone_tmp
="$GIT_DIR/clone-tmp" &&
107 mkdir
-p "$clone_tmp" ||
exit 1
108 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
109 "`git config --bool http.noEPSV`" = true
]; then
110 curl_extra_args
="${curl_extra_args} --disable-epsv"
112 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
113 die
"Cannot get remote repository information.
114 Perhaps git-update-server-info needs to be run there?"
115 test "z$quiet" = z
&& v
=-v || v
=
116 while read sha1 refname
118 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
122 case "$bare,$name" in
123 yes,* |
,heads
/* |
,tags
/*) ;;
126 if test -n "$use_separate_remote" &&
127 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
129 tname
="remotes/$origin/$branch_name"
133 git-http-fetch
$v -a -w "$tname" "$sha1" "$1" ||
exit 1
134 done <"$clone_tmp/refs"
136 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
137 rm -f "$GIT_DIR/REMOTE_HEAD"
138 if test -f "$GIT_DIR/REMOTE_HEAD"; then
139 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
144 git-http-fetch
$v -a "$head_sha1" "$1" ||
145 rm -f "$GIT_DIR/REMOTE_HEAD"
153 use_local_hardlink
=yes
162 use_separate_remote
=t
165 local_explicitly_asked_for
=
166 test -t 1 || no_progress
=--no-progress
176 local_explicitly_asked_for
=yes
177 use_local_hardlink
=yes
180 use_local_hardlink
=no
;;
184 shift; template
="--template=$1" ;;
187 --use-separate-remote|
--no-separate-remote)
188 die
"clones are always made with separate-remote layout" ;;
190 shift; reference
="$1" ;;
197 die
"'$1' is not suitable for an origin name"
199 git check-ref-format
"heads/$1" ||
200 die
"'$1' is not suitable for a branch name"
201 test -z "$origin_override" ||
202 die
"Do not give more than one --origin options."
208 upload_pack
="--upload-pack=$1" ;;
211 depth
="--depth=$1" ;;
223 die
'you must specify a repository to clone.'
225 # --bare implies --no-checkout and --no-separate-remote
226 if test yes = "$bare"
228 if test yes = "$origin_override"
230 die
'--bare and --origin $origin options are incompatible.'
241 # Turn the source into an absolute path if
243 if base
=$
(get_repo_base
"$repo"); then
249 # Try using "humanish" part of source repo if user didn't specify one
250 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
251 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
252 [ yes = "$bare" ] && unset GIT_WORK_TREE
253 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
254 die
"working tree '$GIT_WORK_TREE' already exists."
259 test -z "$D" && rm -rf "$dir"
260 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
262 test -n "$D" && rm -rf "$D"
263 test -n "$W" && rm -rf "$W"
267 mkdir
-p "$dir" && D
=$
(cd "$dir" && pwd) || usage
268 test -n "$GIT_WORK_TREE" && mkdir
-p "$GIT_WORK_TREE" &&
269 W
=$
(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE
="$W"
270 if test yes = "$bare" ||
test -n "$GIT_WORK_TREE"; then
276 GIT_CONFIG
="$GIT_DIR/config" git-init
$quiet ${template+"$template"} || usage
280 GIT_CONFIG
="$GIT_DIR/config" git config core.bare true
283 if test -n "$reference"
286 if test -d "$reference"
288 if test -d "$reference/.git/objects"
290 ref_git
="$reference/.git"
291 elif test -d "$reference/objects"
296 if test -n "$ref_git"
298 ref_git
=$
(cd "$ref_git" && pwd)
299 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
301 GIT_DIR
="$ref_git" git for-each-ref \
302 --format='%(objectname) %(*objectname)'
307 git update-ref
"refs/reference-tmp/$a" "$a"
309 git update-ref
"refs/reference-tmp/$b" "$b"
312 die
"reference repository '$reference' is not a local directory."
316 rm -f "$GIT_DIR/CLONE_HEAD"
318 # We do local magic only when the user tells us to.
321 ( cd "$repo/objects" ) ||
322 die
"cannot chdir to local '$repo/objects'."
324 if test "$local_shared" = yes
326 mkdir
-p "$GIT_DIR/objects/info"
327 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
330 if test "$use_local_hardlink" = yes
332 # See if we can hardlink and drop "l" if not.
333 sample_file
=$
(cd "$repo" && \
334 find objects
-type f
-print |
sed -e 1q
)
335 # objects directory should not be empty because
337 test -f "$repo/$sample_file" ||
exit
338 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
340 rm -f "$GIT_DIR/objects/sample"
342 elif test -n "$local_explicitly_asked_for"
344 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
348 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
350 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
357 *) die
"shallow over rsync not supported" ;;
359 rsync
$quiet -av --ignore-existing \
360 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
362 # Look at objects/info/alternates for rsync -- http will
363 # support it natively and git native ones will do it on the
364 # remote end. Not having that file is not a crime.
365 rsync
-q "$repo/objects/info/alternates" \
366 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
367 rm -f "$GIT_DIR/TMP_ALT"
368 if test -f "$GIT_DIR/TMP_ALT"
371 . git-parse-remote
&&
372 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
375 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
377 '') echo >&2 "Getting alternate: $alt" ;;
379 rsync
$quiet -av --ignore-existing \
380 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
382 rm -f "$GIT_DIR/TMP_ALT"
384 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
386 https
://*|http
://*|
ftp://*)
389 *) die
"shallow over http or ftp not supported" ;;
391 if test -z "@@NO_CURL@@"
393 clone_dumb_http
"$repo" "$D"
395 die
"http transport not supported, rebuild Git with curl support"
399 case "$upload_pack" in
400 '') git-fetch-pack
--all -k $quiet $depth $no_progress "$repo";;
401 *) git-fetch-pack
--all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
402 esac >"$GIT_DIR/CLONE_HEAD" ||
403 die
"fetch-pack from '$repo' failed."
408 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
410 if test -f "$GIT_DIR/CLONE_HEAD"
412 # Read git-fetch-pack -k output and store the remote branches.
413 if [ -n "$use_separate_remote" ]
415 branch_top
="remotes/$origin"
426 destname
="REMOTE_HEAD" ;;
428 destname
="refs/$branch_top/${name#refs/heads/}" ;;
430 destname
="refs/$tag_top/${name#refs/tags/}" ;;
434 git update-ref
-m "clone: from $repo" "$destname" "$sha1" ""
435 done < "$GIT_DIR/CLONE_HEAD"
438 if test -n "$W"; then
444 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
446 # a non-bare repository is always in separate-remote layout
447 remote_top
="refs/remotes/$origin"
448 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
451 # Uh-oh, the remote told us (http transport done against
452 # new style repository with a symref HEAD).
453 # Ideally we should skip the guesswork but for now
454 # opt for minimum change.
455 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
456 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
460 # The name under $remote_top the remote HEAD seems to point at.
463 test -f "$GIT_DIR/$remote_top/master" && echo "master"
464 cd "$GIT_DIR/$remote_top" &&
465 find .
-type f
-print |
sed -e 's/^\.\///'
470 test t
= $done && continue
471 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
472 if test "$head_sha1" = "$branch_tip"
482 git config remote.
"$origin".url
"$repo" &&
484 # Set up the mappings to track the remote branches.
485 git config remote.
"$origin".fetch \
486 "+refs/heads/*:$remote_top/*" '^$' &&
488 # Write out remote.$origin config, and update our "$head_points_at".
489 case "$head_points_at" in
491 # Local default branch
492 git symbolic-ref HEAD
"refs/heads/$head_points_at" &&
494 # Tracking branch for the primary branch at the remote.
495 git update-ref HEAD
"$head_sha1" &&
497 rm -f "refs/remotes/$origin/HEAD"
498 git symbolic-ref
"refs/remotes/$origin/HEAD" \
499 "refs/remotes/$origin/$head_points_at" &&
501 git config branch.
"$head_points_at".remote
"$origin" &&
502 git config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
505 # Source had detached HEAD pointing nowhere
506 git update-ref
--no-deref HEAD
"$head_sha1" &&
507 rm -f "refs/remotes/$origin/HEAD"
511 case "$no_checkout" in
513 test "z$quiet" = z
-a "z$no_progress" = z
&& v
=-v || v
=
514 git read-tree
-m -u $v HEAD HEAD
517 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"