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>]"
21 (cd "$1" && (cd .git
; pwd)) 2> /dev
/null
24 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
29 # $1 = Remote, $2 = Local
30 curl
-nsfL $curl_extra_args "$1" >"$2"
34 # $1 - remote, $2 - local
36 clone_tmp
="$GIT_DIR/clone-tmp" &&
37 mkdir
-p "$clone_tmp" ||
exit 1
38 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
39 "`git-repo-config --bool http.noEPSV`" = true
]; then
40 curl_extra_args
="${curl_extra_args} --disable-epsv"
42 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
43 die
"Cannot get remote repository information.
44 Perhaps git-update-server-info needs to be run there?"
45 while read sha1 refname
47 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
52 yes,* |
,heads
/* |
,tags
/*) ;;
55 if test -n "$use_separate_remote" &&
56 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
58 tname
="remotes/$origin/$branch_name"
62 git-http-fetch
-v -a -w "$tname" "$name" "$1/" ||
exit 1
63 done <"$clone_tmp/refs"
65 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
66 rm -f "$GIT_DIR/REMOTE_HEAD"
69 # Read git-fetch-pack -k output and store the remote branches.
71 use File::Path qw(mkpath);
72 use File::Basename qw(dirname);
73 my $git_dir = $ARGV[0];
74 my $use_separate_remote = $ARGV[1];
75 my $origin = $ARGV[2];
77 my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
81 my ($sha1, $name, $top) = @_;
82 $name = "$git_dir/refs/$top/$name";
83 mkpath(dirname($name));
89 open FH, "<", "$git_dir/CLONE_HEAD";
91 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
92 next if ($name =~ /\^\173/);
93 if ($name eq "HEAD") {
94 open O, ">", "$git_dir/REMOTE_HEAD";
99 if ($name =~ s/^refs\/heads\///) {
100 store($sha1, $name, $branch_top);
103 if ($name =~ s/^refs\/tags\///) {
104 store($sha1, $name, $tag_top);
122 use_separate_remote
=t
127 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
128 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
130 *,--na|
*,--nak|
*,--nake|
*,--naked|\
131 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
132 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
133 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
134 local_shared
=yes; use_local
=yes ;;
135 1,--template) usage
;;
137 shift; template
="--template=$1" ;;
140 *,-q|
*,--quiet) quiet
=-q ;;
141 *,--use-separate-remote) ;;
142 *,--no-separate-remote)
143 die
"clones are always made with separate-remote layout" ;;
144 1,--reference) usage
;;
146 shift; reference
="$1" ;;
148 reference
=`expr "z$1" : 'z--reference=\(.*\)'` ;;
149 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
154 die
"'$2' is not suitable for an origin name"
156 git-check-ref-format
"heads/$2" ||
157 die
"'$2' is not suitable for a branch name"
158 test -z "$origin_override" ||
159 die
"Do not give more than one --origin options."
163 1,-u|
1,--upload-pack) usage
;;
164 *,-u|
*,--upload-pack)
166 upload_pack
="--exec=$1" ;;
180 die
'you must specify a repository to clone.'
182 # --bare implies --no-checkout and --no-separate-remote
183 if test yes = "$bare"
185 if test yes = "$origin_override"
187 die
'--bare and --origin $origin options are incompatible.'
198 # Turn the source into an absolute path if
200 if base
=$
(get_repo_base
"$repo"); then
206 # Try using "humanish" part of source repo if user didn't specify one
207 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
208 [ -e "$dir" ] && die
"destination directory '$dir' already exists."
210 D
=$
(cd "$dir" && pwd) &&
211 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
217 esac && export GIT_DIR
&& git-init
${template+"$template"} || usage
219 if test -n "$reference"
221 if test -d "$reference"
223 if test -d "$reference/.git/objects"
225 reference
="$reference/.git"
227 reference
=$
(cd "$reference" && pwd)
228 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
229 (cd "$reference" && tar cf
- refs
) |
230 (cd "$GIT_DIR/refs" &&
231 mkdir reference-tmp
&&
235 die
"reference repository '$reference' is not a local directory."
239 rm -f "$GIT_DIR/CLONE_HEAD"
241 # We do local magic only when the user tells us to.
242 case "$local,$use_local" in
244 ( cd "$repo/objects" ) ||
245 die
"-l flag seen but repository '$repo' is not local."
247 case "$local_shared" in
249 # See if we can hardlink and drop "l" if not.
250 sample_file
=$
(cd "$repo" && \
251 find objects
-type f
-print |
sed -e 1q
)
253 # objects directory should not be empty since we are cloning!
254 test -f "$repo/$sample_file" ||
exit
257 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
261 rm -f "$GIT_DIR/objects/sample" &&
263 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
266 mkdir
-p "$GIT_DIR/objects/info"
267 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
270 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
277 *) die
"shallow over rsync not supported" ;;
279 rsync
$quiet -av --ignore-existing \
280 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
282 # Look at objects/info/alternates for rsync -- http will
283 # support it natively and git native ones will do it on the
284 # remote end. Not having that file is not a crime.
285 rsync
-q "$repo/objects/info/alternates" \
286 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
287 rm -f "$GIT_DIR/TMP_ALT"
288 if test -f "$GIT_DIR/TMP_ALT"
291 . git-parse-remote
&&
292 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
295 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
297 '') echo >&2 "Getting alternate: $alt" ;;
299 rsync
$quiet -av --ignore-existing \
300 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
302 rm -f "$GIT_DIR/TMP_ALT"
304 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
306 https
://*|http
://*|
ftp://*)
309 *) die
"shallow over http or ftp not supported" ;;
311 if test -z "@@NO_CURL@@"
313 clone_dumb_http
"$repo" "$D"
315 die
"http transport not supported, rebuild Git with curl support"
319 case "$upload_pack" in
320 '') git-fetch-pack
--all -k $quiet $depth "$repo" ;;
321 *) git-fetch-pack
--all -k $quiet "$upload_pack" $depth "$repo" ;;
322 esac >"$GIT_DIR/CLONE_HEAD" ||
323 die
"fetch-pack from '$repo' failed."
328 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
330 if test -f "$GIT_DIR/CLONE_HEAD"
332 # Read git-fetch-pack -k output and store the remote branches.
333 @@PERL@@
-e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
339 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
341 # a non-bare repository is always in separate-remote layout
342 remote_top
="refs/remotes/$origin"
343 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
346 # Uh-oh, the remote told us (http transport done against
347 # new style repository with a symref HEAD).
348 # Ideally we should skip the guesswork but for now
349 # opt for minimum change.
350 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
351 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
355 # The name under $remote_top the remote HEAD seems to point at.
358 test -f "$GIT_DIR/$remote_top/master" && echo "master"
359 cd "$GIT_DIR/$remote_top" &&
360 find .
-type f
-print |
sed -e 's/^\.\///'
365 test t
= $done && continue
366 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
367 if test "$head_sha1" = "$branch_tip"
376 # Write out remote.$origin config, and update our "$head_points_at".
377 case "$head_points_at" in
379 # Local default branch
380 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
382 # Tracking branch for the primary branch at the remote.
383 origin_track
="$remote_top/$head_points_at" &&
384 git-update-ref HEAD
"$head_sha1" &&
387 git-repo-config remote.
"$origin".url
"$repo" &&
389 # Set up the mappings to track the remote branches.
390 git-repo-config remote.
"$origin".fetch \
391 "+refs/heads/*:$remote_top/*" '^$' &&
392 rm -f "refs/remotes/$origin/HEAD"
393 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
394 "refs/remotes/$origin/$head_points_at" &&
396 git-repo-config branch.
"$head_points_at".remote
"$origin" &&
397 git-repo-config branch.
"$head_points_at".merge
"refs/heads/$head_points_at"
400 case "$no_checkout" in
402 test "z$quiet" = z
&& v
=-v || v
=
403 git-read-tree
-m -u $v HEAD HEAD
406 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"