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.
17 die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
23 cd "$1" || cd "$1.git" &&
31 if [ -n "$GIT_SSL_NO_VERIFY" -o \
32 "`git config --bool http.sslVerify`" = false ]; then
37 # $1 = Remote, $2 = Local
38 curl -nsfL $curl_extra_args "$1" >"$2" ||
46 # $1 - remote, $2 - local
48 clone_tmp="$GIT_DIR/clone-tmp" &&
49 mkdir -p "$clone_tmp" || exit 1
50 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
51 "`git config --bool http.noEPSV`" = true ]; then
52 curl_extra_args="${curl_extra_args} --disable-epsv"
54 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
55 die "Cannot get remote repository information.
56 Perhaps git-update-server-info needs to be run there?"
57 test "z$quiet" = z && v=-v || v=
58 while read sha1 refname
60 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
65 yes,* | ,heads/* | ,tags/*) ;;
68 if test -n "$use_separate_remote" &&
69 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
71 tname="remotes/$origin/$branch_name"
75 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
76 done <"$clone_tmp/refs"
78 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
79 rm -f "$GIT_DIR/REMOTE_HEAD"
80 if test -f "$GIT_DIR/REMOTE_HEAD"; then
81 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
86 git-http-fetch $v -a "$head_sha1" "$1" ||
87 rm -f "$GIT_DIR/REMOTE_HEAD"
95 use_local_hardlink=yes
104 use_separate_remote=t
107 local_explicitly_asked_for=
108 test -t 1 || no_progress=--no-progress
112 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
113 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
115 *,--na|*,--nak|*,--nake|*,--naked|\
116 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
117 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
118 local_explicitly_asked_for=yes
119 use_local_hardlink=yes ;;
120 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
121 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
122 use_local_hardlink=no ;;
123 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
125 1,--template) usage ;;
127 shift; template="--template=$1" ;;
130 *,-q|*,--quiet) quiet=-q ;;
131 *,--use-separate-remote) ;;
132 *,--no-separate-remote)
133 die "clones are always made with separate-remote layout" ;;
134 1,--reference) usage ;;
136 shift; reference="$1" ;;
138 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
139 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
144 die "'$2' is not suitable for an origin name"
146 git check-ref-format "heads/$2" ||
147 die "'$2' is not suitable for a branch name"
148 test -z "$origin_override" ||
149 die "Do not give more than one --origin options."
153 1,-u|1,--upload-pack) usage ;;
154 *,-u|*,--upload-pack)
156 upload_pack="--upload-pack=$1" ;;
158 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
172 die 'you must specify a repository to clone.'
174 # --bare implies --no-checkout and --no-separate-remote
175 if test yes = "$bare"
177 if test yes = "$origin_override"
179 die '--bare and --origin $origin options are incompatible.'
190 # Turn the source into an absolute path if
192 if base=$(get_repo_base "$repo"); then
198 # Try using "humanish" part of source repo if user didn't specify one
199 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
200 [ -e "$dir" ] && die "destination directory '$dir' already exists."
201 [ yes = "$bare" ] && unset GIT_WORK_TREE
202 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
203 die "working tree '$GIT_WORK_TREE' already exists."
208 test -z "$D" && rm -rf "$dir"
209 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
211 test -n "$D" && rm -rf "$D"
212 test -n "$W" && rm -rf "$W"
216 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
217 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
218 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
219 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
225 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
229 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
232 if test -n "$reference"
235 if test -d "$reference"
237 if test -d "$reference/.git/objects"
239 ref_git="$reference/.git"
240 elif test -d "$reference/objects"
245 if test -n "$ref_git"
247 ref_git=$(cd "$ref_git" && pwd)
248 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
250 GIT_DIR="$ref_git" git for-each-ref \
251 --format='%(objectname) %(*objectname)'
256 git update-ref "refs/reference-tmp/$a" "$a"
258 git update-ref "refs/reference-tmp/$b" "$b"
261 die "reference repository '$reference' is not a local directory."
265 rm -f "$GIT_DIR/CLONE_HEAD"
267 # We do local magic only when the user tells us to.
270 ( cd "$repo/objects" ) ||
271 die "cannot chdir to local '$repo/objects'."
273 if test "$local_shared" = yes
275 mkdir -p "$GIT_DIR/objects/info"
276 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
279 if test "$use_local_hardlink" = yes
281 # See if we can hardlink and drop "l" if not.
282 sample_file=$(cd "$repo" && \
283 find objects -type f -print | sed -e 1q)
284 # objects directory should not be empty because
286 test -f "$repo/$sample_file" || exit
287 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
289 rm -f "$GIT_DIR/objects/sample"
291 elif test -n "$local_explicitly_asked_for"
293 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
297 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
299 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
306 *) die "shallow over rsync not supported" ;;
308 rsync $quiet -av --ignore-existing \
309 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
311 # Look at objects/info/alternates for rsync -- http will
312 # support it natively and git native ones will do it on the
313 # remote end. Not having that file is not a crime.
314 rsync -q "$repo/objects/info/alternates" \
315 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
316 rm -f "$GIT_DIR/TMP_ALT"
317 if test -f "$GIT_DIR/TMP_ALT"
320 . git-parse-remote &&
321 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
324 case "$alt" in 'bad alternate: '*) die "$alt";; esac
326 '') echo >&2 "Getting alternate: $alt" ;;
328 rsync $quiet -av --ignore-existing \
329 --exclude info "$alt" "$GIT_DIR/objects" || exit
331 rm -f "$GIT_DIR/TMP_ALT"
333 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
335 https://*|http://*|ftp://*)
338 *) die "shallow over http or ftp not supported" ;;
340 if test -z "@@NO_CURL@@"
342 clone_dumb_http "$repo" "$D"
344 die "http transport not supported, rebuild Git with curl support"
348 case "$upload_pack" in
349 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
350 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
351 esac >"$GIT_DIR/CLONE_HEAD" ||
352 die "fetch-pack from '$repo' failed."
357 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
359 if test -f "$GIT_DIR/CLONE_HEAD"
361 # Read git-fetch-pack -k output and store the remote branches.
362 if [ -n "$use_separate_remote" ]
364 branch_top="remotes/$origin"
375 destname="REMOTE_HEAD" ;;
377 destname="refs/$branch_top/${name#refs/heads/}" ;;
379 destname="refs/$tag_top/${name#refs/tags/}" ;;
383 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
384 done < "$GIT_DIR/CLONE_HEAD"
387 if test -n "$W"; then
393 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
395 # a non-bare repository is always in separate-remote layout
396 remote_top="refs/remotes/$origin"
397 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
400 # Uh-oh, the remote told us (http transport done against
401 # new style repository with a symref HEAD).
402 # Ideally we should skip the guesswork but for now
403 # opt for minimum change.
404 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
405 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
409 # The name under $remote_top the remote HEAD seems to point at.
412 test -f "$GIT_DIR/$remote_top/master" && echo "master"
413 cd "$GIT_DIR/$remote_top" &&
414 find . -type f -print | sed -e 's/^\.\///'
419 test t = $done && continue
420 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
421 if test "$head_sha1" = "$branch_tip"
431 git config remote."$origin".url "$repo" &&
433 # Set up the mappings to track the remote branches.
434 git config remote."$origin".fetch \
435 "+refs/heads/*:$remote_top/*" '^$' &&
437 # Write out remote.$origin config, and update our "$head_points_at".
438 case "$head_points_at" in
440 # Local default branch
441 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
443 # Tracking branch for the primary branch at the remote.
444 git update-ref HEAD "$head_sha1" &&
446 rm -f "refs/remotes/$origin/HEAD"
447 git symbolic-ref "refs/remotes/$origin/HEAD" \
448 "refs/remotes/$origin/$head_points_at" &&
450 git config branch."$head_points_at".remote "$origin" &&
451 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
454 # Source had detached HEAD pointing nowhere
455 git update-ref --no-deref HEAD "$head_sha1" &&
456 rm -f "refs/remotes/$origin/HEAD"
460 case "$no_checkout" in
462 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
463 git read-tree -m -u $v HEAD HEAD
466 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"