[PATCH] rsh.c env and quoting cleanup, take 2
[git.git] / git-clone.sh
1 #!/bin/sh
2 #
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.
7
8 # See git-sh-setup why.
9 unset CDPATH
10
11 usage() {
12         echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>"
13         exit 1
14 }
15
16 get_repo_base() {
17         (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18 }
19
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21     curl_extra_args="-k"
22 fi
23
24 http_fetch () {
25         # $1 = Remote, $2 = Local
26         curl -nsf $curl_extra_args "$1" >"$2"
27 }
28
29 clone_dumb_http () {
30         # $1 - remote, $2 - local
31         cd "$2" &&
32         clone_tmp='.git/clone-tmp' &&
33         mkdir -p "$clone_tmp" || exit 1
34         http_fetch "$1/info/refs" "$clone_tmp/refs" &&
35         http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
36                 echo >&2 "Cannot get remote repository information.
37 Perhaps git-update-server-info needs to be run there?"
38                 exit 1;
39         }
40         while read type name
41         do
42                 case "$type" in
43                 P) ;;
44                 *) continue ;;
45                 esac &&
46
47                 idx=`expr "$name" : '\(.*\)\.pack'`.idx
48                 http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
49                 http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
50                 git-verify-pack ".git/objects/pack/$idx" || exit 1
51         done <"$clone_tmp/packs"
52
53         while read sha1 refname
54         do
55                 name=`expr "$refname" : 'refs/\(.*\)'` &&
56                 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
57         done <"$clone_tmp/refs"
58         rm -fr "$clone_tmp"
59 }
60
61 quiet=
62 use_local=no
63 local_shared=no
64 upload_pack=
65 while
66         case "$#,$1" in
67         0,*) break ;;
68         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
69         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
70           local_shared=yes ;;
71         *,-q|*,--quiet) quiet=-q ;;
72         1,-u|1,--upload-pack) usage ;;
73         *,-u|*,--upload-pack)
74                 shift
75                 upload_pack="--exec=$1" ;;
76         *,-*) usage ;;
77         *) break ;;
78         esac
79 do
80         shift
81 done
82
83 # Turn the source into an absolute path if
84 # it is local
85 repo="$1"
86 local=no
87 if base=$(get_repo_base "$repo"); then
88         repo="$base"
89         local=yes
90 fi
91
92 dir="$2"
93 mkdir "$dir" &&
94 D=$(
95         (cd "$dir" && git-init-db && pwd)
96 ) &&
97 test -d "$D" || usage
98
99 # We do local magic only when the user tells us to.
100 case "$local,$use_local" in
101 yes,yes)
102         ( cd "$repo/objects" ) || {
103                 echo >&2 "-l flag seen but $repo is not local."
104                 exit 1
105         }
106
107         case "$local_shared" in
108         no)
109             # See if we can hardlink and drop "l" if not.
110             sample_file=$(cd "$repo" && \
111                           find objects -type f -print | sed -e 1q)
112
113             # objects directory should not be empty since we are cloning!
114             test -f "$repo/$sample_file" || exit
115
116             l=
117             if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
118             then
119                     l=l
120             fi &&
121             rm -f "$D/.git/objects/sample" &&
122             cd "$repo" &&
123             find objects -type f -print |
124             cpio -puamd$l "$D/.git/" || exit 1
125             ;;
126         yes)
127             mkdir -p "$D/.git/objects/info"
128             {
129                 test -f "$repo/objects/info/alternates" &&
130                 cat "$repo/objects/info/alternates";
131                 echo "$repo/objects"
132             } >"$D/.git/objects/info/alternates"
133             ;;
134         esac
135
136         # Make a duplicate of refs and HEAD pointer
137         HEAD=
138         if test -f "$repo/HEAD"
139         then
140                 HEAD=HEAD
141         fi
142         tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1
143         ;;
144 *)
145         case "$repo" in
146         rsync://*)
147                 rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" &&
148                 rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/"
149                 ;;
150         http://*)
151                 clone_dumb_http "$repo" "$D"
152                 ;;
153         *)
154                 cd "$D" && case "$upload_pack" in
155                 '') git-clone-pack $quiet "$repo" ;;
156                 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
157                 esac
158                 ;;
159         esac
160         ;;
161 esac
162
163 # Update origin.
164 mkdir -p "$D/.git/remotes/" &&
165 rm -f "$D/.git/remotes/origin" &&
166 echo >"$D/.git/remotes/origin" \
167 "URL: $repo
168 Pull: master:origin"