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.
9unset CDPATH
10
11usage() {
12 echo >&2 "Usage: $0 [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-n] <repo> [<dir>]"
13 exit 1
14}
15
16get_repo_base() {
17 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18}
19
20if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21 curl_extra_args="-k"
22fi
23
24http_fetch () {
25 # $1 = Remote, $2 = Local
26 curl -nsfL $curl_extra_args "$1" >"$2"
27}
28
29clone_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 echo >&2 "Cannot get remote repository information.
36Perhaps git-update-server-info needs to be run there?"
37 exit 1;
38 }
39 while read sha1 refname
40 do
41 name=`expr "$refname" : 'refs/\(.*\)'` &&
42 case "$name" in
43 *^*) ;;
44 *)
45 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
46 esac
47 done <"$clone_tmp/refs"
48 rm -fr "$clone_tmp"
49}
50
51quiet=
52use_local=no
53local_shared=no
54no_checkout=
55upload_pack=
56bare=
57origin=origin
58origin_override=
59while
60 case "$#,$1" in
61 0,*) break ;;
62 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
63 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
64 no_checkout=yes ;;
65 *,--na|*,--nak|*,--nake|*,--naked|\
66 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
67 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
68 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
69 local_shared=yes; use_local=yes ;;
70 *,-q|*,--quiet) quiet=-q ;;
71 1,-o) usage;;
72 *,-o)
73 git-check-ref-format "$2" || {
74 echo >&2 "'$2' is not suitable for a branch name"
75 exit 1
76 }
77 test -z "$origin_override" || {
78 echo >&2 "Do not give more than one -o options."
79 exit 1
80 }
81 origin_override=yes
82 origin="$2"; shift
83 ;;
84 1,-u|1,--upload-pack) usage ;;
85 *,-u|*,--upload-pack)
86 shift
87 upload_pack="--exec=$1" ;;
88 *,-*) usage ;;
89 *) break ;;
90 esac
91do
92 shift
93done
94
95# --bare implies --no-checkout
96if test yes = "$bare"
97then
98 if test yes = "$origin_override"
99 then
100 echo >&2 '--bare and -o $origin options are incompatible.'
101 exit 1
102 fi
103 no_checkout=yes
104fi
105
106# Turn the source into an absolute path if
107# it is local
108repo="$1"
109local=no
110if base=$(get_repo_base "$repo"); then
111 repo="$base"
112 local=yes
113fi
114
115dir="$2"
116# Try using "humanish" part of source repo if user didn't specify one
117[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
118[ -e "$dir" ] && echo "$dir already exists." && usage
119mkdir -p "$dir" &&
120D=$(cd "$dir" && pwd) &&
121case "$bare" in
122yes) GIT_DIR="$D" ;;
123*) GIT_DIR="$D/.git" ;;
124esac && export GIT_DIR && git-init-db || usage
125case "$bare" in
126yes)
127 GIT_DIR="$D" ;;
128*)
129 GIT_DIR="$D/.git" ;;
130esac
131
132# We do local magic only when the user tells us to.
133case "$local,$use_local" in
134yes,yes)
135 ( cd "$repo/objects" ) || {
136 echo >&2 "-l flag seen but $repo is not local."
137 exit 1
138 }
139
140 case "$local_shared" in
141 no)
142 # See if we can hardlink and drop "l" if not.
143 sample_file=$(cd "$repo" && \
144 find objects -type f -print | sed -e 1q)
145
146 # objects directory should not be empty since we are cloning!
147 test -f "$repo/$sample_file" || exit
148
149 l=
150 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
151 then
152 l=l
153 fi &&
154 rm -f "$GIT_DIR/objects/sample" &&
155 cd "$repo" &&
156 find objects -depth -print | cpio -puamd$l "$GIT_DIR/" || exit 1
157 ;;
158 yes)
159 mkdir -p "$GIT_DIR/objects/info"
160 {
161 test -f "$repo/objects/info/alternates" &&
162 cat "$repo/objects/info/alternates";
163 echo "$repo/objects"
164 } >"$GIT_DIR/objects/info/alternates"
165 ;;
166 esac
167
168 # Make a duplicate of refs and HEAD pointer
169 HEAD=
170 if test -f "$repo/HEAD"
171 then
172 HEAD=HEAD
173 fi
174 (cd "$repo" && tar cf - refs $HEAD) |
175 (cd "$GIT_DIR" && tar xf -) || exit 1
176 ;;
177*)
178 case "$repo" in
179 rsync://*)
180 rsync $quiet -av --ignore-existing \
181 --exclude info "$repo/objects/" "$GIT_DIR/objects/" &&
182 rsync $quiet -av --ignore-existing \
183 --exclude info "$repo/refs/" "$GIT_DIR/refs/" || exit
184
185 # Look at objects/info/alternates for rsync -- http will
186 # support it natively and git native ones will do it on the
187 # remote end. Not having that file is not a crime.
188 rsync -q "$repo/objects/info/alternates" \
189 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
190 rm -f "$GIT_DIR/TMP_ALT"
191 if test -f "$GIT_DIR/TMP_ALT"
192 then
193 ( cd "$D" &&
194 . git-parse-remote &&
195 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
196 while read alt
197 do
198 case "$alt" in 'bad alternate: '*) die "$alt";; esac
199 case "$quiet" in
200 '') echo >&2 "Getting alternate: $alt" ;;
201 esac
202 rsync $quiet -av --ignore-existing \
203 --exclude info "$alt" "$GIT_DIR/objects" || exit
204 done
205 rm -f "$GIT_DIR/TMP_ALT"
206 fi
207 ;;
208 http://*)
209 if test -z "@@NO_CURL@@"
210 then
211 clone_dumb_http "$repo" "$D"
212 else
213 echo >&2 "http transport not supported, rebuild Git with curl support"
214 exit 1
215 fi
216 ;;
217 *)
218 cd "$D" && case "$upload_pack" in
219 '') git-clone-pack $quiet "$repo" ;;
220 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
221 esac || {
222 echo >&2 "clone-pack from '$repo' failed."
223 exit 1
224 }
225 ;;
226 esac
227 ;;
228esac
229
230cd "$D" || exit
231
232if test -f "$GIT_DIR/HEAD" && test -z "$bare"
233then
234 head_points_at=`git-symbolic-ref HEAD`
235 case "$head_points_at" in
236 refs/heads/*)
237 head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
238 mkdir -p "$GIT_DIR/remotes" &&
239 echo >"$GIT_DIR/remotes/origin" \
240 "URL: $repo
241Pull: $head_points_at:$origin" &&
242 git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) &&
243 (cd "$GIT_DIR" && find "refs/heads" -type f -print) |
244 while read ref
245 do
246 head=`expr "$ref" : 'refs/heads/\(.*\)'` &&
247 test "$head_points_at" = "$head" ||
248 test "$origin" = "$head" ||
249 echo "Pull: ${head}:${head}"
250 done >>"$GIT_DIR/remotes/origin"
251 esac
252
253 case "$no_checkout" in
254 '')
255 git checkout
256 esac
257fi