]> git.ipfire.org Git - thirdparty/git.git/blame - http-walker.c
name-rev: add support to exclude refs by pattern match
[thirdparty/git.git] / http-walker.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
30ae764b 3#include "walker.h"
29508e1e 4#include "http.h"
94e99012 5#include "list.h"
7baa3e86 6
9cba13ca 7struct alt_base {
2afea3bc 8 char *base;
b3661567
DB
9 int got_indices;
10 struct packed_git *packs;
11 struct alt_base *next;
12};
13
e388ab74 14enum object_request_state {
1d389ab6
NH
15 WAITING,
16 ABORTED,
17 ACTIVE,
4b05548f 18 COMPLETE
1d389ab6 19};
6eb7ed54 20
9cba13ca 21struct object_request {
30ae764b 22 struct walker *walker;
1d389ab6
NH
23 unsigned char sha1[20];
24 struct alt_base *repo;
e388ab74 25 enum object_request_state state;
5424bc55 26 struct http_object_request *req;
94e99012 27 struct list_head node;
1d389ab6
NH
28};
29
e388ab74 30struct alternates_request {
30ae764b 31 struct walker *walker;
8e29f6a0 32 const char *base;
54ba4c5f 33 struct strbuf *url;
028c2976 34 struct strbuf *buffer;
acc075a8
NH
35 struct active_request_slot *slot;
36 int http_specific;
37};
38
30ae764b
DB
39struct walker_data {
40 const char *url;
41 int got_alternates;
42 struct alt_base *alt;
30ae764b
DB
43};
44
94e99012 45static LIST_HEAD(object_queue_head);
bc8f2652 46
30ae764b 47static void fetch_alternates(struct walker *walker, const char *base);
1d389ab6 48
29508e1e 49static void process_object_response(void *callback_data);
1d389ab6 50
30ae764b
DB
51static void start_object_request(struct walker *walker,
52 struct object_request *obj_req)
1d389ab6 53{
1d389ab6 54 struct active_request_slot *slot;
5424bc55 55 struct http_object_request *req;
1d389ab6 56
5424bc55
TRC
57 req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
58 if (req == NULL) {
e388ab74 59 obj_req->state = ABORTED;
1d389ab6
NH
60 return;
61 }
5424bc55 62 obj_req->req = req;
1d389ab6 63
5424bc55 64 slot = req->slot;
29508e1e 65 slot->callback_func = process_object_response;
e388ab74 66 slot->callback_data = obj_req;
1d389ab6 67
a7a8d378 68 /* Try to get the request started, abort the request on error */
e388ab74 69 obj_req->state = ACTIVE;
1d389ab6 70 if (!start_active_slot(slot)) {
e388ab74 71 obj_req->state = ABORTED;
5424bc55 72 release_http_object_request(req);
e388ab74 73 return;
1d389ab6 74 }
1d389ab6
NH
75}
76
e388ab74 77static void finish_object_request(struct object_request *obj_req)
1d389ab6 78{
5424bc55 79 if (finish_http_object_request(obj_req->req))
1d389ab6 80 return;
1d389ab6 81
5424bc55 82 if (obj_req->req->rename == 0)
30ae764b 83 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
1d389ab6
NH
84}
85
29508e1e
NH
86static void process_object_response(void *callback_data)
87{
e388ab74
NH
88 struct object_request *obj_req =
89 (struct object_request *)callback_data;
30ae764b
DB
90 struct walker *walker = obj_req->walker;
91 struct walker_data *data = walker->data;
92 struct alt_base *alt = data->alt;
29508e1e 93
5424bc55 94 process_http_object_request(obj_req->req);
e388ab74 95 obj_req->state = COMPLETE;
29508e1e
NH
96
97 /* Use alternates if necessary */
5424bc55 98 if (missing_target(obj_req->req)) {
30ae764b 99 fetch_alternates(walker, alt->base);
e388ab74
NH
100 if (obj_req->repo->next != NULL) {
101 obj_req->repo =
102 obj_req->repo->next;
5424bc55 103 release_http_object_request(obj_req->req);
30ae764b 104 start_object_request(walker, obj_req);
29508e1e
NH
105 return;
106 }
107 }
108
e388ab74 109 finish_object_request(obj_req);
29508e1e
NH
110}
111
e388ab74 112static void release_object_request(struct object_request *obj_req)
1d389ab6 113{
5424bc55
TRC
114 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
115 error("fd leakage in release: %d", obj_req->req->localfile);
1d389ab6 116
94e99012 117 list_del(&obj_req->node);
e388ab74 118 free(obj_req);
1d389ab6
NH
119}
120
a7a8d378 121#ifdef USE_CURL_MULTI
30ae764b 122static int fill_active_slot(struct walker *walker)
1d389ab6 123{
45c17412 124 struct object_request *obj_req;
94e99012 125 struct list_head *pos, *tmp, *head = &object_queue_head;
1d389ab6 126
94e99012
EW
127 list_for_each_safe(pos, tmp, head) {
128 obj_req = list_entry(pos, struct object_request, node);
e388ab74
NH
129 if (obj_req->state == WAITING) {
130 if (has_sha1_file(obj_req->sha1))
09db444f 131 obj_req->state = COMPLETE;
45c17412 132 else {
30ae764b 133 start_object_request(walker, obj_req);
45c17412
DB
134 return 1;
135 }
f1a906a3 136 }
8fcf7f9a 137 }
45c17412 138 return 0;
1d389ab6 139}
a7a8d378 140#endif
1d389ab6 141
30ae764b 142static void prefetch(struct walker *walker, unsigned char *sha1)
1d389ab6 143{
e388ab74 144 struct object_request *newreq;
30ae764b 145 struct walker_data *data = walker->data;
1d389ab6
NH
146
147 newreq = xmalloc(sizeof(*newreq));
30ae764b 148 newreq->walker = walker;
e702496e 149 hashcpy(newreq->sha1, sha1);
30ae764b 150 newreq->repo = data->alt;
1d389ab6 151 newreq->state = WAITING;
5424bc55 152 newreq->req = NULL;
1d389ab6 153
e9176745 154 http_is_verbose = walker->get_verbosely;
94e99012 155 list_add_tail(&newreq->node, &object_queue_head);
29508e1e 156
a7a8d378 157#ifdef USE_CURL_MULTI
29508e1e
NH
158 fill_active_slots();
159 step_active_slots();
a7a8d378 160#endif
1d389ab6
NH
161}
162
e388ab74 163static void process_alternates_response(void *callback_data)
b3661567 164{
e388ab74
NH
165 struct alternates_request *alt_req =
166 (struct alternates_request *)callback_data;
30ae764b
DB
167 struct walker *walker = alt_req->walker;
168 struct walker_data *cdata = walker->data;
acc075a8 169 struct active_request_slot *slot = alt_req->slot;
30ae764b 170 struct alt_base *tail = cdata->alt;
8e29f6a0 171 const char *base = alt_req->base;
a04ff3ec 172 const char null_byte = '\0';
acc075a8
NH
173 char *data;
174 int i = 0;
1d389ab6 175
acc075a8
NH
176 if (alt_req->http_specific) {
177 if (slot->curl_result != CURLE_OK ||
028c2976 178 !alt_req->buffer->len) {
acc075a8
NH
179
180 /* Try reusing the slot to get non-http alternates */
181 alt_req->http_specific = 0;
54ba4c5f
JK
182 strbuf_reset(alt_req->url);
183 strbuf_addf(alt_req->url, "%s/objects/info/alternates",
184 base);
acc075a8 185 curl_easy_setopt(slot->curl, CURLOPT_URL,
54ba4c5f 186 alt_req->url->buf);
acc075a8
NH
187 active_requests++;
188 slot->in_use = 1;
c9826473
NH
189 if (slot->finished != NULL)
190 (*slot->finished) = 0;
a3f583cb 191 if (!start_active_slot(slot)) {
30ae764b 192 cdata->got_alternates = -1;
29508e1e 193 slot->in_use = 0;
c9826473
NH
194 if (slot->finished != NULL)
195 (*slot->finished) = 1;
1d389ab6 196 }
a3f583cb 197 return;
b3661567 198 }
acc075a8 199 } else if (slot->curl_result != CURLE_OK) {
be4a015b 200 if (!missing_target(slot)) {
30ae764b 201 cdata->got_alternates = -1;
acc075a8
NH
202 return;
203 }
b3661567
DB
204 }
205
a04ff3ec 206 fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
028c2976
MH
207 alt_req->buffer->len--;
208 data = alt_req->buffer->buf;
1b0c1e67 209
028c2976 210 while (i < alt_req->buffer->len) {
b3661567 211 int posn = i;
028c2976 212 while (posn < alt_req->buffer->len && data[posn] != '\n')
b3661567
DB
213 posn++;
214 if (data[posn] == '\n') {
1b0c1e67
DB
215 int okay = 0;
216 int serverlen = 0;
217 struct alt_base *newalt;
b3661567 218 if (data[i] == '/') {
4c42aa1a
TRC
219 /*
220 * This counts
5df1e0d0
JH
221 * http://git.host/pub/scm/linux.git/
222 * -----------here^
223 * so memcpy(dst, base, serverlen) will
224 * copy up to "...git.host".
225 */
226 const char *colon_ss = strstr(base,"://");
227 if (colon_ss) {
228 serverlen = (strchr(colon_ss + 3, '/')
229 - base);
230 okay = 1;
231 }
1b0c1e67 232 } else if (!memcmp(data + i, "../", 3)) {
4c42aa1a
TRC
233 /*
234 * Relative URL; chop the corresponding
5df1e0d0
JH
235 * number of subpath from base (and ../
236 * from data), and concatenate the result.
237 *
238 * The code first drops ../ from data, and
239 * then drops one ../ from data and one path
240 * from base. IOW, one extra ../ is dropped
241 * from data than path is dropped from base.
242 *
243 * This is not wrong. The alternate in
244 * http://git.host/pub/scm/linux.git/
245 * to borrow from
246 * http://git.host/pub/scm/linus.git/
247 * is ../../linus.git/objects/. You need
248 * two ../../ to borrow from your direct
249 * neighbour.
250 */
1b0c1e67
DB
251 i += 3;
252 serverlen = strlen(base);
8fcf7f9a 253 while (i + 2 < posn &&
1b0c1e67
DB
254 !memcmp(data + i, "../", 3)) {
255 do {
256 serverlen--;
257 } while (serverlen &&
258 base[serverlen - 1] != '/');
259 i += 3;
260 }
a9486b02 261 /* If the server got removed, give up. */
8fcf7f9a 262 okay = strchr(base, ':') - base + 3 <
4c42aa1a 263 serverlen;
acc075a8 264 } else if (alt_req->http_specific) {
1b0c1e67
DB
265 char *colon = strchr(data + i, ':');
266 char *slash = strchr(data + i, '/');
267 if (colon && slash && colon < data + posn &&
268 slash < data + posn && colon < slash) {
269 okay = 1;
270 }
271 }
5df1e0d0 272 /* skip "objects\n" at end */
1b0c1e67 273 if (okay) {
59b8263a
RS
274 struct strbuf target = STRBUF_INIT;
275 strbuf_add(&target, base, serverlen);
276 strbuf_add(&target, data + i, posn - i - 7);
30ae764b 277 if (walker->get_verbosely)
59b8263a
RS
278 fprintf(stderr, "Also look at %s\n",
279 target.buf);
b3661567 280 newalt = xmalloc(sizeof(*newalt));
1d389ab6 281 newalt->next = NULL;
59b8263a 282 newalt->base = strbuf_detach(&target, NULL);
b3661567
DB
283 newalt->got_indices = 0;
284 newalt->packs = NULL;
8d9fbe57 285
1d389ab6
NH
286 while (tail->next != NULL)
287 tail = tail->next;
288 tail->next = newalt;
b3661567
DB
289 }
290 }
291 i = posn + 1;
292 }
bc8f2652 293
30ae764b 294 cdata->got_alternates = 1;
acc075a8
NH
295}
296
30ae764b 297static void fetch_alternates(struct walker *walker, const char *base)
acc075a8 298{
028c2976 299 struct strbuf buffer = STRBUF_INIT;
54ba4c5f 300 struct strbuf url = STRBUF_INIT;
acc075a8 301 struct active_request_slot *slot;
cb754fdf 302 struct alternates_request alt_req;
30ae764b 303 struct walker_data *cdata = walker->data;
acc075a8 304
4c42aa1a
TRC
305 /*
306 * If another request has already started fetching alternates,
307 * wait for them to arrive and return to processing this request's
308 * curl message
309 */
29508e1e 310#ifdef USE_CURL_MULTI
30ae764b 311 while (cdata->got_alternates == 0) {
29508e1e 312 step_active_slots();
acc075a8 313 }
29508e1e 314#endif
acc075a8
NH
315
316 /* Nothing to do if they've already been fetched */
30ae764b 317 if (cdata->got_alternates == 1)
acc075a8
NH
318 return;
319
320 /* Start the fetch */
30ae764b 321 cdata->got_alternates = 0;
acc075a8 322
30ae764b 323 if (walker->get_verbosely)
acc075a8 324 fprintf(stderr, "Getting alternates list for %s\n", base);
8fcf7f9a 325
54ba4c5f 326 strbuf_addf(&url, "%s/objects/info/http-alternates", base);
acc075a8 327
4c42aa1a
TRC
328 /*
329 * Use a callback to process the result, since another request
330 * may fail and need to have alternates loaded before continuing
331 */
acc075a8 332 slot = get_active_slot();
e388ab74 333 slot->callback_func = process_alternates_response;
30ae764b 334 alt_req.walker = walker;
acc075a8
NH
335 slot->callback_data = &alt_req;
336
337 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
29508e1e 338 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
54ba4c5f 339 curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
acc075a8
NH
340
341 alt_req.base = base;
54ba4c5f 342 alt_req.url = &url;
acc075a8
NH
343 alt_req.buffer = &buffer;
344 alt_req.http_specific = 1;
345 alt_req.slot = slot;
346
347 if (start_active_slot(slot))
348 run_active_slot(slot);
349 else
30ae764b 350 cdata->got_alternates = -1;
acc075a8 351
028c2976 352 strbuf_release(&buffer);
54ba4c5f 353 strbuf_release(&url);
b3661567
DB
354}
355
30ae764b 356static int fetch_indices(struct walker *walker, struct alt_base *repo)
182005b9 357{
b8caac2b 358 int ret;
1d389ab6 359
b3661567 360 if (repo->got_indices)
182005b9
DB
361 return 0;
362
30ae764b 363 if (walker->get_verbosely)
6fd72e39 364 fprintf(stderr, "Getting pack list for %s\n", repo->base);
8fcf7f9a 365
b8caac2b
TRC
366 switch (http_get_info_packs(repo->base, &repo->packs)) {
367 case HTTP_OK:
368 case HTTP_MISSING_TARGET:
369 repo->got_indices = 1;
370 ret = 0;
371 break;
372 default:
5e3a7691 373 repo->got_indices = 0;
b8caac2b 374 ret = -1;
b3661567 375 }
182005b9 376
3a462bc9 377 return ret;
182005b9
DB
378}
379
07c19e72 380static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b9 381{
182005b9 382 struct packed_git *target;
49a0f240 383 int ret;
cb754fdf 384 struct slot_results results;
2264dfa5 385 struct http_pack_request *preq;
182005b9 386
30ae764b 387 if (fetch_indices(walker, repo))
182005b9 388 return -1;
b3661567 389 target = find_sha1_pack(sha1, repo->packs);
182005b9 390 if (!target)
b3661567 391 return -1;
182005b9 392
30ae764b 393 if (walker->get_verbosely) {
182005b9
DB
394 fprintf(stderr, "Getting pack %s\n",
395 sha1_to_hex(target->sha1));
396 fprintf(stderr, " which contains %s\n",
397 sha1_to_hex(sha1));
398 }
399
2264dfa5
TRC
400 preq = new_http_pack_request(target, repo->base);
401 if (preq == NULL)
402 goto abort;
403 preq->lst = &repo->packs;
404 preq->slot->results = &results;
182005b9 405
2264dfa5
TRC
406 if (start_active_slot(preq->slot)) {
407 run_active_slot(preq->slot);
c8568e13 408 if (results.curl_result != CURLE_OK) {
2264dfa5
TRC
409 error("Unable to get pack file %s\n%s", preq->url,
410 curl_errorstr);
411 goto abort;
1d389ab6
NH
412 }
413 } else {
2264dfa5
TRC
414 error("Unable to start request");
415 goto abort;
182005b9
DB
416 }
417
2264dfa5
TRC
418 ret = finish_http_pack_request(preq);
419 release_http_pack_request(preq);
49a0f240 420 if (ret)
b721e01f 421 return ret;
49a0f240 422
182005b9 423 return 0;
2264dfa5
TRC
424
425abort:
426 return -1;
182005b9
DB
427}
428
53f31389
MW
429static void abort_object_request(struct object_request *obj_req)
430{
53f31389
MW
431 release_object_request(obj_req);
432}
433
43b8bba6 434static int fetch_object(struct walker *walker, unsigned char *sha1)
6eb7ed54
DB
435{
436 char *hex = sha1_to_hex(sha1);
29508e1e 437 int ret = 0;
94e99012 438 struct object_request *obj_req = NULL;
5424bc55 439 struct http_object_request *req;
94e99012 440 struct list_head *pos, *head = &object_queue_head;
1d389ab6 441
94e99012
EW
442 list_for_each(pos, head) {
443 obj_req = list_entry(pos, struct object_request, node);
444 if (!hashcmp(obj_req->sha1, sha1))
445 break;
446 }
e388ab74 447 if (obj_req == NULL)
1d389ab6
NH
448 return error("Couldn't find request for %s in the queue", hex);
449
e388ab74 450 if (has_sha1_file(obj_req->sha1)) {
5424bc55
TRC
451 if (obj_req->req != NULL)
452 abort_http_object_request(obj_req->req);
53f31389 453 abort_object_request(obj_req);
11f0dafe
NH
454 return 0;
455 }
456
a7a8d378 457#ifdef USE_CURL_MULTI
4c42aa1a 458 while (obj_req->state == WAITING)
29508e1e 459 step_active_slots();
a7a8d378 460#else
30ae764b 461 start_object_request(walker, obj_req);
a7a8d378 462#endif
6eb7ed54 463
5424bc55
TRC
464 /*
465 * obj_req->req might change when fetching alternates in the callback
466 * process_object_response; therefore, the "shortcut" variable, req,
467 * is used only after we're done with slots.
468 */
4c42aa1a 469 while (obj_req->state == ACTIVE)
5424bc55
TRC
470 run_active_slot(obj_req->req->slot);
471
472 req = obj_req->req;
4c42aa1a 473
5424bc55
TRC
474 if (req->localfile != -1) {
475 close(req->localfile);
476 req->localfile = -1;
313c4714 477 }
6eb7ed54 478
17966c0a
EW
479 /*
480 * we turned off CURLOPT_FAILONERROR to avoid losing a
481 * persistent connection and got CURLE_OK.
482 */
483 if (req->http_code == 404 && req->curl_result == CURLE_OK &&
484 (starts_with(req->url, "http://") ||
485 starts_with(req->url, "https://")))
486 req->curl_result = CURLE_HTTP_RETURNED_ERROR;
487
e388ab74 488 if (obj_req->state == ABORTED) {
29508e1e 489 ret = error("Request for %s aborted", hex);
5424bc55
TRC
490 } else if (req->curl_result != CURLE_OK &&
491 req->http_code != 416) {
492 if (missing_target(req))
e2029eb9
PB
493 ret = -1; /* Be silent, it is probably in a pack. */
494 else
495 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
5424bc55
TRC
496 req->errorstr, req->curl_result,
497 req->http_code, hex);
498 } else if (req->zret != Z_STREAM_END) {
30ae764b 499 walker->corrupt_object_found++;
5424bc55
TRC
500 ret = error("File %s (%s) corrupt", hex, req->url);
501 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
bd2afde8 502 ret = error("File %s has bad hash", hex);
5424bc55 503 } else if (req->rename < 0) {
7b934ec0 504 ret = error("unable to write sha1 filename %s",
0da8b2e7 505 sha1_file_name(req->sha1));
6eb7ed54 506 }
49a0f240 507
5424bc55 508 release_http_object_request(req);
e388ab74 509 release_object_request(obj_req);
29508e1e 510 return ret;
6eb7ed54
DB
511}
512
30ae764b 513static int fetch(struct walker *walker, unsigned char *sha1)
b3661567 514{
30ae764b
DB
515 struct walker_data *data = walker->data;
516 struct alt_base *altbase = data->alt;
1d389ab6 517
43b8bba6 518 if (!fetch_object(walker, sha1))
1d389ab6 519 return 0;
b3661567 520 while (altbase) {
07c19e72 521 if (!http_fetch_pack(walker, altbase, sha1))
b3661567 522 return 0;
30ae764b 523 fetch_alternates(walker, data->alt->base);
b3661567
DB
524 altbase = altbase->next;
525 }
bd2afde8 526 return error("Unable to find %s under %s", sha1_to_hex(sha1),
30ae764b 527 data->alt->base);
b3661567
DB
528}
529
c13b2633 530static int fetch_ref(struct walker *walker, struct ref *ref)
cd541a68 531{
30ae764b 532 struct walker_data *data = walker->data;
c13b2633 533 return http_fetch_ref(data->alt->base, ref);
cd541a68
DB
534}
535
30ae764b
DB
536static void cleanup(struct walker *walker)
537{
09ae9aca
TRC
538 struct walker_data *data = walker->data;
539 struct alt_base *alt, *alt_next;
540
541 if (data) {
542 alt = data->alt;
543 while (alt) {
544 alt_next = alt->next;
545
546 free(alt->base);
547 free(alt);
548
549 alt = alt_next;
550 }
551 free(data);
552 walker->data = NULL;
553 }
30ae764b
DB
554}
555
888692b7 556struct walker *get_http_walker(const char *url)
6eb7ed54 557{
9c880b3e 558 char *s;
30ae764b
DB
559 struct walker_data *data = xmalloc(sizeof(struct walker_data));
560 struct walker *walker = xmalloc(sizeof(struct walker));
6eb7ed54 561
30ae764b 562 data->alt = xmalloc(sizeof(*data->alt));
95244ae3 563 data->alt->base = xstrdup(url);
30ae764b 564 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
9c880b3e 565 *s = 0;
6eb7ed54 566
30ae764b
DB
567 data->alt->got_indices = 0;
568 data->alt->packs = NULL;
569 data->alt->next = NULL;
570 data->got_alternates = -1;
fc57b6aa 571
30ae764b
DB
572 walker->corrupt_object_found = 0;
573 walker->fetch = fetch;
574 walker->fetch_ref = fetch_ref;
575 walker->prefetch = prefetch;
576 walker->cleanup = cleanup;
577 walker->data = data;
6eb7ed54 578
30ae764b
DB
579#ifdef USE_CURL_MULTI
580 add_fill_function(walker, (int (*)(void *)) fill_active_slot);
581#endif
8e29f6a0 582
30ae764b 583 return walker;
6eb7ed54 584}