[PATCH] Make the test more shell generic and fix missing Solaris find option
[git.git] / git-clone.sh
1 #!/bin/sh
2 #
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.
7
8 # See git-sh-setup why.
9 unset CDPATH
10
11 usage() {
12         echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] [-n] <repo> <dir>"
13         exit 1
14 }
15
16 get_repo_base() {
17         (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18 }
19
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21     curl_extra_args="-k"
22 fi
23
24 http_fetch () {
25         # $1 = Remote, $2 = Local
26         curl -nsf $curl_extra_args "$1" >"$2"
27 }
28
29 clone_dumb_http () {
30         # $1 - remote, $2 - local
31         cd "$2" &&
32         clone_tmp='.git/clone-tmp' &&
33         mkdir -p "$clone_tmp" || exit 1
34         http_fetch "$1/info/refs" "$clone_tmp/refs" &&
35         http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
36                 echo >&2 "Cannot get remote repository information.
37 Perhaps git-update-server-info needs to be run there?"
38                 exit 1;
39         }
40         while read type name
41         do
42                 case "$type" in
43                 P) ;;
44                 *) continue ;;
45                 esac &&
46
47                 idx=`expr "$name" : '\(.*\)\.pack'`.idx
48                 http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
49                 http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
50                 git-verify-pack ".git/objects/pack/$idx" || exit 1
51         done <"$clone_tmp/packs"
52
53         while read sha1 refname
54         do
55                 name=`expr "$refname" : 'refs/\(.*\)'` &&
56                 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
57         done <"$clone_tmp/refs"
58         rm -fr "$clone_tmp"
59 }
60
61 quiet=
62 use_local=no
63 local_shared=no
64 no_checkout=
65 upload_pack=
66 while
67         case "$#,$1" in
68         0,*) break ;;
69         *,-n) no_checkout=yes ;;
70         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
71         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
72           local_shared=yes ;;
73         *,-q|*,--quiet) quiet=-q ;;
74         1,-u|1,--upload-pack) usage ;;
75         *,-u|*,--upload-pack)
76                 shift
77                 upload_pack="--exec=$1" ;;
78         *,-*) usage ;;
79         *) break ;;
80         esac
81 do
82         shift
83 done
84
85 # Turn the source into an absolute path if
86 # it is local
87 repo="$1"
88 local=no
89 if base=$(get_repo_base "$repo"); then
90         repo="$base"
91         local=yes
92 fi
93
94 dir="$2"
95 mkdir "$dir" &&
96 D=$(
97         (cd "$dir" && git-init-db && pwd)
98 ) &&
99 test -d "$D" || usage
100
101 # We do local magic only when the user tells us to.
102 case "$local,$use_local" in
103 yes,yes)
104         ( cd "$repo/objects" ) || {
105                 echo >&2 "-l flag seen but $repo is not local."
106                 exit 1
107         }
108
109         case "$local_shared" in
110         no)
111             # See if we can hardlink and drop "l" if not.
112             sample_file=$(cd "$repo" && \
113                           find objects -type f -print | sed -e 1q)
114
115             # objects directory should not be empty since we are cloning!
116             test -f "$repo/$sample_file" || exit
117
118             l=
119             if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
120             then
121                     l=l
122             fi &&
123             rm -f "$D/.git/objects/sample" &&
124             cd "$repo" &&
125             find objects -type f -print |
126             cpio -puamd$l "$D/.git/" || exit 1
127             ;;
128         yes)
129             mkdir -p "$D/.git/objects/info"
130             {
131                 test -f "$repo/objects/info/alternates" &&
132                 cat "$repo/objects/info/alternates";
133                 echo "$repo/objects"
134             } >"$D/.git/objects/info/alternates"
135             ;;
136         esac
137
138         # Make a duplicate of refs and HEAD pointer
139         HEAD=
140         if test -f "$repo/HEAD"
141         then
142                 HEAD=HEAD
143         fi
144         (cd "$repo" && tar cf - refs $HEAD) |
145         (cd "$D/.git" && tar xf -) || exit 1
146         ;;
147 *)
148         case "$repo" in
149         rsync://*)
150                 rsync $quiet -av --ignore-existing  \
151                         --exclude info "$repo/objects/" "$D/.git/objects/" &&
152                 rsync $quiet -av --ignore-existing  \
153                         --exclude info "$repo/refs/" "$D/.git/refs/" || exit
154
155                 # Look at objects/info/alternates for rsync -- http will
156                 # support it natively and git native ones will do it on the
157                 # remote end.  Not having that file is not a crime.
158                 rsync -q "$repo/objects/info/alternates" \
159                         "$D/.git/TMP_ALT" 2>/dev/null ||
160                         rm -f "$D/.git/TMP_ALT"
161                 if test -f "$D/.git/TMP_ALT"
162                 then
163                     ( cd $D &&
164                       . git-parse-remote &&
165                       resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
166                     while read alt
167                     do
168                         case "$alt" in 'bad alternate: '*) die "$alt";; esac
169                         case "$quiet" in
170                         '')     echo >&2 "Getting alternate: $alt" ;;
171                         esac
172                         rsync $quiet -av --ignore-existing  \
173                             --exclude info "$alt" "$D/.git/objects" || exit
174                     done
175                     rm -f "$D/.git/TMP_ALT"
176                 fi
177                 ;;
178         http://*)
179                 clone_dumb_http "$repo" "$D"
180                 ;;
181         *)
182                 cd "$D" && case "$upload_pack" in
183                 '') git-clone-pack $quiet "$repo" ;;
184                 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
185                 esac
186                 ;;
187         esac
188         ;;
189 esac
190
191 cd $D || exit
192
193 if test -f ".git/HEAD"
194 then
195         mkdir -p .git/remotes || exit
196         echo >.git/remotes/origin \
197         "URL: $repo
198 Pull: master:origin"
199         case "$no_checkout" in
200         '')
201                 git checkout
202         esac
203 fi