Merge branch 'maint'
[git/debian.git] / git-clone.sh
blob3f006d1a778636e5906ad9e8671adb6561fa0d98
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 die() {
12 echo >&2 "$@"
13 exit 1
16 usage() {
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>]"
20 get_repo_base() {
21 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
24 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
25 curl_extra_args="-k"
28 http_fetch () {
29 # $1 = Remote, $2 = Local
30 curl -nsfL $curl_extra_args "$1" >"$2"
33 clone_dumb_http () {
34 # $1 - remote, $2 - local
35 cd "$2" &&
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/\(.*\)'` &&
48 case "$name" in
49 *^*) continue;;
50 esac
51 if test -n "$use_separate_remote" &&
52 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
53 then
54 tname="remotes/$origin/$branch_name"
55 else
56 tname=$name
58 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
59 done <"$clone_tmp/refs"
60 rm -fr "$clone_tmp"
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.
66 copy_refs='
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");
74 my $tag_top = "tags";
76 sub store {
77 my ($sha1, $name, $top) = @_;
78 $name = "$git_dir/refs/$top/$name";
79 mkpath(dirname($name));
80 open O, ">", "$name";
81 print O "$sha1\n";
82 close O;
85 open FH, "<", "$git_dir/CLONE_HEAD";
86 while (<FH>) {
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";
91 print O "$sha1\n";
92 close O;
93 next;
95 if ($name =~ s/^refs\/heads\///) {
96 store($sha1, $name, $branch_top);
97 next;
99 if ($name =~ s/^refs\/tags\///) {
100 store($sha1, $name, $tag_top);
101 next;
104 close FH;
107 quiet=
108 local=no
109 use_local=no
110 local_shared=no
111 unset template
112 no_checkout=
113 upload_pack=
114 bare=
115 reference=
116 origin=
117 origin_override=
118 use_separate_remote=
119 while
120 case "$#,$1" in
121 0,*) break ;;
122 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
123 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
124 no_checkout=yes ;;
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 ;;
131 *,--template)
132 shift; template="--template=$1" ;;
133 *,--template=*)
134 template="$1" ;;
135 *,-q|*,--quiet) quiet=-q ;;
136 *,--use-separate-remote)
137 use_separate_remote=t ;;
138 1,--reference) usage ;;
139 *,--reference)
140 shift; reference="$1" ;;
141 *,--reference=*)
142 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
143 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
144 case "$2" in
146 usage ;;
147 */*)
148 die "'$2' is not suitable for an origin name"
149 esac
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."
154 origin_override=yes
155 origin="$2"; shift
157 1,-u|1,--upload-pack) usage ;;
158 *,-u|*,--upload-pack)
159 shift
160 upload_pack="--exec=$1" ;;
161 *,-*) usage ;;
162 *) break ;;
163 esac
165 shift
166 done
168 repo="$1"
169 test -n "$repo" ||
170 die 'you must specify a repository to clone.'
172 # --bare implies --no-checkout
173 if test yes = "$bare"
174 then
175 if test yes = "$origin_override"
176 then
177 die '--bare and --origin $origin options are incompatible.'
179 if test t = "$use_separate_remote"
180 then
181 die '--bare and --use-separate-remote options are incompatible.'
183 no_checkout=yes
186 if test -z "$origin"
187 then
188 origin=origin
191 # Turn the source into an absolute path if
192 # it is local
193 if base=$(get_repo_base "$repo"); then
194 repo="$base"
195 local=yes
198 dir="$2"
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."
202 mkdir -p "$dir" &&
203 D=$(cd "$dir" && pwd) &&
204 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
205 case "$bare" in
206 yes)
207 GIT_DIR="$D" ;;
209 GIT_DIR="$D/.git" ;;
210 esac && export GIT_DIR && git-init-db ${template+"$template"} || usage
212 if test -n "$reference"
213 then
214 if test -d "$reference"
215 then
216 if test -d "$reference/.git/objects"
217 then
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 &&
225 cd reference-tmp &&
226 tar xf -)
227 else
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
236 yes,yes)
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
251 then
253 fi &&
254 rm -f "$GIT_DIR/objects/sample" &&
255 cd "$repo" &&
256 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
258 yes)
259 mkdir -p "$GIT_DIR/objects/info"
260 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
262 esac
263 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
266 case "$repo" in
267 rsync://*)
268 rsync $quiet -av --ignore-existing \
269 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
270 exit
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"
278 then
279 ( cd "$D" &&
280 . git-parse-remote &&
281 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
282 while read alt
284 case "$alt" in 'bad alternate: '*) die "$alt";; esac
285 case "$quiet" in
286 '') echo >&2 "Getting alternate: $alt" ;;
287 esac
288 rsync $quiet -av --ignore-existing \
289 --exclude info "$alt" "$GIT_DIR/objects" || exit
290 done
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@@"
297 then
298 clone_dumb_http "$repo" "$D"
299 else
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."
310 esac
312 esac
313 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
315 if test -f "$GIT_DIR/CLONE_HEAD"
316 then
317 # Read git-fetch-pack -k output and store the remote branches.
318 @@PERL@@ -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
319 exit
322 cd "$D" || exit
324 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
325 then
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" ;;
330 esac
332 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
333 case "$head_sha1" in
334 'ref: refs/'*)
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"`
342 esac
344 # The name under $remote_top the remote HEAD seems to point at.
345 head_points_at=$(
347 echo "master"
348 cd "$GIT_DIR/$remote_top" &&
349 find . -type f -print | sed -e 's/^\.\///'
350 ) | (
351 done=f
352 while read name
354 test t = $done && continue
355 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
356 if test "$head_sha1" = "$branch_tip"
357 then
358 echo "$name"
359 done=t
361 done
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" ;;
375 esac &&
376 echo >"$GIT_DIR/remotes/$origin" \
377 "URL: $repo
378 Pull: refs/heads/$head_points_at:$origin_track" &&
379 (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
380 while read dotslref
382 name=`expr "$dotslref" : './\(.*\)'`
383 if test "z$head_points_at" = "z$name"
384 then
385 continue
387 if test "$use_separate_remote" = '' &&
388 test "z$origin" = "z$name"
389 then
390 continue
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"
399 esac
400 esac
402 case "$no_checkout" in
404 test "z$quiet" = z && v=-v || v=
405 git-read-tree -m -u $v HEAD HEAD
406 esac
408 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
410 trap - 0