]>
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() { |
87e80c4b | 12 | echo >&2 "Usage: $0 [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-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 | |
05621105 | 34 | http_fetch "$1/info/refs" "$clone_tmp/refs" || { |
0516de30 JH |
35 | echo >&2 "Cannot get remote repository information. |
36 | Perhaps git-update-server-info needs to be run there?" | |
37 | exit 1; | |
38 | } | |
0516de30 JH |
39 | while read sha1 refname |
40 | do | |
41 | name=`expr "$refname" : 'refs/\(.*\)'` && | |
cdb39508 JH |
42 | case "$name" in |
43 | *^*) ;; | |
44 | *) | |
45 | git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1 | |
46 | esac | |
0516de30 JH |
47 | done <"$clone_tmp/refs" |
48 | rm -fr "$clone_tmp" | |
49 | } | |
50 | ||
167a4a33 | 51 | quiet= |
e95ab1ed | 52 | use_local=no |
aae4f42c | 53 | local_shared=no |
036a72d8 | 54 | no_checkout= |
6ec311da | 55 | upload_pack= |
87e80c4b | 56 | bare= |
e6c310fd | 57 | origin=origin |
e6489a1b | 58 | origin_override= |
e95ab1ed JH |
59 | while |
60 | case "$#,$1" in | |
61 | 0,*) break ;; | |
8a1a120c JH |
62 | *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\ |
63 | *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout) | |
64 | no_checkout=yes ;; | |
87e80c4b JH |
65 | *,--na|*,--nak|*,--nake|*,--naked|\ |
66 | *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;; | |
1cadb5a2 | 67 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; |
aae4f42c | 68 | *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) |
2c4ed386 | 69 | local_shared=yes; use_local=yes ;; |
167a4a33 | 70 | *,-q|*,--quiet) quiet=-q ;; |
e6c310fd JS |
71 | 1,-o) usage;; |
72 | *,-o) | |
73 | git-check-ref-format "$2" || { | |
74 | echo >&2 "'$2' is not suitable for a branch name" | |
75 | exit 1 | |
76 | } | |
e6489a1b JH |
77 | test -z "$origin_override" || { |
78 | echo >&2 "Do not give more than one -o options." | |
79 | exit 1 | |
80 | } | |
81 | origin_override=yes | |
e6c310fd JS |
82 | origin="$2"; shift |
83 | ;; | |
1cadb5a2 | 84 | 1,-u|1,--upload-pack) usage ;; |
6ec311da JH |
85 | *,-u|*,--upload-pack) |
86 | shift | |
1cadb5a2 | 87 | upload_pack="--exec=$1" ;; |
e95ab1ed JH |
88 | *,-*) usage ;; |
89 | *) break ;; | |
90 | esac | |
91 | do | |
92 | shift | |
93 | done | |
94 | ||
87e80c4b | 95 | # --bare implies --no-checkout |
e6489a1b JH |
96 | if test yes = "$bare" |
97 | then | |
98 | if test yes = "$origin_override" | |
99 | then | |
100 | echo >&2 '--bare and -o $origin options are incompatible.' | |
101 | exit 1 | |
102 | fi | |
103 | no_checkout=yes | |
104 | fi | |
8a1a120c | 105 | |
ba375acf LT |
106 | # Turn the source into an absolute path if |
107 | # it is local | |
3f571e0b | 108 | repo="$1" |
ba375acf LT |
109 | local=no |
110 | if base=$(get_repo_base "$repo"); then | |
111 | repo="$base" | |
112 | local=yes | |
113 | fi | |
114 | ||
3f571e0b | 115 | dir="$2" |
0879aa28 | 116 | # Try using "humanish" part of source repo if user didn't specify one |
e7555785 | 117 | [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') |
b0c698a6 | 118 | [ -e "$dir" ] && echo "$dir already exists." && usage |
7f10f7c4 | 119 | mkdir -p "$dir" && |
8a1a120c | 120 | D=$(cd "$dir" && pwd) && |
edd3ebfe | 121 | trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit |
87e80c4b | 122 | case "$bare" in |
8a1a120c JH |
123 | yes) GIT_DIR="$D" ;; |
124 | *) GIT_DIR="$D/.git" ;; | |
125 | esac && export GIT_DIR && git-init-db || usage | |
87e80c4b | 126 | case "$bare" in |
8a1a120c JH |
127 | yes) |
128 | GIT_DIR="$D" ;; | |
129 | *) | |
130 | GIT_DIR="$D/.git" ;; | |
131 | esac | |
e95ab1ed JH |
132 | |
133 | # We do local magic only when the user tells us to. | |
ba375acf LT |
134 | case "$local,$use_local" in |
135 | yes,yes) | |
e95ab1ed | 136 | ( cd "$repo/objects" ) || { |
ab6625e0 JH |
137 | echo >&2 "-l flag seen but $repo is not local." |
138 | exit 1 | |
e95ab1ed JH |
139 | } |
140 | ||
aae4f42c JH |
141 | case "$local_shared" in |
142 | no) | |
143 | # See if we can hardlink and drop "l" if not. | |
144 | sample_file=$(cd "$repo" && \ | |
145 | find objects -type f -print | sed -e 1q) | |
e95ab1ed | 146 | |
aae4f42c JH |
147 | # objects directory should not be empty since we are cloning! |
148 | test -f "$repo/$sample_file" || exit | |
e95ab1ed | 149 | |
aae4f42c | 150 | l= |
8a1a120c | 151 | if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null |
aae4f42c JH |
152 | then |
153 | l=l | |
154 | fi && | |
8a1a120c | 155 | rm -f "$GIT_DIR/objects/sample" && |
aae4f42c | 156 | cd "$repo" && |
8a1a120c | 157 | find objects -depth -print | cpio -puamd$l "$GIT_DIR/" || exit 1 |
aae4f42c JH |
158 | ;; |
159 | yes) | |
8a1a120c | 160 | mkdir -p "$GIT_DIR/objects/info" |
0f87f893 JH |
161 | { |
162 | test -f "$repo/objects/info/alternates" && | |
163 | cat "$repo/objects/info/alternates"; | |
164 | echo "$repo/objects" | |
8a1a120c | 165 | } >"$GIT_DIR/objects/info/alternates" |
aae4f42c JH |
166 | ;; |
167 | esac | |
e95ab1ed JH |
168 | |
169 | # Make a duplicate of refs and HEAD pointer | |
170 | HEAD= | |
171 | if test -f "$repo/HEAD" | |
172 | then | |
173 | HEAD=HEAD | |
174 | fi | |
229a7ed7 | 175 | (cd "$repo" && tar cf - refs $HEAD) | |
8a1a120c | 176 | (cd "$GIT_DIR" && tar xf -) || exit 1 |
7558ef89 LT |
177 | ;; |
178 | *) | |
1cadb5a2 JH |
179 | case "$repo" in |
180 | rsync://*) | |
4447badc | 181 | rsync $quiet -av --ignore-existing \ |
8a1a120c | 182 | --exclude info "$repo/objects/" "$GIT_DIR/objects/" && |
4447badc | 183 | rsync $quiet -av --ignore-existing \ |
8a1a120c | 184 | --exclude info "$repo/refs/" "$GIT_DIR/refs/" || exit |
4447badc JH |
185 | |
186 | # Look at objects/info/alternates for rsync -- http will | |
187 | # support it natively and git native ones will do it on the | |
188 | # remote end. Not having that file is not a crime. | |
89d844d0 | 189 | rsync -q "$repo/objects/info/alternates" \ |
8a1a120c JH |
190 | "$GIT_DIR/TMP_ALT" 2>/dev/null || |
191 | rm -f "$GIT_DIR/TMP_ALT" | |
192 | if test -f "$GIT_DIR/TMP_ALT" | |
4447badc | 193 | then |
0e9ab02d | 194 | ( cd "$D" && |
4447badc | 195 | . git-parse-remote && |
8a1a120c | 196 | resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) | |
4447badc JH |
197 | while read alt |
198 | do | |
199 | case "$alt" in 'bad alternate: '*) die "$alt";; esac | |
200 | case "$quiet" in | |
201 | '') echo >&2 "Getting alternate: $alt" ;; | |
202 | esac | |
203 | rsync $quiet -av --ignore-existing \ | |
8a1a120c | 204 | --exclude info "$alt" "$GIT_DIR/objects" || exit |
4447badc | 205 | done |
8a1a120c | 206 | rm -f "$GIT_DIR/TMP_ALT" |
4447badc | 207 | fi |
1cadb5a2 JH |
208 | ;; |
209 | http://*) | |
6c5c62f3 FP |
210 | if test -z "@@NO_CURL@@" |
211 | then | |
212 | clone_dumb_http "$repo" "$D" | |
213 | else | |
214 | echo >&2 "http transport not supported, rebuild Git with curl support" | |
215 | exit 1 | |
216 | fi | |
1cadb5a2 JH |
217 | ;; |
218 | *) | |
219 | cd "$D" && case "$upload_pack" in | |
220 | '') git-clone-pack $quiet "$repo" ;; | |
221 | *) git-clone-pack $quiet "$upload_pack" "$repo" ;; | |
0a8b4def JH |
222 | esac || { |
223 | echo >&2 "clone-pack from '$repo' failed." | |
224 | exit 1 | |
225 | } | |
1cadb5a2 | 226 | ;; |
6ec311da | 227 | esac |
7558ef89 LT |
228 | ;; |
229 | esac | |
1cadb5a2 | 230 | |
0e9ab02d | 231 | cd "$D" || exit |
036a72d8 | 232 | |
4fb66a62 | 233 | if test -f "$GIT_DIR/HEAD" && test -z "$bare" |
036a72d8 | 234 | then |
e125c1a7 JH |
235 | head_points_at=`git-symbolic-ref HEAD` |
236 | case "$head_points_at" in | |
237 | refs/heads/*) | |
238 | head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'` | |
8a1a120c JH |
239 | mkdir -p "$GIT_DIR/remotes" && |
240 | echo >"$GIT_DIR/remotes/origin" \ | |
e125c1a7 | 241 | "URL: $repo |
e6c310fd JS |
242 | Pull: $head_points_at:$origin" && |
243 | git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) && | |
8a1a120c | 244 | (cd "$GIT_DIR" && find "refs/heads" -type f -print) | |
95d117b6 JH |
245 | while read ref |
246 | do | |
8a1a120c | 247 | head=`expr "$ref" : 'refs/heads/\(.*\)'` && |
95d117b6 | 248 | test "$head_points_at" = "$head" || |
e6c310fd | 249 | test "$origin" = "$head" || |
95d117b6 | 250 | echo "Pull: ${head}:${head}" |
8a1a120c | 251 | done >>"$GIT_DIR/remotes/origin" |
e125c1a7 JH |
252 | esac |
253 | ||
036a72d8 JH |
254 | case "$no_checkout" in |
255 | '') | |
744633cb | 256 | git-read-tree -m -u -v HEAD HEAD |
036a72d8 JH |
257 | esac |
258 | fi | |
41ff7a10 CW |
259 | |
260 | trap - exit | |
261 |