t0001: fix test on MinGW
[git/platforms.git] / git-clone.sh
blob605f4f65149d6fb648aacba4aa25f5547c679a85
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 "@@NO_HARDLINKS@@" && use_local_hardlink=no
103 test -t 1 || no_progress=--no-progress
104 while
105 case "$#,$1" in
106 0,*) break ;;
107 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
108 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
109 no_checkout=yes ;;
110 *,--na|*,--nak|*,--nake|*,--naked|\
111 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
112 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
113 local_explicitly_asked_for=yes
114 (test "@@NO_HARDLINKS@@" &&
115 echo >&2 "Warning: -l asked but hardlinks are not supported") ||
116 use_local_hardlink=yes ;;
117 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
118 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
119 use_local_hardlink=no ;;
120 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
121 local_shared=yes; ;;
122 1,--template) usage ;;
123 *,--template)
124 shift; template="--template=$1" ;;
125 *,--template=*)
126 template="$1" ;;
127 *,-q|*,--quiet) quiet=-q ;;
128 *,--use-separate-remote) ;;
129 *,--no-separate-remote)
130 die "clones are always made with separate-remote layout" ;;
131 1,--reference) usage ;;
132 *,--reference)
133 shift; reference="$1" ;;
134 *,--reference=*)
135 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
136 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
137 case "$2" in
139 usage ;;
140 */*)
141 die "'$2' is not suitable for an origin name"
142 esac
143 git check-ref-format "heads/$2" ||
144 die "'$2' is not suitable for a branch name"
145 test -z "$origin_override" ||
146 die "Do not give more than one --origin options."
147 origin_override=yes
148 origin="$2"; shift
150 1,-u|1,--upload-pack) usage ;;
151 *,-u|*,--upload-pack)
152 shift
153 upload_pack="--upload-pack=$1" ;;
154 *,--upload-pack=*)
155 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
156 1,--depth) usage;;
157 *,--depth)
158 shift
159 depth="--depth=$1";;
160 *,-*) usage ;;
161 *) break ;;
162 esac
164 shift
165 done
167 repo="$1"
168 test -n "$repo" ||
169 die 'you must specify a repository to clone.'
171 # --bare implies --no-checkout and --no-separate-remote
172 if test yes = "$bare"
173 then
174 if test yes = "$origin_override"
175 then
176 die '--bare and --origin $origin options are incompatible.'
178 no_checkout=yes
179 use_separate_remote=
182 if test -z "$origin"
183 then
184 origin=origin
187 # Turn the source into an absolute path if
188 # it is local
189 if base=$(get_repo_base "$repo"); then
190 repo="$base"
191 local=yes
194 dir="$2"
195 # Try using "humanish" part of source repo if user didn't specify one
196 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
197 [ -e "$dir" ] && die "destination directory '$dir' already exists."
198 [ yes = "$bare" ] && unset GIT_WORK_TREE
199 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
200 die "working tree '$GIT_WORK_TREE' already exists."
203 cleanup() {
204 err=$?
205 test -z "$D" && rm -rf "$dir"
206 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
207 cd ..
208 test -n "$D" && rm -rf "$D"
209 test -n "$W" && rm -rf "$W"
210 exit $err
212 trap cleanup 0
213 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
214 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
215 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
216 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
217 GIT_DIR="$D"
218 else
219 GIT_DIR="$D/.git"
220 fi &&
221 export GIT_DIR &&
222 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
224 if test -n "$bare"
225 then
226 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
229 if test -n "$reference"
230 then
231 ref_git=
232 if test -d "$reference"
233 then
234 if test -d "$reference/.git/objects"
235 then
236 ref_git="$reference/.git"
237 elif test -d "$reference/objects"
238 then
239 ref_git="$reference"
242 if test -n "$ref_git"
243 then
244 ref_git=$(cd "$ref_git" && pwd)
245 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
247 GIT_DIR="$ref_git" git for-each-ref \
248 --format='%(objectname) %(*objectname)'
250 while read a b
252 test -z "$a" ||
253 git update-ref "refs/reference-tmp/$a" "$a"
254 test -z "$b" ||
255 git update-ref "refs/reference-tmp/$b" "$b"
256 done
257 else
258 die "reference repository '$reference' is not a local directory."
262 rm -f "$GIT_DIR/CLONE_HEAD"
264 # We do local magic only when the user tells us to.
265 case "$local" in
266 yes)
267 ( cd "$repo/objects" ) ||
268 die "cannot chdir to local '$repo/objects'."
270 if test "$local_shared" = yes
271 then
272 mkdir -p "$GIT_DIR/objects/info"
273 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
274 else
275 l= &&
276 if test "$use_local_hardlink" = yes
277 then
278 # See if we can hardlink and drop "l" if not.
279 sample_file=$(cd "$repo" && \
280 find objects -type f -print | sed -e 1q)
281 # objects directory should not be empty because
282 # we are cloning!
283 test -f "$repo/$sample_file" || exit
284 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
285 then
286 rm -f "$GIT_DIR/objects/sample"
288 elif test -n "$local_explicitly_asked_for"
289 then
290 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
292 fi &&
293 cd "$repo" &&
294 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
296 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
299 case "$repo" in
300 rsync://*)
301 case "$depth" in
302 "") ;;
303 *) die "shallow over rsync not supported" ;;
304 esac
305 rsync $quiet -av --ignore-existing \
306 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
307 exit
308 # Look at objects/info/alternates for rsync -- http will
309 # support it natively and git native ones will do it on the
310 # remote end. Not having that file is not a crime.
311 rsync -q "$repo/objects/info/alternates" \
312 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
313 rm -f "$GIT_DIR/TMP_ALT"
314 if test -f "$GIT_DIR/TMP_ALT"
315 then
316 ( cd "$D" &&
317 . git-parse-remote &&
318 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
319 while read alt
321 case "$alt" in 'bad alternate: '*) die "$alt";; esac
322 case "$quiet" in
323 '') echo >&2 "Getting alternate: $alt" ;;
324 esac
325 rsync $quiet -av --ignore-existing \
326 --exclude info "$alt" "$GIT_DIR/objects" || exit
327 done
328 rm -f "$GIT_DIR/TMP_ALT"
330 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
332 https://*|http://*|ftp://*)
333 case "$depth" in
334 "") ;;
335 *) die "shallow over http or ftp not supported" ;;
336 esac
337 if test -z "@@NO_CURL@@"
338 then
339 clone_dumb_http "$repo" "$D"
340 else
341 die "http transport not supported, rebuild Git with curl support"
345 case "$upload_pack" in
346 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
347 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
348 esac >"$GIT_DIR/CLONE_HEAD" ||
349 die "fetch-pack from '$repo' failed."
351 esac
353 esac
354 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
356 if test -f "$GIT_DIR/CLONE_HEAD"
357 then
358 # Read git-fetch-pack -k output and store the remote branches.
359 if [ -n "$use_separate_remote" ]
360 then
361 branch_top="remotes/$origin"
362 else
363 branch_top="heads"
365 tag_top="tags"
366 while read sha1 name
368 case "$name" in
369 *'^{}')
370 continue ;;
371 HEAD)
372 destname="REMOTE_HEAD" ;;
373 refs/heads/*)
374 destname="refs/$branch_top/${name#refs/heads/}" ;;
375 refs/tags/*)
376 destname="refs/$tag_top/${name#refs/tags/}" ;;
378 continue ;;
379 esac
380 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
381 done < "$GIT_DIR/CLONE_HEAD"
384 if test -n "$W"; then
385 cd "$W" || exit
386 else
387 cd "$D" || exit
390 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
391 then
392 # a non-bare repository is always in separate-remote layout
393 remote_top="refs/remotes/$origin"
394 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
395 case "$head_sha1" in
396 'ref: refs/'*)
397 # Uh-oh, the remote told us (http transport done against
398 # new style repository with a symref HEAD).
399 # Ideally we should skip the guesswork but for now
400 # opt for minimum change.
401 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
402 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
404 esac
406 # The name under $remote_top the remote HEAD seems to point at.
407 head_points_at=$(
409 test -f "$GIT_DIR/$remote_top/master" && echo "master"
410 cd "$GIT_DIR/$remote_top" &&
411 /usr/bin/find . -type f -print | sed -e 's/^\.\///'
412 ) | (
413 done=f
414 while read name
416 test t = $done && continue
417 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
418 if test "$head_sha1" = "$branch_tip"
419 then
420 echo "$name"
421 done=t
423 done
427 # Upstream URL
428 git config remote."$origin".url "$repo" &&
430 # Set up the mappings to track the remote branches.
431 git config remote."$origin".fetch \
432 "+refs/heads/*:$remote_top/*" '^$' &&
434 # Write out remote.$origin config, and update our "$head_points_at".
435 case "$head_points_at" in
437 # Local default branch
438 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
440 # Tracking branch for the primary branch at the remote.
441 git update-ref HEAD "$head_sha1" &&
443 rm -f "refs/remotes/$origin/HEAD"
444 git symbolic-ref "refs/remotes/$origin/HEAD" \
445 "refs/remotes/$origin/$head_points_at" &&
447 git config branch."$head_points_at".remote "$origin" &&
448 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
451 # Source had detached HEAD pointing nowhere
452 git update-ref --no-deref HEAD "$head_sha1" &&
453 rm -f "refs/remotes/$origin/HEAD"
455 esac
457 case "$no_checkout" in
459 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
460 git read-tree -m -u $v HEAD HEAD
461 esac
463 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
465 trap - 0