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 [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <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 "$refname" : 'refs/\(.*\)'` &&
45 if test -n "$use_separate_remote" &&
46 branch_name
=`expr "$name" : 'heads/\(.*\)'`
48 tname
="remotes/$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"
58 # Read git-fetch-pack -k output and store the remote branches.
60 use File::Path qw(mkpath);
61 use File::Basename qw(dirname);
62 my $git_dir = $ARGV[0];
63 my $use_separate_remote = $ARGV[1];
65 my $branch_top = ($use_separate_remote ? "remotes" : "heads");
69 my ($sha1, $name, $top) = @_;
70 $name = "$git_dir/refs/$top/$name";
71 mkpath(dirname($name));
77 open FH, "<", "$git_dir/CLONE_HEAD";
79 my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
80 next if ($name =~ /\^\173/);
81 if ($name eq "HEAD") {
82 open O, ">", "$git_dir/REMOTE_HEAD";
87 if ($name =~ s/^refs\/heads\///) {
88 store($sha1, $name, $branch_top);
91 if ($name =~ s/^refs\/tags\///) {
92 store($sha1, $name, $tag_top);
112 *,-n|
*,--no|
*,--no-|
*,--no-c|
*,--no-ch|
*,--no-che|
*,--no-chec|\
113 *,--no-check|
*,--no-checko|
*,--no-checkou|
*,--no-checkout)
115 *,--na|
*,--nak|
*,--nake|
*,--naked|\
116 *,-b|
*,--b|
*,--ba|
*,--bar|
*,--bare) bare
=yes ;;
117 *,-l|
*,--l|
*,--lo|
*,--loc|
*,--loca|
*,--local) use_local
=yes ;;
118 *,-s|
*,--s|
*,--sh|
*,--sha|
*,--shar|
*,--share|
*,--shared)
119 local_shared
=yes; use_local
=yes ;;
120 *,-q|
*,--quiet) quiet
=-q ;;
121 *,--use-separate-remote)
122 use_separate_remote
=t
;;
124 1,--reference) usage
;;
126 shift; reference
="$1" ;;
128 reference
=`expr "$1" : '--reference=\(.*\)'` ;;
130 git-check-ref-format
"$2" ||
{
131 echo >&2 "'$2' is not suitable for a branch name"
134 test -z "$origin_override" ||
{
135 echo >&2 "Do not give more than one -o options."
141 1,-u|
1,--upload-pack) usage
;;
142 *,-u|
*,--upload-pack)
144 upload_pack
="--exec=$1" ;;
152 # --bare implies --no-checkout
153 if test yes = "$bare"
155 if test yes = "$origin_override"
157 echo >&2 '--bare and -o $origin options are incompatible.'
160 if test t
= "$use_separate_remote"
162 echo >&2 '--bare and --use-separate-remote options are incompatible.'
168 if test -z "$origin_override$origin"
170 if test -n "$use_separate_remote"
172 origin
=remotes
/master
178 # Turn the source into an absolute path if
182 if base
=$
(get_repo_base
"$repo"); then
188 # Try using "humanish" part of source repo if user didn't specify one
189 [ -z "$dir" ] && dir
=$
(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
190 [ -e "$dir" ] && echo "$dir already exists." && usage
192 D
=$
(cd "$dir" && pwd) &&
193 trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
196 *) GIT_DIR
="$D/.git" ;;
197 esac && export GIT_DIR
&& git-init-db || usage
205 if test -n "$reference"
207 if test -d "$reference"
209 if test -d "$reference/.git/objects"
211 reference
="$reference/.git"
213 reference
=$
(cd "$reference" && pwd)
214 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
215 (cd "$reference" && tar cf
- refs
) |
216 (cd "$GIT_DIR/refs" &&
217 mkdir reference-tmp
&&
221 echo >&2 "$reference: not a local directory." && usage
225 rm -f "$GIT_DIR/CLONE_HEAD"
227 # We do local magic only when the user tells us to.
228 case "$local,$use_local" in
230 ( cd "$repo/objects" ) ||
{
231 echo >&2 "-l flag seen but $repo is not local."
235 case "$local_shared" in
237 # See if we can hardlink and drop "l" if not.
238 sample_file
=$
(cd "$repo" && \
239 find objects
-type f
-print |
sed -e 1q
)
241 # objects directory should not be empty since we are cloning!
242 test -f "$repo/$sample_file" ||
exit
245 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev
/null
249 rm -f "$GIT_DIR/objects/sample" &&
251 find objects
-depth -print |
cpio -pumd$l "$GIT_DIR/" ||
exit 1
254 mkdir
-p "$GIT_DIR/objects/info"
256 test -f "$repo/objects/info/alternates" &&
257 cat "$repo/objects/info/alternates";
259 } >"$GIT_DIR/objects/info/alternates"
262 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD"
267 rsync
$quiet -av --ignore-existing \
268 --exclude info
"$repo/objects/" "$GIT_DIR/objects/" ||
270 # Look at objects/info/alternates for rsync -- http will
271 # support it natively and git native ones will do it on the
272 # remote end. Not having that file is not a crime.
273 rsync
-q "$repo/objects/info/alternates" \
274 "$GIT_DIR/TMP_ALT" 2>/dev
/null ||
275 rm -f "$GIT_DIR/TMP_ALT"
276 if test -f "$GIT_DIR/TMP_ALT"
279 . git-parse-remote
&&
280 resolve_alternates
"$repo" <"$GIT_DIR/TMP_ALT" ) |
283 case "$alt" in 'bad alternate: '*) die
"$alt";; esac
285 '') echo >&2 "Getting alternate: $alt" ;;
287 rsync
$quiet -av --ignore-existing \
288 --exclude info
"$alt" "$GIT_DIR/objects" ||
exit
290 rm -f "$GIT_DIR/TMP_ALT"
292 git-ls-remote
"$repo" >"$GIT_DIR/CLONE_HEAD"
295 if test -z "@@NO_CURL@@"
297 clone_dumb_http
"$repo" "$D"
299 echo >&2 "http transport not supported, rebuild Git with curl support"
304 cd "$D" && 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 echo >&2 "fetch-pack from '$repo' failed."
315 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
317 if test -f "$GIT_DIR/CLONE_HEAD"
319 # Figure out where the remote HEAD points at.
320 perl
-e "$copy_refs" "$GIT_DIR" "$use_separate_remote"
325 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
327 head_sha1
=`cat "$GIT_DIR/REMOTE_HEAD"`
328 # Figure out which remote branch HEAD points at.
329 case "$use_separate_remote" in
330 '') remote_top
=refs
/heads
;;
331 *) remote_top
=refs
/remotes
;;
336 cd "$GIT_DIR/$remote_top" &&
337 find .
-type f
-print |
sed -e 's/^\.\///'
342 test t
= $done && continue
343 branch_tip
=`cat "$GIT_DIR/$remote_top/$name"`
344 if test "$head_sha1" = "$branch_tip"
352 case "$head_points_at" in
354 mkdir
-p "$GIT_DIR/remotes" &&
355 echo >"$GIT_DIR/remotes/origin" \
357 Pull: refs/heads/$head_points_at:refs/$origin" &&
358 case "$use_separate_remote" in
359 t
) git-update-ref HEAD
"$head_sha1" ;;
360 *) git-update-ref
"refs/$origin" $
(git-rev-parse HEAD
)
362 (cd "$GIT_DIR" && find "$remote_top" -type f
-print) |
365 head=`expr "$ref" : 'refs/\(.*\)'` &&
366 name
=`expr "$ref" : 'refs/[^\/]*/\(.*\)'` &&
367 test "$head_points_at" = "$name" ||
368 test "$origin" = "$head" ||
369 echo "Pull: refs/heads/${name}:$remote_top/${name}"
370 done >>"$GIT_DIR/remotes/origin"
373 case "$no_checkout" in
375 git-read-tree
-m -u -v HEAD HEAD
378 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"