Merge branch 'sp/make'
[git/git-svn.git] / git-clone.sh
blobde51983584bb0fd015ed75704b72bec8fdb55430
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 # See git-sh-setup why.
9 unset CDPATH
11 die() {
12 echo >&2 "$@"
13 exit 1
16 usage() {
17 die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
20 get_repo_base() {
21 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
24 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
25 curl_extra_args="-k"
28 http_fetch () {
29 # $1 = Remote, $2 = Local
30 curl -nsfL $curl_extra_args "$1" >"$2"
33 clone_dumb_http () {
34 # $1 - remote, $2 - local
35 cd "$2" &&
36 clone_tmp="$GIT_DIR/clone-tmp" &&
37 mkdir -p "$clone_tmp" || exit 1
38 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
39 "`git-config --bool http.noEPSV`" = true ]; then
40 curl_extra_args="${curl_extra_args} --disable-epsv"
42 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
43 die "Cannot get remote repository information.
44 Perhaps git-update-server-info needs to be run there?"
45 while read sha1 refname
47 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
48 case "$name" in
49 *^*) continue;;
50 esac
51 case "$bare,$name" in
52 yes,* | ,heads/* | ,tags/*) ;;
53 *) continue ;;
54 esac
55 if test -n "$use_separate_remote" &&
56 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
57 then
58 tname="remotes/$origin/$branch_name"
59 else
60 tname=$name
62 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
63 done <"$clone_tmp/refs"
64 rm -fr "$clone_tmp"
65 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
66 rm -f "$GIT_DIR/REMOTE_HEAD"
69 quiet=
70 local=no
71 use_local=no
72 local_shared=no
73 unset template
74 no_checkout=
75 upload_pack=
76 bare=
77 reference=
78 origin=
79 origin_override=
80 use_separate_remote=t
81 depth=
82 no_progress=
83 test -t 1 || no_progress=--no-progress
84 while
85 case "$#,$1" in
86 0,*) break ;;
87 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
88 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
89 no_checkout=yes ;;
90 *,--na|*,--nak|*,--nake|*,--naked|\
91 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
92 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
93 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
94 local_shared=yes; use_local=yes ;;
95 1,--template) usage ;;
96 *,--template)
97 shift; template="--template=$1" ;;
98 *,--template=*)
99 template="$1" ;;
100 *,-q|*,--quiet) quiet=-q ;;
101 *,--use-separate-remote) ;;
102 *,--no-separate-remote)
103 die "clones are always made with separate-remote layout" ;;
104 1,--reference) usage ;;
105 *,--reference)
106 shift; reference="$1" ;;
107 *,--reference=*)
108 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
109 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
110 case "$2" in
112 usage ;;
113 */*)
114 die "'$2' is not suitable for an origin name"
115 esac
116 git-check-ref-format "heads/$2" ||
117 die "'$2' is not suitable for a branch name"
118 test -z "$origin_override" ||
119 die "Do not give more than one --origin options."
120 origin_override=yes
121 origin="$2"; shift
123 1,-u|1,--upload-pack) usage ;;
124 *,-u|*,--upload-pack)
125 shift
126 upload_pack="--upload-pack=$1" ;;
127 *,--upload-pack=*)
128 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
129 1,--depth) usage;;
130 *,--depth)
131 shift
132 depth="--depth=$1";;
133 *,-*) usage ;;
134 *) break ;;
135 esac
137 shift
138 done
140 repo="$1"
141 test -n "$repo" ||
142 die 'you must specify a repository to clone.'
144 # --bare implies --no-checkout and --no-separate-remote
145 if test yes = "$bare"
146 then
147 if test yes = "$origin_override"
148 then
149 die '--bare and --origin $origin options are incompatible.'
151 no_checkout=yes
152 use_separate_remote=
155 if test -z "$origin"
156 then
157 origin=origin
160 # Turn the source into an absolute path if
161 # it is local
162 if base=$(get_repo_base "$repo"); then
163 repo="$base"
164 local=yes
167 dir="$2"
168 # Try using "humanish" part of source repo if user didn't specify one
169 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
170 [ -e "$dir" ] && die "destination directory '$dir' already exists."
171 mkdir -p "$dir" &&
172 D=$(cd "$dir" && pwd) &&
173 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
174 case "$bare" in
175 yes)
176 GIT_DIR="$D" ;;
178 GIT_DIR="$D/.git" ;;
179 esac && export GIT_DIR && git-init ${template+"$template"} || usage
181 if test -n "$reference"
182 then
183 ref_git=
184 if test -d "$reference"
185 then
186 if test -d "$reference/.git/objects"
187 then
188 ref_git="$reference/.git"
189 elif test -d "$reference/objects"
190 then
191 ref_git="$reference"
194 if test -n "$ref_git"
195 then
196 ref_git=$(cd "$ref_git" && pwd)
197 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
199 GIT_DIR="$ref_git" git for-each-ref \
200 --format='%(objectname) %(*objectname)'
202 while read a b
204 test -z "$a" ||
205 git update-ref "refs/reference-tmp/$a" "$a"
206 test -z "$b" ||
207 git update-ref "refs/reference-tmp/$b" "$b"
208 done
209 else
210 die "reference repository '$reference' is not a local directory."
214 rm -f "$GIT_DIR/CLONE_HEAD"
216 # We do local magic only when the user tells us to.
217 case "$local,$use_local" in
218 yes,yes)
219 ( cd "$repo/objects" ) ||
220 die "-l flag seen but repository '$repo' is not local."
222 case "$local_shared" in
224 # See if we can hardlink and drop "l" if not.
225 sample_file=$(cd "$repo" && \
226 find objects -type f -print | sed -e 1q)
228 # objects directory should not be empty since we are cloning!
229 test -f "$repo/$sample_file" || exit
232 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
233 then
235 fi &&
236 rm -f "$GIT_DIR/objects/sample" &&
237 cd "$repo" &&
238 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
240 yes)
241 mkdir -p "$GIT_DIR/objects/info"
242 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
244 esac
245 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
248 case "$repo" in
249 rsync://*)
250 case "$depth" in
251 "") ;;
252 *) die "shallow over rsync not supported" ;;
253 esac
254 rsync $quiet -av --ignore-existing \
255 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
256 exit
257 # Look at objects/info/alternates for rsync -- http will
258 # support it natively and git native ones will do it on the
259 # remote end. Not having that file is not a crime.
260 rsync -q "$repo/objects/info/alternates" \
261 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
262 rm -f "$GIT_DIR/TMP_ALT"
263 if test -f "$GIT_DIR/TMP_ALT"
264 then
265 ( cd "$D" &&
266 . git-parse-remote &&
267 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
268 while read alt
270 case "$alt" in 'bad alternate: '*) die "$alt";; esac
271 case "$quiet" in
272 '') echo >&2 "Getting alternate: $alt" ;;
273 esac
274 rsync $quiet -av --ignore-existing \
275 --exclude info "$alt" "$GIT_DIR/objects" || exit
276 done
277 rm -f "$GIT_DIR/TMP_ALT"
279 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
281 https://*|http://*|ftp://*)
282 case "$depth" in
283 "") ;;
284 *) die "shallow over http or ftp not supported" ;;
285 esac
286 if test -z "@@NO_CURL@@"
287 then
288 clone_dumb_http "$repo" "$D"
289 else
290 die "http transport not supported, rebuild Git with curl support"
294 case "$upload_pack" in
295 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
296 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
297 esac >"$GIT_DIR/CLONE_HEAD" ||
298 die "fetch-pack from '$repo' failed."
300 esac
302 esac
303 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
305 if test -f "$GIT_DIR/CLONE_HEAD"
306 then
307 # Read git-fetch-pack -k output and store the remote branches.
308 if [ -n "$use_separate_remote" ]
309 then
310 branch_top="remotes/$origin"
311 else
312 branch_top="heads"
314 tag_top="tags"
315 while read sha1 name
317 case "$name" in
318 *'^{}')
319 continue ;;
320 HEAD)
321 destname="REMOTE_HEAD" ;;
322 refs/heads/*)
323 destname="refs/$branch_top/${name#refs/heads/}" ;;
324 refs/tags/*)
325 destname="refs/$tag_top/${name#refs/tags/}" ;;
327 continue ;;
328 esac
329 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
330 done < "$GIT_DIR/CLONE_HEAD"
333 cd "$D" || exit
335 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
336 then
337 # a non-bare repository is always in separate-remote layout
338 remote_top="refs/remotes/$origin"
339 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
340 case "$head_sha1" in
341 'ref: refs/'*)
342 # Uh-oh, the remote told us (http transport done against
343 # new style repository with a symref HEAD).
344 # Ideally we should skip the guesswork but for now
345 # opt for minimum change.
346 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
347 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
349 esac
351 # The name under $remote_top the remote HEAD seems to point at.
352 head_points_at=$(
354 test -f "$GIT_DIR/$remote_top/master" && echo "master"
355 cd "$GIT_DIR/$remote_top" &&
356 find . -type f -print | sed -e 's/^\.\///'
357 ) | (
358 done=f
359 while read name
361 test t = $done && continue
362 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
363 if test "$head_sha1" = "$branch_tip"
364 then
365 echo "$name"
366 done=t
368 done
372 # Write out remote.$origin config, and update our "$head_points_at".
373 case "$head_points_at" in
375 # Local default branch
376 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
378 # Tracking branch for the primary branch at the remote.
379 origin_track="$remote_top/$head_points_at" &&
380 git-update-ref HEAD "$head_sha1" &&
382 # Upstream URL
383 git-config remote."$origin".url "$repo" &&
385 # Set up the mappings to track the remote branches.
386 git-config remote."$origin".fetch \
387 "+refs/heads/*:$remote_top/*" '^$' &&
388 rm -f "refs/remotes/$origin/HEAD"
389 git-symbolic-ref "refs/remotes/$origin/HEAD" \
390 "refs/remotes/$origin/$head_points_at" &&
392 git-config branch."$head_points_at".remote "$origin" &&
393 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
394 esac
396 case "$no_checkout" in
398 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
399 git-read-tree -m -u $v HEAD HEAD
400 esac
402 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
404 trap - 0