]>
Commit | Line | Data |
---|---|---|
5e3f94df | 1 | #include "git-compat-util.h" |
53a50892 DS |
2 | #include "bundle-uri.h" |
3 | #include "bundle.h" | |
d5fff46f | 4 | #include "copy.h" |
32a8f510 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
53a50892 DS |
7 | #include "object-store.h" |
8 | #include "refs.h" | |
9 | #include "run-command.h" | |
0634f717 DS |
10 | #include "hashmap.h" |
11 | #include "pkt-line.h" | |
bff03c47 | 12 | #include "config.h" |
ebc39479 | 13 | #include "remote.h" |
0634f717 | 14 | |
c93c3d2f DS |
15 | static struct { |
16 | enum bundle_list_heuristic heuristic; | |
17 | const char *name; | |
18 | } heuristics[BUNDLE_HEURISTIC__COUNT] = { | |
19 | { BUNDLE_HEURISTIC_NONE, ""}, | |
20 | { BUNDLE_HEURISTIC_CREATIONTOKEN, "creationToken" }, | |
21 | }; | |
22 | ||
0634f717 DS |
23 | static int compare_bundles(const void *hashmap_cmp_fn_data, |
24 | const struct hashmap_entry *he1, | |
25 | const struct hashmap_entry *he2, | |
26 | const void *id) | |
27 | { | |
28 | const struct remote_bundle_info *e1 = | |
29 | container_of(he1, const struct remote_bundle_info, ent); | |
30 | const struct remote_bundle_info *e2 = | |
31 | container_of(he2, const struct remote_bundle_info, ent); | |
32 | ||
33 | return strcmp(e1->id, id ? (const char *)id : e2->id); | |
34 | } | |
35 | ||
36 | void init_bundle_list(struct bundle_list *list) | |
37 | { | |
38 | memset(list, 0, sizeof(*list)); | |
39 | ||
40 | /* Implied defaults. */ | |
41 | list->mode = BUNDLE_MODE_ALL; | |
42 | list->version = 1; | |
43 | ||
44 | hashmap_init(&list->bundles, compare_bundles, NULL, 0); | |
45 | } | |
46 | ||
47 | static int clear_remote_bundle_info(struct remote_bundle_info *bundle, | |
48 | void *data) | |
49 | { | |
50 | FREE_AND_NULL(bundle->id); | |
51 | FREE_AND_NULL(bundle->uri); | |
c23f5921 DS |
52 | FREE_AND_NULL(bundle->file); |
53 | bundle->unbundled = 0; | |
0634f717 DS |
54 | return 0; |
55 | } | |
56 | ||
57 | void clear_bundle_list(struct bundle_list *list) | |
58 | { | |
59 | if (!list) | |
60 | return; | |
61 | ||
62 | for_all_bundles_in_list(list, clear_remote_bundle_info, NULL); | |
63 | hashmap_clear_and_free(&list->bundles, struct remote_bundle_info, ent); | |
ebc39479 | 64 | free(list->baseURI); |
0634f717 DS |
65 | } |
66 | ||
67 | int for_all_bundles_in_list(struct bundle_list *list, | |
68 | bundle_iterator iter, | |
69 | void *data) | |
70 | { | |
71 | struct remote_bundle_info *info; | |
72 | struct hashmap_iter i; | |
73 | ||
74 | hashmap_for_each_entry(&list->bundles, &i, info, ent) { | |
75 | int result = iter(info, data); | |
76 | ||
77 | if (result) | |
78 | return result; | |
79 | } | |
80 | ||
81 | return 0; | |
82 | } | |
53a50892 | 83 | |
d796cedb ÆAB |
84 | static int summarize_bundle(struct remote_bundle_info *info, void *data) |
85 | { | |
86 | FILE *fp = data; | |
87 | fprintf(fp, "[bundle \"%s\"]\n", info->id); | |
88 | fprintf(fp, "\turi = %s\n", info->uri); | |
512fccf8 DS |
89 | |
90 | if (info->creationToken) | |
91 | fprintf(fp, "\tcreationToken = %"PRIu64"\n", info->creationToken); | |
d796cedb ÆAB |
92 | return 0; |
93 | } | |
94 | ||
95 | void print_bundle_list(FILE *fp, struct bundle_list *list) | |
96 | { | |
97 | const char *mode; | |
98 | ||
99 | switch (list->mode) { | |
100 | case BUNDLE_MODE_ALL: | |
101 | mode = "all"; | |
102 | break; | |
103 | ||
104 | case BUNDLE_MODE_ANY: | |
105 | mode = "any"; | |
106 | break; | |
107 | ||
108 | case BUNDLE_MODE_NONE: | |
109 | default: | |
110 | mode = "<unknown>"; | |
111 | } | |
112 | ||
113 | fprintf(fp, "[bundle]\n"); | |
114 | fprintf(fp, "\tversion = %d\n", list->version); | |
115 | fprintf(fp, "\tmode = %s\n", mode); | |
116 | ||
c93c3d2f DS |
117 | if (list->heuristic) { |
118 | int i; | |
119 | for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) { | |
120 | if (heuristics[i].heuristic == list->heuristic) { | |
121 | printf("\theuristic = %s\n", | |
122 | heuristics[list->heuristic].name); | |
123 | break; | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
d796cedb ÆAB |
128 | for_all_bundles_in_list(list, summarize_bundle, fp); |
129 | } | |
130 | ||
bff03c47 DS |
131 | /** |
132 | * Given a key-value pair, update the state of the given bundle list. | |
133 | * Returns 0 if the key-value pair is understood. Returns -1 if the key | |
134 | * is not understood or the value is malformed. | |
135 | */ | |
bff03c47 DS |
136 | static int bundle_list_update(const char *key, const char *value, |
137 | struct bundle_list *list) | |
138 | { | |
139 | struct strbuf id = STRBUF_INIT; | |
140 | struct remote_bundle_info lookup = REMOTE_BUNDLE_INFO_INIT; | |
141 | struct remote_bundle_info *bundle; | |
142 | const char *subsection, *subkey; | |
143 | size_t subsection_len; | |
144 | ||
145 | if (parse_config_key(key, "bundle", &subsection, &subsection_len, &subkey)) | |
146 | return -1; | |
147 | ||
148 | if (!subsection_len) { | |
149 | if (!strcmp(subkey, "version")) { | |
150 | int version; | |
151 | if (!git_parse_int(value, &version)) | |
152 | return -1; | |
153 | if (version != 1) | |
154 | return -1; | |
155 | ||
156 | list->version = version; | |
157 | return 0; | |
158 | } | |
159 | ||
160 | if (!strcmp(subkey, "mode")) { | |
161 | if (!strcmp(value, "all")) | |
162 | list->mode = BUNDLE_MODE_ALL; | |
163 | else if (!strcmp(value, "any")) | |
164 | list->mode = BUNDLE_MODE_ANY; | |
165 | else | |
166 | return -1; | |
167 | return 0; | |
168 | } | |
169 | ||
c93c3d2f DS |
170 | if (!strcmp(subkey, "heuristic")) { |
171 | int i; | |
172 | for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) { | |
173 | if (heuristics[i].heuristic && | |
174 | heuristics[i].name && | |
175 | !strcmp(value, heuristics[i].name)) { | |
176 | list->heuristic = heuristics[i].heuristic; | |
177 | return 0; | |
178 | } | |
179 | } | |
180 | ||
181 | /* Ignore unknown heuristics. */ | |
182 | return 0; | |
183 | } | |
184 | ||
bff03c47 DS |
185 | /* Ignore other unknown global keys. */ |
186 | return 0; | |
187 | } | |
188 | ||
189 | strbuf_add(&id, subsection, subsection_len); | |
190 | ||
191 | /* | |
192 | * Check for an existing bundle with this <id>, or create one | |
193 | * if necessary. | |
194 | */ | |
195 | lookup.id = id.buf; | |
196 | hashmap_entry_init(&lookup.ent, strhash(lookup.id)); | |
197 | if (!(bundle = hashmap_get_entry(&list->bundles, &lookup, ent, NULL))) { | |
198 | CALLOC_ARRAY(bundle, 1); | |
199 | bundle->id = strbuf_detach(&id, NULL); | |
200 | hashmap_entry_init(&bundle->ent, strhash(bundle->id)); | |
201 | hashmap_add(&list->bundles, &bundle->ent); | |
202 | } | |
203 | strbuf_release(&id); | |
204 | ||
205 | if (!strcmp(subkey, "uri")) { | |
206 | if (bundle->uri) | |
207 | return -1; | |
ebc39479 | 208 | bundle->uri = relative_url(list->baseURI, value, NULL); |
bff03c47 DS |
209 | return 0; |
210 | } | |
211 | ||
512fccf8 DS |
212 | if (!strcmp(subkey, "creationtoken")) { |
213 | if (sscanf(value, "%"PRIu64, &bundle->creationToken) != 1) | |
214 | warning(_("could not parse bundle list key %s with value '%s'"), | |
215 | "creationToken", value); | |
216 | return 0; | |
217 | } | |
218 | ||
bff03c47 DS |
219 | /* |
220 | * At this point, we ignore any information that we don't | |
221 | * understand, assuming it to be hints for a heuristic the client | |
222 | * does not currently understand. | |
223 | */ | |
224 | return 0; | |
225 | } | |
226 | ||
738e5245 DS |
227 | static int config_to_bundle_list(const char *key, const char *value, void *data) |
228 | { | |
229 | struct bundle_list *list = data; | |
230 | return bundle_list_update(key, value, list); | |
231 | } | |
232 | ||
233 | int bundle_uri_parse_config_format(const char *uri, | |
234 | const char *filename, | |
235 | struct bundle_list *list) | |
236 | { | |
237 | int result; | |
238 | struct config_options opts = { | |
239 | .error_action = CONFIG_ERROR_ERROR, | |
240 | }; | |
241 | ||
ebc39479 DS |
242 | if (!list->baseURI) { |
243 | struct strbuf baseURI = STRBUF_INIT; | |
244 | strbuf_addstr(&baseURI, uri); | |
245 | ||
246 | /* | |
247 | * If the URI does not end with a trailing slash, then | |
248 | * remove the filename portion of the path. This is | |
249 | * important for relative URIs. | |
250 | */ | |
251 | strbuf_strip_file_from_path(&baseURI); | |
252 | list->baseURI = strbuf_detach(&baseURI, NULL); | |
253 | } | |
738e5245 DS |
254 | result = git_config_from_file_with_options(config_to_bundle_list, |
255 | filename, list, | |
256 | &opts); | |
257 | ||
258 | if (!result && list->mode == BUNDLE_MODE_NONE) { | |
259 | warning(_("bundle list at '%s' has no mode"), uri); | |
260 | result = 1; | |
261 | } | |
262 | ||
263 | return result; | |
264 | } | |
265 | ||
23b6d00b | 266 | static char *find_temp_filename(void) |
53a50892 DS |
267 | { |
268 | int fd; | |
23b6d00b | 269 | struct strbuf name = STRBUF_INIT; |
53a50892 DS |
270 | /* |
271 | * Find a temporary filename that is available. This is briefly | |
272 | * racy, but unlikely to collide. | |
273 | */ | |
23b6d00b | 274 | fd = odb_mkstemp(&name, "bundles/tmp_uri_XXXXXX"); |
53a50892 DS |
275 | if (fd < 0) { |
276 | warning(_("failed to create temporary file")); | |
23b6d00b | 277 | return NULL; |
53a50892 DS |
278 | } |
279 | ||
280 | close(fd); | |
23b6d00b DS |
281 | unlink(name.buf); |
282 | return strbuf_detach(&name, NULL); | |
53a50892 DS |
283 | } |
284 | ||
59c1752a | 285 | static int download_https_uri_to_file(const char *file, const char *uri) |
53a50892 | 286 | { |
59c1752a DS |
287 | int result = 0; |
288 | struct child_process cp = CHILD_PROCESS_INIT; | |
289 | FILE *child_in = NULL, *child_out = NULL; | |
290 | struct strbuf line = STRBUF_INIT; | |
291 | int found_get = 0; | |
292 | ||
293 | strvec_pushl(&cp.args, "git-remote-https", uri, NULL); | |
8628a842 | 294 | cp.err = -1; |
59c1752a DS |
295 | cp.in = -1; |
296 | cp.out = -1; | |
297 | ||
298 | if (start_command(&cp)) | |
299 | return 1; | |
300 | ||
301 | child_in = fdopen(cp.in, "w"); | |
302 | if (!child_in) { | |
303 | result = 1; | |
304 | goto cleanup; | |
305 | } | |
306 | ||
307 | child_out = fdopen(cp.out, "r"); | |
308 | if (!child_out) { | |
309 | result = 1; | |
310 | goto cleanup; | |
311 | } | |
312 | ||
313 | fprintf(child_in, "capabilities\n"); | |
314 | fflush(child_in); | |
315 | ||
316 | while (!strbuf_getline(&line, child_out)) { | |
317 | if (!line.len) | |
318 | break; | |
319 | if (!strcmp(line.buf, "get")) | |
320 | found_get = 1; | |
321 | } | |
322 | strbuf_release(&line); | |
323 | ||
324 | if (!found_get) { | |
325 | result = error(_("insufficient capabilities")); | |
326 | goto cleanup; | |
327 | } | |
328 | ||
329 | fprintf(child_in, "get %s %s\n\n", uri, file); | |
330 | ||
331 | cleanup: | |
332 | if (child_in) | |
333 | fclose(child_in); | |
334 | if (finish_command(&cp)) | |
335 | return 1; | |
336 | if (child_out) | |
337 | fclose(child_out); | |
338 | return result; | |
339 | } | |
340 | ||
341 | static int copy_uri_to_file(const char *filename, const char *uri) | |
342 | { | |
343 | const char *out; | |
344 | ||
345 | if (starts_with(uri, "https:") || | |
346 | starts_with(uri, "http:")) | |
347 | return download_https_uri_to_file(filename, uri); | |
348 | ||
349 | if (skip_prefix(uri, "file://", &out)) | |
350 | uri = out; | |
351 | ||
352 | /* Copy as a file */ | |
353 | return copy_file(filename, uri, 0); | |
53a50892 DS |
354 | } |
355 | ||
356 | static int unbundle_from_file(struct repository *r, const char *file) | |
357 | { | |
358 | int result = 0; | |
359 | int bundle_fd; | |
360 | struct bundle_header header = BUNDLE_HEADER_INIT; | |
361 | struct string_list_item *refname; | |
362 | struct strbuf bundle_ref = STRBUF_INIT; | |
363 | size_t bundle_prefix_len; | |
364 | ||
365 | if ((bundle_fd = read_bundle_header(file, &header)) < 0) | |
366 | return 1; | |
367 | ||
89bd7fed DS |
368 | /* |
369 | * Skip the reachability walk here, since we will be adding | |
370 | * a reachable ref pointing to the new tips, which will reach | |
371 | * the prerequisite commits. | |
372 | */ | |
70334fc3 DS |
373 | if ((result = unbundle(r, &header, bundle_fd, NULL, |
374 | VERIFY_BUNDLE_QUIET))) | |
53a50892 DS |
375 | return 1; |
376 | ||
377 | /* | |
378 | * Convert all refs/heads/ from the bundle into refs/bundles/ | |
379 | * in the local repository. | |
380 | */ | |
381 | strbuf_addstr(&bundle_ref, "refs/bundles/"); | |
382 | bundle_prefix_len = bundle_ref.len; | |
383 | ||
384 | for_each_string_list_item(refname, &header.references) { | |
385 | struct object_id *oid = refname->util; | |
386 | struct object_id old_oid; | |
387 | const char *branch_name; | |
388 | int has_old; | |
389 | ||
390 | if (!skip_prefix(refname->string, "refs/heads/", &branch_name)) | |
391 | continue; | |
392 | ||
393 | strbuf_setlen(&bundle_ref, bundle_prefix_len); | |
394 | strbuf_addstr(&bundle_ref, branch_name); | |
395 | ||
396 | has_old = !read_ref(bundle_ref.buf, &old_oid); | |
397 | update_ref("fetched bundle", bundle_ref.buf, oid, | |
398 | has_old ? &old_oid : NULL, | |
399 | REF_SKIP_OID_VERIFICATION, | |
400 | UPDATE_REFS_MSG_ON_ERR); | |
401 | } | |
402 | ||
403 | bundle_header_release(&header); | |
404 | return result; | |
405 | } | |
406 | ||
c23f5921 DS |
407 | struct bundle_list_context { |
408 | struct repository *r; | |
409 | struct bundle_list *list; | |
410 | enum bundle_list_mode mode; | |
411 | int count; | |
412 | int depth; | |
413 | }; | |
414 | ||
415 | /* | |
416 | * This early definition is necessary because we use indirect recursion: | |
417 | * | |
418 | * While iterating through a bundle list that was downloaded as part | |
419 | * of fetch_bundle_uri_internal(), iterator methods eventually call it | |
420 | * again, but with depth + 1. | |
421 | */ | |
422 | static int fetch_bundle_uri_internal(struct repository *r, | |
423 | struct remote_bundle_info *bundle, | |
424 | int depth, | |
425 | struct bundle_list *list); | |
426 | ||
427 | static int download_bundle_to_file(struct remote_bundle_info *bundle, void *data) | |
428 | { | |
429 | int res; | |
430 | struct bundle_list_context *ctx = data; | |
431 | ||
432 | if (ctx->mode == BUNDLE_MODE_ANY && ctx->count) | |
433 | return 0; | |
434 | ||
435 | res = fetch_bundle_uri_internal(ctx->r, bundle, ctx->depth + 1, ctx->list); | |
436 | ||
437 | /* | |
438 | * Only increment count if the download succeeded. If our mode is | |
439 | * BUNDLE_MODE_ANY, then we will want to try other URIs in the | |
440 | * list in case they work instead. | |
441 | */ | |
442 | if (!res) | |
443 | ctx->count++; | |
444 | ||
445 | /* | |
446 | * To be opportunistic as possible, we continue iterating and | |
447 | * download as many bundles as we can, so we can apply the ones | |
448 | * that work, even in BUNDLE_MODE_ALL mode. | |
449 | */ | |
450 | return 0; | |
451 | } | |
452 | ||
7903efb7 DS |
453 | struct bundles_for_sorting { |
454 | struct remote_bundle_info **items; | |
455 | size_t alloc; | |
456 | size_t nr; | |
457 | }; | |
458 | ||
459 | static int append_bundle(struct remote_bundle_info *bundle, void *data) | |
460 | { | |
461 | struct bundles_for_sorting *list = data; | |
462 | list->items[list->nr++] = bundle; | |
463 | return 0; | |
464 | } | |
465 | ||
466 | /** | |
467 | * For use in QSORT() to get a list sorted by creationToken | |
468 | * in decreasing order. | |
469 | */ | |
470 | static int compare_creation_token_decreasing(const void *va, const void *vb) | |
471 | { | |
472 | const struct remote_bundle_info * const *a = va; | |
473 | const struct remote_bundle_info * const *b = vb; | |
474 | ||
475 | if ((*a)->creationToken > (*b)->creationToken) | |
476 | return -1; | |
477 | if ((*a)->creationToken < (*b)->creationToken) | |
478 | return 1; | |
479 | return 0; | |
480 | } | |
481 | ||
482 | static int fetch_bundles_by_token(struct repository *r, | |
483 | struct bundle_list *list) | |
484 | { | |
485 | int cur; | |
486 | int move_direction = 0; | |
c429bed1 DS |
487 | const char *creationTokenStr; |
488 | uint64_t maxCreationToken = 0, newMaxCreationToken = 0; | |
7903efb7 DS |
489 | struct bundle_list_context ctx = { |
490 | .r = r, | |
491 | .list = list, | |
492 | .mode = list->mode, | |
493 | }; | |
494 | struct bundles_for_sorting bundles = { | |
495 | .alloc = hashmap_get_size(&list->bundles), | |
496 | }; | |
497 | ||
498 | ALLOC_ARRAY(bundles.items, bundles.alloc); | |
499 | ||
500 | for_all_bundles_in_list(list, append_bundle, &bundles); | |
501 | ||
c429bed1 DS |
502 | if (!bundles.nr) { |
503 | free(bundles.items); | |
504 | return 0; | |
505 | } | |
506 | ||
7903efb7 DS |
507 | QSORT(bundles.items, bundles.nr, compare_creation_token_decreasing); |
508 | ||
c429bed1 DS |
509 | /* |
510 | * If fetch.bundleCreationToken exists, parses to a uint64t, and | |
511 | * is not strictly smaller than the maximum creation token in the | |
512 | * bundle list, then do not download any bundles. | |
513 | */ | |
514 | if (!repo_config_get_value(r, | |
515 | "fetch.bundlecreationtoken", | |
516 | &creationTokenStr) && | |
517 | sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) == 1 && | |
518 | bundles.items[0]->creationToken <= maxCreationToken) { | |
519 | free(bundles.items); | |
520 | return 0; | |
521 | } | |
522 | ||
7903efb7 DS |
523 | /* |
524 | * Attempt to download and unbundle the minimum number of bundles by | |
525 | * creationToken in decreasing order. If we fail to unbundle (after | |
526 | * a successful download) then move to the next non-downloaded bundle | |
527 | * and attempt downloading. Once we succeed in applying a bundle, | |
528 | * move to the previous unapplied bundle and attempt to unbundle it | |
529 | * again. | |
530 | * | |
531 | * In the case of a fresh clone, we will likely download all of the | |
532 | * bundles before successfully unbundling the oldest one, then the | |
533 | * rest of the bundles unbundle successfully in increasing order | |
534 | * of creationToken. | |
535 | * | |
536 | * If there are existing objects, then this process may terminate | |
537 | * early when all required commits from "new" bundles exist in the | |
538 | * repo's object store. | |
539 | */ | |
540 | cur = 0; | |
541 | while (cur >= 0 && cur < bundles.nr) { | |
542 | struct remote_bundle_info *bundle = bundles.items[cur]; | |
c429bed1 DS |
543 | |
544 | /* | |
545 | * If we need to dig into bundles below the previous | |
546 | * creation token value, then likely we are in an erroneous | |
547 | * state due to missing or invalid bundles. Halt the process | |
548 | * instead of continuing to download extra data. | |
549 | */ | |
550 | if (bundle->creationToken <= maxCreationToken) | |
551 | break; | |
552 | ||
7903efb7 DS |
553 | if (!bundle->file) { |
554 | /* | |
555 | * Not downloaded yet. Try downloading. | |
556 | * | |
557 | * Note that bundle->file is non-NULL if a download | |
558 | * was attempted, even if it failed to download. | |
559 | */ | |
560 | if (fetch_bundle_uri_internal(ctx.r, bundle, ctx.depth + 1, ctx.list)) { | |
561 | /* Mark as unbundled so we do not retry. */ | |
562 | bundle->unbundled = 1; | |
563 | ||
564 | /* Try looking deeper in the list. */ | |
565 | move_direction = 1; | |
566 | goto move; | |
567 | } | |
568 | ||
569 | /* We expect bundles when using creationTokens. */ | |
570 | if (!is_bundle(bundle->file, 1)) { | |
571 | warning(_("file downloaded from '%s' is not a bundle"), | |
572 | bundle->uri); | |
573 | break; | |
574 | } | |
575 | } | |
576 | ||
577 | if (bundle->file && !bundle->unbundled) { | |
578 | /* | |
579 | * This was downloaded, but not successfully | |
580 | * unbundled. Try unbundling again. | |
581 | */ | |
582 | if (unbundle_from_file(ctx.r, bundle->file)) { | |
583 | /* Try looking deeper in the list. */ | |
584 | move_direction = 1; | |
585 | } else { | |
586 | /* | |
587 | * Succeeded in unbundle. Retry bundles | |
588 | * that previously failed to unbundle. | |
589 | */ | |
590 | move_direction = -1; | |
591 | bundle->unbundled = 1; | |
c429bed1 DS |
592 | |
593 | if (bundle->creationToken > newMaxCreationToken) | |
594 | newMaxCreationToken = bundle->creationToken; | |
7903efb7 DS |
595 | } |
596 | } | |
597 | ||
598 | /* | |
599 | * Else case: downloaded and unbundled successfully. | |
600 | * Skip this by moving in the same direction as the | |
601 | * previous step. | |
602 | */ | |
603 | ||
604 | move: | |
605 | /* Move in the specified direction and repeat. */ | |
606 | cur += move_direction; | |
607 | } | |
608 | ||
7903efb7 DS |
609 | /* |
610 | * We succeed if the loop terminates because 'cur' drops below | |
611 | * zero. The other case is that we terminate because 'cur' | |
612 | * reaches the end of the list, so we have a failure no matter | |
613 | * which bundles we apply from the list. | |
614 | */ | |
c429bed1 DS |
615 | if (cur < 0) { |
616 | struct strbuf value = STRBUF_INIT; | |
617 | strbuf_addf(&value, "%"PRIu64"", newMaxCreationToken); | |
618 | if (repo_config_set_multivar_gently(ctx.r, | |
619 | "fetch.bundleCreationToken", | |
620 | value.buf, NULL, 0)) | |
621 | warning(_("failed to store maximum creation token")); | |
622 | ||
623 | strbuf_release(&value); | |
624 | } | |
625 | ||
626 | free(bundles.items); | |
7903efb7 DS |
627 | return cur >= 0; |
628 | } | |
629 | ||
c23f5921 DS |
630 | static int download_bundle_list(struct repository *r, |
631 | struct bundle_list *local_list, | |
632 | struct bundle_list *global_list, | |
633 | int depth) | |
634 | { | |
635 | struct bundle_list_context ctx = { | |
636 | .r = r, | |
637 | .list = global_list, | |
638 | .depth = depth + 1, | |
639 | .mode = local_list->mode, | |
640 | }; | |
641 | ||
642 | return for_all_bundles_in_list(local_list, download_bundle_to_file, &ctx); | |
643 | } | |
644 | ||
645 | static int fetch_bundle_list_in_config_format(struct repository *r, | |
646 | struct bundle_list *global_list, | |
647 | struct remote_bundle_info *bundle, | |
648 | int depth) | |
649 | { | |
650 | int result; | |
651 | struct bundle_list list_from_bundle; | |
652 | ||
653 | init_bundle_list(&list_from_bundle); | |
654 | ||
655 | if ((result = bundle_uri_parse_config_format(bundle->uri, | |
656 | bundle->file, | |
657 | &list_from_bundle))) | |
658 | goto cleanup; | |
659 | ||
660 | if (list_from_bundle.mode == BUNDLE_MODE_NONE) { | |
661 | warning(_("unrecognized bundle mode from URI '%s'"), | |
662 | bundle->uri); | |
663 | result = -1; | |
664 | goto cleanup; | |
665 | } | |
666 | ||
7903efb7 DS |
667 | /* |
668 | * If this list uses the creationToken heuristic, then the URIs | |
669 | * it advertises are expected to be bundles, not nested lists. | |
670 | * We can drop 'global_list' and 'depth'. | |
671 | */ | |
672 | if (list_from_bundle.heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) { | |
673 | result = fetch_bundles_by_token(r, &list_from_bundle); | |
674 | global_list->heuristic = BUNDLE_HEURISTIC_CREATIONTOKEN; | |
675 | } else if ((result = download_bundle_list(r, &list_from_bundle, | |
c23f5921 DS |
676 | global_list, depth))) |
677 | goto cleanup; | |
678 | ||
679 | cleanup: | |
680 | clear_bundle_list(&list_from_bundle); | |
681 | return result; | |
682 | } | |
683 | ||
20c1e2a6 DS |
684 | /** |
685 | * This limits the recursion on fetch_bundle_uri_internal() when following | |
686 | * bundle lists. | |
687 | */ | |
688 | static int max_bundle_uri_depth = 4; | |
689 | ||
c23f5921 DS |
690 | /** |
691 | * Recursively download all bundles advertised at the given URI | |
692 | * to files. If the file is a bundle, then add it to the given | |
693 | * 'list'. Otherwise, expect a bundle list and recurse on the | |
694 | * URIs in that list according to the list mode (ANY or ALL). | |
695 | */ | |
20c1e2a6 | 696 | static int fetch_bundle_uri_internal(struct repository *r, |
c23f5921 DS |
697 | struct remote_bundle_info *bundle, |
698 | int depth, | |
699 | struct bundle_list *list) | |
53a50892 DS |
700 | { |
701 | int result = 0; | |
c23f5921 | 702 | struct remote_bundle_info *bcopy; |
53a50892 | 703 | |
20c1e2a6 DS |
704 | if (depth >= max_bundle_uri_depth) { |
705 | warning(_("exceeded bundle URI recursion limit (%d)"), | |
706 | max_bundle_uri_depth); | |
707 | return -1; | |
708 | } | |
709 | ||
c23f5921 DS |
710 | if (!bundle->file && |
711 | !(bundle->file = find_temp_filename())) { | |
23b6d00b | 712 | result = -1; |
53a50892 | 713 | goto cleanup; |
23b6d00b | 714 | } |
53a50892 | 715 | |
c23f5921 DS |
716 | if ((result = copy_uri_to_file(bundle->file, bundle->uri))) { |
717 | warning(_("failed to download bundle from URI '%s'"), bundle->uri); | |
53a50892 DS |
718 | goto cleanup; |
719 | } | |
720 | ||
c23f5921 DS |
721 | if ((result = !is_bundle(bundle->file, 1))) { |
722 | result = fetch_bundle_list_in_config_format( | |
723 | r, list, bundle, depth); | |
724 | if (result) | |
725 | warning(_("file at URI '%s' is not a bundle or bundle list"), | |
726 | bundle->uri); | |
53a50892 DS |
727 | goto cleanup; |
728 | } | |
729 | ||
c23f5921 DS |
730 | /* Copy the bundle and insert it into the global list. */ |
731 | CALLOC_ARRAY(bcopy, 1); | |
732 | bcopy->id = xstrdup(bundle->id); | |
733 | bcopy->file = xstrdup(bundle->file); | |
734 | hashmap_entry_init(&bcopy->ent, strhash(bcopy->id)); | |
735 | hashmap_add(&list->bundles, &bcopy->ent); | |
53a50892 DS |
736 | |
737 | cleanup: | |
c23f5921 DS |
738 | if (result && bundle->file) |
739 | unlink(bundle->file); | |
53a50892 DS |
740 | return result; |
741 | } | |
9424e373 | 742 | |
c23f5921 DS |
743 | /** |
744 | * This loop iterator breaks the loop with nonzero return code on the | |
745 | * first successful unbundling of a bundle. | |
746 | */ | |
747 | static int attempt_unbundle(struct remote_bundle_info *info, void *data) | |
748 | { | |
749 | struct repository *r = data; | |
750 | ||
751 | if (!info->file || info->unbundled) | |
752 | return 0; | |
753 | ||
754 | if (!unbundle_from_file(r, info->file)) { | |
755 | info->unbundled = 1; | |
756 | return 1; | |
757 | } | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
762 | static int unbundle_all_bundles(struct repository *r, | |
763 | struct bundle_list *list) | |
764 | { | |
765 | /* | |
766 | * Iterate through all bundles looking for ones that can | |
767 | * successfully unbundle. If any succeed, then perhaps another | |
768 | * will succeed in the next attempt. | |
769 | * | |
770 | * Keep in mind that a non-zero result for the loop here means | |
771 | * the loop terminated early on a successful unbundling, which | |
772 | * signals that we can try again. | |
773 | */ | |
774 | while (for_all_bundles_in_list(list, attempt_unbundle, r)) ; | |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
779 | static int unlink_bundle(struct remote_bundle_info *info, void *data) | |
780 | { | |
781 | if (info->file) | |
782 | unlink_or_warn(info->file); | |
783 | return 0; | |
784 | } | |
785 | ||
4074d3c7 DS |
786 | int fetch_bundle_uri(struct repository *r, const char *uri, |
787 | int *has_heuristic) | |
20c1e2a6 | 788 | { |
c23f5921 DS |
789 | int result; |
790 | struct bundle_list list; | |
791 | struct remote_bundle_info bundle = { | |
792 | .uri = xstrdup(uri), | |
793 | .id = xstrdup(""), | |
794 | }; | |
795 | ||
796 | init_bundle_list(&list); | |
797 | ||
25bccb4b | 798 | /* |
0b1a95ef | 799 | * Do not fetch an empty bundle URI. An empty bundle URI |
25bccb4b DS |
800 | * could signal that a configured bundle URI has been disabled. |
801 | */ | |
0b1a95ef | 802 | if (!*uri) { |
25bccb4b DS |
803 | result = 0; |
804 | goto cleanup; | |
805 | } | |
806 | ||
c23f5921 DS |
807 | /* If a bundle is added to this global list, then it is required. */ |
808 | list.mode = BUNDLE_MODE_ALL; | |
809 | ||
810 | if ((result = fetch_bundle_uri_internal(r, &bundle, 0, &list))) | |
811 | goto cleanup; | |
812 | ||
813 | result = unbundle_all_bundles(r, &list); | |
814 | ||
815 | cleanup: | |
4074d3c7 DS |
816 | if (has_heuristic) |
817 | *has_heuristic = (list.heuristic != BUNDLE_HEURISTIC_NONE); | |
c23f5921 DS |
818 | for_all_bundles_in_list(&list, unlink_bundle, NULL); |
819 | clear_bundle_list(&list); | |
820 | clear_remote_bundle_info(&bundle, NULL); | |
821 | return result; | |
20c1e2a6 DS |
822 | } |
823 | ||
12b0a14b DS |
824 | int fetch_bundle_list(struct repository *r, struct bundle_list *list) |
825 | { | |
826 | int result; | |
827 | struct bundle_list global_list; | |
828 | ||
7903efb7 DS |
829 | /* |
830 | * If the creationToken heuristic is used, then the URIs | |
831 | * advertised by 'list' are not nested lists and instead | |
832 | * direct bundles. We do not need to use global_list. | |
833 | */ | |
834 | if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) | |
835 | return fetch_bundles_by_token(r, list); | |
836 | ||
12b0a14b DS |
837 | init_bundle_list(&global_list); |
838 | ||
839 | /* If a bundle is added to this global list, then it is required. */ | |
840 | global_list.mode = BUNDLE_MODE_ALL; | |
841 | ||
842 | if ((result = download_bundle_list(r, list, &global_list, 0))) | |
843 | goto cleanup; | |
844 | ||
7903efb7 DS |
845 | if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) |
846 | result = fetch_bundles_by_token(r, list); | |
847 | else | |
848 | result = unbundle_all_bundles(r, &global_list); | |
12b0a14b DS |
849 | |
850 | cleanup: | |
851 | for_all_bundles_in_list(&global_list, unlink_bundle, NULL); | |
852 | clear_bundle_list(&global_list); | |
853 | return result; | |
854 | } | |
855 | ||
8b8d9a22 ÆAB |
856 | /** |
857 | * API for serve.c. | |
858 | */ | |
859 | ||
860 | int bundle_uri_advertise(struct repository *r, struct strbuf *value UNUSED) | |
861 | { | |
862 | static int advertise_bundle_uri = -1; | |
863 | ||
864 | if (advertise_bundle_uri != -1) | |
865 | goto cached; | |
866 | ||
867 | advertise_bundle_uri = 0; | |
868 | repo_config_get_maybe_bool(r, "uploadpack.advertisebundleuris", &advertise_bundle_uri); | |
869 | ||
870 | cached: | |
871 | return advertise_bundle_uri; | |
872 | } | |
873 | ||
738dc7d4 DS |
874 | static int config_to_packet_line(const char *key, const char *value, void *data) |
875 | { | |
876 | struct packet_reader *writer = data; | |
877 | ||
20869d1a | 878 | if (starts_with(key, "bundle.")) |
738dc7d4 DS |
879 | packet_write_fmt(writer->fd, "%s=%s", key, value); |
880 | ||
881 | return 0; | |
882 | } | |
883 | ||
8b8d9a22 ÆAB |
884 | int bundle_uri_command(struct repository *r, |
885 | struct packet_reader *request) | |
886 | { | |
887 | struct packet_writer writer; | |
888 | packet_writer_init(&writer, 1); | |
889 | ||
890 | while (packet_reader_read(request) == PACKET_READ_NORMAL) | |
891 | die(_("bundle-uri: unexpected argument: '%s'"), request->line); | |
892 | if (request->status != PACKET_READ_FLUSH) | |
893 | die(_("bundle-uri: expected flush after arguments")); | |
894 | ||
738dc7d4 DS |
895 | /* |
896 | * Read all "bundle.*" config lines to the client as key=value | |
897 | * packet lines. | |
898 | */ | |
4b4e75dd | 899 | repo_config(r, config_to_packet_line, &writer); |
8b8d9a22 ÆAB |
900 | |
901 | packet_writer_flush(&writer); | |
902 | ||
903 | return 0; | |
904 | } | |
905 | ||
9424e373 ÆAB |
906 | /** |
907 | * General API for {transport,connect}.c etc. | |
908 | */ | |
909 | int bundle_uri_parse_line(struct bundle_list *list, const char *line) | |
910 | { | |
911 | int result; | |
912 | const char *equals; | |
913 | struct strbuf key = STRBUF_INIT; | |
914 | ||
915 | if (!strlen(line)) | |
916 | return error(_("bundle-uri: got an empty line")); | |
917 | ||
918 | equals = strchr(line, '='); | |
919 | ||
920 | if (!equals) | |
921 | return error(_("bundle-uri: line is not of the form 'key=value'")); | |
922 | if (line == equals || !*(equals + 1)) | |
923 | return error(_("bundle-uri: line has empty key or value")); | |
924 | ||
925 | strbuf_add(&key, line, equals - line); | |
926 | result = bundle_list_update(key.buf, equals + 1, list); | |
927 | strbuf_release(&key); | |
928 | ||
929 | return result; | |
930 | } |