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" ]; then
36 # $1 = Remote, $2 = Local
37 curl -nsfL $curl_extra_args "$1" >"$2"
41 # $1 - remote, $2 - local
43 clone_tmp="$GIT_DIR/clone-tmp" &&
44 mkdir -p "$clone_tmp" || exit 1
45 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
46 "`git-config --bool http.noEPSV`" = true ]; then
47 curl_extra_args="${curl_extra_args} --disable-epsv"
49 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
50 die "Cannot get remote repository information.
51 Perhaps git-update-server-info needs to be run there?"
52 test "z$quiet" = z && v=-v || v=
53 while read sha1 refname
55 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
60 yes,* | ,heads/* | ,tags/*) ;;
63 if test -n "$use_separate_remote" &&
64 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
66 tname="remotes/$origin/$branch_name"
70 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
71 done <"$clone_tmp/refs"
73 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
74 rm -f "$GIT_DIR/REMOTE_HEAD"
91 test -t 1 || no_progress=--no-progress
95 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
96 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
98 *,--na|*,--nak|*,--nake|*,--naked|\
99 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
100 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
101 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
102 local_shared=yes; use_local=yes ;;
103 1,--template) usage ;;
105 shift; template="--template=$1" ;;
108 *,-q|*,--quiet) quiet=-q ;;
109 *,--use-separate-remote) ;;
110 *,--no-separate-remote)
111 die "clones are always made with separate-remote layout" ;;
112 1,--reference) usage ;;
114 shift; reference="$1" ;;
116 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
117 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
122 die "'$2' is not suitable for an origin name"
124 git-check-ref-format "heads/$2" ||
125 die "'$2' is not suitable for a branch name"
126 test -z "$origin_override" ||
127 die "Do not give more than one --origin options."
131 1,-u|1,--upload-pack) usage ;;
132 *,-u|*,--upload-pack)
134 upload_pack="--upload-pack=$1" ;;
136 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
150 die 'you must specify a repository to clone.'
152 # --bare implies --no-checkout and --no-separate-remote
153 if test yes = "$bare"
155 if test yes = "$origin_override"
157 die '--bare and --origin $origin options are incompatible.'
168 # Turn the source into an absolute path if
170 if base=$(get_repo_base "$repo"); then
176 # Try using "humanish" part of source repo if user didn't specify one
177 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
178 [ -e "$dir" ] && die "destination directory '$dir' already exists."
180 D=$(cd "$dir" && pwd) &&
181 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
187 esac && export GIT_DIR && git-init $quiet ${template+"$template"} || usage
189 if test -n "$reference"
192 if test -d "$reference"
194 if test -d "$reference/.git/objects"
196 ref_git="$reference/.git"
197 elif test -d "$reference/objects"
202 if test -n "$ref_git"
204 ref_git=$(cd "$ref_git" && pwd)
205 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
207 GIT_DIR="$ref_git" git for-each-ref \
208 --format='%(objectname) %(*objectname)'
213 git update-ref "refs/reference-tmp/$a" "$a"
215 git update-ref "refs/reference-tmp/$b" "$b"
218 die "reference repository '$reference' is not a local directory."
222 rm -f "$GIT_DIR/CLONE_HEAD"
224 # We do local magic only when the user tells us to.
225 case "$local,$use_local" in
227 ( cd "$repo/objects" ) ||
228 die "-l flag seen but repository '$repo' is not local."
230 case "$local_shared" in
232 # See if we can hardlink and drop "l" if not.
233 sample_file=$(cd "$repo" && \
234 find objects -type f -print | sed -e 1q)
236 # objects directory should not be empty since we are cloning!
237 test -f "$repo/$sample_file" || exit
240 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
244 rm -f "$GIT_DIR/objects/sample" &&
246 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
249 mkdir -p "$GIT_DIR/objects/info"
250 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
253 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
260 *) die "shallow over rsync not supported" ;;
262 rsync $quiet -av --ignore-existing \
263 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
265 # Look at objects/info/alternates for rsync -- http will
266 # support it natively and git native ones will do it on the
267 # remote end. Not having that file is not a crime.
268 rsync -q "$repo/objects/info/alternates" \
269 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
270 rm -f "$GIT_DIR/TMP_ALT"
271 if test -f "$GIT_DIR/TMP_ALT"
274 . git-parse-remote &&
275 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
278 case "$alt" in 'bad alternate: '*) die "$alt";; esac
280 '') echo >&2 "Getting alternate: $alt" ;;
282 rsync $quiet -av --ignore-existing \
283 --exclude info "$alt" "$GIT_DIR/objects" || exit
285 rm -f "$GIT_DIR/TMP_ALT"
287 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
289 https://*|http://*|ftp://*)
292 *) die "shallow over http or ftp not supported" ;;
294 if test -z "@@NO_CURL@@"
296 clone_dumb_http "$repo" "$D"
298 die "http transport not supported, rebuild Git with curl support"
302 case "$upload_pack" in
303 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
304 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
305 esac >"$GIT_DIR/CLONE_HEAD" ||
306 die "fetch-pack from '$repo' failed."
311 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
313 if test -f "$GIT_DIR/CLONE_HEAD"
315 # Read git-fetch-pack -k output and store the remote branches.
316 if [ -n "$use_separate_remote" ]
318 branch_top="remotes/$origin"
329 destname="REMOTE_HEAD" ;;
331 destname="refs/$branch_top/${name#refs/heads/}" ;;
333 destname="refs/$tag_top/${name#refs/tags/}" ;;
337 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
338 done < "$GIT_DIR/CLONE_HEAD"
343 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
345 # a non-bare repository is always in separate-remote layout
346 remote_top="refs/remotes/$origin"
347 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
350 # Uh-oh, the remote told us (http transport done against
351 # new style repository with a symref HEAD).
352 # Ideally we should skip the guesswork but for now
353 # opt for minimum change.
354 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
355 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
359 # The name under $remote_top the remote HEAD seems to point at.
362 test -f "$GIT_DIR/$remote_top/master" && echo "master"
363 cd "$GIT_DIR/$remote_top" &&
364 find . -type f -print | sed -e 's/^\.\///'
369 test t = $done && continue
370 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
371 if test "$head_sha1" = "$branch_tip"
381 git-config remote."$origin".url "$repo" &&
383 # Set up the mappings to track the remote branches.
384 git-config remote."$origin".fetch \
385 "+refs/heads/*:$remote_top/*" '^$' &&
387 # Write out remote.$origin config, and update our "$head_points_at".
388 case "$head_points_at" in
390 # Local default branch
391 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
393 # Tracking branch for the primary branch at the remote.
394 git-update-ref HEAD "$head_sha1" &&
396 rm -f "refs/remotes/$origin/HEAD"
397 git-symbolic-ref "refs/remotes/$origin/HEAD" \
398 "refs/remotes/$origin/$head_points_at" &&
400 git-config branch."$head_points_at".remote "$origin" &&
401 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
404 # Source had detached HEAD pointing nowhere
405 git-update-ref --no-deref HEAD "$head_sha1" &&
406 rm -f "refs/remotes/$origin/HEAD"
410 case "$no_checkout" in
412 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
413 git-read-tree -m -u $v HEAD HEAD
416 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"