git-submodule: fix subcommand parser
[git/platforms.git] / git-clone.sh
blobb4e858c3885dab55398670d9497304eb83545d1f
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 OPTIONS_SPEC="\
12 git-clone [options] [--] <repo> [<dir>]
14 n,no-checkout don't create a checkout
15 bare create a bare repository
16 naked create a bare repository
17 l,local to clone from a local repository
18 no-hardlinks don't use local hardlinks, always copy
19 s,shared setup as a shared repository
20 template= path to the template directory
21 q,quiet be quiet
22 reference= reference repository
23 o,origin= use <name> instead of 'origin' to track upstream
24 u,upload-pack= path to git-upload-pack on the remote
25 depth= create a shallow clone of that depth
27 use-separate-remote compatibility, do not use
28 no-separate-remote compatibility, do not use"
30 die() {
31 echo >&2 "$@"
32 exit 1
35 usage() {
36 exec "$0" -h
39 eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
41 get_repo_base() {
43 cd "`/bin/pwd`" &&
44 cd "$1" || cd "$1.git" &&
46 cd .git
47 pwd
49 ) 2>/dev/null
52 if [ -n "$GIT_SSL_NO_VERIFY" -o \
53 "`git config --bool http.sslVerify`" = false ]; then
54 curl_extra_args="-k"
57 http_fetch () {
58 # $1 = Remote, $2 = Local
59 curl -nsfL $curl_extra_args "$1" >"$2"
60 curl_exit_status=$?
61 case $curl_exit_status in
62 126|127) exit ;;
63 *) return $curl_exit_status ;;
64 esac
67 clone_dumb_http () {
68 # $1 - remote, $2 - local
69 cd "$2" &&
70 clone_tmp="$GIT_DIR/clone-tmp" &&
71 mkdir -p "$clone_tmp" || exit 1
72 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
73 "`git config --bool http.noEPSV`" = true ]; then
74 curl_extra_args="${curl_extra_args} --disable-epsv"
76 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
77 die "Cannot get remote repository information.
78 Perhaps git-update-server-info needs to be run there?"
79 test "z$quiet" = z && v=-v || v=
80 while read sha1 refname
82 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
83 case "$name" in
84 *^*) continue;;
85 esac
86 case "$bare,$name" in
87 yes,* | ,heads/* | ,tags/*) ;;
88 *) continue ;;
89 esac
90 if test -n "$use_separate_remote" &&
91 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
92 then
93 tname="remotes/$origin/$branch_name"
94 else
95 tname=$name
97 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
98 done <"$clone_tmp/refs"
99 rm -fr "$clone_tmp"
100 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
101 rm -f "$GIT_DIR/REMOTE_HEAD"
102 if test -f "$GIT_DIR/REMOTE_HEAD"; then
103 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
104 case "$head_sha1" in
105 'ref: refs/'*)
108 git-http-fetch $v -a "$head_sha1" "$1" ||
109 rm -f "$GIT_DIR/REMOTE_HEAD"
111 esac
115 quiet=
116 local=no
117 use_local_hardlink=yes
118 local_shared=no
119 unset template
120 no_checkout=
121 upload_pack=
122 bare=
123 reference=
124 origin=
125 origin_override=
126 use_separate_remote=t
127 depth=
128 no_progress=
129 local_explicitly_asked_for=
130 test -t 1 || no_progress=--no-progress
132 while test $# != 0
134 case "$1" in
135 -n|--no-checkout)
136 no_checkout=yes ;;
137 --naked|--bare)
138 bare=yes ;;
139 -l|--local)
140 local_explicitly_asked_for=yes
141 use_local_hardlink=yes
143 --no-hardlinks)
144 use_local_hardlink=no ;;
145 -s|--shared)
146 local_shared=yes ;;
147 --template)
148 shift; template="--template=$1" ;;
149 -q|--quiet)
150 quiet=-q ;;
151 --use-separate-remote|--no-separate-remote)
152 die "clones are always made with separate-remote layout" ;;
153 --reference)
154 shift; reference="$1" ;;
155 -o|--origin)
156 shift;
157 case "$1" in
159 usage ;;
160 */*)
161 die "'$1' is not suitable for an origin name"
162 esac
163 git check-ref-format "heads/$1" ||
164 die "'$1' is not suitable for a branch name"
165 test -z "$origin_override" ||
166 die "Do not give more than one --origin options."
167 origin_override=yes
168 origin="$1"
170 -u|--upload-pack)
171 shift
172 upload_pack="--upload-pack=$1" ;;
173 --depth)
174 shift
175 depth="--depth=$1" ;;
177 shift
178 break ;;
180 usage ;;
181 esac
182 shift
183 done
185 repo="$1"
186 test -n "$repo" ||
187 die 'you must specify a repository to clone.'
189 # --bare implies --no-checkout and --no-separate-remote
190 if test yes = "$bare"
191 then
192 if test yes = "$origin_override"
193 then
194 die '--bare and --origin $origin options are incompatible.'
196 no_checkout=yes
197 use_separate_remote=
200 if test -z "$origin"
201 then
202 origin=origin
205 # Turn the source into an absolute path if
206 # it is local
207 if base=$(get_repo_base "$repo"); then
208 repo="$base"
209 if test -z "$depth"
210 then
211 local=yes
215 dir="$2"
216 # Try using "humanish" part of source repo if user didn't specify one
217 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
218 [ -e "$dir" ] && die "destination directory '$dir' already exists."
219 [ yes = "$bare" ] && unset GIT_WORK_TREE
220 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
221 die "working tree '$GIT_WORK_TREE' already exists."
224 cleanup() {
225 err=$?
226 test -z "$D" && rm -rf "$dir"
227 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
228 cd ..
229 test -n "$D" && rm -rf "$D"
230 test -n "$W" && rm -rf "$W"
231 exit $err
233 trap cleanup 0
234 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
235 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
236 W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
237 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
238 GIT_DIR="$D"
239 else
240 GIT_DIR="$D/.git"
241 fi &&
242 export GIT_DIR &&
243 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
245 if test -n "$bare"
246 then
247 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
250 if test -n "$reference"
251 then
252 ref_git=
253 if test -d "$reference"
254 then
255 if test -d "$reference/.git/objects"
256 then
257 ref_git="$reference/.git"
258 elif test -d "$reference/objects"
259 then
260 ref_git="$reference"
263 if test -n "$ref_git"
264 then
265 ref_git=$(cd "$ref_git" && pwd)
266 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
268 GIT_DIR="$ref_git" git for-each-ref \
269 --format='%(objectname) %(*objectname)'
271 while read a b
273 test -z "$a" ||
274 git update-ref "refs/reference-tmp/$a" "$a"
275 test -z "$b" ||
276 git update-ref "refs/reference-tmp/$b" "$b"
277 done
278 else
279 die "reference repository '$reference' is not a local directory."
283 rm -f "$GIT_DIR/CLONE_HEAD"
285 # We do local magic only when the user tells us to.
286 case "$local" in
287 yes)
288 ( cd "$repo/objects" ) ||
289 die "cannot chdir to local '$repo/objects'."
291 if test "$local_shared" = yes
292 then
293 mkdir -p "$GIT_DIR/objects/info"
294 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
295 else
296 l= &&
297 if test "$use_local_hardlink" = yes
298 then
299 # See if we can hardlink and drop "l" if not.
300 sample_file=$(cd "$repo" && \
301 find objects -type f -print | sed -e 1q)
302 # objects directory should not be empty because
303 # we are cloning!
304 test -f "$repo/$sample_file" ||
305 die "fatal: cannot clone empty repository"
306 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
307 then
308 rm -f "$GIT_DIR/objects/sample"
310 elif test -n "$local_explicitly_asked_for"
311 then
312 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
314 fi &&
315 cd "$repo" &&
316 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
318 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
321 case "$repo" in
322 rsync://*)
323 case "$depth" in
324 "") ;;
325 *) die "shallow over rsync not supported" ;;
326 esac
327 rsync $quiet -av --ignore-existing \
328 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
329 exit
330 # Look at objects/info/alternates for rsync -- http will
331 # support it natively and git native ones will do it on the
332 # remote end. Not having that file is not a crime.
333 rsync -q "$repo/objects/info/alternates" \
334 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
335 rm -f "$GIT_DIR/TMP_ALT"
336 if test -f "$GIT_DIR/TMP_ALT"
337 then
338 ( cd "$D" &&
339 . git-parse-remote &&
340 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
341 while read alt
343 case "$alt" in 'bad alternate: '*) die "$alt";; esac
344 case "$quiet" in
345 '') echo >&2 "Getting alternate: $alt" ;;
346 esac
347 rsync $quiet -av --ignore-existing \
348 --exclude info "$alt" "$GIT_DIR/objects" || exit
349 done
350 rm -f "$GIT_DIR/TMP_ALT"
352 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
354 https://*|http://*|ftp://*)
355 case "$depth" in
356 "") ;;
357 *) die "shallow over http or ftp not supported" ;;
358 esac
359 if test -z "@@NO_CURL@@"
360 then
361 clone_dumb_http "$repo" "$D"
362 else
363 die "http transport not supported, rebuild Git with curl support"
367 case "$upload_pack" in
368 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
369 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
370 esac >"$GIT_DIR/CLONE_HEAD" ||
371 die "fetch-pack from '$repo' failed."
373 esac
375 esac
376 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
378 if test -f "$GIT_DIR/CLONE_HEAD"
379 then
380 # Read git-fetch-pack -k output and store the remote branches.
381 if [ -n "$use_separate_remote" ]
382 then
383 branch_top="remotes/$origin"
384 else
385 branch_top="heads"
387 tag_top="tags"
388 while read sha1 name
390 case "$name" in
391 *'^{}')
392 continue ;;
393 HEAD)
394 destname="REMOTE_HEAD" ;;
395 refs/heads/*)
396 destname="refs/$branch_top/${name#refs/heads/}" ;;
397 refs/tags/*)
398 destname="refs/$tag_top/${name#refs/tags/}" ;;
400 continue ;;
401 esac
402 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
403 done < "$GIT_DIR/CLONE_HEAD"
406 if test -n "$W"; then
407 cd "$W" || exit
408 else
409 cd "$D" || exit
412 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
413 then
414 # a non-bare repository is always in separate-remote layout
415 remote_top="refs/remotes/$origin"
416 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
417 case "$head_sha1" in
418 'ref: refs/'*)
419 # Uh-oh, the remote told us (http transport done against
420 # new style repository with a symref HEAD).
421 # Ideally we should skip the guesswork but for now
422 # opt for minimum change.
423 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
424 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
426 esac
428 # The name under $remote_top the remote HEAD seems to point at.
429 head_points_at=$(
431 test -f "$GIT_DIR/$remote_top/master" && echo "master"
432 cd "$GIT_DIR/$remote_top" &&
433 find . -type f -print | sed -e 's/^\.\///'
434 ) | (
435 done=f
436 while read name
438 test t = $done && continue
439 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
440 if test "$head_sha1" = "$branch_tip"
441 then
442 echo "$name"
443 done=t
445 done
449 # Upstream URL
450 git config remote."$origin".url "$repo" &&
452 # Set up the mappings to track the remote branches.
453 git config remote."$origin".fetch \
454 "+refs/heads/*:$remote_top/*" '^$' &&
456 # Write out remote.$origin config, and update our "$head_points_at".
457 case "$head_points_at" in
459 # Local default branch
460 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
462 # Tracking branch for the primary branch at the remote.
463 git update-ref HEAD "$head_sha1" &&
465 rm -f "refs/remotes/$origin/HEAD"
466 git symbolic-ref "refs/remotes/$origin/HEAD" \
467 "refs/remotes/$origin/$head_points_at" &&
469 git config branch."$head_points_at".remote "$origin" &&
470 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
473 # Source had detached HEAD pointing nowhere
474 git update-ref --no-deref HEAD "$head_sha1" &&
475 rm -f "refs/remotes/$origin/HEAD"
477 esac
479 case "$no_checkout" in
481 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
482 git read-tree -m -u $v HEAD HEAD
483 esac
485 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
487 trap - 0