git-apply(1): document missing options and improve existing ones
[git.git] / git-clone.sh
blob7060bdab01f696c4a664fb243aac96b9a786f2f7
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 [--template=<template_directory>] [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <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_DIR/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 "z$refname" : 'zrefs/\(.*\)'` &&
42 case "$name" in
43 *^*) continue;;
44 esac
45 if test -n "$use_separate_remote" &&
46 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
47 then
48 tname="remotes/$origin/$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" ||
56 rm -f "$GIT_DIR/REMOTE_HEAD"
59 # Read git-fetch-pack -k output and store the remote branches.
60 copy_refs='
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");
68 my $tag_top = "tags";
70 sub store {
71 my ($sha1, $name, $top) = @_;
72 $name = "$git_dir/refs/$top/$name";
73 mkpath(dirname($name));
74 open O, ">", "$name";
75 print O "$sha1\n";
76 close O;
79 open FH, "<", "$git_dir/CLONE_HEAD";
80 while (<FH>) {
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";
85 print O "$sha1\n";
86 close O;
87 next;
89 if ($name =~ s/^refs\/heads\///) {
90 store($sha1, $name, $branch_top);
91 next;
93 if ($name =~ s/^refs\/tags\///) {
94 store($sha1, $name, $tag_top);
95 next;
98 close FH;
101 quiet=
102 local=no
103 use_local=no
104 local_shared=no
105 unset template
106 no_checkout=
107 upload_pack=
108 bare=
109 reference=
110 origin=
111 origin_override=
112 use_separate_remote=
113 while
114 case "$#,$1" in
115 0,*) break ;;
116 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
117 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
118 no_checkout=yes ;;
119 *,--na|*,--nak|*,--nake|*,--naked|\
120 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
121 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
122 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
123 local_shared=yes; use_local=yes ;;
124 1,--template) usage ;;
125 *,--template)
126 shift; template="--template=$1" ;;
127 *,--template=*)
128 template="$1" ;;
129 *,-q|*,--quiet) quiet=-q ;;
130 *,--use-separate-remote)
131 use_separate_remote=t ;;
132 1,--reference) usage ;;
133 *,--reference)
134 shift; reference="$1" ;;
135 *,--reference=*)
136 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
137 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
138 case "$2" in
140 usage ;;
141 */*)
142 echo >&2 "'$2' is not suitable for an origin name"
143 exit 1
144 esac
145 git-check-ref-format "heads/$2" || {
146 echo >&2 "'$2' is not suitable for a branch name"
147 exit 1
149 test -z "$origin_override" || {
150 echo >&2 "Do not give more than one --origin options."
151 exit 1
153 origin_override=yes
154 origin="$2"; shift
156 1,-u|1,--upload-pack) usage ;;
157 *,-u|*,--upload-pack)
158 shift
159 upload_pack="--exec=$1" ;;
160 *,-*) usage ;;
161 *) break ;;
162 esac
164 shift
165 done
167 repo="$1"
168 if test -z "$repo"
169 then
170 echo >&2 'you must specify a repository to clone.'
171 exit 1
174 # --bare implies --no-checkout
175 if test yes = "$bare"
176 then
177 if test yes = "$origin_override"
178 then
179 echo >&2 '--bare and --origin $origin options are incompatible.'
180 exit 1
182 if test t = "$use_separate_remote"
183 then
184 echo >&2 '--bare and --use-separate-remote options are incompatible.'
185 exit 1
187 no_checkout=yes
190 if test -z "$origin"
191 then
192 origin=origin
195 # Turn the source into an absolute path if
196 # it is local
197 if base=$(get_repo_base "$repo"); then
198 repo="$base"
199 local=yes
202 dir="$2"
203 # Try using "humanish" part of source repo if user didn't specify one
204 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
205 [ -e "$dir" ] && echo "$dir already exists." && usage
206 mkdir -p "$dir" &&
207 D=$(cd "$dir" && pwd) &&
208 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
209 case "$bare" in
210 yes)
211 GIT_DIR="$D" ;;
213 GIT_DIR="$D/.git" ;;
214 esac && export GIT_DIR && git-init-db ${template+"$template"} || usage
216 if test -n "$reference"
217 then
218 if test -d "$reference"
219 then
220 if test -d "$reference/.git/objects"
221 then
222 reference="$reference/.git"
224 reference=$(cd "$reference" && pwd)
225 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
226 (cd "$reference" && tar cf - refs) |
227 (cd "$GIT_DIR/refs" &&
228 mkdir reference-tmp &&
229 cd reference-tmp &&
230 tar xf -)
231 else
232 echo >&2 "$reference: not a local directory." && usage
236 rm -f "$GIT_DIR/CLONE_HEAD"
238 # We do local magic only when the user tells us to.
239 case "$local,$use_local" in
240 yes,yes)
241 ( cd "$repo/objects" ) || {
242 echo >&2 "-l flag seen but $repo is not local."
243 exit 1
246 case "$local_shared" in
248 # See if we can hardlink and drop "l" if not.
249 sample_file=$(cd "$repo" && \
250 find objects -type f -print | sed -e 1q)
252 # objects directory should not be empty since we are cloning!
253 test -f "$repo/$sample_file" || exit
256 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
257 then
259 fi &&
260 rm -f "$GIT_DIR/objects/sample" &&
261 cd "$repo" &&
262 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
264 yes)
265 mkdir -p "$GIT_DIR/objects/info"
266 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
268 esac
269 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
272 case "$repo" in
273 rsync://*)
274 rsync $quiet -av --ignore-existing \
275 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
276 exit
277 # Look at objects/info/alternates for rsync -- http will
278 # support it natively and git native ones will do it on the
279 # remote end. Not having that file is not a crime.
280 rsync -q "$repo/objects/info/alternates" \
281 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
282 rm -f "$GIT_DIR/TMP_ALT"
283 if test -f "$GIT_DIR/TMP_ALT"
284 then
285 ( cd "$D" &&
286 . git-parse-remote &&
287 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
288 while read alt
290 case "$alt" in 'bad alternate: '*) die "$alt";; esac
291 case "$quiet" in
292 '') echo >&2 "Getting alternate: $alt" ;;
293 esac
294 rsync $quiet -av --ignore-existing \
295 --exclude info "$alt" "$GIT_DIR/objects" || exit
296 done
297 rm -f "$GIT_DIR/TMP_ALT"
299 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
301 https://*|http://*)
302 if test -z "@@NO_CURL@@"
303 then
304 clone_dumb_http "$repo" "$D"
305 else
306 echo >&2 "http transport not supported, rebuild Git with curl support"
307 exit 1
311 cd "$D" && case "$upload_pack" in
312 '') git-fetch-pack --all -k $quiet "$repo" ;;
313 *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
314 esac >"$GIT_DIR/CLONE_HEAD" || {
315 echo >&2 "fetch-pack from '$repo' failed."
316 exit 1
319 esac
321 esac
322 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
324 if test -f "$GIT_DIR/CLONE_HEAD"
325 then
326 # Read git-fetch-pack -k output and store the remote branches.
327 @@PERL@@ -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
328 exit
331 cd "$D" || exit
333 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
334 then
335 # Figure out which remote branch HEAD points at.
336 case "$use_separate_remote" in
337 '') remote_top=refs/heads ;;
338 *) remote_top="refs/remotes/$origin" ;;
339 esac
341 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
342 case "$head_sha1" in
343 'ref: refs/'*)
344 # Uh-oh, the remote told us (http transport done against
345 # new style repository with a symref HEAD).
346 # Ideally we should skip the guesswork but for now
347 # opt for minimum change.
348 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
349 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
351 esac
353 # The name under $remote_top the remote HEAD seems to point at.
354 head_points_at=$(
356 echo "master"
357 cd "$GIT_DIR/$remote_top" &&
358 find . -type f -print | sed -e 's/^\.\///'
359 ) | (
360 done=f
361 while read name
363 test t = $done && continue
364 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
365 if test "$head_sha1" = "$branch_tip"
366 then
367 echo "$name"
368 done=t
370 done
374 # Write out remotes/$origin file, and update our "$head_points_at".
375 case "$head_points_at" in
377 mkdir -p "$GIT_DIR/remotes" &&
378 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
379 case "$use_separate_remote" in
380 t) origin_track="$remote_top/$head_points_at"
381 git-update-ref HEAD "$head_sha1" ;;
382 *) origin_track="$remote_top/$origin"
383 git-update-ref "refs/heads/$origin" "$head_sha1" ;;
384 esac &&
385 echo >"$GIT_DIR/remotes/$origin" \
386 "URL: $repo
387 Pull: refs/heads/$head_points_at:$origin_track" &&
388 (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
389 while read dotslref
391 name=`expr "$dotslref" : './\(.*\)'`
392 if test "z$head_points_at" = "z$name"
393 then
394 continue
396 if test "$use_separate_remote" = '' &&
397 test "z$origin" = "z$name"
398 then
399 continue
401 echo "Pull: refs/heads/${name}:$remote_top/${name}"
402 done >>"$GIT_DIR/remotes/$origin" &&
403 case "$use_separate_remote" in
405 rm -f "refs/remotes/$origin/HEAD"
406 git-symbolic-ref "refs/remotes/$origin/HEAD" \
407 "refs/remotes/$origin/$head_points_at"
408 esac
409 esac
411 case "$no_checkout" in
413 git-read-tree -m -u -v HEAD HEAD
414 esac
416 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
418 trap - 0