[PATCH] write_sha1_to_fd()
[alt-git.git] / git-clone-script
blob5a241fb25e40759a4db21ac3862a848955bc0847
1 #!/bin/sh
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
5 #
6 # Clone a repository into a different directory that does not yet exist.
8 usage() {
9 echo >&2 "* git clone [-l] <repo> <dir>"
10 exit 1
13 get_repo_base() {
14 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
17 quiet=
18 use_local=no
19 while
20 case "$#,$1" in
21 0,*) break ;;
22 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
23 *,-q|*,--quiet) quiet=-q ;;
24 *,-*) usage ;;
25 *) break ;;
26 esac
28 shift
29 done
31 # Turn the source into an absolute path if
32 # it is local
33 repo="$1"
34 local=no
35 if base=$(get_repo_base "$repo"); then
36 repo="$base"
37 local=yes
40 dir="$2"
41 mkdir "$dir" &&
42 D=$(
43 (cd "$dir" && git-init-db && pwd)
44 ) &&
45 test -d "$D" || usage
47 # We do local magic only when the user tells us to.
48 case "$local,$use_local" in
49 yes,yes)
50 ( cd "$repo/objects" ) || {
51 repo="$repo/.git"
52 ( cd "$repo/objects" ) || {
53 echo >&2 "-l flag seen but $repo is not local."
54 exit 1
58 # See if we can hardlink and drop "l" if not.
59 sample_file=$(cd "$repo" && \
60 find objects -type f -print | sed -e 1q)
62 # objects directory should not be empty since we are cloning!
63 test -f "$repo/$sample_file" || exit
66 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
67 then
68 l=l
69 fi &&
70 rm -f "$D/.git/objects/sample" &&
71 cp -r$l "$repo/objects" "$D/.git/" || exit 1
73 # Make a duplicate of refs and HEAD pointer
74 HEAD=
75 if test -f "$repo/HEAD"
76 then
77 HEAD=HEAD
79 tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1
80 exit 0
82 esac
84 case "$repo" in
85 rsync://*)
86 rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" &&
87 rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/"
89 http://*)
90 echo "Somebody should add http fetch" >&2
91 exit 1
94 cd "$D" && git-clone-pack $quiet "$repo"
96 esac