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" ||
67 # $1 - remote, $2 - local
69 clone_tmp
="$GIT_DIR/clone-tmp" &&
70 mkdir
-p "$clone_tmp" ||
exit 1
71 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
72 "`git config --bool http.noEPSV`" = true
]; then
73 curl_extra_args
="${curl_extra_args} --disable-epsv"
75 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
76 die
"Cannot get remote repository information.
77 Perhaps git-update-server-info needs to be run there?"
78 test "z$quiet" = z
&& v
=-v || v
=
79 while read sha1 refname
81 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
86 yes,* |
,heads
/* |
,tags
/*) ;;
89 if test -n "$use_separate_remote" &&
90 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
92 tname
="remotes/$origin/$branch_name"
96 git-http-fetch
$v -a -w "$tname" "$sha1" "$1" ||
exit 1
97 done <"$clone_tmp/refs"
99 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
100 rm -f "$GIT_DIR/REMOTE_HEAD"
101 if test -f "$GIT_DIR/REMOTE_HEAD"; then
102 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
107 git-http-fetch
$v -a "$head_sha1" "$1" ||
108 rm -f "$GIT_DIR/REMOTE_HEAD"
116 use_local_hardlink
=yes
125 use_separate_remote
=t
128 local_explicitly_asked_for
=
129 test -t 1 || no_progress
=--no-progress
139 local_explicitly_asked_for
=yes
140 use_local_hardlink
=yes
143 use_local_hardlink
=no
;;
147 shift; template
="--template=$1" ;;
150 --use-separate-remote|
--no-separate-remote)
151 die
"clones are always made with separate-remote layout" ;;
153 shift; reference
="$1" ;;
160 die
"'$1' is not suitable for an origin name"
162 git check-ref-format
"heads/$1" ||
163 die
"'$1' is not suitable for a branch name"
164 test -z "$origin_override" ||
165 die
"Do not give more than one --origin options."
171 upload_pack
="--upload-pack=$1" ;;
174 depth
="--depth=$1" ;;
186 die
'you must specify a repository to clone.'
188 # --bare implies --no-checkout and --no-separate-remote
189 if test yes = "$bare"
191 if test yes = "$origin_override"
193 die
'--bare and --origin $origin options are incompatible.'
204 # Turn the source into an absolute path if
206 if base
=$
(get_repo_base
"$repo"); then
212 # Try using "humanish" part of source repo if user didn't specify one
213 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
214 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
215 [ yes = "$bare" ] && unset GIT_WORK_TREE
216 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
217 die
"working tree '$GIT_WORK_TREE' already exists."
222 test -z "$D" && rm -rf "$dir"
223 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
225 test -n "$D" && rm -rf "$D"
226 test -n "$W" && rm -rf "$W"
230 mkdir
-p "$dir" && D
=$
(cd "$dir" && pwd) || usage
231 test -n "$GIT_WORK_TREE" && mkdir
-p "$GIT_WORK_TREE" &&
232 W
=$
(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE
="$W" && export GIT_WORK_TREE
233 if test yes = "$bare" ||
test -n "$GIT_WORK_TREE"; then
239 GIT_CONFIG
="$GIT_DIR/config" git-init
$quiet ${template+"$template"} || usage
243 GIT_CONFIG
="$GIT_DIR/config" git config core.bare true
246 if test -n "$reference"
249 if test -d "$reference"
251 if test -d "$reference/.git/objects"
253 ref_git
="$reference/.git"
254 elif test -d "$reference/objects"
259 if test -n "$ref_git"
261 ref_git
=$
(cd "$ref_git" && pwd)
262 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
264 GIT_DIR
="$ref_git" git for-each-ref \
265 --format='%(objectname) %(*objectname)'
270 git update-ref
"refs/reference-tmp/$a" "$a"
272 git update-ref
"refs/reference-tmp/$b" "$b"
275 die
"reference repository '$reference' is not a local directory."
279 rm -f "$GIT_DIR/CLONE_HEAD"
281 # We do local magic only when the user tells us to.
284 ( cd "$repo/objects" ) ||
285 die
"cannot chdir to local '$repo/objects'."
287 if test "$local_shared" = yes
289 mkdir
-p "$GIT_DIR/objects/info"
290 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
293 if test "$use_local_hardlink" = yes
295 # See if we can hardlink and drop "l" if not.
296 sample_file
=$
(cd "$repo" && \
297 find objects
-type f
-print |
sed -e 1q
)
298 # objects directory should not be empty because
300 test -f "$repo/$sample_file" ||
exit
301 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
303 rm -f "$GIT_DIR/objects/sample"
305 elif test -n "$local_explicitly_asked_for"
307 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
311 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
313 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
320 *) die
"shallow over rsync not supported" ;;
322 rsync
$quiet -av --ignore-existing \
323 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
325 # Look at objects/info/alternates for rsync -- http will
326 # support it natively and git native ones will do it on the
327 # remote end. Not having that file is not a crime.
328 rsync
-q "$repo/objects/info/alternates" \
329 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
330 rm -f "$GIT_DIR/TMP_ALT"
331 if test -f "$GIT_DIR/TMP_ALT"
334 . git-parse-remote
&&
335 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
338 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
340 '') echo >&2 "Getting alternate: $alt" ;;
342 rsync
$quiet -av --ignore-existing \
343 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
345 rm -f "$GIT_DIR/TMP_ALT"
347 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
349 https
://*|http
://*|
ftp://*)
352 *) die
"shallow over http or ftp not supported" ;;
354 if test -z "@@NO_CURL@@"
356 clone_dumb_http
"$repo" "$D"
358 die
"http transport not supported, rebuild Git with curl support"
362 case "$upload_pack" in
363 '') git-fetch-pack
--all -k $quiet $depth $no_progress "$repo";;
364 *) git-fetch-pack
--all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
365 esac >"$GIT_DIR/CLONE_HEAD" ||
366 die
"fetch-pack from '$repo' failed."
371 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
373 if test -f "$GIT_DIR/CLONE_HEAD"
375 # Read git-fetch-pack -k output and store the remote branches.
376 if [ -n "$use_separate_remote" ]
378 branch_top
="remotes/$origin"
389 destname
="REMOTE_HEAD" ;;
391 destname
="refs/$branch_top/${name#refs/heads/}" ;;
393 destname
="refs/$tag_top/${name#refs/tags/}" ;;
397 git update-ref
-m "clone: from $repo" "$destname" "$sha1" ""
398 done < "$GIT_DIR/CLONE_HEAD"
401 if test -n "$W"; then
407 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
409 # a non-bare repository is always in separate-remote layout
410 remote_top
="refs/remotes/$origin"
411 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
414 # Uh-oh, the remote told us (http transport done against
415 # new style repository with a symref HEAD).
416 # Ideally we should skip the guesswork but for now
417 # opt for minimum change.
418 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
419 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
423 # The name under $remote_top the remote HEAD seems to point at.
426 test -f "$GIT_DIR/$remote_top/master" && echo "master"
427 cd "$GIT_DIR/$remote_top" &&
428 find .
-type f
-print |
sed -e 's/^\.\///'
433 test t
= $done && continue
434 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
435 if test "$head_sha1" = "$branch_tip"
445 git config remote.
"$origin".url
"$repo" &&
447 # Set up the mappings to track the remote branches.
448 git config remote.
"$origin".fetch \
449 "+refs/heads/*:$remote_top/*" '^$' &&
451 # Write out remote.$origin config, and update our "$head_points_at".
452 case "$head_points_at" in
454 # Local default branch
455 git symbolic-ref HEAD
"refs/heads/$head_points_at" &&
457 # Tracking branch for the primary branch at the remote.
458 git update-ref HEAD
"$head_sha1" &&
460 rm -f "refs/remotes/$origin/HEAD"
461 git symbolic-ref
"refs/remotes/$origin/HEAD" \
462 "refs/remotes/$origin/$head_points_at" &&
464 git config branch.
"$head_points_at".remote
"$origin" &&
465 git config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
468 # Source had detached HEAD pointing nowhere
469 git update-ref
--no-deref HEAD
"$head_sha1" &&
470 rm -f "refs/remotes/$origin/HEAD"
474 case "$no_checkout" in
476 test "z$quiet" = z
-a "z$no_progress" = z
&& v
=-v || v
=
477 git read-tree
-m -u $v HEAD HEAD
480 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"