tests for various pack index features
[git/dkf.git] / git-clone.sh
blob513b574d13858f1a81e6f66251890d81bf0e55ce
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 test "z$quiet" = z && v=-v || v=
46 while read sha1 refname
48 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
49 case "$name" in
50 *^*) continue;;
51 esac
52 case "$bare,$name" in
53 yes,* | ,heads/* | ,tags/*) ;;
54 *) continue ;;
55 esac
56 if test -n "$use_separate_remote" &&
57 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
58 then
59 tname="remotes/$origin/$branch_name"
60 else
61 tname=$name
63 git-http-fetch $v -a -w "$tname" "$name" "$1" || exit 1
64 done <"$clone_tmp/refs"
65 rm -fr "$clone_tmp"
66 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
67 rm -f "$GIT_DIR/REMOTE_HEAD"
70 quiet=
71 local=no
72 use_local=no
73 local_shared=no
74 unset template
75 no_checkout=
76 upload_pack=
77 bare=
78 reference=
79 origin=
80 origin_override=
81 use_separate_remote=t
82 depth=
83 no_progress=
84 test -t 1 || no_progress=--no-progress
85 while
86 case "$#,$1" in
87 0,*) break ;;
88 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
89 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
90 no_checkout=yes ;;
91 *,--na|*,--nak|*,--nake|*,--naked|\
92 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
93 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
94 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
95 local_shared=yes; use_local=yes ;;
96 1,--template) usage ;;
97 *,--template)
98 shift; template="--template=$1" ;;
99 *,--template=*)
100 template="$1" ;;
101 *,-q|*,--quiet) quiet=-q ;;
102 *,--use-separate-remote) ;;
103 *,--no-separate-remote)
104 die "clones are always made with separate-remote layout" ;;
105 1,--reference) usage ;;
106 *,--reference)
107 shift; reference="$1" ;;
108 *,--reference=*)
109 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
110 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
111 case "$2" in
113 usage ;;
114 */*)
115 die "'$2' is not suitable for an origin name"
116 esac
117 git-check-ref-format "heads/$2" ||
118 die "'$2' is not suitable for a branch name"
119 test -z "$origin_override" ||
120 die "Do not give more than one --origin options."
121 origin_override=yes
122 origin="$2"; shift
124 1,-u|1,--upload-pack) usage ;;
125 *,-u|*,--upload-pack)
126 shift
127 upload_pack="--upload-pack=$1" ;;
128 *,--upload-pack=*)
129 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
130 1,--depth) usage;;
131 *,--depth)
132 shift
133 depth="--depth=$1";;
134 *,-*) usage ;;
135 *) break ;;
136 esac
138 shift
139 done
141 repo="$1"
142 test -n "$repo" ||
143 die 'you must specify a repository to clone.'
145 # --bare implies --no-checkout and --no-separate-remote
146 if test yes = "$bare"
147 then
148 if test yes = "$origin_override"
149 then
150 die '--bare and --origin $origin options are incompatible.'
152 no_checkout=yes
153 use_separate_remote=
156 if test -z "$origin"
157 then
158 origin=origin
161 # Turn the source into an absolute path if
162 # it is local
163 if base=$(get_repo_base "$repo"); then
164 repo="$base"
165 local=yes
168 dir="$2"
169 # Try using "humanish" part of source repo if user didn't specify one
170 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
171 [ -e "$dir" ] && die "destination directory '$dir' already exists."
172 mkdir -p "$dir" &&
173 D=$(cd "$dir" && pwd) &&
174 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
175 case "$bare" in
176 yes)
177 GIT_DIR="$D" ;;
179 GIT_DIR="$D/.git" ;;
180 esac && export GIT_DIR && git-init ${template+"$template"} || usage
182 if test -n "$reference"
183 then
184 ref_git=
185 if test -d "$reference"
186 then
187 if test -d "$reference/.git/objects"
188 then
189 ref_git="$reference/.git"
190 elif test -d "$reference/objects"
191 then
192 ref_git="$reference"
195 if test -n "$ref_git"
196 then
197 ref_git=$(cd "$ref_git" && pwd)
198 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
200 GIT_DIR="$ref_git" git for-each-ref \
201 --format='%(objectname) %(*objectname)'
203 while read a b
205 test -z "$a" ||
206 git update-ref "refs/reference-tmp/$a" "$a"
207 test -z "$b" ||
208 git update-ref "refs/reference-tmp/$b" "$b"
209 done
210 else
211 die "reference repository '$reference' is not a local directory."
215 rm -f "$GIT_DIR/CLONE_HEAD"
217 # We do local magic only when the user tells us to.
218 case "$local,$use_local" in
219 yes,yes)
220 ( cd "$repo/objects" ) ||
221 die "-l flag seen but repository '$repo' is not local."
223 case "$local_shared" in
225 # See if we can hardlink and drop "l" if not.
226 sample_file=$(cd "$repo" && \
227 find objects -type f -print | sed -e 1q)
229 # objects directory should not be empty since we are cloning!
230 test -f "$repo/$sample_file" || exit
233 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
234 then
236 fi &&
237 rm -f "$GIT_DIR/objects/sample" &&
238 cd "$repo" &&
239 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
241 yes)
242 mkdir -p "$GIT_DIR/objects/info"
243 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
245 esac
246 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
249 case "$repo" in
250 rsync://*)
251 case "$depth" in
252 "") ;;
253 *) die "shallow over rsync not supported" ;;
254 esac
255 rsync $quiet -av --ignore-existing \
256 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
257 exit
258 # Look at objects/info/alternates for rsync -- http will
259 # support it natively and git native ones will do it on the
260 # remote end. Not having that file is not a crime.
261 rsync -q "$repo/objects/info/alternates" \
262 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
263 rm -f "$GIT_DIR/TMP_ALT"
264 if test -f "$GIT_DIR/TMP_ALT"
265 then
266 ( cd "$D" &&
267 . git-parse-remote &&
268 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
269 while read alt
271 case "$alt" in 'bad alternate: '*) die "$alt";; esac
272 case "$quiet" in
273 '') echo >&2 "Getting alternate: $alt" ;;
274 esac
275 rsync $quiet -av --ignore-existing \
276 --exclude info "$alt" "$GIT_DIR/objects" || exit
277 done
278 rm -f "$GIT_DIR/TMP_ALT"
280 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
282 https://*|http://*|ftp://*)
283 case "$depth" in
284 "") ;;
285 *) die "shallow over http or ftp not supported" ;;
286 esac
287 if test -z "@@NO_CURL@@"
288 then
289 clone_dumb_http "$repo" "$D"
290 else
291 die "http transport not supported, rebuild Git with curl support"
295 case "$upload_pack" in
296 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
297 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
298 esac >"$GIT_DIR/CLONE_HEAD" ||
299 die "fetch-pack from '$repo' failed."
301 esac
303 esac
304 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
306 if test -f "$GIT_DIR/CLONE_HEAD"
307 then
308 # Read git-fetch-pack -k output and store the remote branches.
309 if [ -n "$use_separate_remote" ]
310 then
311 branch_top="remotes/$origin"
312 else
313 branch_top="heads"
315 tag_top="tags"
316 while read sha1 name
318 case "$name" in
319 *'^{}')
320 continue ;;
321 HEAD)
322 destname="REMOTE_HEAD" ;;
323 refs/heads/*)
324 destname="refs/$branch_top/${name#refs/heads/}" ;;
325 refs/tags/*)
326 destname="refs/$tag_top/${name#refs/tags/}" ;;
328 continue ;;
329 esac
330 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
331 done < "$GIT_DIR/CLONE_HEAD"
334 cd "$D" || exit
336 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
337 then
338 # a non-bare repository is always in separate-remote layout
339 remote_top="refs/remotes/$origin"
340 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
341 case "$head_sha1" in
342 'ref: refs/'*)
343 # Uh-oh, the remote told us (http transport done against
344 # new style repository with a symref HEAD).
345 # Ideally we should skip the guesswork but for now
346 # opt for minimum change.
347 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
348 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
350 esac
352 # The name under $remote_top the remote HEAD seems to point at.
353 head_points_at=$(
355 test -f "$GIT_DIR/$remote_top/master" && echo "master"
356 cd "$GIT_DIR/$remote_top" &&
357 find . -type f -print | sed -e 's/^\.\///'
358 ) | (
359 done=f
360 while read name
362 test t = $done && continue
363 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
364 if test "$head_sha1" = "$branch_tip"
365 then
366 echo "$name"
367 done=t
369 done
373 # Write out remote.$origin config, and update our "$head_points_at".
374 case "$head_points_at" in
376 # Local default branch
377 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
379 # Tracking branch for the primary branch at the remote.
380 origin_track="$remote_top/$head_points_at" &&
381 git-update-ref HEAD "$head_sha1" &&
383 # Upstream URL
384 git-config remote."$origin".url "$repo" &&
386 # Set up the mappings to track the remote branches.
387 git-config remote."$origin".fetch \
388 "+refs/heads/*:$remote_top/*" '^$' &&
389 rm -f "refs/remotes/$origin/HEAD"
390 git-symbolic-ref "refs/remotes/$origin/HEAD" \
391 "refs/remotes/$origin/$head_points_at" &&
393 git-config branch."$head_points_at".remote "$origin" &&
394 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
395 esac
397 case "$no_checkout" in
399 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
400 git-read-tree -m -u $v HEAD HEAD
401 esac
403 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
405 trap - 0