]> git.ipfire.org Git - thirdparty/git.git/blame - http-fetch.c
Diff: -l<num> to limit rename/copy detection.
[thirdparty/git.git] / http-fetch.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
6eb7ed54 3
215a7ad1 4#include "fetch.h"
4250a5e5 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 16static CURL *curl;
1db69b57 17static struct curl_slist *no_pragma_header;
6eb7ed54 18
b3661567
DB
19static char *initial_base;
20
21struct alt_base
22{
23 char *base;
24 int got_indices;
25 struct packed_git *packs;
26 struct alt_base *next;
27};
28
29struct alt_base *alt = NULL;
6eb7ed54 30
6eb7ed54
DB
31static SHA_CTX c;
32static z_stream stream;
33
34static int local;
35static int zret;
36
3dcb90f5
DT
37static int curl_ssl_verify;
38
fa3e0655
DB
39struct buffer
40{
41 size_t posn;
42 size_t size;
43 void *buffer;
44};
45
46static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
182005b9
DB
47 struct buffer *buffer)
48{
fa3e0655
DB
49 size_t size = eltsize * nmemb;
50 if (size > buffer->size - buffer->posn)
51 size = buffer->size - buffer->posn;
52 memcpy(buffer->buffer + buffer->posn, ptr, size);
53 buffer->posn += size;
54 return size;
55}
56
182005b9
DB
57static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
58 void *data)
59{
bf0f910d 60 unsigned char expn[4096];
6eb7ed54
DB
61 size_t size = eltsize * nmemb;
62 int posn = 0;
63 do {
64 ssize_t retval = write(local, ptr + posn, size - posn);
65 if (retval < 0)
66 return posn;
67 posn += retval;
68 } while (posn < size);
69
70 stream.avail_in = size;
71 stream.next_in = ptr;
72 do {
73 stream.next_out = expn;
74 stream.avail_out = sizeof(expn);
75 zret = inflate(&stream, Z_SYNC_FLUSH);
76 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
77 } while (stream.avail_in && zret == Z_OK);
78 return size;
79}
80
1e8be59d
DB
81void prefetch(unsigned char *sha1)
82{
83}
84
b3661567 85static int got_alternates = 0;
182005b9 86
b3661567 87static int fetch_index(struct alt_base *repo, unsigned char *sha1)
182005b9
DB
88{
89 char *filename;
90 char *url;
91
92 FILE *indexfile;
93
94 if (has_pack_index(sha1))
95 return 0;
96
97 if (get_verbosely)
98 fprintf(stderr, "Getting index for pack %s\n",
99 sha1_to_hex(sha1));
100
b3661567 101 url = xmalloc(strlen(repo->base) + 64);
182005b9 102 sprintf(url, "%s/objects/pack/pack-%s.idx",
b3661567 103 repo->base, sha1_to_hex(sha1));
182005b9
DB
104
105 filename = sha1_pack_index_name(sha1);
106 indexfile = fopen(filename, "w");
107 if (!indexfile)
108 return error("Unable to open local file %s for pack index",
109 filename);
110
111 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
112 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
113 curl_easy_setopt(curl, CURLOPT_URL, url);
1db69b57 114 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
182005b9
DB
115
116 if (curl_easy_perform(curl)) {
117 fclose(indexfile);
118 return error("Unable to get pack index %s", url);
119 }
120
121 fclose(indexfile);
122 return 0;
123}
124
b3661567 125static int setup_index(struct alt_base *repo, unsigned char *sha1)
182005b9
DB
126{
127 struct packed_git *new_pack;
128 if (has_pack_file(sha1))
129 return 0; // don't list this as something we can get
130
b3661567 131 if (fetch_index(repo, sha1))
182005b9
DB
132 return -1;
133
134 new_pack = parse_pack_index(sha1);
b3661567
DB
135 new_pack->next = repo->packs;
136 repo->packs = new_pack;
182005b9
DB
137 return 0;
138}
139
b3661567
DB
140static int fetch_alternates(char *base)
141{
142 int ret = 0;
143 struct buffer buffer;
144 char *url;
145 char *data;
146 int i = 0;
1b0c1e67 147 int http_specific = 1;
b3661567
DB
148 if (got_alternates)
149 return 0;
150 data = xmalloc(4096);
1b0c1e67 151 buffer.size = 4095;
b3661567
DB
152 buffer.posn = 0;
153 buffer.buffer = data;
154
155 if (get_verbosely)
156 fprintf(stderr, "Getting alternates list\n");
157
158 url = xmalloc(strlen(base) + 31);
159 sprintf(url, "%s/objects/info/http-alternates", base);
160
161 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
162 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
163 curl_easy_setopt(curl, CURLOPT_URL, url);
164
165 if (curl_easy_perform(curl) || !buffer.posn) {
1b0c1e67
DB
166 http_specific = 0;
167
b3661567
DB
168 sprintf(url, "%s/objects/info/alternates", base);
169
170 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
171 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
172 curl_easy_setopt(curl, CURLOPT_URL, url);
173
174 if (curl_easy_perform(curl)) {
175 return 0;
176 }
177 }
178
1b0c1e67
DB
179 data[buffer.posn] = '\0';
180
b3661567
DB
181 while (i < buffer.posn) {
182 int posn = i;
183 while (posn < buffer.posn && data[posn] != '\n')
184 posn++;
185 if (data[posn] == '\n') {
1b0c1e67
DB
186 int okay = 0;
187 int serverlen = 0;
188 struct alt_base *newalt;
189 char *target = NULL;
b3661567 190 if (data[i] == '/') {
1b0c1e67
DB
191 serverlen = strchr(base + 8, '/') - base;
192 okay = 1;
193 } else if (!memcmp(data + i, "../", 3)) {
194 i += 3;
195 serverlen = strlen(base);
196 while (i + 2 < posn &&
197 !memcmp(data + i, "../", 3)) {
198 do {
199 serverlen--;
200 } while (serverlen &&
201 base[serverlen - 1] != '/');
202 i += 3;
203 }
204 // If the server got removed, give up.
205 okay = strchr(base, ':') - base + 3 <
206 serverlen;
207 } else if (http_specific) {
208 char *colon = strchr(data + i, ':');
209 char *slash = strchr(data + i, '/');
210 if (colon && slash && colon < data + posn &&
211 slash < data + posn && colon < slash) {
212 okay = 1;
213 }
214 }
215 // skip 'objects' at end
216 if (okay) {
217 target = xmalloc(serverlen + posn - i - 6);
b3661567
DB
218 strncpy(target, base, serverlen);
219 strncpy(target + serverlen, data + i,
220 posn - i - 7);
221 target[serverlen + posn - i - 7] = '\0';
222 if (get_verbosely)
223 fprintf(stderr,
224 "Also look at %s\n", target);
225 newalt = xmalloc(sizeof(*newalt));
226 newalt->next = alt;
227 newalt->base = target;
228 newalt->got_indices = 0;
229 newalt->packs = NULL;
230 alt = newalt;
231 ret++;
232 }
233 }
234 i = posn + 1;
235 }
236 got_alternates = 1;
237
238 return ret;
239}
240
241static int fetch_indices(struct alt_base *repo)
182005b9
DB
242{
243 unsigned char sha1[20];
244 char *url;
245 struct buffer buffer;
246 char *data;
247 int i = 0;
248
b3661567 249 if (repo->got_indices)
182005b9
DB
250 return 0;
251
252 data = xmalloc(4096);
253 buffer.size = 4096;
254 buffer.posn = 0;
255 buffer.buffer = data;
256
257 if (get_verbosely)
258 fprintf(stderr, "Getting pack list\n");
259
b3661567
DB
260 url = xmalloc(strlen(repo->base) + 21);
261 sprintf(url, "%s/objects/info/packs", repo->base);
182005b9
DB
262
263 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
264 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
265 curl_easy_setopt(curl, CURLOPT_URL, url);
1db69b57 266 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
182005b9
DB
267
268 if (curl_easy_perform(curl)) {
1b0c1e67 269 return -1;
182005b9
DB
270 }
271
b3661567 272 while (i < buffer.posn) {
182005b9
DB
273 switch (data[i]) {
274 case 'P':
275 i++;
276 if (i + 52 < buffer.posn &&
277 !strncmp(data + i, " pack-", 6) &&
278 !strncmp(data + i + 46, ".pack\n", 6)) {
279 get_sha1_hex(data + i + 6, sha1);
b3661567 280 setup_index(repo, sha1);
182005b9
DB
281 i += 51;
282 break;
283 }
284 default:
285 while (data[i] != '\n')
286 i++;
287 }
288 i++;
b3661567 289 }
182005b9 290
b3661567 291 repo->got_indices = 1;
182005b9
DB
292 return 0;
293}
294
b3661567 295static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
182005b9
DB
296{
297 char *url;
298 struct packed_git *target;
299 struct packed_git **lst;
300 FILE *packfile;
301 char *filename;
302
b3661567 303 if (fetch_indices(repo))
182005b9 304 return -1;
b3661567 305 target = find_sha1_pack(sha1, repo->packs);
182005b9 306 if (!target)
b3661567 307 return -1;
182005b9
DB
308
309 if (get_verbosely) {
310 fprintf(stderr, "Getting pack %s\n",
311 sha1_to_hex(target->sha1));
312 fprintf(stderr, " which contains %s\n",
313 sha1_to_hex(sha1));
314 }
315
b3661567 316 url = xmalloc(strlen(repo->base) + 65);
182005b9 317 sprintf(url, "%s/objects/pack/pack-%s.pack",
b3661567 318 repo->base, sha1_to_hex(target->sha1));
182005b9
DB
319
320 filename = sha1_pack_name(target->sha1);
321 packfile = fopen(filename, "w");
322 if (!packfile)
323 return error("Unable to open local file %s for pack",
324 filename);
325
326 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
327 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
328 curl_easy_setopt(curl, CURLOPT_URL, url);
1db69b57 329 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
182005b9
DB
330
331 if (curl_easy_perform(curl)) {
332 fclose(packfile);
333 return error("Unable to get pack file %s", url);
334 }
335
336 fclose(packfile);
337
b3661567 338 lst = &repo->packs;
182005b9
DB
339 while (*lst != target)
340 lst = &((*lst)->next);
341 *lst = (*lst)->next;
342
343 install_packed_git(target);
344
345 return 0;
346}
347
b3661567 348int fetch_object(struct alt_base *repo, unsigned char *sha1)
6eb7ed54
DB
349{
350 char *hex = sha1_to_hex(sha1);
351 char *filename = sha1_file_name(sha1);
bf0f910d 352 unsigned char real_sha1[20];
09d92083
JH
353 char tmpfile[PATH_MAX];
354 int ret;
6eb7ed54
DB
355 char *url;
356 char *posn;
357
09d92083
JH
358 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX",
359 get_object_directory());
6eb7ed54 360
09d92083 361 local = mkstemp(tmpfile);
6eb7ed54 362 if (local < 0)
09d92083
JH
363 return error("Couldn't create temporary file %s for %s: %s\n",
364 tmpfile, filename, strerror(errno));
6eb7ed54
DB
365
366 memset(&stream, 0, sizeof(stream));
367
368 inflateInit(&stream);
369
370 SHA1_Init(&c);
371
182005b9 372 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
6eb7ed54
DB
373 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
374 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1db69b57 375 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
6eb7ed54 376
b3661567
DB
377 url = xmalloc(strlen(repo->base) + 50);
378 strcpy(url, repo->base);
379 posn = url + strlen(repo->base);
6eb7ed54
DB
380 strcpy(posn, "objects/");
381 posn += 8;
382 memcpy(posn, hex, 2);
383 posn += 2;
384 *(posn++) = '/';
385 strcpy(posn, hex + 2);
386
387 curl_easy_setopt(curl, CURLOPT_URL, url);
388
182005b9
DB
389 if (curl_easy_perform(curl)) {
390 unlink(filename);
b3661567 391 return -1;
182005b9 392 }
6eb7ed54 393
09d92083 394 fchmod(local, 0444);
6eb7ed54
DB
395 close(local);
396 inflateEnd(&stream);
397 SHA1_Final(real_sha1, &c);
398 if (zret != Z_STREAM_END) {
09d92083 399 unlink(tmpfile);
6eb7ed54
DB
400 return error("File %s (%s) corrupt\n", hex, url);
401 }
402 if (memcmp(sha1, real_sha1, 20)) {
09d92083 403 unlink(tmpfile);
6eb7ed54
DB
404 return error("File %s has bad hash\n", hex);
405 }
09d92083
JH
406 ret = link(tmpfile, filename);
407 if (ret < 0) {
408 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
409 ret = errno;
410 if (ret == EXDEV && !rename(tmpfile, filename))
411 goto out;
412 }
413 unlink(tmpfile);
414 if (ret) {
415 if (ret != EEXIST)
416 return error("unable to write sha1 filename %s: %s",
417 filename, strerror(ret));
418 }
419 out:
e78d9772 420 pull_say("got %s\n", hex);
6eb7ed54
DB
421 return 0;
422}
423
b3661567
DB
424int fetch(unsigned char *sha1)
425{
426 struct alt_base *altbase = alt;
427 while (altbase) {
428 if (!fetch_object(altbase, sha1))
429 return 0;
430 if (!fetch_pack(altbase, sha1))
431 return 0;
432 if (fetch_alternates(altbase->base) > 0) {
433 altbase = alt;
434 continue;
435 }
436 altbase = altbase->next;
437 }
438 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
439 initial_base);
440}
441
cd541a68
DB
442int fetch_ref(char *ref, unsigned char *sha1)
443{
fa3e0655
DB
444 char *url, *posn;
445 char hex[42];
446 struct buffer buffer;
b3661567 447 char *base = initial_base;
fa3e0655
DB
448 buffer.size = 41;
449 buffer.posn = 0;
450 buffer.buffer = hex;
451 hex[41] = '\0';
452
453 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
454 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1db69b57 455 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
fa3e0655
DB
456
457 url = xmalloc(strlen(base) + 6 + strlen(ref));
458 strcpy(url, base);
459 posn = url + strlen(base);
460 strcpy(posn, "refs/");
461 posn += 5;
462 strcpy(posn, ref);
463
464 curl_easy_setopt(curl, CURLOPT_URL, url);
465
466 if (curl_easy_perform(curl))
467 return error("Couldn't get %s for %s\n", url, ref);
468
469 hex[40] = '\0';
470 get_sha1_hex(hex, sha1);
471 return 0;
cd541a68
DB
472}
473
6eb7ed54
DB
474int main(int argc, char **argv)
475{
476 char *commit_id;
477 char *url;
478 int arg = 1;
6eb7ed54
DB
479
480 while (arg < argc && argv[arg][0] == '-') {
481 if (argv[arg][1] == 't') {
4250a5e5 482 get_tree = 1;
6eb7ed54 483 } else if (argv[arg][1] == 'c') {
4250a5e5 484 get_history = 1;
6eb7ed54 485 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
486 get_all = 1;
487 get_tree = 1;
488 get_history = 1;
e78d9772
JH
489 } else if (argv[arg][1] == 'v') {
490 get_verbosely = 1;
fa3e0655
DB
491 } else if (argv[arg][1] == 'w') {
492 write_ref = argv[arg + 1];
493 arg++;
6eb7ed54
DB
494 }
495 arg++;
496 }
497 if (argc < arg + 2) {
215a7ad1 498 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
6eb7ed54
DB
499 return 1;
500 }
501 commit_id = argv[arg];
502 url = argv[arg + 1];
503
6eb7ed54
DB
504 curl_global_init(CURL_GLOBAL_ALL);
505
506 curl = curl_easy_init();
1db69b57 507 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
6eb7ed54 508
a9ab586a 509 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
3dcb90f5 510 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
9f6cf65e 511#if LIBCURL_VERSION_NUM >= 0x070907
3dcb90f5 512 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
9f6cf65e 513#endif
3dcb90f5 514
b3661567
DB
515 alt = xmalloc(sizeof(*alt));
516 alt->base = url;
517 alt->got_indices = 0;
518 alt->packs = NULL;
519 alt->next = NULL;
520 initial_base = url;
6eb7ed54 521
4250a5e5 522 if (pull(commit_id))
6eb7ed54
DB
523 return 1;
524
1db69b57 525 curl_slist_free_all(no_pragma_header);
6eb7ed54
DB
526 curl_global_cleanup();
527 return 0;
528}