let pack-objects do the writing of unreachable objects as loose objects
[git/spearce.git] / git-clone.sh
blob9d88d1ce60d9c34bd16976d403f210fb450940a5
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
213 elif test -f "$repo"
214 then
215 case "$repo" in /*) ;; *) repo="$PWD/$repo" ;; esac
218 # Decide the directory name of the new repository
219 if test -n "$2"
220 then
221 dir="$2"
222 test $# = 2 || die "excess parameter to git-clone"
223 else
224 # Derive one from the repository name
225 # Try using "humanish" part of source repo if user didn't specify one
226 if test -f "$repo"
227 then
228 # Cloning from a bundle
229 dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
230 else
231 dir=$(echo "$repo" |
232 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) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
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 cpio_quiet_flag=""
315 cpio --help 2>&1 | grep -- --quiet >/dev/null && \
316 cpio_quiet_flag=--quiet
317 l= &&
318 if test "$use_local_hardlink" = yes
319 then
320 # See if we can hardlink and drop "l" if not.
321 sample_file=$(cd "$repo" && \
322 find objects -type f -print | sed -e 1q)
323 # objects directory should not be empty because
324 # we are cloning!
325 test -f "$repo/$sample_file" ||
326 die "fatal: cannot clone empty repository"
327 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
328 then
329 rm -f "$GIT_DIR/objects/sample"
331 elif test -n "$local_explicitly_asked_for"
332 then
333 echo >&2 "Warning: -l asked but cannot hardlink to $repo"
335 fi &&
336 cd "$repo" &&
337 # Create dirs using umask and permissions and destination
338 find objects -type d -print | (cd "$GIT_DIR" && xargs mkdir -p) &&
339 # Copy existing 0444 permissions on content
340 find objects ! -type d -print | cpio $cpio_quiet_flag -pumd$l "$GIT_DIR/" || \
341 exit 1
343 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
346 case "$repo" in
347 rsync://*)
348 case "$depth" in
349 "") ;;
350 *) die "shallow over rsync not supported" ;;
351 esac
352 rsync $quiet -av --ignore-existing \
353 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
354 exit
355 # Look at objects/info/alternates for rsync -- http will
356 # support it natively and git native ones will do it on the
357 # remote end. Not having that file is not a crime.
358 rsync -q "$repo/objects/info/alternates" \
359 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
360 rm -f "$GIT_DIR/TMP_ALT"
361 if test -f "$GIT_DIR/TMP_ALT"
362 then
363 ( cd "$D" &&
364 . git-parse-remote &&
365 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
366 while read alt
368 case "$alt" in 'bad alternate: '*) die "$alt";; esac
369 case "$quiet" in
370 '') echo >&2 "Getting alternate: $alt" ;;
371 esac
372 rsync $quiet -av --ignore-existing \
373 --exclude info "$alt" "$GIT_DIR/objects" || exit
374 done
375 rm -f "$GIT_DIR/TMP_ALT"
377 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
379 https://*|http://*|ftp://*)
380 case "$depth" in
381 "") ;;
382 *) die "shallow over http or ftp not supported" ;;
383 esac
384 if test -z "@@NO_CURL@@"
385 then
386 clone_dumb_http "$repo" "$D"
387 else
388 die "http transport not supported, rebuild Git with curl support"
392 if [ -f "$repo" ] ; then
393 git bundle unbundle "$repo" > "$GIT_DIR/CLONE_HEAD" ||
394 die "unbundle from '$repo' failed."
395 else
396 case "$upload_pack" in
397 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
398 *) git-fetch-pack --all -k \
399 $quiet "$upload_pack" $depth $no_progress "$repo" ;;
400 esac >"$GIT_DIR/CLONE_HEAD" ||
401 die "fetch-pack from '$repo' failed."
404 esac
406 esac
407 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
409 if test -f "$GIT_DIR/CLONE_HEAD"
410 then
411 # Read git-fetch-pack -k output and store the remote branches.
412 if [ -n "$use_separate_remote" ]
413 then
414 branch_top="remotes/$origin"
415 else
416 branch_top="heads"
418 tag_top="tags"
419 while read sha1 name
421 case "$name" in
422 *'^{}')
423 continue ;;
424 HEAD)
425 destname="REMOTE_HEAD" ;;
426 refs/heads/*)
427 destname="refs/$branch_top/${name#refs/heads/}" ;;
428 refs/tags/*)
429 destname="refs/$tag_top/${name#refs/tags/}" ;;
431 continue ;;
432 esac
433 git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
434 done < "$GIT_DIR/CLONE_HEAD"
437 if test -n "$W"; then
438 cd "$W" || exit
439 else
440 cd "$D" || exit
443 if test -z "$bare"
444 then
445 # a non-bare repository is always in separate-remote layout
446 remote_top="refs/remotes/$origin"
447 head_sha1=
448 test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
449 case "$head_sha1" in
450 'ref: refs/'*)
451 # Uh-oh, the remote told us (http transport done against
452 # new style repository with a symref HEAD).
453 # Ideally we should skip the guesswork but for now
454 # opt for minimum change.
455 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
456 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
458 esac
460 # The name under $remote_top the remote HEAD seems to point at.
461 head_points_at=$(
463 test -f "$GIT_DIR/$remote_top/master" && echo "master"
464 cd "$GIT_DIR/$remote_top" &&
465 find . -type f -print | sed -e 's/^\.\///'
466 ) | (
467 done=f
468 while read name
470 test t = $done && continue
471 branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
472 if test "$head_sha1" = "$branch_tip"
473 then
474 echo "$name"
475 done=t
477 done
481 # Upstream URL
482 git config remote."$origin".url "$repo" &&
484 # Set up the mappings to track the remote branches.
485 git config remote."$origin".fetch \
486 "+refs/heads/*:$remote_top/*" '^$' &&
488 # Write out remote.$origin config, and update our "$head_points_at".
489 case "$head_points_at" in
491 # Local default branch
492 git symbolic-ref HEAD "refs/heads/$head_points_at" &&
494 # Tracking branch for the primary branch at the remote.
495 git update-ref HEAD "$head_sha1" &&
497 rm -f "refs/remotes/$origin/HEAD"
498 git symbolic-ref "refs/remotes/$origin/HEAD" \
499 "refs/remotes/$origin/$head_points_at" &&
501 git config branch."$head_points_at".remote "$origin" &&
502 git config branch."$head_points_at".merge "refs/heads/$head_points_at"
505 if test -z "$head_sha1"
506 then
507 # Source had nonexistent ref in HEAD
508 echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
509 no_checkout=t
510 else
511 # Source had detached HEAD pointing nowhere
512 git update-ref --no-deref HEAD "$head_sha1" &&
513 rm -f "refs/remotes/$origin/HEAD"
516 esac
518 case "$no_checkout" in
520 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
521 git read-tree -m -u $v HEAD HEAD
522 esac
524 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
526 trap - 0