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"
39 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
44 cd "$1" || cd "$1.git" &&
52 if [ -n "$GIT_SSL_NO_VERIFY" -o \
53 "`git config --bool http.sslVerify`" = false ]; then
58 # $1 = Remote, $2 = Local
59 curl -nsfL $curl_extra_args "$1" >"$2"
61 case $curl_exit_status in
63 *) return $curl_exit_status ;;
68 # $1 - remote, $2 - local
70 clone_tmp="$GIT_DIR/clone-tmp" &&
71 mkdir -p "$clone_tmp" || exit 1
72 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
73 "`git config --bool http.noEPSV`" = true ]; then
74 curl_extra_args="${curl_extra_args} --disable-epsv"
76 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
77 die "Cannot get remote repository information.
78 Perhaps git-update-server-info needs to be run there?"
79 test "z$quiet" = z && v=-v || v=
80 while read sha1 refname
82 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
87 yes,* | ,heads/* | ,tags/*) ;;
90 if test -n "$use_separate_remote" &&
91 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
93 tname="remotes/$origin/$branch_name"
97 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
98 done <"$clone_tmp/refs"
100 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
101 rm -f "$GIT_DIR/REMOTE_HEAD"
102 if test -f "$GIT_DIR/REMOTE_HEAD"; then
103 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
108 git-http-fetch $v -a "$head_sha1" "$1" ||
109 rm -f "$GIT_DIR/REMOTE_HEAD"
117 use_local_hardlink=yes
126 use_separate_remote=t
129 local_explicitly_asked_for=
130 test -t 1 || no_progress=--no-progress
140 local_explicitly_asked_for=yes
141 use_local_hardlink=yes
144 use_local_hardlink=no ;;
148 shift; template="--template=$1" ;;
151 --use-separate-remote|--no-separate-remote)
152 die "clones are always made with separate-remote layout" ;;
154 shift; reference="$1" ;;
161 die "'$1' is not suitable for an origin name"
163 git check-ref-format "heads/$1" ||
164 die "'$1' is not suitable for a branch name"
165 test -z "$origin_override" ||
166 die "Do not give more than one --origin options."
172 upload_pack="--upload-pack=$1" ;;
175 depth="--depth=$1" ;;
187 die 'you must specify a repository to clone.'
189 # --bare implies --no-checkout and --no-separate-remote
190 if test yes = "$bare"
192 if test yes = "$origin_override"
194 die '--bare and --origin $origin options are incompatible.'
205 # Turn the source into an absolute path if
207 if base=$(get_repo_base "$repo"); then
215 case "$repo" in /*) ;; *) repo="$PWD/$repo" ;; esac
218 # Decide the directory name of the new repository
223 # Derive one from the repository name
224 # Try using "humanish" part of source repo if user didn't specify one
227 # Cloning from a bundle
228 dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
231 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
235 [ -e "$dir" ] && die "destination directory '$dir' already exists."
236 [ yes = "$bare" ] && unset GIT_WORK_TREE
237 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
238 die "working tree '$GIT_WORK_TREE' already exists."
243 test -z "$D" && rm -rf "$dir"
244 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
246 test -n "$D" && rm -rf "$D"
247 test -n "$W" && rm -rf "$W"
251 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
252 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
253 W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
254 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
260 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
264 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
267 if test -n "$reference"
270 if test -d "$reference"
272 if test -d "$reference/.git/objects"
274 ref_git="$reference/.git"
275 elif test -d "$reference/objects"
280 if test -n "$ref_git"
282 ref_git=$(cd "$ref_git" && pwd)
283 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
285 GIT_DIR="$ref_git" git for-each-ref \
286 --format='%(objectname) %(*objectname)'
291 git update-ref "refs/reference-tmp/$a" "$a"
293 git update-ref "refs/reference-tmp/$b" "$b"
296 die "reference repository '$reference' is not a local directory."
300 rm -f "$GIT_DIR/CLONE_HEAD"
302 # We do local magic only when the user tells us to.
305 ( cd "$repo/objects" ) ||
306 die "cannot chdir to local '$repo/objects'."
308 if test "$local_shared" = yes
310 mkdir -p "$GIT_DIR/objects/info"
311 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
314 if test "$use_local_hardlink" = yes
316 # See if we can hardlink and drop "l" if not.
317 sample_file=$(cd "$repo" && \
318 find objects -type f -print | sed -e 1q)
319 # objects directory should not be empty because
321 test -f "$repo/$sample_file" ||
322 die "fatal: cannot clone empty repository"
323 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
325 rm -f "$GIT_DIR/objects/sample"
327 elif test -n "$local_explicitly_asked_for"
329 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
333 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
335 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
342 *) die "shallow over rsync not supported" ;;
344 rsync $quiet -av --ignore-existing \
345 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
347 # Look at objects/info/alternates for rsync -- http will
348 # support it natively and git native ones will do it on the
349 # remote end. Not having that file is not a crime.
350 rsync -q "$repo/objects/info/alternates" \
351 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
352 rm -f "$GIT_DIR/TMP_ALT"
353 if test -f "$GIT_DIR/TMP_ALT"
356 . git-parse-remote &&
357 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
360 case "$alt" in 'bad alternate: '*) die "$alt";; esac
362 '') echo >&2 "Getting alternate: $alt" ;;
364 rsync $quiet -av --ignore-existing \
365 --exclude info "$alt" "$GIT_DIR/objects" || exit
367 rm -f "$GIT_DIR/TMP_ALT"
369 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
371 https://*|http://*|ftp://*)
374 *) die "shallow over http or ftp not supported" ;;
376 if test -z "@@NO_CURL@@"
378 clone_dumb_http "$repo" "$D"
380 die "http transport not supported, rebuild Git with curl support"
384 if [ -f "$repo" ] ; then
385 git bundle unbundle "$repo" > "$GIT_DIR/CLONE_HEAD" ||
386 die "unbundle from '$repo' failed."
388 case "$upload_pack" in
389 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
390 *) git-fetch-pack --all -k \
391 $quiet "$upload_pack" $depth $no_progress "$repo" ;;
392 esac >"$GIT_DIR/CLONE_HEAD" ||
393 die "fetch-pack from '$repo' failed."
399 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
401 if test -f "$GIT_DIR/CLONE_HEAD"
403 # Read git-fetch-pack -k output and store the remote branches.
404 if [ -n "$use_separate_remote" ]
406 branch_top="remotes/$origin"
417 destname="REMOTE_HEAD" ;;
419 destname="refs/$branch_top/${name#refs/heads/}" ;;
421 destname="refs/$tag_top/${name#refs/tags/}" ;;
425 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
426 done < "$GIT_DIR/CLONE_HEAD"
429 if test -n "$W"; then
437 # a non-bare repository is always in separate-remote layout
438 remote_top="refs/remotes/$origin"
440 test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
443 # Uh-oh, the remote told us (http transport done against
444 # new style repository with a symref HEAD).
445 # Ideally we should skip the guesswork but for now
446 # opt for minimum change.
447 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
448 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
452 # The name under $remote_top the remote HEAD seems to point at.
455 test -f "$GIT_DIR/$remote_top/master" && echo "master"
456 cd "$GIT_DIR/$remote_top" &&
457 find . -type f -print | sed -e 's/^\.\///'
462 test t = $done && continue
463 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
464 if test "$head_sha1" = "$branch_tip"
474 git config remote."$origin".url "$repo" &&
476 # Set up the mappings to track the remote branches.
477 git config remote."$origin".fetch \
478 "+refs/heads/*:$remote_top/*" '^$' &&
480 # Write out remote.$origin config, and update our "$head_points_at".
481 case "$head_points_at" in
483 # Local default branch
484 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
486 # Tracking branch for the primary branch at the remote.
487 git update-ref HEAD "$head_sha1" &&
489 rm -f "refs/remotes/$origin/HEAD"
490 git symbolic-ref "refs/remotes/$origin/HEAD" \
491 "refs/remotes/$origin/$head_points_at" &&
493 git config branch."$head_points_at".remote "$origin" &&
494 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
497 if test -z "$head_sha1"
499 # Source had nonexistent ref in HEAD
500 echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
503 # Source had detached HEAD pointing nowhere
504 git update-ref --no-deref HEAD "$head_sha1" &&
505 rm -f "refs/remotes/$origin/HEAD"
510 case "$no_checkout" in
512 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
513 git read-tree -m -u $v HEAD HEAD
516 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"