Merge branch 'master' of git://repo.or.cz/alt-git to sync with v1.5.4-rc0
[git/platforms/storm.git] / git-clone.sh
blob7748e23b39fbe3aefc96858ab2849f9d29ac494a
1 #!/bin/sh
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.
9 unset CDPATH
11 OPTIONS_SPEC="\
12 git-clone [options] [--] <repo> [<dir>]
14 n,no-checkout don't create a checkout
15 bare create a bare repository
16 naked create a bare repository
17 l,local to clone from a local repository
18 no-hardlinks don't use local hardlinks, always copy
19 s,shared setup as a shared repository
20 template= path to the template directory
21 q,quiet be quiet
22 reference= reference repository
23 o,origin= use <name> instead of 'origin' to track upstream
24 u,upload-pack= path to git-upload-pack on the remote
25 depth= create a shallow clone of that depth
27 use-separate-remote compatibility, do not use
28 no-separate-remote compatibility, do not use"
30 die() {
31 echo >&2 "$@"
32 exit 1
35 # Fix some commands on Windows
36 case $(uname -s) in
37 *MINGW*)
38 # Windows has its own (incompatible) find
39 find () {
40 /usr/bin/find "$@"
42 # need an emulation of cpio
43 cpio() {
44 case "$1" in
45 -pumd) cp_arg=-pr;;
46 -pumdl) cp_arg=-lr;;
47 *) die "cpio $1 unexpected";;
48 esac
49 # copy only files and empty directories
50 prev=
51 while read f; do
52 if test -d "$f"; then
53 # here we assume that directories are listed after
54 # its files (aka 'find -depth'), hence, a directory
55 # that is not empty will be a leading sub-string
56 # of the preceding entry
57 case "$prev" in
58 "$f"/* ) ;;
59 *) echo "$f";;
60 esac
61 else
62 echo "$f"
64 prev="$f"
65 done |
66 xargs --no-run-if-empty \
67 cp $cp_arg --target-directory="$2" --parents
70 esac
72 usage() {
73 exec "$0" -h
76 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
78 get_repo_base() {
80 cd "`/bin/pwd -W`" &&
81 cd "$1" || cd "$1.git" &&
83 cd .git
84 pwd -W
86 ) 2>/dev/null
89 if [ -n "$GIT_SSL_NO_VERIFY" -o \
90 "`git config --bool http.sslVerify`" = false ]; then
91 curl_extra_args="-k"
94 http_fetch () {
95 # $1 = Remote, $2 = Local
96 curl -nsfL $curl_extra_args "$1" >"$2" ||
97 case $? in
98 126|127) exit ;;
99 *) return $? ;;
100 esac
103 clone_dumb_http () {
104 # $1 - remote, $2 - local
105 cd "$2" &&
106 clone_tmp="$GIT_DIR/clone-tmp" &&
107 mkdir -p "$clone_tmp" || exit 1
108 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
109 "`git config --bool http.noEPSV`" = true ]; then
110 curl_extra_args="${curl_extra_args} --disable-epsv"
112 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
113 die "Cannot get remote repository information.
114 Perhaps git-update-server-info needs to be run there?"
115 test "z$quiet" = z && v=-v || v=
116 while read sha1 refname
118 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
119 case "$name" in
120 *^*) continue;;
121 esac
122 case "$bare,$name" in
123 yes,* | ,heads/* | ,tags/*) ;;
124 *) continue ;;
125 esac
126 if test -n "$use_separate_remote" &&
127 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
128 then
129 tname="remotes/$origin/$branch_name"
130 else
131 tname=$name
133 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
134 done <"$clone_tmp/refs"
135 rm -fr "$clone_tmp"
136 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
137 rm -f "$GIT_DIR/REMOTE_HEAD"
138 if test -f "$GIT_DIR/REMOTE_HEAD"; then
139 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
140 case "$head_sha1" in
141 'ref: refs/'*)
144 git-http-fetch $v -a "$head_sha1" "$1" ||
145 rm -f "$GIT_DIR/REMOTE_HEAD"
147 esac
151 quiet=
152 local=no
153 use_local_hardlink=yes
154 local_shared=no
155 unset template
156 no_checkout=
157 upload_pack=
158 bare=
159 reference=
160 origin=
161 origin_override=
162 use_separate_remote=t
163 depth=
164 no_progress=
165 local_explicitly_asked_for=
166 test -t 1 || no_progress=--no-progress
168 while test $# != 0
170 case "$1" in
171 -n|--no-checkout)
172 no_checkout=yes ;;
173 --naked|--bare)
174 bare=yes ;;
175 -l|--local)
176 local_explicitly_asked_for=yes
177 use_local_hardlink=yes
179 --no-hardlinks)
180 use_local_hardlink=no ;;
181 -s|--shared)
182 local_shared=yes ;;
183 --template)
184 shift; template="--template=$1" ;;
185 -q|--quiet)
186 quiet=-q ;;
187 --use-separate-remote|--no-separate-remote)
188 die "clones are always made with separate-remote layout" ;;
189 --reference)
190 shift; reference="$1" ;;
191 -o,--origin)
192 shift;
193 case "$1" in
195 usage ;;
196 */*)
197 die "'$1' is not suitable for an origin name"
198 esac
199 git check-ref-format "heads/$1" ||
200 die "'$1' is not suitable for a branch name"
201 test -z "$origin_override" ||
202 die "Do not give more than one --origin options."
203 origin_override=yes
204 origin="$1"
206 -u|--upload-pack)
207 shift
208 upload_pack="--upload-pack=$1" ;;
209 --depth)
210 shift
211 depth="--depth=$1" ;;
213 shift
214 break ;;
216 usage ;;
217 esac
218 shift
219 done
221 repo="$1"
222 test -n "$repo" ||
223 die 'you must specify a repository to clone.'
225 # --bare implies --no-checkout and --no-separate-remote
226 if test yes = "$bare"
227 then
228 if test yes = "$origin_override"
229 then
230 die '--bare and --origin $origin options are incompatible.'
232 no_checkout=yes
233 use_separate_remote=
236 if test -z "$origin"
237 then
238 origin=origin
241 # Turn the source into an absolute path if
242 # it is local
243 if base=$(get_repo_base "$repo"); then
244 repo="$base"
245 if test -z "$depth"
246 then
247 local=yes
251 dir="$2"
252 # Try using "humanish" part of source repo if user didn't specify one
253 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
254 [ -e "$dir" ] && die "destination directory '$dir' already exists."
255 [ yes = "$bare" ] && unset GIT_WORK_TREE
256 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
257 die "working tree '$GIT_WORK_TREE' already exists."
260 cleanup() {
261 err=$?
262 test -z "$D" && rm -rf "$dir"
263 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
264 cd ..
265 test -n "$D" && rm -rf "$D"
266 test -n "$W" && rm -rf "$W"
267 exit $err
269 trap cleanup 0
270 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
271 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
272 W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
273 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
274 GIT_DIR="$D"
275 else
276 GIT_DIR="$D/.git"
277 fi &&
278 export GIT_DIR &&
279 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
281 if test -n "$bare"
282 then
283 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
286 if test -n "$reference"
287 then
288 ref_git=
289 if test -d "$reference"
290 then
291 if test -d "$reference/.git/objects"
292 then
293 ref_git="$reference/.git"
294 elif test -d "$reference/objects"
295 then
296 ref_git="$reference"
299 if test -n "$ref_git"
300 then
301 ref_git=$(cd "$ref_git" && pwd)
302 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
304 GIT_DIR="$ref_git" git for-each-ref \
305 --format='%(objectname) %(*objectname)'
307 while read a b
309 test -z "$a" ||
310 git update-ref "refs/reference-tmp/$a" "$a"
311 test -z "$b" ||
312 git update-ref "refs/reference-tmp/$b" "$b"
313 done
314 else
315 die "reference repository '$reference' is not a local directory."
319 rm -f "$GIT_DIR/CLONE_HEAD"
321 # We do local magic only when the user tells us to.
322 case "$local" in
323 yes)
324 ( cd "$repo/objects" ) ||
325 die "cannot chdir to local '$repo/objects'."
327 if test "$local_shared" = yes
328 then
329 mkdir -p "$GIT_DIR/objects/info"
330 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
331 else
332 l= &&
333 if test "$use_local_hardlink" = yes
334 then
335 # See if we can hardlink and drop "l" if not.
336 sample_file=$(cd "$repo" && \
337 find objects -type f -print | sed -e 1q)
338 # objects directory should not be empty because
339 # we are cloning!
340 test -f "$repo/$sample_file" ||
341 die "fatal: cannot clone empty repository"
342 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
343 then
344 rm -f "$GIT_DIR/objects/sample"
346 elif test -n "$local_explicitly_asked_for"
347 then
348 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
350 fi &&
351 cd "$repo" &&
352 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
354 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
357 case "$repo" in
358 rsync://*)
359 case "$depth" in
360 "") ;;
361 *) die "shallow over rsync not supported" ;;
362 esac
363 rsync $quiet -av --ignore-existing \
364 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
365 exit
366 # Look at objects/info/alternates for rsync -- http will
367 # support it natively and git native ones will do it on the
368 # remote end. Not having that file is not a crime.
369 rsync -q "$repo/objects/info/alternates" \
370 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
371 rm -f "$GIT_DIR/TMP_ALT"
372 if test -f "$GIT_DIR/TMP_ALT"
373 then
374 ( cd "$D" &&
375 . git-parse-remote &&
376 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
377 while read alt
379 case "$alt" in 'bad alternate: '*) die "$alt";; esac
380 case "$quiet" in
381 '') echo >&2 "Getting alternate: $alt" ;;
382 esac
383 rsync $quiet -av --ignore-existing \
384 --exclude info "$alt" "$GIT_DIR/objects" || exit
385 done
386 rm -f "$GIT_DIR/TMP_ALT"
388 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
390 https://*|http://*|ftp://*)
391 case "$depth" in
392 "") ;;
393 *) die "shallow over http or ftp not supported" ;;
394 esac
395 if test -z "@@NO_CURL@@"
396 then
397 clone_dumb_http "$repo" "$D"
398 else
399 die "http transport not supported, rebuild Git with curl support"
403 case "$upload_pack" in
404 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
405 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
406 esac >"$GIT_DIR/CLONE_HEAD" ||
407 die "fetch-pack from '$repo' failed."
409 esac
411 esac
412 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
414 if test -f "$GIT_DIR/CLONE_HEAD"
415 then
416 # Read git-fetch-pack -k output and store the remote branches.
417 if [ -n "$use_separate_remote" ]
418 then
419 branch_top="remotes/$origin"
420 else
421 branch_top="heads"
423 tag_top="tags"
424 while read sha1 name
426 case "$name" in
427 *'^{}')
428 continue ;;
429 HEAD)
430 destname="REMOTE_HEAD" ;;
431 refs/heads/*)
432 destname="refs/$branch_top/${name#refs/heads/}" ;;
433 refs/tags/*)
434 destname="refs/$tag_top/${name#refs/tags/}" ;;
436 continue ;;
437 esac
438 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
439 done < "$GIT_DIR/CLONE_HEAD"
442 if test -n "$W"; then
443 cd "$W" || exit
444 else
445 cd "$D" || exit
448 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
449 then
450 # a non-bare repository is always in separate-remote layout
451 remote_top="refs/remotes/$origin"
452 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
453 case "$head_sha1" in
454 'ref: refs/'*)
455 # Uh-oh, the remote told us (http transport done against
456 # new style repository with a symref HEAD).
457 # Ideally we should skip the guesswork but for now
458 # opt for minimum change.
459 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
460 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
462 esac
464 # The name under $remote_top the remote HEAD seems to point at.
465 head_points_at=$(
467 test -f "$GIT_DIR/$remote_top/master" && echo "master"
468 cd "$GIT_DIR/$remote_top" &&
469 find . -type f -print | sed -e 's/^\.\///'
470 ) | (
471 done=f
472 while read name
474 test t = $done && continue
475 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
476 if test "$head_sha1" = "$branch_tip"
477 then
478 echo "$name"
479 done=t
481 done
485 # Upstream URL
486 git config remote."$origin".url "$repo" &&
488 # Set up the mappings to track the remote branches.
489 git config remote."$origin".fetch \
490 "+refs/heads/*:$remote_top/*" '^$' &&
492 # Write out remote.$origin config, and update our "$head_points_at".
493 case "$head_points_at" in
495 # Local default branch
496 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
498 # Tracking branch for the primary branch at the remote.
499 git update-ref HEAD "$head_sha1" &&
501 rm -f "refs/remotes/$origin/HEAD"
502 git symbolic-ref "refs/remotes/$origin/HEAD" \
503 "refs/remotes/$origin/$head_points_at" &&
505 git config branch."$head_points_at".remote "$origin" &&
506 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
509 # Source had detached HEAD pointing nowhere
510 git update-ref --no-deref HEAD "$head_sha1" &&
511 rm -f "refs/remotes/$origin/HEAD"
513 esac
515 case "$no_checkout" in
517 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
518 git read-tree -m -u $v HEAD HEAD
519 esac
521 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
523 trap - 0