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