Fix replacing of a directory with a file/symlink in git-checkout-cache
[git/jnareb-git.git] / git-clone-script
blob88e2c1f70686d35e6f2d9070931e40ff3ae7e1a5
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 usage() {
9 echo >&2 "* git clone [-l] [-q] [-u <upload-pack>] <repo> <dir>"
10 exit 1
13 get_repo_base() {
14 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
17 quiet=
18 use_local=no
19 upload_pack=
20 while
21 case "$#,$1" in
22 0,*) break ;;
23 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
24 *,-q|*,--quiet) quiet=-q ;;
25 1,-u|*,--upload-pack) usage ;;
26 *,-u|*,--upload-pack)
27 shift
28 upload_pack="--exec=$2" ;;
29 *,-*) usage ;;
30 *) break ;;
31 esac
33 shift
34 done
36 # Turn the source into an absolute path if
37 # it is local
38 repo="$1"
39 local=no
40 if base=$(get_repo_base "$repo"); then
41 repo="$base"
42 local=yes
45 dir="$2"
46 mkdir "$dir" &&
47 D=$(
48 (cd "$dir" && git-init-db && pwd)
49 ) &&
50 test -d "$D" || usage
52 # We do local magic only when the user tells us to.
53 case "$local,$use_local" in
54 yes,yes)
55 ( cd "$repo/objects" ) || {
56 echo >&2 "-l flag seen but $repo is not local."
57 exit 1
60 # See if we can hardlink and drop "l" if not.
61 sample_file=$(cd "$repo" && \
62 find objects -type f -print | sed -e 1q)
64 # objects directory should not be empty since we are cloning!
65 test -f "$repo/$sample_file" || exit
68 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
69 then
70 l=l
71 fi &&
72 rm -f "$D/.git/objects/sample" &&
73 cd "$repo" &&
74 find objects -type f -print |
75 cpio -puamd$l "$D/.git/" || exit 1
77 # Make a duplicate of refs and HEAD pointer
78 HEAD=
79 if test -f "$repo/HEAD"
80 then
81 HEAD=HEAD
83 tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1
84 exit 0
86 esac
88 case "$repo" in
89 rsync://*)
90 rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" &&
91 rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/"
93 http://*)
94 echo "Somebody should add http fetch" >&2
95 exit 1
98 cd "$D" && case "$upload_pack" in
99 '') git-clone-pack $quiet "$repo" ;;
100 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
101 esac
103 esac