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 echo >&2 "Usage: $0 [--template=<template_directory>] [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
17 (cd "$1" && (cd .git
; pwd)) 2> /dev
/null
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
25 # $1 = Remote, $2 = Local
26 curl
-nsfL $curl_extra_args "$1" >"$2"
30 # $1 - remote, $2 - local
32 clone_tmp
="$GIT_DIR/clone-tmp" &&
33 mkdir
-p "$clone_tmp" ||
exit 1
34 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
35 "`git-repo-config --bool http.noEPSV`" = true
]; then
36 curl_extra_args
="${curl_extra_args} --disable-epsv"
38 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
{
39 echo >&2 "Cannot get remote repository information.
40 Perhaps git-update-server-info needs to be run there?"
43 while read sha1 refname
45 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
49 if test -n "$use_separate_remote" &&
50 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
52 tname
="remotes/$origin/$branch_name"
56 git-http-fetch
-v -a -w "$tname" "$name" "$1/" ||
exit 1
57 done <"$clone_tmp/refs"
59 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
60 rm -f "$GIT_DIR/REMOTE_HEAD"
63 # Read git-fetch-pack -k output and store the remote branches.
65 use File::Path qw(mkpath);
66 use File::Basename qw(dirname);
67 my $git_dir = $ARGV[0];
68 my $use_separate_remote = $ARGV[1];
69 my $origin = $ARGV[2];
71 my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
75 my ($sha1, $name, $top) = @_;
76 $name = "$git_dir/refs/$top/$name";
77 mkpath(dirname($name));
83 open FH, "<", "$git_dir/CLONE_HEAD";
85 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
86 next if ($name =~ /\^\173/);
87 if ($name eq "HEAD") {
88 open O, ">", "$git_dir/REMOTE_HEAD";
93 if ($name =~ s/^refs\/heads\///) {
94 store($sha1, $name, $branch_top);
97 if ($name =~ s/^refs\/tags\///) {
98 store($sha1, $name, $tag_top);
120 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
121 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
123 *,--na|
*,--nak|
*,--nake|
*,--naked|\
124 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
125 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
126 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
127 local_shared
=yes; use_local
=yes ;;
128 1,--template) usage
;;
130 shift; template
="--template=$1" ;;
133 *,-q|
*,--quiet) quiet
=-q ;;
134 *,--use-separate-remote)
135 use_separate_remote
=t
;;
136 1,--reference) usage
;;
138 shift; reference
="$1" ;;
140 reference
=`expr "z$1" : 'z--reference=\(.*\)'` ;;
141 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
146 echo >&2 "'$2' is not suitable for an origin name"
149 git-check-ref-format
"heads/$2" ||
{
150 echo >&2 "'$2' is not suitable for a branch name"
153 test -z "$origin_override" ||
{
154 echo >&2 "Do not give more than one --origin options."
160 1,-u|
1,--upload-pack) usage
;;
161 *,-u|
*,--upload-pack)
163 upload_pack
="--exec=$1" ;;
174 echo >&2 'you must specify a repository to clone.'
178 # --bare implies --no-checkout
179 if test yes = "$bare"
181 if test yes = "$origin_override"
183 echo >&2 '--bare and --origin $origin options are incompatible.'
186 if test t
= "$use_separate_remote"
188 echo >&2 '--bare and --use-separate-remote options are incompatible.'
199 # Turn the source into an absolute path if
201 if base
=$
(get_repo_base
"$repo"); then
207 # Try using "humanish" part of source repo if user didn't specify one
208 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
209 [ -e "$dir" ] && echo "$dir already exists." && usage
211 D
=$
(cd "$dir" && pwd) &&
212 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
218 esac && export GIT_DIR
&& git-init-db
${template+"$template"} || usage
220 if test -n "$reference"
222 if test -d "$reference"
224 if test -d "$reference/.git/objects"
226 reference
="$reference/.git"
228 reference
=$
(cd "$reference" && pwd)
229 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
230 (cd "$reference" && tar cf
- refs
) |
231 (cd "$GIT_DIR/refs" &&
232 mkdir reference-tmp
&&
236 echo >&2 "$reference: not a local directory." && usage
240 rm -f "$GIT_DIR/CLONE_HEAD"
242 # We do local magic only when the user tells us to.
243 case "$local,$use_local" in
245 ( cd "$repo/objects" ) ||
{
246 echo >&2 "-l flag seen but $repo is not local."
250 case "$local_shared" in
252 # See if we can hardlink and drop "l" if not.
253 sample_file
=$
(cd "$repo" && \
254 find objects
-type f
-print |
sed -e 1q
)
256 # objects directory should not be empty since we are cloning!
257 test -f "$repo/$sample_file" ||
exit
260 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
264 rm -f "$GIT_DIR/objects/sample" &&
266 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
269 mkdir
-p "$GIT_DIR/objects/info"
270 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
273 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
278 rsync
$quiet -av --ignore-existing \
279 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
281 # Look at objects/info/alternates for rsync -- http will
282 # support it natively and git native ones will do it on the
283 # remote end. Not having that file is not a crime.
284 rsync
-q "$repo/objects/info/alternates" \
285 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
286 rm -f "$GIT_DIR/TMP_ALT"
287 if test -f "$GIT_DIR/TMP_ALT"
290 . git-parse-remote
&&
291 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
294 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
296 '') echo >&2 "Getting alternate: $alt" ;;
298 rsync
$quiet -av --ignore-existing \
299 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
301 rm -f "$GIT_DIR/TMP_ALT"
303 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD" ||
exit 1
305 https
://*|http
://*|
ftp://*)
306 if test -z "@@NO_CURL@@"
308 clone_dumb_http
"$repo" "$D"
310 echo >&2 "http transport not supported, rebuild Git with curl support"
315 case "$upload_pack" in
316 '') git-fetch-pack
--all -k $quiet "$repo" ;;
317 *) git-fetch-pack
--all -k $quiet "$upload_pack" "$repo" ;;
318 esac >"$GIT_DIR/CLONE_HEAD" ||
{
319 echo >&2 "fetch-pack from '$repo' failed."
326 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
328 if test -f "$GIT_DIR/CLONE_HEAD"
330 # Read git-fetch-pack -k output and store the remote branches.
331 @@PERL@@
-e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
337 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
339 # Figure out which remote branch HEAD points at.
340 case "$use_separate_remote" in
341 '') remote_top
=refs
/heads
;;
342 *) remote_top
="refs/remotes/$origin" ;;
345 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
348 # Uh-oh, the remote told us (http transport done against
349 # new style repository with a symref HEAD).
350 # Ideally we should skip the guesswork but for now
351 # opt for minimum change.
352 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
353 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
357 # The name under $remote_top the remote HEAD seems to point at.
361 cd "$GIT_DIR/$remote_top" &&
362 find .
-type f
-print |
sed -e 's/^\.\///'
367 test t
= $done && continue
368 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
369 if test "$head_sha1" = "$branch_tip"
378 # Write out remotes/$origin file, and update our "$head_points_at".
379 case "$head_points_at" in
381 mkdir
-p "$GIT_DIR/remotes" &&
382 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
383 case "$use_separate_remote" in
384 t
) origin_track
="$remote_top/$head_points_at"
385 git-update-ref HEAD
"$head_sha1" ;;
386 *) origin_track
="$remote_top/$origin"
387 git-update-ref
"refs/heads/$origin" "$head_sha1" ;;
389 echo >"$GIT_DIR/remotes/$origin" \
391 Pull: refs/heads/$head_points_at:$origin_track" &&
392 (cd "$GIT_DIR/$remote_top" && find .
-type f
-print) |
395 name
=`expr "$dotslref" : './\(.*\)'`
396 if test "z$head_points_at" = "z$name"
400 if test "$use_separate_remote" = '' &&
401 test "z$origin" = "z$name"
405 echo "Pull: refs/heads/${name}:$remote_top/${name}"
406 done >>"$GIT_DIR/remotes/$origin" &&
407 case "$use_separate_remote" in
409 rm -f "refs/remotes/$origin/HEAD"
410 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
411 "refs/remotes/$origin/$head_points_at"
415 case "$no_checkout" in
417 test "z$quiet" = z
&& v
=-v || v
=
418 git-read-tree
-m -u $v HEAD HEAD
421 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"