]>
Commit | Line | Data |
---|---|---|
3f571e0b | 1 | #!/bin/sh |
e95ab1ed JH |
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 | ||
365527ad JH |
8 | # See git-sh-setup why. |
9 | unset CDPATH | |
10 | ||
e95ab1ed | 11 | usage() { |
806f36d4 | 12 | echo >&2 "Usage: $0 [-l [-s]] [-q] [-u <upload-pack>] [-n] <repo> [<dir>]" |
e95ab1ed JH |
13 | exit 1 |
14 | } | |
15 | ||
ba375acf LT |
16 | get_repo_base() { |
17 | (cd "$1" && (cd .git ; pwd)) 2> /dev/null | |
18 | } | |
19 | ||
0516de30 JH |
20 | if [ -n "$GIT_SSL_NO_VERIFY" ]; then |
21 | curl_extra_args="-k" | |
22 | fi | |
23 | ||
24 | http_fetch () { | |
25 | # $1 = Remote, $2 = Local | |
66c9ec25 | 26 | curl -nsfL $curl_extra_args "$1" >"$2" |
0516de30 JH |
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/\(.*\)'` && | |
cdb39508 JH |
56 | case "$name" in |
57 | *^*) ;; | |
58 | *) | |
59 | git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1 | |
60 | esac | |
0516de30 JH |
61 | done <"$clone_tmp/refs" |
62 | rm -fr "$clone_tmp" | |
63 | } | |
64 | ||
167a4a33 | 65 | quiet= |
e95ab1ed | 66 | use_local=no |
aae4f42c | 67 | local_shared=no |
036a72d8 | 68 | no_checkout= |
6ec311da | 69 | upload_pack= |
e95ab1ed JH |
70 | while |
71 | case "$#,$1" in | |
72 | 0,*) break ;; | |
036a72d8 | 73 | *,-n) no_checkout=yes ;; |
1cadb5a2 | 74 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; |
aae4f42c | 75 | *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) |
2c4ed386 | 76 | local_shared=yes; use_local=yes ;; |
167a4a33 | 77 | *,-q|*,--quiet) quiet=-q ;; |
1cadb5a2 | 78 | 1,-u|1,--upload-pack) usage ;; |
6ec311da JH |
79 | *,-u|*,--upload-pack) |
80 | shift | |
1cadb5a2 | 81 | upload_pack="--exec=$1" ;; |
e95ab1ed JH |
82 | *,-*) usage ;; |
83 | *) break ;; | |
84 | esac | |
85 | do | |
86 | shift | |
87 | done | |
88 | ||
ba375acf LT |
89 | # Turn the source into an absolute path if |
90 | # it is local | |
3f571e0b | 91 | repo="$1" |
ba375acf LT |
92 | local=no |
93 | if base=$(get_repo_base "$repo"); then | |
94 | repo="$base" | |
95 | local=yes | |
96 | fi | |
97 | ||
3f571e0b | 98 | dir="$2" |
0879aa28 AE |
99 | # Try using "humanish" part of source repo if user didn't specify one |
100 | [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g') | |
b0c698a6 | 101 | [ -e "$dir" ] && echo "$dir already exists." && usage |
7f10f7c4 | 102 | mkdir -p "$dir" && |
e95ab1ed JH |
103 | D=$( |
104 | (cd "$dir" && git-init-db && pwd) | |
105 | ) && | |
106 | test -d "$D" || usage | |
107 | ||
108 | # We do local magic only when the user tells us to. | |
ba375acf LT |
109 | case "$local,$use_local" in |
110 | yes,yes) | |
e95ab1ed | 111 | ( cd "$repo/objects" ) || { |
ab6625e0 JH |
112 | echo >&2 "-l flag seen but $repo is not local." |
113 | exit 1 | |
e95ab1ed JH |
114 | } |
115 | ||
aae4f42c JH |
116 | case "$local_shared" in |
117 | no) | |
118 | # See if we can hardlink and drop "l" if not. | |
119 | sample_file=$(cd "$repo" && \ | |
120 | find objects -type f -print | sed -e 1q) | |
e95ab1ed | 121 | |
aae4f42c JH |
122 | # objects directory should not be empty since we are cloning! |
123 | test -f "$repo/$sample_file" || exit | |
e95ab1ed | 124 | |
aae4f42c JH |
125 | l= |
126 | if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null | |
127 | then | |
128 | l=l | |
129 | fi && | |
130 | rm -f "$D/.git/objects/sample" && | |
131 | cd "$repo" && | |
3d95bf07 | 132 | find objects -depth -print | cpio -puamd$l "$D/.git/" || exit 1 |
aae4f42c JH |
133 | ;; |
134 | yes) | |
135 | mkdir -p "$D/.git/objects/info" | |
0f87f893 JH |
136 | { |
137 | test -f "$repo/objects/info/alternates" && | |
138 | cat "$repo/objects/info/alternates"; | |
139 | echo "$repo/objects" | |
140 | } >"$D/.git/objects/info/alternates" | |
aae4f42c JH |
141 | ;; |
142 | esac | |
e95ab1ed JH |
143 | |
144 | # Make a duplicate of refs and HEAD pointer | |
145 | HEAD= | |
146 | if test -f "$repo/HEAD" | |
147 | then | |
148 | HEAD=HEAD | |
149 | fi | |
229a7ed7 JH |
150 | (cd "$repo" && tar cf - refs $HEAD) | |
151 | (cd "$D/.git" && tar xf -) || exit 1 | |
7558ef89 LT |
152 | ;; |
153 | *) | |
1cadb5a2 JH |
154 | case "$repo" in |
155 | rsync://*) | |
4447badc JH |
156 | rsync $quiet -av --ignore-existing \ |
157 | --exclude info "$repo/objects/" "$D/.git/objects/" && | |
158 | rsync $quiet -av --ignore-existing \ | |
159 | --exclude info "$repo/refs/" "$D/.git/refs/" || exit | |
160 | ||
161 | # Look at objects/info/alternates for rsync -- http will | |
162 | # support it natively and git native ones will do it on the | |
163 | # remote end. Not having that file is not a crime. | |
89d844d0 JH |
164 | rsync -q "$repo/objects/info/alternates" \ |
165 | "$D/.git/TMP_ALT" 2>/dev/null || | |
4447badc JH |
166 | rm -f "$D/.git/TMP_ALT" |
167 | if test -f "$D/.git/TMP_ALT" | |
168 | then | |
0e9ab02d | 169 | ( cd "$D" && |
4447badc JH |
170 | . git-parse-remote && |
171 | resolve_alternates "$repo" <"./.git/TMP_ALT" ) | | |
172 | while read alt | |
173 | do | |
174 | case "$alt" in 'bad alternate: '*) die "$alt";; esac | |
175 | case "$quiet" in | |
176 | '') echo >&2 "Getting alternate: $alt" ;; | |
177 | esac | |
178 | rsync $quiet -av --ignore-existing \ | |
179 | --exclude info "$alt" "$D/.git/objects" || exit | |
180 | done | |
181 | rm -f "$D/.git/TMP_ALT" | |
182 | fi | |
1cadb5a2 JH |
183 | ;; |
184 | http://*) | |
0516de30 | 185 | clone_dumb_http "$repo" "$D" |
1cadb5a2 JH |
186 | ;; |
187 | *) | |
188 | cd "$D" && case "$upload_pack" in | |
189 | '') git-clone-pack $quiet "$repo" ;; | |
190 | *) git-clone-pack $quiet "$upload_pack" "$repo" ;; | |
0a8b4def JH |
191 | esac || { |
192 | echo >&2 "clone-pack from '$repo' failed." | |
193 | exit 1 | |
194 | } | |
1cadb5a2 | 195 | ;; |
6ec311da | 196 | esac |
7558ef89 LT |
197 | ;; |
198 | esac | |
1cadb5a2 | 199 | |
0e9ab02d | 200 | cd "$D" || exit |
036a72d8 JH |
201 | |
202 | if test -f ".git/HEAD" | |
203 | then | |
e125c1a7 JH |
204 | head_points_at=`git-symbolic-ref HEAD` |
205 | case "$head_points_at" in | |
206 | refs/heads/*) | |
207 | head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'` | |
208 | mkdir -p .git/remotes && | |
209 | echo >.git/remotes/origin \ | |
210 | "URL: $repo | |
95d117b6 JH |
211 | Pull: $head_points_at:origin" && |
212 | cp ".git/refs/heads/$head_points_at" .git/refs/heads/origin && | |
213 | find .git/refs/heads -type f -print | | |
214 | while read ref | |
215 | do | |
216 | head=`expr "$ref" : '.git/refs/heads/\(.*\)'` && | |
217 | test "$head_points_at" = "$head" || | |
218 | test "origin" = "$head" || | |
219 | echo "Pull: ${head}:${head}" | |
220 | done >>.git/remotes/origin | |
e125c1a7 JH |
221 | esac |
222 | ||
036a72d8 JH |
223 | case "$no_checkout" in |
224 | '') | |
225 | git checkout | |
226 | esac | |
227 | fi |