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"
40 case $curl_exit_status in
42 *) return $curl_exit_status ;;
47 # $1 - remote, $2 - local
49 clone_tmp="$GIT_DIR/clone-tmp" &&
50 mkdir -p "$clone_tmp" || exit 1
51 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
52 "`git config --bool http.noEPSV`" = true ]; then
53 curl_extra_args="${curl_extra_args} --disable-epsv"
55 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
56 die "Cannot get remote repository information.
57 Perhaps git-update-server-info needs to be run there?"
58 test "z$quiet" = z && v=-v || v=
59 while read sha1 refname
61 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
66 yes,* | ,heads/* | ,tags/*) ;;
69 if test -n "$use_separate_remote" &&
70 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
72 tname="remotes/$origin/$branch_name"
76 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
77 done <"$clone_tmp/refs"
79 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
80 rm -f "$GIT_DIR/REMOTE_HEAD"
81 if test -f "$GIT_DIR/REMOTE_HEAD"; then
82 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
87 git-http-fetch $v -a "$head_sha1" "$1" ||
88 rm -f "$GIT_DIR/REMOTE_HEAD"
96 use_local_hardlink=yes
105 use_separate_remote=t
108 local_explicitly_asked_for=
109 test -t 1 || no_progress=--no-progress
113 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
114 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
116 *,--na|*,--nak|*,--nake|*,--naked|\
117 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
118 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
119 local_explicitly_asked_for=yes
120 use_local_hardlink=yes ;;
121 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
122 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
123 use_local_hardlink=no ;;
124 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
126 1,--template) usage ;;
128 shift; template="--template=$1" ;;
131 *,-q|*,--quiet) quiet=-q ;;
132 *,--use-separate-remote) ;;
133 *,--no-separate-remote)
134 die "clones are always made with separate-remote layout" ;;
135 1,--reference) usage ;;
137 shift; reference="$1" ;;
139 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
140 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
145 die "'$2' is not suitable for an origin name"
147 git check-ref-format "heads/$2" ||
148 die "'$2' is not suitable for a branch name"
149 test -z "$origin_override" ||
150 die "Do not give more than one --origin options."
154 1,-u|1,--upload-pack) usage ;;
155 *,-u|*,--upload-pack)
157 upload_pack="--upload-pack=$1" ;;
159 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
173 die 'you must specify a repository to clone.'
175 # --bare implies --no-checkout and --no-separate-remote
176 if test yes = "$bare"
178 if test yes = "$origin_override"
180 die '--bare and --origin $origin options are incompatible.'
191 # Turn the source into an absolute path if
193 if base=$(get_repo_base "$repo"); then
199 # Try using "humanish" part of source repo if user didn't specify one
200 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
201 [ -e "$dir" ] && die "destination directory '$dir' already exists."
202 [ yes = "$bare" ] && unset GIT_WORK_TREE
203 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
204 die "working tree '$GIT_WORK_TREE' already exists."
209 test -z "$D" && rm -rf "$dir"
210 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
212 test -n "$D" && rm -rf "$D"
213 test -n "$W" && rm -rf "$W"
217 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
218 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
219 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
220 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
226 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
230 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
233 if test -n "$reference"
236 if test -d "$reference"
238 if test -d "$reference/.git/objects"
240 ref_git="$reference/.git"
241 elif test -d "$reference/objects"
246 if test -n "$ref_git"
248 ref_git=$(cd "$ref_git" && pwd)
249 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
251 GIT_DIR="$ref_git" git for-each-ref \
252 --format='%(objectname) %(*objectname)'
257 git update-ref "refs/reference-tmp/$a" "$a"
259 git update-ref "refs/reference-tmp/$b" "$b"
262 die "reference repository '$reference' is not a local directory."
266 rm -f "$GIT_DIR/CLONE_HEAD"
268 # We do local magic only when the user tells us to.
271 ( cd "$repo/objects" ) ||
272 die "cannot chdir to local '$repo/objects'."
274 if test "$local_shared" = yes
276 mkdir -p "$GIT_DIR/objects/info"
277 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
280 if test "$use_local_hardlink" = yes
282 # See if we can hardlink and drop "l" if not.
283 sample_file=$(cd "$repo" && \
284 find objects -type f -print | sed -e 1q)
285 # objects directory should not be empty because
287 test -f "$repo/$sample_file" || exit
288 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
290 rm -f "$GIT_DIR/objects/sample"
292 elif test -n "$local_explicitly_asked_for"
294 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
298 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
300 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
307 *) die "shallow over rsync not supported" ;;
309 rsync $quiet -av --ignore-existing \
310 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
312 # Look at objects/info/alternates for rsync -- http will
313 # support it natively and git native ones will do it on the
314 # remote end. Not having that file is not a crime.
315 rsync -q "$repo/objects/info/alternates" \
316 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
317 rm -f "$GIT_DIR/TMP_ALT"
318 if test -f "$GIT_DIR/TMP_ALT"
321 . git-parse-remote &&
322 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
325 case "$alt" in 'bad alternate: '*) die "$alt";; esac
327 '') echo >&2 "Getting alternate: $alt" ;;
329 rsync $quiet -av --ignore-existing \
330 --exclude info "$alt" "$GIT_DIR/objects" || exit
332 rm -f "$GIT_DIR/TMP_ALT"
334 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
336 https://*|http://*|ftp://*)
339 *) die "shallow over http or ftp not supported" ;;
341 if test -z "@@NO_CURL@@"
343 clone_dumb_http "$repo" "$D"
345 die "http transport not supported, rebuild Git with curl support"
349 case "$upload_pack" in
350 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
351 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
352 esac >"$GIT_DIR/CLONE_HEAD" ||
353 die "fetch-pack from '$repo' failed."
358 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
360 if test -f "$GIT_DIR/CLONE_HEAD"
362 # Read git-fetch-pack -k output and store the remote branches.
363 if [ -n "$use_separate_remote" ]
365 branch_top="remotes/$origin"
376 destname="REMOTE_HEAD" ;;
378 destname="refs/$branch_top/${name#refs/heads/}" ;;
380 destname="refs/$tag_top/${name#refs/tags/}" ;;
384 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
385 done < "$GIT_DIR/CLONE_HEAD"
388 if test -n "$W"; then
394 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
396 # a non-bare repository is always in separate-remote layout
397 remote_top="refs/remotes/$origin"
398 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
401 # Uh-oh, the remote told us (http transport done against
402 # new style repository with a symref HEAD).
403 # Ideally we should skip the guesswork but for now
404 # opt for minimum change.
405 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
406 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
410 # The name under $remote_top the remote HEAD seems to point at.
413 test -f "$GIT_DIR/$remote_top/master" && echo "master"
414 cd "$GIT_DIR/$remote_top" &&
415 find . -type f -print | sed -e 's/^\.\///'
420 test t = $done && continue
421 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
422 if test "$head_sha1" = "$branch_tip"
432 git config remote."$origin".url "$repo" &&
434 # Set up the mappings to track the remote branches.
435 git config remote."$origin".fetch \
436 "+refs/heads/*:$remote_top/*" '^$' &&
438 # Write out remote.$origin config, and update our "$head_points_at".
439 case "$head_points_at" in
441 # Local default branch
442 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
444 # Tracking branch for the primary branch at the remote.
445 git update-ref HEAD "$head_sha1" &&
447 rm -f "refs/remotes/$origin/HEAD"
448 git symbolic-ref "refs/remotes/$origin/HEAD" \
449 "refs/remotes/$origin/$head_points_at" &&
451 git config branch."$head_points_at".remote "$origin" &&
452 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
455 # Source had detached HEAD pointing nowhere
456 git update-ref --no-deref HEAD "$head_sha1" &&
457 rm -f "refs/remotes/$origin/HEAD"
461 case "$no_checkout" in
463 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
464 git read-tree -m -u $v HEAD HEAD
467 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"