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>] [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-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/\(.*\)'` &&
51 if test -n "$use_separate_remote" &&
52 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
54 tname
="remotes/$origin/$branch_name"
58 git-http-fetch
-v -a -w "$tname" "$name" "$1/" ||
exit 1
59 done <"$clone_tmp/refs"
61 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
62 rm -f "$GIT_DIR/REMOTE_HEAD"
65 # Read git-fetch-pack -k output and store the remote branches.
67 use File::Path qw(mkpath);
68 use File::Basename qw(dirname);
69 my $git_dir = $ARGV[0];
70 my $use_separate_remote = $ARGV[1];
71 my $origin = $ARGV[2];
73 my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
77 my ($sha1, $name, $top) = @_;
78 $name = "$git_dir/refs/$top/$name";
79 mkpath(dirname($name));
85 open FH, "<", "$git_dir/CLONE_HEAD";
87 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
88 next if ($name =~ /\^\173/);
89 if ($name eq "HEAD") {
90 open O, ">", "$git_dir/REMOTE_HEAD";
95 if ($name =~ s/^refs\/heads\///) {
96 store($sha1, $name, $branch_top);
99 if ($name =~ s/^refs\/tags\///) {
100 store($sha1, $name, $tag_top);
122 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
123 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
125 *,--na|
*,--nak|
*,--nake|
*,--naked|\
126 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
127 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
128 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
129 local_shared
=yes; use_local
=yes ;;
130 1,--template) usage
;;
132 shift; template
="--template=$1" ;;
135 *,-q|
*,--quiet) quiet
=-q ;;
136 *,--use-separate-remote)
137 use_separate_remote
=t
;;
138 1,--reference) usage
;;
140 shift; reference
="$1" ;;
142 reference
=`expr "z$1" : 'z--reference=\(.*\)'` ;;
143 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
148 die
"'$2' is not suitable for an origin name"
150 git-check-ref-format
"heads/$2" ||
151 die
"'$2' is not suitable for a branch name"
152 test -z "$origin_override" ||
153 die
"Do not give more than one --origin options."
157 1,-u|
1,--upload-pack) usage
;;
158 *,-u|
*,--upload-pack)
160 upload_pack
="--exec=$1" ;;
170 die
'you must specify a repository to clone.'
172 # --bare implies --no-checkout
173 if test yes = "$bare"
175 if test yes = "$origin_override"
177 die
'--bare and --origin $origin options are incompatible.'
179 if test t
= "$use_separate_remote"
181 die
'--bare and --use-separate-remote 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."
203 D
=$
(cd "$dir" && pwd) &&
204 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
210 esac && export GIT_DIR
&& git-init-db
${template+"$template"} || usage
212 if test -n "$reference"
214 if test -d "$reference"
216 if test -d "$reference/.git/objects"
218 reference
="$reference/.git"
220 reference
=$
(cd "$reference" && pwd)
221 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
222 (cd "$reference" && tar cf
- refs
) |
223 (cd "$GIT_DIR/refs" &&
224 mkdir reference-tmp
&&
228 die
"reference repository '$reference' is not a local directory."
232 rm -f "$GIT_DIR/CLONE_HEAD"
234 # We do local magic only when the user tells us to.
235 case "$local,$use_local" in
237 ( cd "$repo/objects" ) ||
238 die
"-l flag seen but repository '$repo' is not local."
240 case "$local_shared" in
242 # See if we can hardlink and drop "l" if not.
243 sample_file
=$
(cd "$repo" && \
244 find objects
-type f
-print |
sed -e 1q
)
246 # objects directory should not be empty since we are cloning!
247 test -f "$repo/$sample_file" ||
exit
250 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
254 rm -f "$GIT_DIR/objects/sample" &&
256 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
259 mkdir
-p "$GIT_DIR/objects/info"
260 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
263 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
268 rsync
$quiet -av --ignore-existing \
269 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
271 # Look at objects/info/alternates for rsync -- http will
272 # support it natively and git native ones will do it on the
273 # remote end. Not having that file is not a crime.
274 rsync
-q "$repo/objects/info/alternates" \
275 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
276 rm -f "$GIT_DIR/TMP_ALT"
277 if test -f "$GIT_DIR/TMP_ALT"
280 . git-parse-remote
&&
281 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
284 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
286 '') echo >&2 "Getting alternate: $alt" ;;
288 rsync
$quiet -av --ignore-existing \
289 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
291 rm -f "$GIT_DIR/TMP_ALT"
293 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
295 https
://*|http
://*|
ftp://*)
296 if test -z "@@NO_CURL@@"
298 clone_dumb_http
"$repo" "$D"
300 die
"http transport not supported, rebuild Git with curl support"
304 case "$upload_pack" in
305 '') git-fetch-pack
--all -k $quiet "$repo" ;;
306 *) git-fetch-pack
--all -k $quiet "$upload_pack" "$repo" ;;
307 esac >"$GIT_DIR/CLONE_HEAD" ||
308 die
"fetch-pack from '$repo' failed."
313 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
315 if test -f "$GIT_DIR/CLONE_HEAD"
317 # Read git-fetch-pack -k output and store the remote branches.
318 @@PERL@@
-e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
324 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
326 # Figure out which remote branch HEAD points at.
327 case "$use_separate_remote" in
328 '') remote_top
=refs
/heads
;;
329 *) remote_top
="refs/remotes/$origin" ;;
332 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
335 # Uh-oh, the remote told us (http transport done against
336 # new style repository with a symref HEAD).
337 # Ideally we should skip the guesswork but for now
338 # opt for minimum change.
339 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
340 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
344 # The name under $remote_top the remote HEAD seems to point at.
348 cd "$GIT_DIR/$remote_top" &&
349 find .
-type f
-print |
sed -e 's/^\.\///'
354 test t
= $done && continue
355 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
356 if test "$head_sha1" = "$branch_tip"
365 # Write out remotes/$origin file, and update our "$head_points_at".
366 case "$head_points_at" in
368 mkdir
-p "$GIT_DIR/remotes" &&
369 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
370 case "$use_separate_remote" in
371 t
) origin_track
="$remote_top/$head_points_at"
372 git-update-ref HEAD
"$head_sha1" ;;
373 *) origin_track
="$remote_top/$origin"
374 git-update-ref
"refs/heads/$origin" "$head_sha1" ;;
376 echo >"$GIT_DIR/remotes/$origin" \
378 Pull: refs/heads/$head_points_at:$origin_track" &&
379 (cd "$GIT_DIR/$remote_top" && find .
-type f
-print) |
382 name
=`expr "$dotslref" : './\(.*\)'`
383 if test "z$head_points_at" = "z$name"
387 if test "$use_separate_remote" = '' &&
388 test "z$origin" = "z$name"
392 echo "Pull: refs/heads/${name}:$remote_top/${name}"
393 done >>"$GIT_DIR/remotes/$origin" &&
394 case "$use_separate_remote" in
396 rm -f "refs/remotes/$origin/HEAD"
397 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
398 "refs/remotes/$origin/$head_points_at"
402 case "$no_checkout" in
404 test "z$quiet" = z
&& v
=-v || v
=
405 git-read-tree
-m -u $v HEAD HEAD
408 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"