Relocate the python file under appropriate hierarchy
[git-modules-bs.git] / git-clone
blob75fc863a879cb504b1c5c6477c8b10fbf7fa4350
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] [-w|--with-submodule] [-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" -o \
32 "`git config --bool http.sslVerify`" = false ]; then
33 curl_extra_args="-k"
36 http_fetch () {
37 # $1 = Remote, $2 = Local
38 curl -nsfL $curl_extra_args "$1" >"$2" ||
39 case $? in
40 126|127) exit ;;
41 *) return $? ;;
42 esac
45 clone_dumb_http () {
46 # $1 - remote, $2 - local
47 cd "$2" &&
48 clone_tmp="$GIT_DIR/clone-tmp" &&
49 mkdir -p "$clone_tmp" || exit 1
50 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
51 "`git config --bool http.noEPSV`" = true ]; then
52 curl_extra_args="${curl_extra_args} --disable-epsv"
54 http_fetch "$1/info/refs" "$clone_tmp/refs" ||
55 die "Cannot get remote repository information.
56 Perhaps git-update-server-info needs to be run there?"
57 test "z$quiet" = z && v=-v || v=
58 while read sha1 refname
60 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
61 case "$name" in
62 *^*) continue;;
63 esac
64 case "$bare,$name" in
65 yes,* | ,heads/* | ,tags/*) ;;
66 *) continue ;;
67 esac
68 if test -n "$use_separate_remote" &&
69 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
70 then
71 tname="remotes/$origin/$branch_name"
72 else
73 tname=$name
75 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
76 done <"$clone_tmp/refs"
77 rm -fr "$clone_tmp"
78 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
79 rm -f "$GIT_DIR/REMOTE_HEAD"
80 if test -f "$GIT_DIR/REMOTE_HEAD"; then
81 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
82 case "$head_sha1" in
83 'ref: refs/'*)
86 git-http-fetch $v -a "$head_sha1" "$1" ||
87 rm -f "$GIT_DIR/REMOTE_HEAD"
89 esac
93 initializeSubModule() {
94 if [ ! -d "$1"/.git ]; then
95 git-submodule-test init "$1"; git-submodule-test update "$1"
99 initSubModules() {
100 current_dir=`pwd`
101 dir_path="$current_dir:$dir_path"
102 initializeSubModule "$1"
103 cd "$1"
104 if [ -f .gitmodules ]; then
105 for sub_mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
106 initSubModules "$sub_mod_path"
107 done
109 old_dir=$(echo $dir_path | cut -d':' -f1-1)
110 length_old_dir=`expr "$old_dir" : '.*'`
111 cd $old_dir
112 index=$(echo "$length_old_dir+2" | bc)
113 dir_path=`echo $dir_path $index | awk '{print substr($1, $2)}'`
116 quiet=
117 local=no
118 use_local_hardlink=yes
119 local_shared=no
120 unset template
121 no_checkout=
122 upload_pack=
123 bare=
124 reference=
125 origin=
126 origin_override=
127 use_separate_remote=t
128 depth=
129 no_progress=
130 local_explicitly_asked_for=
131 test -t 1 || no_progress=--no-progress
132 with_submodule=0
133 while
134 case "$#,$1" in
135 0,*) break ;;
136 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
137 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
138 no_checkout=yes ;;
139 *,--na|*,--nak|*,--nake|*,--naked|\
140 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
141 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local)
142 local_explicitly_asked_for=yes
143 use_local_hardlink=yes ;;
144 *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\
145 *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks)
146 use_local_hardlink=no ;;
147 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
148 local_shared=yes; ;;
149 1,--template) usage ;;
150 *,--template)
151 shift; template="--template=$1" ;;
152 *,--template=*)
153 template="$1" ;;
154 *,-q|*,--quiet) quiet=-q ;;
155 *,--use-separate-remote) ;;
156 *,--no-separate-remote)
157 die "clones are always made with separate-remote layout" ;;
158 1,--reference) usage ;;
159 *,--reference)
160 shift; reference="$1" ;;
161 *,--reference=*)
162 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
163 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
164 case "$2" in
166 usage ;;
167 */*)
168 die "'$2' is not suitable for an origin name"
169 esac
170 git check-ref-format "heads/$2" ||
171 die "'$2' is not suitable for a branch name"
172 test -z "$origin_override" ||
173 die "Do not give more than one --origin options."
174 origin_override=yes
175 origin="$2"; shift
177 1,-u|1,--upload-pack) usage ;;
178 *,-u|*,--upload-pack)
179 shift
180 upload_pack="--upload-pack=$1" ;;
181 *,--upload-pack=*)
182 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
183 *,-w|*,--w|*,--wi|*,--wit|*,--with|*,--with-s|*,--with-su|*,--with-sub|*,--with-subm|*,--with-submo|*,--with-submod|*,--with-submodu|*,--with-submodul|*,--with-submodule) with_submodule=1 ;;
184 1,--depth) usage;;
185 *,--depth)
186 shift
187 depth="--depth=$1";;
188 *,-*) usage ;;
189 *) break ;;
190 esac
192 shift
193 done
195 repo="$1"
196 test -n "$repo" ||
197 die 'you must specify a repository to clone.'
199 # --bare implies --no-checkout and --no-separate-remote
200 if test yes = "$bare"
201 then
202 if test yes = "$origin_override"
203 then
204 die '--bare and --origin $origin options are incompatible.'
206 no_checkout=yes
207 use_separate_remote=
210 if test -z "$origin"
211 then
212 origin=origin
215 # Turn the source into an absolute path if
216 # it is local
217 if base=$(get_repo_base "$repo"); then
218 repo="$base"
219 local=yes
222 dir="$2"
223 # Try using "humanish" part of source repo if user didn't specify one
224 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
225 [ -e "$dir" ] && die "destination directory '$dir' already exists."
226 [ yes = "$bare" ] && unset GIT_WORK_TREE
227 [ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
228 die "working tree '$GIT_WORK_TREE' already exists."
231 cleanup() {
232 err=$?
233 test -z "$D" && rm -rf "$dir"
234 test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
235 cd ..
236 test -n "$D" && rm -rf "$D"
237 test -n "$W" && rm -rf "$W"
238 exit $err
240 trap cleanup 0
241 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
242 test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
243 W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
244 if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
245 GIT_DIR="$D"
246 else
247 GIT_DIR="$D/.git"
248 fi &&
249 export GIT_DIR &&
250 GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
252 if test -n "$bare"
253 then
254 GIT_CONFIG="$GIT_DIR/config" git config core.bare true
257 if test -n "$reference"
258 then
259 ref_git=
260 if test -d "$reference"
261 then
262 if test -d "$reference/.git/objects"
263 then
264 ref_git="$reference/.git"
265 elif test -d "$reference/objects"
266 then
267 ref_git="$reference"
270 if test -n "$ref_git"
271 then
272 ref_git=$(cd "$ref_git" && pwd)
273 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
275 GIT_DIR="$ref_git" git for-each-ref \
276 --format='%(objectname) %(*objectname)'
278 while read a b
280 test -z "$a" ||
281 git update-ref "refs/reference-tmp/$a" "$a"
282 test -z "$b" ||
283 git update-ref "refs/reference-tmp/$b" "$b"
284 done
285 else
286 die "reference repository '$reference' is not a local directory."
290 rm -f "$GIT_DIR/CLONE_HEAD"
292 # We do local magic only when the user tells us to.
293 case "$local" in
294 yes)
295 ( cd "$repo/objects" ) ||
296 die "cannot chdir to local '$repo/objects'."
298 if test "$local_shared" = yes
299 then
300 mkdir -p "$GIT_DIR/objects/info"
301 echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
302 else
303 l= &&
304 if test "$use_local_hardlink" = yes
305 then
306 # See if we can hardlink and drop "l" if not.
307 sample_file=$(cd "$repo" && \
308 find objects -type f -print | sed -e 1q)
309 # objects directory should not be empty because
310 # we are cloning!
311 test -f "$repo/$sample_file" || exit
312 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
313 then
314 rm -f "$GIT_DIR/objects/sample"
316 elif test -n "$local_explicitly_asked_for"
317 then
318 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
320 fi &&
321 cd "$repo" &&
322 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
324 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
327 case "$repo" in
328 rsync://*)
329 case "$depth" in
330 "") ;;
331 *) die "shallow over rsync not supported" ;;
332 esac
333 rsync $quiet -av --ignore-existing \
334 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
335 exit
336 # Look at objects/info/alternates for rsync -- http will
337 # support it natively and git native ones will do it on the
338 # remote end. Not having that file is not a crime.
339 rsync -q "$repo/objects/info/alternates" \
340 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
341 rm -f "$GIT_DIR/TMP_ALT"
342 if test -f "$GIT_DIR/TMP_ALT"
343 then
344 ( cd "$D" &&
345 . git-parse-remote &&
346 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
347 while read alt
349 case "$alt" in 'bad alternate: '*) die "$alt";; esac
350 case "$quiet" in
351 '') echo >&2 "Getting alternate: $alt" ;;
352 esac
353 rsync $quiet -av --ignore-existing \
354 --exclude info "$alt" "$GIT_DIR/objects" || exit
355 done
356 rm -f "$GIT_DIR/TMP_ALT"
358 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
360 https://*|http://*|ftp://*)
361 case "$depth" in
362 "") ;;
363 *) die "shallow over http or ftp not supported" ;;
364 esac
365 if test -z ""
366 then
367 clone_dumb_http "$repo" "$D"
368 else
369 die "http transport not supported, rebuild Git with curl support"
373 case "$upload_pack" in
374 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
375 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
376 esac >"$GIT_DIR/CLONE_HEAD" ||
377 die "fetch-pack from '$repo' failed."
379 esac
381 esac
382 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
384 if test -f "$GIT_DIR/CLONE_HEAD"
385 then
386 # Read git-fetch-pack -k output and store the remote branches.
387 if [ -n "$use_separate_remote" ]
388 then
389 branch_top="remotes/$origin"
390 else
391 branch_top="heads"
393 tag_top="tags"
394 while read sha1 name
396 case "$name" in
397 *'^{}')
398 continue ;;
399 HEAD)
400 destname="REMOTE_HEAD" ;;
401 refs/heads/*)
402 destname="refs/$branch_top/${name#refs/heads/}" ;;
403 refs/tags/*)
404 destname="refs/$tag_top/${name#refs/tags/}" ;;
406 continue ;;
407 esac
408 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
409 done < "$GIT_DIR/CLONE_HEAD"
412 if test -n "$W"; then
413 cd "$W" || exit
414 else
415 cd "$D" || exit
418 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
419 then
420 # a non-bare repository is always in separate-remote layout
421 remote_top="refs/remotes/$origin"
422 head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
423 case "$head_sha1" in
424 'ref: refs/'*)
425 # Uh-oh, the remote told us (http transport done against
426 # new style repository with a symref HEAD).
427 # Ideally we should skip the guesswork but for now
428 # opt for minimum change.
429 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
430 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
432 esac
434 # The name under $remote_top the remote HEAD seems to point at.
435 head_points_at=$(
437 test -f "$GIT_DIR/$remote_top/master" && echo "master"
438 cd "$GIT_DIR/$remote_top" &&
439 find . -type f -print | sed -e 's/^\.\///'
440 ) | (
441 done=f
442 while read name
444 test t = $done && continue
445 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
446 if test "$head_sha1" = "$branch_tip"
447 then
448 echo "$name"
449 done=t
451 done
455 # Upstream URL
456 git config remote."$origin".url "$repo" &&
458 # Set up the mappings to track the remote branches.
459 git config remote."$origin".fetch \
460 "+refs/heads/*:$remote_top/*" '^$' &&
462 # Write out remote.$origin config, and update our "$head_points_at".
463 case "$head_points_at" in
465 # Local default branch
466 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
468 # Tracking branch for the primary branch at the remote.
469 git update-ref HEAD "$head_sha1" &&
471 rm -f "refs/remotes/$origin/HEAD"
472 git symbolic-ref "refs/remotes/$origin/HEAD" \
473 "refs/remotes/$origin/$head_points_at" &&
475 git config branch."$head_points_at".remote "$origin" &&
476 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
479 # Source had detached HEAD pointing nowhere
480 git update-ref --no-deref HEAD "$head_sha1" &&
481 rm -f "refs/remotes/$origin/HEAD"
483 esac
485 case "$no_checkout" in
487 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
488 git read-tree -m -u $v HEAD HEAD
489 esac
491 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
493 # The following section initializes the submodules of the repo
494 if [ $with_submodule -eq 1 ]; then
495 if [ -f .gitmodules ]; then
497 # Need to know either what is the alternate
498 # or what else to backup and unset
499 # If this is not done in that case recursion
500 # in a subdirectory of the top level fails
501 # Thus the old values are backed up and restored
502 # after recursion
503 OLD_GIT_DIR="$GIT_DIR"
504 OLD_GIT_CONFIG="$GIT_CONFIG"
505 OLD_GIT_WORK_TREE="$GIT_WORK_TREE"
506 unset GIT_DIR
507 unset GIT_CONFIG
508 unset GIT_WORK_TREE
510 # The following loop iterates over the submodules
511 # visible from he top of the module
512 for mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
513 initSubModules "$mod_path"
514 done
516 GIT_DIR="$OLD_GIT_DIR"
517 GIT_CONFIG="$OLD_GIT_CONFIG"
518 GIT_WORK_TREE="$OLD_GIT_WORK_TREE"
520 # The following export is probably not required
521 export GIT_DIR
522 export GIT_CONFIG
523 export GIT_WORK_TREE
527 trap - 0