]>
Commit | Line | Data |
---|---|---|
6eb7ed54 DB |
1 | #include "cache.h" |
2 | #include "commit.h" | |
6eb7ed54 | 3 | |
4250a5e5 DB |
4 | #include "pull.h" |
5 | ||
6eb7ed54 DB |
6 | #include <curl/curl.h> |
7 | #include <curl/easy.h> | |
8 | ||
4a30976e JS |
9 | #if LIBCURL_VERSION_NUM < 0x070704 |
10 | #define curl_global_cleanup() do { /* nothing */ } while(0) | |
11 | #endif | |
12 | #if LIBCURL_VERSION_NUM < 0x070800 | |
13 | #define curl_global_init(a) do { /* nothing */ } while(0) | |
14 | #endif | |
4a30976e | 15 | |
6eb7ed54 DB |
16 | static CURL *curl; |
17 | ||
18 | static char *base; | |
19 | ||
6eb7ed54 DB |
20 | static SHA_CTX c; |
21 | static z_stream stream; | |
22 | ||
23 | static int local; | |
24 | static int zret; | |
25 | ||
3dcb90f5 DT |
26 | static int curl_ssl_verify; |
27 | ||
fa3e0655 DB |
28 | struct buffer |
29 | { | |
30 | size_t posn; | |
31 | size_t size; | |
32 | void *buffer; | |
33 | }; | |
34 | ||
35 | static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb, | |
182005b9 DB |
36 | struct buffer *buffer) |
37 | { | |
fa3e0655 DB |
38 | size_t size = eltsize * nmemb; |
39 | if (size > buffer->size - buffer->posn) | |
40 | size = buffer->size - buffer->posn; | |
41 | memcpy(buffer->buffer + buffer->posn, ptr, size); | |
42 | buffer->posn += size; | |
43 | return size; | |
44 | } | |
45 | ||
182005b9 DB |
46 | static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, |
47 | void *data) | |
48 | { | |
bf0f910d | 49 | unsigned char expn[4096]; |
6eb7ed54 DB |
50 | size_t size = eltsize * nmemb; |
51 | int posn = 0; | |
52 | do { | |
53 | ssize_t retval = write(local, ptr + posn, size - posn); | |
54 | if (retval < 0) | |
55 | return posn; | |
56 | posn += retval; | |
57 | } while (posn < size); | |
58 | ||
59 | stream.avail_in = size; | |
60 | stream.next_in = ptr; | |
61 | do { | |
62 | stream.next_out = expn; | |
63 | stream.avail_out = sizeof(expn); | |
64 | zret = inflate(&stream, Z_SYNC_FLUSH); | |
65 | SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out); | |
66 | } while (stream.avail_in && zret == Z_OK); | |
67 | return size; | |
68 | } | |
69 | ||
1e8be59d DB |
70 | void prefetch(unsigned char *sha1) |
71 | { | |
72 | } | |
73 | ||
182005b9 DB |
74 | static int got_indices = 0; |
75 | ||
76 | static struct packed_git *packs = NULL; | |
77 | ||
78 | static int fetch_index(unsigned char *sha1) | |
79 | { | |
80 | char *filename; | |
81 | char *url; | |
82 | ||
83 | FILE *indexfile; | |
84 | ||
85 | if (has_pack_index(sha1)) | |
86 | return 0; | |
87 | ||
88 | if (get_verbosely) | |
89 | fprintf(stderr, "Getting index for pack %s\n", | |
90 | sha1_to_hex(sha1)); | |
91 | ||
92 | url = xmalloc(strlen(base) + 64); | |
93 | sprintf(url, "%s/objects/pack/pack-%s.idx", | |
94 | base, sha1_to_hex(sha1)); | |
95 | ||
96 | filename = sha1_pack_index_name(sha1); | |
97 | indexfile = fopen(filename, "w"); | |
98 | if (!indexfile) | |
99 | return error("Unable to open local file %s for pack index", | |
100 | filename); | |
101 | ||
102 | curl_easy_setopt(curl, CURLOPT_FILE, indexfile); | |
103 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite); | |
104 | curl_easy_setopt(curl, CURLOPT_URL, url); | |
105 | ||
106 | if (curl_easy_perform(curl)) { | |
107 | fclose(indexfile); | |
108 | return error("Unable to get pack index %s", url); | |
109 | } | |
110 | ||
111 | fclose(indexfile); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | static int setup_index(unsigned char *sha1) | |
116 | { | |
117 | struct packed_git *new_pack; | |
118 | if (has_pack_file(sha1)) | |
119 | return 0; // don't list this as something we can get | |
120 | ||
121 | if (fetch_index(sha1)) | |
122 | return -1; | |
123 | ||
124 | new_pack = parse_pack_index(sha1); | |
125 | new_pack->next = packs; | |
126 | packs = new_pack; | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static int fetch_indices(void) | |
131 | { | |
132 | unsigned char sha1[20]; | |
133 | char *url; | |
134 | struct buffer buffer; | |
135 | char *data; | |
136 | int i = 0; | |
137 | ||
138 | if (got_indices) | |
139 | return 0; | |
140 | ||
141 | data = xmalloc(4096); | |
142 | buffer.size = 4096; | |
143 | buffer.posn = 0; | |
144 | buffer.buffer = data; | |
145 | ||
146 | if (get_verbosely) | |
147 | fprintf(stderr, "Getting pack list\n"); | |
148 | ||
149 | url = xmalloc(strlen(base) + 21); | |
150 | sprintf(url, "%s/objects/info/packs", base); | |
151 | ||
152 | curl_easy_setopt(curl, CURLOPT_FILE, &buffer); | |
153 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
154 | curl_easy_setopt(curl, CURLOPT_URL, url); | |
155 | ||
156 | if (curl_easy_perform(curl)) { | |
157 | return error("Unable to get pack index %s", url); | |
158 | } | |
159 | ||
160 | do { | |
161 | switch (data[i]) { | |
162 | case 'P': | |
163 | i++; | |
164 | if (i + 52 < buffer.posn && | |
165 | !strncmp(data + i, " pack-", 6) && | |
166 | !strncmp(data + i + 46, ".pack\n", 6)) { | |
167 | get_sha1_hex(data + i + 6, sha1); | |
168 | setup_index(sha1); | |
169 | i += 51; | |
170 | break; | |
171 | } | |
172 | default: | |
173 | while (data[i] != '\n') | |
174 | i++; | |
175 | } | |
176 | i++; | |
177 | } while (i < buffer.posn); | |
178 | ||
179 | got_indices = 1; | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int fetch_pack(unsigned char *sha1) | |
184 | { | |
185 | char *url; | |
186 | struct packed_git *target; | |
187 | struct packed_git **lst; | |
188 | FILE *packfile; | |
189 | char *filename; | |
190 | ||
191 | if (fetch_indices()) | |
192 | return -1; | |
193 | target = find_sha1_pack(sha1, packs); | |
194 | if (!target) | |
195 | return error("Couldn't get %s: not separate or in any pack", | |
196 | sha1_to_hex(sha1)); | |
197 | ||
198 | if (get_verbosely) { | |
199 | fprintf(stderr, "Getting pack %s\n", | |
200 | sha1_to_hex(target->sha1)); | |
201 | fprintf(stderr, " which contains %s\n", | |
202 | sha1_to_hex(sha1)); | |
203 | } | |
204 | ||
205 | url = xmalloc(strlen(base) + 65); | |
206 | sprintf(url, "%s/objects/pack/pack-%s.pack", | |
207 | base, sha1_to_hex(target->sha1)); | |
208 | ||
209 | filename = sha1_pack_name(target->sha1); | |
210 | packfile = fopen(filename, "w"); | |
211 | if (!packfile) | |
212 | return error("Unable to open local file %s for pack", | |
213 | filename); | |
214 | ||
215 | curl_easy_setopt(curl, CURLOPT_FILE, packfile); | |
216 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite); | |
217 | curl_easy_setopt(curl, CURLOPT_URL, url); | |
218 | ||
219 | if (curl_easy_perform(curl)) { | |
220 | fclose(packfile); | |
221 | return error("Unable to get pack file %s", url); | |
222 | } | |
223 | ||
224 | fclose(packfile); | |
225 | ||
226 | lst = &packs; | |
227 | while (*lst != target) | |
228 | lst = &((*lst)->next); | |
229 | *lst = (*lst)->next; | |
230 | ||
231 | install_packed_git(target); | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
4250a5e5 | 236 | int fetch(unsigned char *sha1) |
6eb7ed54 DB |
237 | { |
238 | char *hex = sha1_to_hex(sha1); | |
239 | char *filename = sha1_file_name(sha1); | |
bf0f910d | 240 | unsigned char real_sha1[20]; |
6eb7ed54 DB |
241 | char *url; |
242 | char *posn; | |
243 | ||
6eb7ed54 DB |
244 | local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); |
245 | ||
246 | if (local < 0) | |
182005b9 | 247 | return error("Couldn't open local object %s\n", filename); |
6eb7ed54 DB |
248 | |
249 | memset(&stream, 0, sizeof(stream)); | |
250 | ||
251 | inflateInit(&stream); | |
252 | ||
253 | SHA1_Init(&c); | |
254 | ||
182005b9 | 255 | curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); |
6eb7ed54 DB |
256 | curl_easy_setopt(curl, CURLOPT_FILE, NULL); |
257 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); | |
258 | ||
812666c8 | 259 | url = xmalloc(strlen(base) + 50); |
6eb7ed54 DB |
260 | strcpy(url, base); |
261 | posn = url + strlen(base); | |
262 | strcpy(posn, "objects/"); | |
263 | posn += 8; | |
264 | memcpy(posn, hex, 2); | |
265 | posn += 2; | |
266 | *(posn++) = '/'; | |
267 | strcpy(posn, hex + 2); | |
268 | ||
269 | curl_easy_setopt(curl, CURLOPT_URL, url); | |
270 | ||
182005b9 DB |
271 | if (curl_easy_perform(curl)) { |
272 | unlink(filename); | |
273 | if (fetch_pack(sha1)) | |
274 | return error("Tried %s", url); | |
275 | return 0; | |
276 | } | |
6eb7ed54 DB |
277 | |
278 | close(local); | |
279 | inflateEnd(&stream); | |
280 | SHA1_Final(real_sha1, &c); | |
281 | if (zret != Z_STREAM_END) { | |
282 | unlink(filename); | |
283 | return error("File %s (%s) corrupt\n", hex, url); | |
284 | } | |
285 | if (memcmp(sha1, real_sha1, 20)) { | |
286 | unlink(filename); | |
287 | return error("File %s has bad hash\n", hex); | |
288 | } | |
289 | ||
e78d9772 | 290 | pull_say("got %s\n", hex); |
6eb7ed54 DB |
291 | return 0; |
292 | } | |
293 | ||
cd541a68 DB |
294 | int fetch_ref(char *ref, unsigned char *sha1) |
295 | { | |
fa3e0655 DB |
296 | char *url, *posn; |
297 | char hex[42]; | |
298 | struct buffer buffer; | |
299 | buffer.size = 41; | |
300 | buffer.posn = 0; | |
301 | buffer.buffer = hex; | |
302 | hex[41] = '\0'; | |
303 | ||
304 | curl_easy_setopt(curl, CURLOPT_FILE, &buffer); | |
305 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
306 | ||
307 | url = xmalloc(strlen(base) + 6 + strlen(ref)); | |
308 | strcpy(url, base); | |
309 | posn = url + strlen(base); | |
310 | strcpy(posn, "refs/"); | |
311 | posn += 5; | |
312 | strcpy(posn, ref); | |
313 | ||
314 | curl_easy_setopt(curl, CURLOPT_URL, url); | |
315 | ||
316 | if (curl_easy_perform(curl)) | |
317 | return error("Couldn't get %s for %s\n", url, ref); | |
318 | ||
319 | hex[40] = '\0'; | |
320 | get_sha1_hex(hex, sha1); | |
321 | return 0; | |
cd541a68 DB |
322 | } |
323 | ||
6eb7ed54 DB |
324 | int main(int argc, char **argv) |
325 | { | |
326 | char *commit_id; | |
327 | char *url; | |
328 | int arg = 1; | |
6eb7ed54 DB |
329 | |
330 | while (arg < argc && argv[arg][0] == '-') { | |
331 | if (argv[arg][1] == 't') { | |
4250a5e5 | 332 | get_tree = 1; |
6eb7ed54 | 333 | } else if (argv[arg][1] == 'c') { |
4250a5e5 | 334 | get_history = 1; |
6eb7ed54 | 335 | } else if (argv[arg][1] == 'a') { |
4250a5e5 DB |
336 | get_all = 1; |
337 | get_tree = 1; | |
338 | get_history = 1; | |
e78d9772 JH |
339 | } else if (argv[arg][1] == 'v') { |
340 | get_verbosely = 1; | |
fa3e0655 DB |
341 | } else if (argv[arg][1] == 'w') { |
342 | write_ref = argv[arg + 1]; | |
343 | arg++; | |
6eb7ed54 DB |
344 | } |
345 | arg++; | |
346 | } | |
347 | if (argc < arg + 2) { | |
fa3e0655 | 348 | usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url"); |
6eb7ed54 DB |
349 | return 1; |
350 | } | |
351 | commit_id = argv[arg]; | |
352 | url = argv[arg + 1]; | |
353 | ||
6eb7ed54 DB |
354 | curl_global_init(CURL_GLOBAL_ALL); |
355 | ||
356 | curl = curl_easy_init(); | |
357 | ||
3dcb90f5 DT |
358 | curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1; |
359 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify); | |
9f6cf65e | 360 | #if LIBCURL_VERSION_NUM >= 0x070907 |
3dcb90f5 | 361 | curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); |
9f6cf65e | 362 | #endif |
3dcb90f5 | 363 | |
6eb7ed54 DB |
364 | base = url; |
365 | ||
4250a5e5 | 366 | if (pull(commit_id)) |
6eb7ed54 DB |
367 | return 1; |
368 | ||
369 | curl_global_cleanup(); | |
370 | return 0; | |
371 | } |