Merge branch 'master' of git://repo.or.cz/git/spearce
[git/mingw.git] / git-clone.sh
blob0154a4a381897030633e4dcc8f6bd20fb795ac5e
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 # Fix some commands on Windows
17 case $(uname -s) in
18 *MINGW*)
19 # Windows has its own (incompatible) find
20 find () {
21 /usr/bin/find "$@"
23 # need an emulation of cpio
24 cpio() {
25 case "$1" in
26 -pumd) cp_arg=-pr;;
27 -pumdl) cp_arg=-lr;;
28 *) die "cpio $1 unexpected";;
29 esac
30 # copy only files and empty directories
31 prev=
32 while read f; do
33 if test -d "$f"; then
34 # here we assume that directories are listed after
35 # its files (aka 'find -depth'), hence, a directory
36 # that is not empty will be a leading sub-string
37 # of the preceding entry
38 case "$prev" in
39 "$f"/* ) ;;
40 *) echo "$f";;
41 esac
42 else
43 echo "$f"
45 prev="$f"
46 done |
47 xargs --no-run-if-empty \
48 cp $cp_arg --target-directory="$2" --parents
51 esac
53 usage() {
54 die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
57 get_repo_base() {
59 cd "`/bin/pwd -W`" &&
60 cd "$1" || cd "$1.git" &&
62 cd .git
63 pwd -W
65 ) 2>/dev/null
68 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
69 curl_extra_args="-k"
72 http_fetch () {
73 # $1 = Remote, $2 = Local
74 curl -nsfL $curl_extra_args "$1" >"$2" ||
75 case $? in
76 126|127) exit ;;
77 *) return $? ;;
78 esac
81 clone_dumb_http () {
82 # $1 - remote, $2 - local
83 cd "$2" &&
84 clone_tmp="$GIT_DIR/clone-tmp" &&
85 mkdir -p "$clone_tmp" || exit 1
86 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
87 "`git config --bool http.noEPSV`" = true ]; then
88 curl_extra_args="${curl_extra_args} --disable-epsv"
90 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
91 die "Cannot get remote repository information.
92 Perhaps git-update-server-info needs to be run there?"
93 test "z$quiet" = z && v=-v || v=
94 while read sha1 refname
96 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
97 case "$name" in
98 *^*) continue;;
99 esac
100 case "$bare,$name" in
101 yes,* | ,heads/* | ,tags/*) ;;
102 *) continue ;;
103 esac
104 if test -n "$use_separate_remote" &&
105 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
106 then
107 tname="remotes/$origin/$branch_name"
108 else
109 tname=$name
111 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
112 done <"$clone_tmp/refs"
113 rm -fr "$clone_tmp"
114 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
115 rm -f "$GIT_DIR/REMOTE_HEAD"
116 if test -f "$GIT_DIR/REMOTE_HEAD"; then
117 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
118 case "$head_sha1" in
119 'ref: refs/'*)
122 git-http-fetch $v -a "$head_sha1" "$1" ||
123 rm -f "$GIT_DIR/REMOTE_HEAD"
125 esac
129 quiet=
130 local=no
131 use_local_hardlink=yes
132 local_shared=no
133 unset template
134 no_checkout=
135 upload_pack=
136 bare=
137 reference=
138 origin=
139 origin_override=
140 use_separate_remote=t
141 depth=
142 no_progress=
143 local_explicitly_asked_for=
144 test -t 1 || no_progress=--no-progress
145 while
146 case "$#,$1" in
147 0,*) break ;;
148 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
149 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
150 no_checkout=yes ;;
151 *,--na|*,--nak|*,--nake|*,--naked|\
152 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
153 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
154 local_explicitly_asked_for=yes
155 use_local_hardlink=yes ;;
156 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
157 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
158 use_local_hardlink=no ;;
159 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
160 local_shared=yes; ;;
161 1,--template) usage ;;
162 *,--template)
163 shift; template="--template=$1" ;;
164 *,--template=*)
165 template="$1" ;;
166 *,-q|*,--quiet) quiet=-q ;;
167 *,--use-separate-remote) ;;
168 *,--no-separate-remote)
169 die "clones are always made with separate-remote layout" ;;
170 1,--reference) usage ;;
171 *,--reference)
172 shift; reference="$1" ;;
173 *,--reference=*)
174 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
175 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
176 case "$2" in
178 usage ;;
179 */*)
180 die "'$2' is not suitable for an origin name"
181 esac
182 git check-ref-format "heads/$2" ||
183 die "'$2' is not suitable for a branch name"
184 test -z "$origin_override" ||
185 die "Do not give more than one --origin options."
186 origin_override=yes
187 origin="$2"; shift
189 1,-u|1,--upload-pack) usage ;;
190 *,-u|*,--upload-pack)
191 shift
192 upload_pack="--upload-pack=$1" ;;
193 *,--upload-pack=*)
194 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
195 1,--depth) usage;;
196 *,--depth)
197 shift
198 depth="--depth=$1";;
199 *,-*) usage ;;
200 *) break ;;
201 esac
203 shift
204 done
206 repo="$1"
207 test -n "$repo" ||
208 die 'you must specify a repository to clone.'
210 # --bare implies --no-checkout and --no-separate-remote
211 if test yes = "$bare"
212 then
213 if test yes = "$origin_override"
214 then
215 die '--bare and --origin $origin options are incompatible.'
217 no_checkout=yes
218 use_separate_remote=
221 if test -z "$origin"
222 then
223 origin=origin
226 # Turn the source into an absolute path if
227 # it is local
228 if base=$(get_repo_base "$repo"); then
229 repo="$base"
230 local=yes
233 dir="$2"
234 # Try using "humanish" part of source repo if user didn't specify one
235 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
236 [ -e "$dir" ] && die "destination directory '$dir' already exists."
237 [ yes = "$bare" ] && unset GIT_WORK_TREE
238 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
239 die "working tree '$GIT_WORK_TREE' already exists."
242 cleanup() {
243 err=$?
244 test -z "$D" && rm -rf "$dir"
245 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
246 cd ..
247 test -n "$D" && rm -rf "$D"
248 test -n "$W" && rm -rf "$W"
249 exit $err
251 trap cleanup 0
252 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
253 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
254 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
255 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
256 GIT_DIR="$D"
257 else
258 GIT_DIR="$D/.git"
259 fi &&
260 export GIT_DIR &&
261 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
263 if test -n "$bare"
264 then
265 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
268 if test -n "$reference"
269 then
270 ref_git=
271 if test -d "$reference"
272 then
273 if test -d "$reference/.git/objects"
274 then
275 ref_git="$reference/.git"
276 elif test -d "$reference/objects"
277 then
278 ref_git="$reference"
281 if test -n "$ref_git"
282 then
283 ref_git=$(cd "$ref_git" && pwd)
284 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
286 GIT_DIR="$ref_git" git for-each-ref \
287 --format='%(objectname) %(*objectname)'
289 while read a b
291 test -z "$a" ||
292 git update-ref "refs/reference-tmp/$a" "$a"
293 test -z "$b" ||
294 git update-ref "refs/reference-tmp/$b" "$b"
295 done
296 else
297 die "reference repository '$reference' is not a local directory."
301 rm -f "$GIT_DIR/CLONE_HEAD"
303 # We do local magic only when the user tells us to.
304 case "$local" in
305 yes)
306 ( cd "$repo/objects" ) ||
307 die "cannot chdir to local '$repo/objects'."
309 if test "$local_shared" = yes
310 then
311 mkdir -p "$GIT_DIR/objects/info"
312 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
313 else
314 l= &&
315 if test "$use_local_hardlink" = yes
316 then
317 # See if we can hardlink and drop "l" if not.
318 sample_file=$(cd "$repo" && \
319 find objects -type f -print | sed -e 1q)
320 # objects directory should not be empty because
321 # we are cloning!
322 test -f "$repo/$sample_file" || exit
323 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
324 then
325 rm -f "$GIT_DIR/objects/sample"
327 elif test -n "$local_explicitly_asked_for"
328 then
329 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
331 fi &&
332 cd "$repo" &&
333 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
335 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
338 case "$repo" in
339 rsync://*)
340 case "$depth" in
341 "") ;;
342 *) die "shallow over rsync not supported" ;;
343 esac
344 rsync $quiet -av --ignore-existing \
345 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
346 exit
347 # Look at objects/info/alternates for rsync -- http will
348 # support it natively and git native ones will do it on the
349 # remote end. Not having that file is not a crime.
350 rsync -q "$repo/objects/info/alternates" \
351 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
352 rm -f "$GIT_DIR/TMP_ALT"
353 if test -f "$GIT_DIR/TMP_ALT"
354 then
355 ( cd "$D" &&
356 . git-parse-remote &&
357 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
358 while read alt
360 case "$alt" in 'bad alternate: '*) die "$alt";; esac
361 case "$quiet" in
362 '') echo >&2 "Getting alternate: $alt" ;;
363 esac
364 rsync $quiet -av --ignore-existing \
365 --exclude info "$alt" "$GIT_DIR/objects" || exit
366 done
367 rm -f "$GIT_DIR/TMP_ALT"
369 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
371 https://*|http://*|ftp://*)
372 case "$depth" in
373 "") ;;
374 *) die "shallow over http or ftp not supported" ;;
375 esac
376 if test -z "@@NO_CURL@@"
377 then
378 clone_dumb_http "$repo" "$D"
379 else
380 die "http transport not supported, rebuild Git with curl support"
384 case "$upload_pack" in
385 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
386 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
387 esac >"$GIT_DIR/CLONE_HEAD" ||
388 die "fetch-pack from '$repo' failed."
390 esac
392 esac
393 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
395 if test -f "$GIT_DIR/CLONE_HEAD"
396 then
397 # Read git-fetch-pack -k output and store the remote branches.
398 if [ -n "$use_separate_remote" ]
399 then
400 branch_top="remotes/$origin"
401 else
402 branch_top="heads"
404 tag_top="tags"
405 while read sha1 name
407 case "$name" in
408 *'^{}')
409 continue ;;
410 HEAD)
411 destname="REMOTE_HEAD" ;;
412 refs/heads/*)
413 destname="refs/$branch_top/${name#refs/heads/}" ;;
414 refs/tags/*)
415 destname="refs/$tag_top/${name#refs/tags/}" ;;
417 continue ;;
418 esac
419 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
420 done < "$GIT_DIR/CLONE_HEAD"
423 if test -n "$W"; then
424 cd "$W" || exit
425 else
426 cd "$D" || exit
429 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
430 then
431 # a non-bare repository is always in separate-remote layout
432 remote_top="refs/remotes/$origin"
433 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
434 case "$head_sha1" in
435 'ref: refs/'*)
436 # Uh-oh, the remote told us (http transport done against
437 # new style repository with a symref HEAD).
438 # Ideally we should skip the guesswork but for now
439 # opt for minimum change.
440 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
441 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
443 esac
445 # The name under $remote_top the remote HEAD seems to point at.
446 head_points_at=$(
448 test -f "$GIT_DIR/$remote_top/master" && echo "master"
449 cd "$GIT_DIR/$remote_top" &&
450 find . -type f -print | sed -e 's/^\.\///'
451 ) | (
452 done=f
453 while read name
455 test t = $done && continue
456 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
457 if test "$head_sha1" = "$branch_tip"
458 then
459 echo "$name"
460 done=t
462 done
466 # Upstream URL
467 git config remote."$origin".url "$repo" &&
469 # Set up the mappings to track the remote branches.
470 git config remote."$origin".fetch \
471 "+refs/heads/*:$remote_top/*" '^$' &&
473 # Write out remote.$origin config, and update our "$head_points_at".
474 case "$head_points_at" in
476 # Local default branch
477 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
479 # Tracking branch for the primary branch at the remote.
480 git update-ref HEAD "$head_sha1" &&
482 rm -f "refs/remotes/$origin/HEAD"
483 git symbolic-ref "refs/remotes/$origin/HEAD" \
484 "refs/remotes/$origin/$head_points_at" &&
486 git config branch."$head_points_at".remote "$origin" &&
487 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
490 # Source had detached HEAD pointing nowhere
491 git update-ref --no-deref HEAD "$head_sha1" &&
492 rm -f "refs/remotes/$origin/HEAD"
494 esac
496 case "$no_checkout" in
498 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
499 git read-tree -m -u $v HEAD HEAD
500 esac
502 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
504 trap - 0