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"
75 if test -f "$GIT_DIR/REMOTE_HEAD"; then
76 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
81 git-http-fetch $v -a "$head_sha1" "$1" ||
82 rm -f "$GIT_DIR/REMOTE_HEAD"
102 test -t 1 || no_progress=--no-progress
106 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
107 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
109 *,--na|*,--nak|*,--nake|*,--naked|\
110 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
111 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
112 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
113 local_shared=yes; use_local=yes ;;
114 1,--template) usage ;;
116 shift; template="--template=$1" ;;
119 *,-q|*,--quiet) quiet=-q ;;
120 *,--use-separate-remote) ;;
121 *,--no-separate-remote)
122 die "clones are always made with separate-remote layout" ;;
123 1,--reference) usage ;;
125 shift; reference="$1" ;;
127 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
128 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
133 die "'$2' is not suitable for an origin name"
135 git check-ref-format "heads/$2" ||
136 die "'$2' is not suitable for a branch name"
137 test -z "$origin_override" ||
138 die "Do not give more than one --origin options."
142 1,-u|1,--upload-pack) usage ;;
143 *,-u|*,--upload-pack)
145 upload_pack="--upload-pack=$1" ;;
147 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
161 die 'you must specify a repository to clone.'
163 # --bare implies --no-checkout and --no-separate-remote
164 if test yes = "$bare"
166 if test yes = "$origin_override"
168 die '--bare and --origin $origin options are incompatible.'
179 # Turn the source into an absolute path if
181 if base=$(get_repo_base "$repo"); then
187 # Try using "humanish" part of source repo if user didn't specify one
188 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
189 [ -e "$dir" ] && die "destination directory '$dir' already exists."
190 [ yes = "$bare" ] && unset GIT_WORK_TREE
191 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
192 die "working tree '$GIT_WORK_TREE' already exists."
197 test -z "$D" && rm -rf "$dir"
198 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
200 test -n "$D" && rm -rf "$D"
201 test -n "$W" && rm -rf "$W"
205 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
206 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
207 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
208 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
214 git-init $quiet ${template+"$template"} || usage
216 if test -n "$reference"
219 if test -d "$reference"
221 if test -d "$reference/.git/objects"
223 ref_git="$reference/.git"
224 elif test -d "$reference/objects"
229 if test -n "$ref_git"
231 ref_git=$(cd "$ref_git" && pwd)
232 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
234 GIT_DIR="$ref_git" git for-each-ref \
235 --format='%(objectname) %(*objectname)'
240 git update-ref "refs/reference-tmp/$a" "$a"
242 git update-ref "refs/reference-tmp/$b" "$b"
245 die "reference repository '$reference' is not a local directory."
249 rm -f "$GIT_DIR/CLONE_HEAD"
251 # We do local magic only when the user tells us to.
252 case "$local,$use_local" in
254 ( cd "$repo/objects" ) ||
255 die "-l flag seen but repository '$repo' is not local."
257 case "$local_shared" in
259 # See if we can hardlink and drop "l" if not.
260 sample_file=$(cd "$repo" && \
261 find objects -type f -print | sed -e 1q)
263 # objects directory should not be empty since we are cloning!
264 test -f "$repo/$sample_file" || exit
267 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
271 rm -f "$GIT_DIR/objects/sample" &&
273 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
276 mkdir -p "$GIT_DIR/objects/info"
277 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
280 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
287 *) die "shallow over rsync not supported" ;;
289 rsync $quiet -av --ignore-existing \
290 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
292 # Look at objects/info/alternates for rsync -- http will
293 # support it natively and git native ones will do it on the
294 # remote end. Not having that file is not a crime.
295 rsync -q "$repo/objects/info/alternates" \
296 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
297 rm -f "$GIT_DIR/TMP_ALT"
298 if test -f "$GIT_DIR/TMP_ALT"
301 . git-parse-remote &&
302 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
305 case "$alt" in 'bad alternate: '*) die "$alt";; esac
307 '') echo >&2 "Getting alternate: $alt" ;;
309 rsync $quiet -av --ignore-existing \
310 --exclude info "$alt" "$GIT_DIR/objects" || exit
312 rm -f "$GIT_DIR/TMP_ALT"
314 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
316 https://*|http://*|ftp://*)
319 *) die "shallow over http or ftp not supported" ;;
321 if test -z "@@NO_CURL@@"
323 clone_dumb_http "$repo" "$D"
325 die "http transport not supported, rebuild Git with curl support"
329 case "$upload_pack" in
330 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
331 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
332 esac >"$GIT_DIR/CLONE_HEAD" ||
333 die "fetch-pack from '$repo' failed."
338 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
340 if test -f "$GIT_DIR/CLONE_HEAD"
342 # Read git-fetch-pack -k output and store the remote branches.
343 if [ -n "$use_separate_remote" ]
345 branch_top="remotes/$origin"
356 destname="REMOTE_HEAD" ;;
358 destname="refs/$branch_top/${name#refs/heads/}" ;;
360 destname="refs/$tag_top/${name#refs/tags/}" ;;
364 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
365 done < "$GIT_DIR/CLONE_HEAD"
368 if test -n "$W"; then
374 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
376 # a non-bare repository is always in separate-remote layout
377 remote_top="refs/remotes/$origin"
378 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
381 # Uh-oh, the remote told us (http transport done against
382 # new style repository with a symref HEAD).
383 # Ideally we should skip the guesswork but for now
384 # opt for minimum change.
385 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
386 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
390 # The name under $remote_top the remote HEAD seems to point at.
393 test -f "$GIT_DIR/$remote_top/master" && echo "master"
394 cd "$GIT_DIR/$remote_top" &&
395 find . -type f -print | sed -e 's/^\.\///'
400 test t = $done && continue
401 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
402 if test "$head_sha1" = "$branch_tip"
412 git config remote."$origin".url "$repo" &&
414 # Set up the mappings to track the remote branches.
415 git config remote."$origin".fetch \
416 "+refs/heads/*:$remote_top/*" '^$' &&
418 # Write out remote.$origin config, and update our "$head_points_at".
419 case "$head_points_at" in
421 # Local default branch
422 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
424 # Tracking branch for the primary branch at the remote.
425 git update-ref HEAD "$head_sha1" &&
427 rm -f "refs/remotes/$origin/HEAD"
428 git symbolic-ref "refs/remotes/$origin/HEAD" \
429 "refs/remotes/$origin/$head_points_at" &&
431 git config branch."$head_points_at".remote "$origin" &&
432 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
435 # Source had detached HEAD pointing nowhere
436 git update-ref --no-deref HEAD "$head_sha1" &&
437 rm -f "refs/remotes/$origin/HEAD"
441 case "$no_checkout" in
443 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
444 git read-tree -m -u $v HEAD HEAD
447 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"