Merge branch 'jc/clone' into next
[git/jnareb-git.git] / git-clone.sh
blob9db678beef97db1027389badf29a3ecf60659187
1 #!/bin/sh
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
5 #
6 # Clone a repository into a different directory that does not yet exist.
8 # See git-sh-setup why.
9 unset CDPATH
11 usage() {
12 echo >&2 "Usage: $0 [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
13 exit 1
16 get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
24 http_fetch () {
25 # $1 = Remote, $2 = Local
26 curl -nsfL $curl_extra_args "$1" >"$2"
29 clone_dumb_http () {
30 # $1 - remote, $2 - local
31 cd "$2" &&
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?"
37 exit 1;
39 while read sha1 refname
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
42 case "$name" in
43 *^*) continue;;
44 esac
45 if test -n "$use_separate_remote" &&
46 branch_name=`expr "$name" : 'heads/\(.*\)'`
47 then
48 tname="remotes/$branch_name"
49 else
50 tname=$name
52 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
53 done <"$clone_tmp/refs"
54 rm -fr "$clone_tmp"
55 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD"
58 # Read git-fetch-pack -k output and store the remote branches.
59 copy_refs='
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");
66 my $tag_top = "tags";
68 sub store {
69 my ($sha1, $name, $top) = @_;
70 $name = "$git_dir/refs/$top/$name";
71 mkpath(dirname($name));
72 open O, ">", "$name";
73 print O "$sha1\n";
74 close O;
77 open FH, "<", "$git_dir/CLONE_HEAD";
78 while (<FH>) {
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";
83 print O "$sha1\n";
84 close O;
85 next;
87 if ($name =~ s/^refs\/heads\///) {
88 store($sha1, $name, $branch_top);
89 next;
91 if ($name =~ s/^refs\/tags\///) {
92 store($sha1, $name, $tag_top);
93 next;
96 close FH;
99 quiet=
100 use_local=no
101 local_shared=no
102 no_checkout=
103 upload_pack=
104 bare=
105 reference=
106 origin=
107 origin_override=
108 use_separate_remote=
109 while
110 case "$#,$1" in
111 0,*) break ;;
112 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
113 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
114 no_checkout=yes ;;
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 ;;
123 1,-o) usage;;
124 1,--reference) usage ;;
125 *,--reference)
126 shift; reference="$1" ;;
127 *,--reference=*)
128 reference=`expr "$1" : '--reference=\(.*\)'` ;;
129 *,-o)
130 git-check-ref-format "$2" || {
131 echo >&2 "'$2' is not suitable for a branch name"
132 exit 1
134 test -z "$origin_override" || {
135 echo >&2 "Do not give more than one -o options."
136 exit 1
138 origin_override=yes
139 origin="$2"; shift
141 1,-u|1,--upload-pack) usage ;;
142 *,-u|*,--upload-pack)
143 shift
144 upload_pack="--exec=$1" ;;
145 *,-*) usage ;;
146 *) break ;;
147 esac
149 shift
150 done
152 # --bare implies --no-checkout
153 if test yes = "$bare"
154 then
155 if test yes = "$origin_override"
156 then
157 echo >&2 '--bare and -o $origin options are incompatible.'
158 exit 1
160 if test t = "$use_separate_remote"
161 then
162 echo >&2 '--bare and --use-separate-remote options are incompatible.'
163 exit 1
165 no_checkout=yes
168 if test -z "$origin_override$origin"
169 then
170 if test -n "$use_separate_remote"
171 then
172 origin=remotes/master
173 else
174 origin=heads/origin
178 # Turn the source into an absolute path if
179 # it is local
180 repo="$1"
181 local=no
182 if base=$(get_repo_base "$repo"); then
183 repo="$base"
184 local=yes
187 dir="$2"
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
191 mkdir -p "$dir" &&
192 D=$(cd "$dir" && pwd) &&
193 trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
194 case "$bare" in
195 yes) GIT_DIR="$D" ;;
196 *) GIT_DIR="$D/.git" ;;
197 esac && export GIT_DIR && git-init-db || usage
198 case "$bare" in
199 yes)
200 GIT_DIR="$D" ;;
202 GIT_DIR="$D/.git" ;;
203 esac
205 if test -n "$reference"
206 then
207 if test -d "$reference"
208 then
209 if test -d "$reference/.git/objects"
210 then
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 &&
218 cd reference-tmp &&
219 tar xf -)
220 else
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
229 yes,yes)
230 ( cd "$repo/objects" ) || {
231 echo >&2 "-l flag seen but $repo is not local."
232 exit 1
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
246 then
248 fi &&
249 rm -f "$GIT_DIR/objects/sample" &&
250 cd "$repo" &&
251 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
253 yes)
254 mkdir -p "$GIT_DIR/objects/info"
256 test -f "$repo/objects/info/alternates" &&
257 cat "$repo/objects/info/alternates";
258 echo "$repo/objects"
259 } >"$GIT_DIR/objects/info/alternates"
261 esac
262 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
265 case "$repo" in
266 rsync://*)
267 rsync $quiet -av --ignore-existing \
268 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
269 exit
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"
277 then
278 ( cd "$D" &&
279 . git-parse-remote &&
280 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
281 while read alt
283 case "$alt" in 'bad alternate: '*) die "$alt";; esac
284 case "$quiet" in
285 '') echo >&2 "Getting alternate: $alt" ;;
286 esac
287 rsync $quiet -av --ignore-existing \
288 --exclude info "$alt" "$GIT_DIR/objects" || exit
289 done
290 rm -f "$GIT_DIR/TMP_ALT"
292 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
294 http://*)
295 if test -z "@@NO_CURL@@"
296 then
297 clone_dumb_http "$repo" "$D"
298 else
299 echo >&2 "http transport not supported, rebuild Git with curl support"
300 exit 1
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."
309 exit 1
312 esac
314 esac
315 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
317 if test -f "$GIT_DIR/CLONE_HEAD"
318 then
319 # Figure out where the remote HEAD points at.
320 perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote"
323 cd "$D" || exit
325 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
326 then
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 ;;
332 esac
333 head_points_at=$(
335 echo "master"
336 cd "$GIT_DIR/$remote_top" &&
337 find . -type f -print | sed -e 's/^\.\///'
338 ) | (
339 done=f
340 while read name
342 test t = $done && continue
343 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
344 if test "$head_sha1" = "$branch_tip"
345 then
346 echo "$name"
347 done=t
349 done
352 case "$head_points_at" in
354 mkdir -p "$GIT_DIR/remotes" &&
355 echo >"$GIT_DIR/remotes/origin" \
356 "URL: $repo
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)
361 esac &&
362 (cd "$GIT_DIR" && find "$remote_top" -type f -print) |
363 while read ref
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"
371 esac
373 case "$no_checkout" in
375 git-read-tree -m -u -v HEAD HEAD
376 esac
378 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
380 trap - exit