t5300-pack-object: Set up the objects for --strict tests only once.
[git/mingw.git] / git-clone.sh
blob73afcf52efd6f659663d0565d502b337d3f6d60a
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
69 # pwd must return a path with a drive letter
70 bin_pwd() {
71 # there are no symlinks to resolve: /bin/pwd is not needed
72 builtin pwd -W
74 pwd() {
75 builtin pwd -W
79 bin_pwd() {
80 /bin/pwd
83 esac
85 usage() {
86 exec "$0" -h
89 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
91 get_repo_base() {
93 cd "$(bin_pwd)" &&
94 cd "$1" || cd "$1.git" &&
96 cd .git
97 pwd
99 ) 2>/dev/null
102 if [ -n "$GIT_SSL_NO_VERIFY" -o \
103 "`git config --bool http.sslVerify`" = false ]; then
104 curl_extra_args="-k"
107 http_fetch () {
108 # $1 = Remote, $2 = Local
109 curl -nsfL $curl_extra_args "$1" >"$2"
110 curl_exit_status=$?
111 case $curl_exit_status in
112 126|127) exit ;;
113 *) return $curl_exit_status ;;
114 esac
117 clone_dumb_http () {
118 # $1 - remote, $2 - local
119 cd "$2" &&
120 clone_tmp="$GIT_DIR/clone-tmp" &&
121 mkdir -p "$clone_tmp" || exit 1
122 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
123 "`git config --bool http.noEPSV`" = true ]; then
124 curl_extra_args="${curl_extra_args} --disable-epsv"
126 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
127 die "Cannot get remote repository information.
128 Perhaps git-update-server-info needs to be run there?"
129 test "z$quiet" = z && v=-v || v=
130 while read sha1 refname
132 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
133 case "$name" in
134 *^*) continue;;
135 esac
136 case "$bare,$name" in
137 yes,* | ,heads/* | ,tags/*) ;;
138 *) continue ;;
139 esac
140 if test -n "$use_separate_remote" &&
141 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
142 then
143 tname="remotes/$origin/$branch_name"
144 else
145 tname=$name
147 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
148 done <"$clone_tmp/refs"
149 rm -fr "$clone_tmp"
150 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
151 rm -f "$GIT_DIR/REMOTE_HEAD"
152 if test -f "$GIT_DIR/REMOTE_HEAD"; then
153 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
154 case "$head_sha1" in
155 'ref: refs/'*)
158 git-http-fetch $v -a "$head_sha1" "$1" ||
159 rm -f "$GIT_DIR/REMOTE_HEAD"
161 esac
165 quiet=
166 local=no
167 use_local_hardlink=yes
168 local_shared=no
169 unset template
170 no_checkout=
171 upload_pack=
172 bare=
173 reference=
174 origin=
175 origin_override=
176 use_separate_remote=t
177 depth=
178 no_progress=
179 local_explicitly_asked_for=
180 test -t 1 || no_progress=--no-progress
182 while test $# != 0
184 case "$1" in
185 -n|--no-checkout)
186 no_checkout=yes ;;
187 --naked|--bare)
188 bare=yes ;;
189 -l|--local)
190 local_explicitly_asked_for=yes
191 use_local_hardlink=yes
193 --no-hardlinks)
194 use_local_hardlink=no ;;
195 -s|--shared)
196 local_shared=yes ;;
197 --template)
198 shift; template="--template=$1" ;;
199 -q|--quiet)
200 quiet=-q ;;
201 --use-separate-remote|--no-separate-remote)
202 die "clones are always made with separate-remote layout" ;;
203 --reference)
204 shift; reference="$1" ;;
205 -o|--origin)
206 shift;
207 case "$1" in
209 usage ;;
210 */*)
211 die "'$1' is not suitable for an origin name"
212 esac
213 git check-ref-format "heads/$1" ||
214 die "'$1' is not suitable for a branch name"
215 test -z "$origin_override" ||
216 die "Do not give more than one --origin options."
217 origin_override=yes
218 origin="$1"
220 -u|--upload-pack)
221 shift
222 upload_pack="--upload-pack=$1" ;;
223 --depth)
224 shift
225 depth="--depth=$1" ;;
227 shift
228 break ;;
230 usage ;;
231 esac
232 shift
233 done
235 repo="$1"
236 test -n "$repo" ||
237 die 'you must specify a repository to clone.'
239 # --bare implies --no-checkout and --no-separate-remote
240 if test yes = "$bare"
241 then
242 if test yes = "$origin_override"
243 then
244 die '--bare and --origin $origin options are incompatible.'
246 no_checkout=yes
247 use_separate_remote=
250 if test -z "$origin"
251 then
252 origin=origin
255 # Turn the source into an absolute path if
256 # it is local
257 if base=$(get_repo_base "$repo"); then
258 repo="$base"
259 if test -z "$depth"
260 then
261 local=yes
263 elif test -f "$repo"
264 then
265 case "$repo" in /*) ;; *) repo="$PWD/$repo" ;; esac
268 # Decide the directory name of the new repository
269 if test -n "$2"
270 then
271 dir="$2"
272 else
273 # Derive one from the repository name
274 # Try using "humanish" part of source repo if user didn't specify one
275 if test -f "$repo"
276 then
277 # Cloning from a bundle
278 dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
279 else
280 dir=$(echo "$repo" |
281 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
285 [ -e "$dir" ] && die "destination directory '$dir' already exists."
286 [ yes = "$bare" ] && unset GIT_WORK_TREE
287 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
288 die "working tree '$GIT_WORK_TREE' already exists."
291 cleanup() {
292 err=$?
293 test -z "$D" && rm -rf "$dir"
294 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
295 cd ..
296 test -n "$D" && rm -rf "$D"
297 test -n "$W" && rm -rf "$W"
298 exit $err
300 trap cleanup 0
301 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
302 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
303 W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
304 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
305 GIT_DIR="$D"
306 else
307 GIT_DIR="$D/.git"
308 fi &&
309 export GIT_DIR &&
310 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
312 if test -n "$bare"
313 then
314 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
317 if test -n "$reference"
318 then
319 ref_git=
320 if test -d "$reference"
321 then
322 if test -d "$reference/.git/objects"
323 then
324 ref_git="$reference/.git"
325 elif test -d "$reference/objects"
326 then
327 ref_git="$reference"
330 if test -n "$ref_git"
331 then
332 ref_git=$(cd "$ref_git" && pwd)
333 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
335 GIT_DIR="$ref_git" git for-each-ref \
336 --format='%(objectname) %(*objectname)'
338 while read a b
340 test -z "$a" ||
341 git update-ref "refs/reference-tmp/$a" "$a"
342 test -z "$b" ||
343 git update-ref "refs/reference-tmp/$b" "$b"
344 done
345 else
346 die "reference repository '$reference' is not a local directory."
350 rm -f "$GIT_DIR/CLONE_HEAD"
352 # We do local magic only when the user tells us to.
353 case "$local" in
354 yes)
355 ( cd "$repo/objects" ) ||
356 die "cannot chdir to local '$repo/objects'."
358 if test "$local_shared" = yes
359 then
360 mkdir -p "$GIT_DIR/objects/info"
361 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
362 else
363 cpio_quiet_flag=""
364 cpio --help 2>&1 | grep -- --quiet >/dev/null && \
365 cpio_quiet_flag=--quiet
366 l= &&
367 if test "$use_local_hardlink" = yes
368 then
369 # See if we can hardlink and drop "l" if not.
370 sample_file=$(cd "$repo" && \
371 find objects -type f -print | sed -e 1q)
372 # objects directory should not be empty because
373 # we are cloning!
374 test -f "$repo/$sample_file" ||
375 die "fatal: cannot clone empty repository"
376 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
377 then
378 rm -f "$GIT_DIR/objects/sample"
380 elif test -n "$local_explicitly_asked_for"
381 then
382 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
384 fi &&
385 cd "$repo" &&
386 find objects -depth -print | cpio $cpio_quiet_flag -pumd$l "$GIT_DIR/" || \
387 exit 1
389 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
392 case "$repo" in
393 rsync://*)
394 case "$depth" in
395 "") ;;
396 *) die "shallow over rsync not supported" ;;
397 esac
398 rsync $quiet -av --ignore-existing \
399 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
400 exit
401 # Look at objects/info/alternates for rsync -- http will
402 # support it natively and git native ones will do it on the
403 # remote end. Not having that file is not a crime.
404 rsync -q "$repo/objects/info/alternates" \
405 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
406 rm -f "$GIT_DIR/TMP_ALT"
407 if test -f "$GIT_DIR/TMP_ALT"
408 then
409 ( cd "$D" &&
410 . git-parse-remote &&
411 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
412 while read alt
414 case "$alt" in 'bad alternate: '*) die "$alt";; esac
415 case "$quiet" in
416 '') echo >&2 "Getting alternate: $alt" ;;
417 esac
418 rsync $quiet -av --ignore-existing \
419 --exclude info "$alt" "$GIT_DIR/objects" || exit
420 done
421 rm -f "$GIT_DIR/TMP_ALT"
423 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
425 https://*|http://*|ftp://*)
426 case "$depth" in
427 "") ;;
428 *) die "shallow over http or ftp not supported" ;;
429 esac
430 if test -z "@@NO_CURL@@"
431 then
432 clone_dumb_http "$repo" "$D"
433 else
434 die "http transport not supported, rebuild Git with curl support"
438 if [ -f "$repo" ] ; then
439 git bundle unbundle "$repo" > "$GIT_DIR/CLONE_HEAD" ||
440 die "unbundle from '$repo' failed."
441 else
442 case "$upload_pack" in
443 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
444 *) git-fetch-pack --all -k \
445 $quiet "$upload_pack" $depth $no_progress "$repo" ;;
446 esac >"$GIT_DIR/CLONE_HEAD" ||
447 die "fetch-pack from '$repo' failed."
450 esac
452 esac
453 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
455 if test -f "$GIT_DIR/CLONE_HEAD"
456 then
457 # Read git-fetch-pack -k output and store the remote branches.
458 if [ -n "$use_separate_remote" ]
459 then
460 branch_top="remotes/$origin"
461 else
462 branch_top="heads"
464 tag_top="tags"
465 while read sha1 name
467 case "$name" in
468 *'^{}')
469 continue ;;
470 HEAD)
471 destname="REMOTE_HEAD" ;;
472 refs/heads/*)
473 destname="refs/$branch_top/${name#refs/heads/}" ;;
474 refs/tags/*)
475 destname="refs/$tag_top/${name#refs/tags/}" ;;
477 continue ;;
478 esac
479 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
480 done < "$GIT_DIR/CLONE_HEAD"
483 if test -n "$W"; then
484 cd "$W" || exit
485 else
486 cd "$D" || exit
489 if test -z "$bare"
490 then
491 # a non-bare repository is always in separate-remote layout
492 remote_top="refs/remotes/$origin"
493 head_sha1=
494 test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
495 case "$head_sha1" in
496 'ref: refs/'*)
497 # Uh-oh, the remote told us (http transport done against
498 # new style repository with a symref HEAD).
499 # Ideally we should skip the guesswork but for now
500 # opt for minimum change.
501 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
502 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
504 esac
506 # The name under $remote_top the remote HEAD seems to point at.
507 head_points_at=$(
509 test -f "$GIT_DIR/$remote_top/master" && echo "master"
510 cd "$GIT_DIR/$remote_top" &&
511 find . -type f -print | sed -e 's/^\.\///'
512 ) | (
513 done=f
514 while read name
516 test t = $done && continue
517 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
518 if test "$head_sha1" = "$branch_tip"
519 then
520 echo "$name"
521 done=t
523 done
527 # Upstream URL
528 git config remote."$origin".url "$repo" &&
530 # Set up the mappings to track the remote branches.
531 git config remote."$origin".fetch \
532 "+refs/heads/*:$remote_top/*" '^$' &&
534 # Write out remote.$origin config, and update our "$head_points_at".
535 case "$head_points_at" in
537 # Local default branch
538 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
540 # Tracking branch for the primary branch at the remote.
541 git update-ref HEAD "$head_sha1" &&
543 rm -f "refs/remotes/$origin/HEAD"
544 git symbolic-ref "refs/remotes/$origin/HEAD" \
545 "refs/remotes/$origin/$head_points_at" &&
547 git config branch."$head_points_at".remote "$origin" &&
548 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
551 if test -z "$head_sha1"
552 then
553 # Source had nonexistent ref in HEAD
554 echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
555 no_checkout=t
556 else
557 # Source had detached HEAD pointing nowhere
558 git update-ref --no-deref HEAD "$head_sha1" &&
559 rm -f "refs/remotes/$origin/HEAD"
562 esac
564 case "$no_checkout" in
566 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
567 git read-tree -m -u $v HEAD HEAD
568 esac
570 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
572 trap - 0