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