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 [--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/clone-tmp' &&
33 mkdir
-p "$clone_tmp" ||
exit 1
34 http_fetch
"$1/info/refs" "$clone_tmp/refs" ||
{
35 echo >&2 "Cannot get remote repository information.
36 Perhaps git-update-server-info needs to be run there?"
39 while read sha1 refname
41 name
=`expr "z$refname" : 'zrefs/\(.*\)'` &&
45 if test -n "$use_separate_remote" &&
46 branch_name
=`expr "z$name" : 'zheads/\(.*\)'`
48 tname
="remotes/$origin/$branch_name"
52 git-http-fetch
-v -a -w "$tname" "$name" "$1/" ||
exit 1
53 done <"$clone_tmp/refs"
55 http_fetch
"$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
56 rm -f "$GIT_DIR/REMOTE_HEAD"
59 # Read git-fetch-pack -k output and store the remote branches.
61 use File::Path qw(mkpath);
62 use File::Basename qw(dirname);
63 my $git_dir = $ARGV[0];
64 my $use_separate_remote = $ARGV[1];
65 my $origin = $ARGV[2];
67 my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
71 my ($sha1, $name, $top) = @_;
72 $name = "$git_dir/refs/$top/$name";
73 mkpath(dirname($name));
79 open FH, "<", "$git_dir/CLONE_HEAD";
81 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
82 next if ($name =~ /\^\173/);
83 if ($name eq "HEAD") {
84 open O, ">", "$git_dir/REMOTE_HEAD";
89 if ($name =~ s/^refs\/heads\///) {
90 store($sha1, $name, $branch_top);
93 if ($name =~ s/^refs\/tags\///) {
94 store($sha1, $name, $tag_top);
115 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
116 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
118 *,--na|
*,--nak|
*,--nake|
*,--naked|\
119 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
120 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
121 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
122 local_shared
=yes; use_local
=yes ;;
123 *,-q|
*,--quiet) quiet
=-q ;;
124 *,--use-separate-remote)
125 use_separate_remote
=t
;;
126 1,--reference) usage
;;
128 shift; reference
="$1" ;;
130 reference
=`expr "$1" : '--reference=\(.*\)'` ;;
131 *,-o|
*,--or|
*,--ori|
*,--orig|
*,--origi|
*,--origin)
136 echo >&2 "'$2' is not suitable for an origin name"
139 git-check-ref-format
"heads/$2" ||
{
140 echo >&2 "'$2' is not suitable for a branch name"
143 test -z "$origin_override" ||
{
144 echo >&2 "Do not give more than one --origin options."
150 1,-u|
1,--upload-pack) usage
;;
151 *,-u|
*,--upload-pack)
153 upload_pack
="--exec=$1" ;;
164 echo >&2 'you must specify a repository to clone.'
168 # --bare implies --no-checkout
169 if test yes = "$bare"
171 if test yes = "$origin_override"
173 echo >&2 '--bare and --origin $origin options are incompatible.'
176 if test t
= "$use_separate_remote"
178 echo >&2 '--bare and --use-separate-remote options are incompatible.'
189 # Turn the source into an absolute path if
191 if base
=$
(get_repo_base
"$repo"); then
197 # Try using "humanish" part of source repo if user didn't specify one
198 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
199 [ -e "$dir" ] && echo "$dir already exists." && usage
201 D
=$
(cd "$dir" && pwd) &&
202 trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
205 *) GIT_DIR
="$D/.git" ;;
206 esac && export GIT_DIR
&& git-init-db || usage
214 if test -n "$reference"
216 if test -d "$reference"
218 if test -d "$reference/.git/objects"
220 reference
="$reference/.git"
222 reference
=$
(cd "$reference" && pwd)
223 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
224 (cd "$reference" && tar cf
- refs
) |
225 (cd "$GIT_DIR/refs" &&
226 mkdir reference-tmp
&&
230 echo >&2 "$reference: not a local directory." && usage
234 rm -f "$GIT_DIR/CLONE_HEAD"
236 # We do local magic only when the user tells us to.
237 case "$local,$use_local" in
239 ( cd "$repo/objects" ) ||
{
240 echo >&2 "-l flag seen but $repo is not local."
244 case "$local_shared" in
246 # See if we can hardlink and drop "l" if not.
247 sample_file
=$
(cd "$repo" && \
248 find objects
-type f
-print |
sed -e 1q
)
250 # objects directory should not be empty since we are cloning!
251 test -f "$repo/$sample_file" ||
exit
254 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
258 rm -f "$GIT_DIR/objects/sample" &&
260 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
263 mkdir
-p "$GIT_DIR/objects/info"
265 test -f "$repo/objects/info/alternates" &&
266 cat "$repo/objects/info/alternates";
268 } >>"$GIT_DIR/objects/info/alternates"
271 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD"
276 rsync
$quiet -av --ignore-existing \
277 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
279 # Look at objects/info/alternates for rsync -- http will
280 # support it natively and git native ones will do it on the
281 # remote end. Not having that file is not a crime.
282 rsync
-q "$repo/objects/info/alternates" \
283 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
284 rm -f "$GIT_DIR/TMP_ALT"
285 if test -f "$GIT_DIR/TMP_ALT"
288 . git-parse-remote
&&
289 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
292 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
294 '') echo >&2 "Getting alternate: $alt" ;;
296 rsync
$quiet -av --ignore-existing \
297 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
299 rm -f "$GIT_DIR/TMP_ALT"
301 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD"
304 if test -z "@@NO_CURL@@"
306 clone_dumb_http
"$repo" "$D"
308 echo >&2 "http transport not supported, rebuild Git with curl support"
313 cd "$D" && case "$upload_pack" in
314 '') git-fetch-pack
--all -k $quiet "$repo" ;;
315 *) git-fetch-pack
--all -k $quiet "$upload_pack" "$repo" ;;
316 esac >"$GIT_DIR/CLONE_HEAD" ||
{
317 echo >&2 "fetch-pack from '$repo' failed."
324 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
326 if test -f "$GIT_DIR/CLONE_HEAD"
328 # Read git-fetch-pack -k output and store the remote branches.
329 perl
-e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
334 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
336 # Figure out which remote branch HEAD points at.
337 case "$use_separate_remote" in
338 '') remote_top
=refs
/heads
;;
339 *) remote_top
="refs/remotes/$origin" ;;
342 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
345 # Uh-oh, the remote told us (http transport done against
346 # new style repository with a symref HEAD).
347 # Ideally we should skip the guesswork but for now
348 # opt for minimum change.
349 head_sha1
=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
350 head_sha1
=`cat "$GIT_DIR/$remote_top/$head_sha1"`
354 # The name under $remote_top the remote HEAD seems to point at.
358 cd "$GIT_DIR/$remote_top" &&
359 find .
-type f
-print |
sed -e 's/^\.\///'
364 test t
= $done && continue
365 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
366 if test "$head_sha1" = "$branch_tip"
375 # Write out remotes/$origin file, and update our "$head_points_at".
376 case "$head_points_at" in
378 mkdir
-p "$GIT_DIR/remotes" &&
379 git-symbolic-ref HEAD
"refs/heads/$head_points_at" &&
380 case "$use_separate_remote" in
381 t
) origin_track
="$remote_top/$head_points_at"
382 git-update-ref HEAD
"$head_sha1" ;;
383 *) origin_track
="$remote_top/$origin"
384 git-update-ref
"refs/heads/$origin" "$head_sha1" ;;
386 echo >"$GIT_DIR/remotes/$origin" \
388 Pull: refs/heads/$head_points_at:$origin_track" &&
389 (cd "$GIT_DIR/$remote_top" && find .
-type f
-print) |
392 name
=`expr "$dotslref" : './\(.*\)'` &&
393 test "$use_separate_remote" = '' && {
394 test "$head_points_at" = "$name" ||
395 test "$origin" = "$name"
397 echo "Pull: refs/heads/${name}:$remote_top/${name}"
398 done >>"$GIT_DIR/remotes/$origin" &&
399 case "$use_separate_remote" in
401 rm -f "refs/remotes/$origin/HEAD"
402 git-symbolic-ref
"refs/remotes/$origin/HEAD" \
403 "refs/remotes/$origin/$head_points_at"
407 case "$no_checkout" in
409 git-read-tree
-m -u -v HEAD HEAD
412 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"