nicer display of thin pack completion
[git/dscho.git] / git-clone.sh
blob3f006936085cedcdca436455643b05d2198e3606
1 #!/bin/sh
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
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() {
22 cd "`/bin/pwd`" &&
23 cd "$1" || cd "$1.git" &&
25 cd .git
26 pwd
28 ) 2>/dev/null
31 if [ -n "$GIT_SSL_NO_VERIFY" -o \
32 "`git config --bool http.sslVerify`" = false ]; then
33 curl_extra_args="-k"
36 http_fetch () {
37 # $1 = Remote, $2 = Local
38 curl -nsfL $curl_extra_args "$1" >"$2" ||
39 case $? in
40 126|127) exit ;;
41 *) return $? ;;
42 esac
45 clone_dumb_http () {
46 # $1 - remote, $2 - local
47 cd "$2" &&
48 clone_tmp="$GIT_DIR/clone-tmp" &&
49 mkdir -p "$clone_tmp" || exit 1
50 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
51 "`git config --bool http.noEPSV`" = true ]; then
52 curl_extra_args="${curl_extra_args} --disable-epsv"
54 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
55 die "Cannot get remote repository information.
56 Perhaps git-update-server-info needs to be run there?"
57 test "z$quiet" = z && v=-v || v=
58 while read sha1 refname
60 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
61 case "$name" in
62 *^*) continue;;
63 esac
64 case "$bare,$name" in
65 yes,* | ,heads/* | ,tags/*) ;;
66 *) continue ;;
67 esac
68 if test -n "$use_separate_remote" &&
69 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
70 then
71 tname="remotes/$origin/$branch_name"
72 else
73 tname=$name
75 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
76 done <"$clone_tmp/refs"
77 rm -fr "$clone_tmp"
78 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
79 rm -f "$GIT_DIR/REMOTE_HEAD"
80 if test -f "$GIT_DIR/REMOTE_HEAD"; then
81 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
82 case "$head_sha1" in
83 'ref: refs/'*)
86 git-http-fetch $v -a "$head_sha1" "$1" ||
87 rm -f "$GIT_DIR/REMOTE_HEAD"
89 esac
93 quiet=
94 local=no
95 use_local_hardlink=yes
96 local_shared=no
97 unset template
98 no_checkout=
99 upload_pack=
100 bare=
101 reference=
102 origin=
103 origin_override=
104 use_separate_remote=t
105 depth=
106 no_progress=
107 local_explicitly_asked_for=
108 test -t 1 || no_progress=--no-progress
109 while
110 case "$#,$1" in
111 0,*) break ;;
112 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
113 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
114 no_checkout=yes ;;
115 *,--na|*,--nak|*,--nake|*,--naked|\
116 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
117 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
118 local_explicitly_asked_for=yes
119 use_local_hardlink=yes ;;
120 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
121 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
122 use_local_hardlink=no ;;
123 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
124 local_shared=yes; ;;
125 1,--template) usage ;;
126 *,--template)
127 shift; template="--template=$1" ;;
128 *,--template=*)
129 template="$1" ;;
130 *,-q|*,--quiet) quiet=-q ;;
131 *,--use-separate-remote) ;;
132 *,--no-separate-remote)
133 die "clones are always made with separate-remote layout" ;;
134 1,--reference) usage ;;
135 *,--reference)
136 shift; reference="$1" ;;
137 *,--reference=*)
138 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
139 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
140 case "$2" in
142 usage ;;
143 */*)
144 die "'$2' is not suitable for an origin name"
145 esac
146 git check-ref-format "heads/$2" ||
147 die "'$2' is not suitable for a branch name"
148 test -z "$origin_override" ||
149 die "Do not give more than one --origin options."
150 origin_override=yes
151 origin="$2"; shift
153 1,-u|1,--upload-pack) usage ;;
154 *,-u|*,--upload-pack)
155 shift
156 upload_pack="--upload-pack=$1" ;;
157 *,--upload-pack=*)
158 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
159 1,--depth) usage;;
160 *,--depth)
161 shift
162 depth="--depth=$1";;
163 *,--)
164 shift
165 break ;;
166 *,-*) usage ;;
167 *) break ;;
168 esac
170 shift
171 done
173 repo="$1"
174 test -n "$repo" ||
175 die 'you must specify a repository to clone.'
177 # --bare implies --no-checkout and --no-separate-remote
178 if test yes = "$bare"
179 then
180 if test yes = "$origin_override"
181 then
182 die '--bare and --origin $origin options are incompatible.'
184 no_checkout=yes
185 use_separate_remote=
188 if test -z "$origin"
189 then
190 origin=origin
193 # Turn the source into an absolute path if
194 # it is local
195 if base=$(get_repo_base "$repo"); then
196 repo="$base"
197 local=yes
200 dir="$2"
201 # Try using "humanish" part of source repo if user didn't specify one
202 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
203 [ -e "$dir" ] && die "destination directory '$dir' already exists."
204 [ yes = "$bare" ] && unset GIT_WORK_TREE
205 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
206 die "working tree '$GIT_WORK_TREE' already exists."
209 cleanup() {
210 err=$?
211 test -z "$D" && rm -rf "$dir"
212 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
213 cd ..
214 test -n "$D" && rm -rf "$D"
215 test -n "$W" && rm -rf "$W"
216 exit $err
218 trap cleanup 0
219 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
220 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
221 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
222 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
223 GIT_DIR="$D"
224 else
225 GIT_DIR="$D/.git"
226 fi &&
227 export GIT_DIR &&
228 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
230 if test -n "$bare"
231 then
232 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
235 if test -n "$reference"
236 then
237 ref_git=
238 if test -d "$reference"
239 then
240 if test -d "$reference/.git/objects"
241 then
242 ref_git="$reference/.git"
243 elif test -d "$reference/objects"
244 then
245 ref_git="$reference"
248 if test -n "$ref_git"
249 then
250 ref_git=$(cd "$ref_git" && pwd)
251 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
253 GIT_DIR="$ref_git" git for-each-ref \
254 --format='%(objectname) %(*objectname)'
256 while read a b
258 test -z "$a" ||
259 git update-ref "refs/reference-tmp/$a" "$a"
260 test -z "$b" ||
261 git update-ref "refs/reference-tmp/$b" "$b"
262 done
263 else
264 die "reference repository '$reference' is not a local directory."
268 rm -f "$GIT_DIR/CLONE_HEAD"
270 # We do local magic only when the user tells us to.
271 case "$local" in
272 yes)
273 ( cd "$repo/objects" ) ||
274 die "cannot chdir to local '$repo/objects'."
276 if test "$local_shared" = yes
277 then
278 mkdir -p "$GIT_DIR/objects/info"
279 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
280 else
281 l= &&
282 if test "$use_local_hardlink" = yes
283 then
284 # See if we can hardlink and drop "l" if not.
285 sample_file=$(cd "$repo" && \
286 find objects -type f -print | sed -e 1q)
287 # objects directory should not be empty because
288 # we are cloning!
289 test -f "$repo/$sample_file" || exit
290 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
291 then
292 rm -f "$GIT_DIR/objects/sample"
294 elif test -n "$local_explicitly_asked_for"
295 then
296 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
298 fi &&
299 cd "$repo" &&
300 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
302 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
305 case "$repo" in
306 rsync://*)
307 case "$depth" in
308 "") ;;
309 *) die "shallow over rsync not supported" ;;
310 esac
311 rsync $quiet -av --ignore-existing \
312 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
313 exit
314 # Look at objects/info/alternates for rsync -- http will
315 # support it natively and git native ones will do it on the
316 # remote end. Not having that file is not a crime.
317 rsync -q "$repo/objects/info/alternates" \
318 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
319 rm -f "$GIT_DIR/TMP_ALT"
320 if test -f "$GIT_DIR/TMP_ALT"
321 then
322 ( cd "$D" &&
323 . git-parse-remote &&
324 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
325 while read alt
327 case "$alt" in 'bad alternate: '*) die "$alt";; esac
328 case "$quiet" in
329 '') echo >&2 "Getting alternate: $alt" ;;
330 esac
331 rsync $quiet -av --ignore-existing \
332 --exclude info "$alt" "$GIT_DIR/objects" || exit
333 done
334 rm -f "$GIT_DIR/TMP_ALT"
336 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
338 https://*|http://*|ftp://*)
339 case "$depth" in
340 "") ;;
341 *) die "shallow over http or ftp not supported" ;;
342 esac
343 if test -z "@@NO_CURL@@"
344 then
345 clone_dumb_http "$repo" "$D"
346 else
347 die "http transport not supported, rebuild Git with curl support"
351 case "$upload_pack" in
352 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
353 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
354 esac >"$GIT_DIR/CLONE_HEAD" ||
355 die "fetch-pack from '$repo' failed."
357 esac
359 esac
360 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
362 if test -f "$GIT_DIR/CLONE_HEAD"
363 then
364 # Read git-fetch-pack -k output and store the remote branches.
365 if [ -n "$use_separate_remote" ]
366 then
367 branch_top="remotes/$origin"
368 else
369 branch_top="heads"
371 tag_top="tags"
372 while read sha1 name
374 case "$name" in
375 *'^{}')
376 continue ;;
377 HEAD)
378 destname="REMOTE_HEAD" ;;
379 refs/heads/*)
380 destname="refs/$branch_top/${name#refs/heads/}" ;;
381 refs/tags/*)
382 destname="refs/$tag_top/${name#refs/tags/}" ;;
384 continue ;;
385 esac
386 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
387 done < "$GIT_DIR/CLONE_HEAD"
390 if test -n "$W"; then
391 cd "$W" || exit
392 else
393 cd "$D" || exit
396 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
397 then
398 # a non-bare repository is always in separate-remote layout
399 remote_top="refs/remotes/$origin"
400 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
401 case "$head_sha1" in
402 'ref: refs/'*)
403 # Uh-oh, the remote told us (http transport done against
404 # new style repository with a symref HEAD).
405 # Ideally we should skip the guesswork but for now
406 # opt for minimum change.
407 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
408 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
410 esac
412 # The name under $remote_top the remote HEAD seems to point at.
413 head_points_at=$(
415 test -f "$GIT_DIR/$remote_top/master" && echo "master"
416 cd "$GIT_DIR/$remote_top" &&
417 find . -type f -print | sed -e 's/^\.\///'
418 ) | (
419 done=f
420 while read name
422 test t = $done && continue
423 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
424 if test "$head_sha1" = "$branch_tip"
425 then
426 echo "$name"
427 done=t
429 done
433 # Upstream URL
434 git config remote."$origin".url "$repo" &&
436 # Set up the mappings to track the remote branches.
437 git config remote."$origin".fetch \
438 "+refs/heads/*:$remote_top/*" '^$' &&
440 # Write out remote.$origin config, and update our "$head_points_at".
441 case "$head_points_at" in
443 # Local default branch
444 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
446 # Tracking branch for the primary branch at the remote.
447 git update-ref HEAD "$head_sha1" &&
449 rm -f "refs/remotes/$origin/HEAD"
450 git symbolic-ref "refs/remotes/$origin/HEAD" \
451 "refs/remotes/$origin/$head_points_at" &&
453 git config branch."$head_points_at".remote "$origin" &&
454 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
457 # Source had detached HEAD pointing nowhere
458 git update-ref --no-deref HEAD "$head_sha1" &&
459 rm -f "refs/remotes/$origin/HEAD"
461 esac
463 case "$no_checkout" in
465 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
466 git read-tree -m -u $v HEAD HEAD
467 esac
469 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
471 trap - 0