skip t5512 because remote does not yet work
[git/platforms/storm.git] / git-clone.sh
blob091b84414b1d906cb371f3f201e7e685dae2e0d4
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 test "@@NO_HARDLINKS@@" && use_local_hardlink=no
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 (test "@@NO_HARDLINKS@@" &&
178 echo >&2 "Warning: -l asked but hardlinks are not supported") ||
179 use_local_hardlink=yes
181 --no-hardlinks)
182 use_local_hardlink=no ;;
183 -s|--shared)
184 local_shared=yes ;;
185 --template)
186 shift; template="--template=$1" ;;
187 -q|--quiet)
188 quiet=-q ;;
189 --use-separate-remote|--no-separate-remote)
190 die "clones are always made with separate-remote layout" ;;
191 --reference)
192 shift; reference="$1" ;;
193 -o,--origin)
194 shift;
195 case "$1" in
197 usage ;;
198 */*)
199 die "'$1' is not suitable for an origin name"
200 esac
201 git check-ref-format "heads/$1" ||
202 die "'$1' is not suitable for a branch name"
203 test -z "$origin_override" ||
204 die "Do not give more than one --origin options."
205 origin_override=yes
206 origin="$1"
208 -u|--upload-pack)
209 shift
210 upload_pack="--upload-pack=$1" ;;
211 --depth)
212 shift
213 depth="--depth=$1" ;;
215 shift
216 break ;;
218 usage ;;
219 esac
220 shift
221 done
223 repo="$1"
224 test -n "$repo" ||
225 die 'you must specify a repository to clone.'
227 # --bare implies --no-checkout and --no-separate-remote
228 if test yes = "$bare"
229 then
230 if test yes = "$origin_override"
231 then
232 die '--bare and --origin $origin options are incompatible.'
234 no_checkout=yes
235 use_separate_remote=
238 if test -z "$origin"
239 then
240 origin=origin
243 # Turn the source into an absolute path if
244 # it is local
245 if base=$(get_repo_base "$repo"); then
246 repo="$base"
247 local=yes
250 dir="$2"
251 # Try using "humanish" part of source repo if user didn't specify one
252 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
253 [ -e "$dir" ] && die "destination directory '$dir' already exists."
254 [ yes = "$bare" ] && unset GIT_WORK_TREE
255 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
256 die "working tree '$GIT_WORK_TREE' already exists."
259 cleanup() {
260 err=$?
261 test -z "$D" && rm -rf "$dir"
262 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
263 cd ..
264 test -n "$D" && rm -rf "$D"
265 test -n "$W" && rm -rf "$W"
266 exit $err
268 trap cleanup 0
269 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
270 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
271 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
272 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
273 GIT_DIR="$D"
274 else
275 GIT_DIR="$D/.git"
276 fi &&
277 export GIT_DIR &&
278 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
280 if test -n "$bare"
281 then
282 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
285 if test -n "$reference"
286 then
287 ref_git=
288 if test -d "$reference"
289 then
290 if test -d "$reference/.git/objects"
291 then
292 ref_git="$reference/.git"
293 elif test -d "$reference/objects"
294 then
295 ref_git="$reference"
298 if test -n "$ref_git"
299 then
300 ref_git=$(cd "$ref_git" && pwd)
301 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
303 GIT_DIR="$ref_git" git for-each-ref \
304 --format='%(objectname) %(*objectname)'
306 while read a b
308 test -z "$a" ||
309 git update-ref "refs/reference-tmp/$a" "$a"
310 test -z "$b" ||
311 git update-ref "refs/reference-tmp/$b" "$b"
312 done
313 else
314 die "reference repository '$reference' is not a local directory."
318 rm -f "$GIT_DIR/CLONE_HEAD"
320 # We do local magic only when the user tells us to.
321 case "$local" in
322 yes)
323 ( cd "$repo/objects" ) ||
324 die "cannot chdir to local '$repo/objects'."
326 if test "$local_shared" = yes
327 then
328 mkdir -p "$GIT_DIR/objects/info"
329 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
330 else
331 l= &&
332 if test "$use_local_hardlink" = yes
333 then
334 # See if we can hardlink and drop "l" if not.
335 sample_file=$(cd "$repo" && \
336 find objects -type f -print | sed -e 1q)
337 # objects directory should not be empty because
338 # we are cloning!
339 test -f "$repo/$sample_file" || exit
340 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
341 then
342 rm -f "$GIT_DIR/objects/sample"
344 elif test -n "$local_explicitly_asked_for"
345 then
346 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
348 fi &&
349 cd "$repo" &&
350 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
352 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
355 case "$repo" in
356 rsync://*)
357 case "$depth" in
358 "") ;;
359 *) die "shallow over rsync not supported" ;;
360 esac
361 rsync $quiet -av --ignore-existing \
362 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
363 exit
364 # Look at objects/info/alternates for rsync -- http will
365 # support it natively and git native ones will do it on the
366 # remote end. Not having that file is not a crime.
367 rsync -q "$repo/objects/info/alternates" \
368 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
369 rm -f "$GIT_DIR/TMP_ALT"
370 if test -f "$GIT_DIR/TMP_ALT"
371 then
372 ( cd "$D" &&
373 . git-parse-remote &&
374 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
375 while read alt
377 case "$alt" in 'bad alternate: '*) die "$alt";; esac
378 case "$quiet" in
379 '') echo >&2 "Getting alternate: $alt" ;;
380 esac
381 rsync $quiet -av --ignore-existing \
382 --exclude info "$alt" "$GIT_DIR/objects" || exit
383 done
384 rm -f "$GIT_DIR/TMP_ALT"
386 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
388 https://*|http://*|ftp://*)
389 case "$depth" in
390 "") ;;
391 *) die "shallow over http or ftp not supported" ;;
392 esac
393 if test -z "@@NO_CURL@@"
394 then
395 clone_dumb_http "$repo" "$D"
396 else
397 die "http transport not supported, rebuild Git with curl support"
401 case "$upload_pack" in
402 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
403 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
404 esac >"$GIT_DIR/CLONE_HEAD" ||
405 die "fetch-pack from '$repo' failed."
407 esac
409 esac
410 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
412 if test -f "$GIT_DIR/CLONE_HEAD"
413 then
414 # Read git-fetch-pack -k output and store the remote branches.
415 if [ -n "$use_separate_remote" ]
416 then
417 branch_top="remotes/$origin"
418 else
419 branch_top="heads"
421 tag_top="tags"
422 while read sha1 name
424 case "$name" in
425 *'^{}')
426 continue ;;
427 HEAD)
428 destname="REMOTE_HEAD" ;;
429 refs/heads/*)
430 destname="refs/$branch_top/${name#refs/heads/}" ;;
431 refs/tags/*)
432 destname="refs/$tag_top/${name#refs/tags/}" ;;
434 continue ;;
435 esac
436 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
437 done < "$GIT_DIR/CLONE_HEAD"
440 if test -n "$W"; then
441 cd "$W" || exit
442 else
443 cd "$D" || exit
446 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
447 then
448 # a non-bare repository is always in separate-remote layout
449 remote_top="refs/remotes/$origin"
450 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
451 case "$head_sha1" in
452 'ref: refs/'*)
453 # Uh-oh, the remote told us (http transport done against
454 # new style repository with a symref HEAD).
455 # Ideally we should skip the guesswork but for now
456 # opt for minimum change.
457 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
458 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
460 esac
462 # The name under $remote_top the remote HEAD seems to point at.
463 head_points_at=$(
465 test -f "$GIT_DIR/$remote_top/master" && echo "master"
466 cd "$GIT_DIR/$remote_top" &&
467 find . -type f -print | sed -e 's/^\.\///'
468 ) | (
469 done=f
470 while read name
472 test t = $done && continue
473 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
474 if test "$head_sha1" = "$branch_tip"
475 then
476 echo "$name"
477 done=t
479 done
483 # Upstream URL
484 git config remote."$origin".url "$repo" &&
486 # Set up the mappings to track the remote branches.
487 git config remote."$origin".fetch \
488 "+refs/heads/*:$remote_top/*" '^$' &&
490 # Write out remote.$origin config, and update our "$head_points_at".
491 case "$head_points_at" in
493 # Local default branch
494 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
496 # Tracking branch for the primary branch at the remote.
497 git update-ref HEAD "$head_sha1" &&
499 rm -f "refs/remotes/$origin/HEAD"
500 git symbolic-ref "refs/remotes/$origin/HEAD" \
501 "refs/remotes/$origin/$head_points_at" &&
503 git config branch."$head_points_at".remote "$origin" &&
504 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
507 # Source had detached HEAD pointing nowhere
508 git update-ref --no-deref HEAD "$head_sha1" &&
509 rm -f "refs/remotes/$origin/HEAD"
511 esac
513 case "$no_checkout" in
515 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
516 git read-tree -m -u $v HEAD HEAD
517 esac
519 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
521 trap - 0