The fifth batch
[alt-git.git] / bundle-uri.c
blob744257c49c1328f249c9b611c66084ded75cbe24
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "bundle-uri.h"
6 #include "bundle.h"
7 #include "copy.h"
8 #include "gettext.h"
9 #include "refs.h"
10 #include "run-command.h"
11 #include "hashmap.h"
12 #include "pkt-line.h"
13 #include "config.h"
14 #include "fetch-pack.h"
15 #include "remote.h"
16 #include "trace2.h"
17 #include "object-store-ll.h"
19 static struct {
20 enum bundle_list_heuristic heuristic;
21 const char *name;
22 } heuristics[BUNDLE_HEURISTIC__COUNT] = {
23 { BUNDLE_HEURISTIC_NONE, ""},
24 { BUNDLE_HEURISTIC_CREATIONTOKEN, "creationToken" },
27 static int compare_bundles(const void *hashmap_cmp_fn_data UNUSED,
28 const struct hashmap_entry *he1,
29 const struct hashmap_entry *he2,
30 const void *id)
32 const struct remote_bundle_info *e1 =
33 container_of(he1, const struct remote_bundle_info, ent);
34 const struct remote_bundle_info *e2 =
35 container_of(he2, const struct remote_bundle_info, ent);
37 return strcmp(e1->id, id ? (const char *)id : e2->id);
40 void init_bundle_list(struct bundle_list *list)
42 memset(list, 0, sizeof(*list));
44 /* Implied defaults. */
45 list->mode = BUNDLE_MODE_ALL;
46 list->version = 1;
48 hashmap_init(&list->bundles, compare_bundles, NULL, 0);
51 static int clear_remote_bundle_info(struct remote_bundle_info *bundle,
52 void *data UNUSED)
54 FREE_AND_NULL(bundle->id);
55 FREE_AND_NULL(bundle->uri);
56 FREE_AND_NULL(bundle->file);
57 bundle->unbundled = 0;
58 return 0;
61 void clear_bundle_list(struct bundle_list *list)
63 if (!list)
64 return;
66 for_all_bundles_in_list(list, clear_remote_bundle_info, NULL);
67 hashmap_clear_and_free(&list->bundles, struct remote_bundle_info, ent);
68 free(list->baseURI);
71 int for_all_bundles_in_list(struct bundle_list *list,
72 bundle_iterator iter,
73 void *data)
75 struct remote_bundle_info *info;
76 struct hashmap_iter i;
78 hashmap_for_each_entry(&list->bundles, &i, info, ent) {
79 int result = iter(info, data);
81 if (result)
82 return result;
85 return 0;
88 static int summarize_bundle(struct remote_bundle_info *info, void *data)
90 FILE *fp = data;
91 fprintf(fp, "[bundle \"%s\"]\n", info->id);
92 fprintf(fp, "\turi = %s\n", info->uri);
94 if (info->creationToken)
95 fprintf(fp, "\tcreationToken = %"PRIu64"\n", info->creationToken);
96 return 0;
99 void print_bundle_list(FILE *fp, struct bundle_list *list)
101 const char *mode;
103 switch (list->mode) {
104 case BUNDLE_MODE_ALL:
105 mode = "all";
106 break;
108 case BUNDLE_MODE_ANY:
109 mode = "any";
110 break;
112 case BUNDLE_MODE_NONE:
113 default:
114 mode = "<unknown>";
117 fprintf(fp, "[bundle]\n");
118 fprintf(fp, "\tversion = %d\n", list->version);
119 fprintf(fp, "\tmode = %s\n", mode);
121 if (list->heuristic) {
122 int i;
123 for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) {
124 if (heuristics[i].heuristic == list->heuristic) {
125 printf("\theuristic = %s\n",
126 heuristics[list->heuristic].name);
127 break;
132 for_all_bundles_in_list(list, summarize_bundle, fp);
136 * Given a key-value pair, update the state of the given bundle list.
137 * Returns 0 if the key-value pair is understood. Returns -1 if the key
138 * is not understood or the value is malformed.
140 static int bundle_list_update(const char *key, const char *value,
141 struct bundle_list *list)
143 struct strbuf id = STRBUF_INIT;
144 struct remote_bundle_info lookup = REMOTE_BUNDLE_INFO_INIT;
145 struct remote_bundle_info *bundle;
146 const char *subsection, *subkey;
147 size_t subsection_len;
149 if (parse_config_key(key, "bundle", &subsection, &subsection_len, &subkey))
150 return -1;
152 if (!subsection_len) {
153 if (!strcmp(subkey, "version")) {
154 int version;
155 if (!git_parse_int(value, &version))
156 return -1;
157 if (version != 1)
158 return -1;
160 list->version = version;
161 return 0;
164 if (!strcmp(subkey, "mode")) {
165 if (!strcmp(value, "all"))
166 list->mode = BUNDLE_MODE_ALL;
167 else if (!strcmp(value, "any"))
168 list->mode = BUNDLE_MODE_ANY;
169 else
170 return -1;
171 return 0;
174 if (!strcmp(subkey, "heuristic")) {
175 int i;
176 for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) {
177 if (heuristics[i].heuristic &&
178 heuristics[i].name &&
179 !strcmp(value, heuristics[i].name)) {
180 list->heuristic = heuristics[i].heuristic;
181 return 0;
185 /* Ignore unknown heuristics. */
186 return 0;
189 /* Ignore other unknown global keys. */
190 return 0;
193 strbuf_add(&id, subsection, subsection_len);
196 * Check for an existing bundle with this <id>, or create one
197 * if necessary.
199 lookup.id = id.buf;
200 hashmap_entry_init(&lookup.ent, strhash(lookup.id));
201 if (!(bundle = hashmap_get_entry(&list->bundles, &lookup, ent, NULL))) {
202 CALLOC_ARRAY(bundle, 1);
203 bundle->id = strbuf_detach(&id, NULL);
204 hashmap_entry_init(&bundle->ent, strhash(bundle->id));
205 hashmap_add(&list->bundles, &bundle->ent);
207 strbuf_release(&id);
209 if (!strcmp(subkey, "uri")) {
210 if (bundle->uri)
211 return -1;
212 bundle->uri = relative_url(list->baseURI, value, NULL);
213 return 0;
216 if (!strcmp(subkey, "creationtoken")) {
217 if (sscanf(value, "%"PRIu64, &bundle->creationToken) != 1)
218 warning(_("could not parse bundle list key %s with value '%s'"),
219 "creationToken", value);
220 return 0;
224 * At this point, we ignore any information that we don't
225 * understand, assuming it to be hints for a heuristic the client
226 * does not currently understand.
228 return 0;
231 static int config_to_bundle_list(const char *key, const char *value,
232 const struct config_context *ctx UNUSED,
233 void *data)
235 struct bundle_list *list = data;
236 return bundle_list_update(key, value, list);
239 int bundle_uri_parse_config_format(const char *uri,
240 const char *filename,
241 struct bundle_list *list)
243 int result;
244 struct config_options opts = {
245 .error_action = CONFIG_ERROR_ERROR,
248 if (!list->baseURI) {
249 struct strbuf baseURI = STRBUF_INIT;
250 strbuf_addstr(&baseURI, uri);
253 * If the URI does not end with a trailing slash, then
254 * remove the filename portion of the path. This is
255 * important for relative URIs.
257 strbuf_strip_file_from_path(&baseURI);
258 list->baseURI = strbuf_detach(&baseURI, NULL);
260 result = git_config_from_file_with_options(config_to_bundle_list,
261 filename, list,
262 CONFIG_SCOPE_UNKNOWN,
263 &opts);
265 if (!result && list->mode == BUNDLE_MODE_NONE) {
266 warning(_("bundle list at '%s' has no mode"), uri);
267 result = 1;
270 return result;
273 static char *find_temp_filename(void)
275 int fd;
276 struct strbuf name = STRBUF_INIT;
278 * Find a temporary filename that is available. This is briefly
279 * racy, but unlikely to collide.
281 fd = odb_mkstemp(&name, "bundles/tmp_uri_XXXXXX");
282 if (fd < 0) {
283 warning(_("failed to create temporary file"));
284 return NULL;
287 close(fd);
288 unlink(name.buf);
289 return strbuf_detach(&name, NULL);
292 static int download_https_uri_to_file(const char *file, const char *uri)
294 int result = 0;
295 struct child_process cp = CHILD_PROCESS_INIT;
296 FILE *child_in = NULL, *child_out = NULL;
297 struct strbuf line = STRBUF_INIT;
298 int found_get = 0;
300 strvec_pushl(&cp.args, "git-remote-https", uri, NULL);
301 cp.err = -1;
302 cp.in = -1;
303 cp.out = -1;
305 if (start_command(&cp))
306 return 1;
308 child_in = fdopen(cp.in, "w");
309 if (!child_in) {
310 result = 1;
311 goto cleanup;
314 child_out = fdopen(cp.out, "r");
315 if (!child_out) {
316 result = 1;
317 goto cleanup;
320 fprintf(child_in, "capabilities\n");
321 fflush(child_in);
323 while (!strbuf_getline(&line, child_out)) {
324 if (!line.len)
325 break;
326 if (!strcmp(line.buf, "get"))
327 found_get = 1;
329 strbuf_release(&line);
331 if (!found_get) {
332 result = error(_("insufficient capabilities"));
333 goto cleanup;
336 fprintf(child_in, "get %s %s\n\n", uri, file);
338 cleanup:
339 if (child_in)
340 fclose(child_in);
341 if (finish_command(&cp))
342 return 1;
343 if (child_out)
344 fclose(child_out);
345 return result;
348 static int copy_uri_to_file(const char *filename, const char *uri)
350 const char *out;
352 if (starts_with(uri, "https:") ||
353 starts_with(uri, "http:"))
354 return download_https_uri_to_file(filename, uri);
356 if (skip_prefix(uri, "file://", &out))
357 uri = out;
359 /* Copy as a file */
360 return copy_file(filename, uri, 0);
363 static int unbundle_from_file(struct repository *r, const char *file)
365 int result = 0;
366 int bundle_fd;
367 struct bundle_header header = BUNDLE_HEADER_INIT;
368 struct string_list_item *refname;
369 struct strbuf bundle_ref = STRBUF_INIT;
370 size_t bundle_prefix_len;
371 struct unbundle_opts opts = {
372 .flags = VERIFY_BUNDLE_QUIET |
373 (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0),
376 bundle_fd = read_bundle_header(file, &header);
377 if (bundle_fd < 0) {
378 result = 1;
379 goto cleanup;
383 * Skip the reachability walk here, since we will be adding
384 * a reachable ref pointing to the new tips, which will reach
385 * the prerequisite commits.
387 result = unbundle(r, &header, bundle_fd, NULL, &opts);
388 if (result) {
389 result = 1;
390 goto cleanup;
394 * Convert all refs/heads/ from the bundle into refs/bundles/
395 * in the local repository.
397 strbuf_addstr(&bundle_ref, "refs/bundles/");
398 bundle_prefix_len = bundle_ref.len;
400 for_each_string_list_item(refname, &header.references) {
401 struct object_id *oid = refname->util;
402 struct object_id old_oid;
403 const char *branch_name;
404 int has_old;
406 if (!skip_prefix(refname->string, "refs/heads/", &branch_name))
407 continue;
409 strbuf_setlen(&bundle_ref, bundle_prefix_len);
410 strbuf_addstr(&bundle_ref, branch_name);
412 has_old = !refs_read_ref(get_main_ref_store(the_repository),
413 bundle_ref.buf, &old_oid);
414 refs_update_ref(get_main_ref_store(the_repository),
415 "fetched bundle", bundle_ref.buf, oid,
416 has_old ? &old_oid : NULL,
417 0, UPDATE_REFS_MSG_ON_ERR);
420 cleanup:
421 strbuf_release(&bundle_ref);
422 bundle_header_release(&header);
423 return result;
426 struct bundle_list_context {
427 struct repository *r;
428 struct bundle_list *list;
429 enum bundle_list_mode mode;
430 int count;
431 int depth;
435 * This early definition is necessary because we use indirect recursion:
437 * While iterating through a bundle list that was downloaded as part
438 * of fetch_bundle_uri_internal(), iterator methods eventually call it
439 * again, but with depth + 1.
441 static int fetch_bundle_uri_internal(struct repository *r,
442 struct remote_bundle_info *bundle,
443 int depth,
444 struct bundle_list *list);
446 static int download_bundle_to_file(struct remote_bundle_info *bundle, void *data)
448 int res;
449 struct bundle_list_context *ctx = data;
451 if (ctx->mode == BUNDLE_MODE_ANY && ctx->count)
452 return 0;
454 res = fetch_bundle_uri_internal(ctx->r, bundle, ctx->depth + 1, ctx->list);
457 * Only increment count if the download succeeded. If our mode is
458 * BUNDLE_MODE_ANY, then we will want to try other URIs in the
459 * list in case they work instead.
461 if (!res)
462 ctx->count++;
465 * To be opportunistic as possible, we continue iterating and
466 * download as many bundles as we can, so we can apply the ones
467 * that work, even in BUNDLE_MODE_ALL mode.
469 return 0;
472 struct bundles_for_sorting {
473 struct remote_bundle_info **items;
474 size_t alloc;
475 size_t nr;
478 static int append_bundle(struct remote_bundle_info *bundle, void *data)
480 struct bundles_for_sorting *list = data;
481 list->items[list->nr++] = bundle;
482 return 0;
486 * For use in QSORT() to get a list sorted by creationToken
487 * in decreasing order.
489 static int compare_creation_token_decreasing(const void *va, const void *vb)
491 const struct remote_bundle_info * const *a = va;
492 const struct remote_bundle_info * const *b = vb;
494 if ((*a)->creationToken > (*b)->creationToken)
495 return -1;
496 if ((*a)->creationToken < (*b)->creationToken)
497 return 1;
498 return 0;
501 static int fetch_bundles_by_token(struct repository *r,
502 struct bundle_list *list)
504 int cur;
505 int move_direction = 0;
506 const char *creationTokenStr;
507 uint64_t maxCreationToken = 0, newMaxCreationToken = 0;
508 struct bundle_list_context ctx = {
509 .r = r,
510 .list = list,
511 .mode = list->mode,
513 struct bundles_for_sorting bundles = {
514 .alloc = hashmap_get_size(&list->bundles),
517 ALLOC_ARRAY(bundles.items, bundles.alloc);
519 for_all_bundles_in_list(list, append_bundle, &bundles);
521 if (!bundles.nr) {
522 free(bundles.items);
523 return 0;
526 QSORT(bundles.items, bundles.nr, compare_creation_token_decreasing);
529 * If fetch.bundleCreationToken exists, parses to a uint64t, and
530 * is not strictly smaller than the maximum creation token in the
531 * bundle list, then do not download any bundles.
533 if (!repo_config_get_value(r,
534 "fetch.bundlecreationtoken",
535 &creationTokenStr) &&
536 sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) == 1 &&
537 bundles.items[0]->creationToken <= maxCreationToken) {
538 free(bundles.items);
539 return 0;
543 * Attempt to download and unbundle the minimum number of bundles by
544 * creationToken in decreasing order. If we fail to unbundle (after
545 * a successful download) then move to the next non-downloaded bundle
546 * and attempt downloading. Once we succeed in applying a bundle,
547 * move to the previous unapplied bundle and attempt to unbundle it
548 * again.
550 * In the case of a fresh clone, we will likely download all of the
551 * bundles before successfully unbundling the oldest one, then the
552 * rest of the bundles unbundle successfully in increasing order
553 * of creationToken.
555 * If there are existing objects, then this process may terminate
556 * early when all required commits from "new" bundles exist in the
557 * repo's object store.
559 cur = 0;
560 while (cur >= 0 && cur < bundles.nr) {
561 struct remote_bundle_info *bundle = bundles.items[cur];
564 * If we need to dig into bundles below the previous
565 * creation token value, then likely we are in an erroneous
566 * state due to missing or invalid bundles. Halt the process
567 * instead of continuing to download extra data.
569 if (bundle->creationToken <= maxCreationToken)
570 break;
572 if (!bundle->file) {
574 * Not downloaded yet. Try downloading.
576 * Note that bundle->file is non-NULL if a download
577 * was attempted, even if it failed to download.
579 if (fetch_bundle_uri_internal(ctx.r, bundle, ctx.depth + 1, ctx.list)) {
580 /* Mark as unbundled so we do not retry. */
581 bundle->unbundled = 1;
583 /* Try looking deeper in the list. */
584 move_direction = 1;
585 goto move;
588 /* We expect bundles when using creationTokens. */
589 if (!is_bundle(bundle->file, 1)) {
590 warning(_("file downloaded from '%s' is not a bundle"),
591 bundle->uri);
592 break;
596 if (bundle->file && !bundle->unbundled) {
598 * This was downloaded, but not successfully
599 * unbundled. Try unbundling again.
601 if (unbundle_from_file(ctx.r, bundle->file)) {
602 /* Try looking deeper in the list. */
603 move_direction = 1;
604 } else {
606 * Succeeded in unbundle. Retry bundles
607 * that previously failed to unbundle.
609 move_direction = -1;
610 bundle->unbundled = 1;
612 if (bundle->creationToken > newMaxCreationToken)
613 newMaxCreationToken = bundle->creationToken;
618 * Else case: downloaded and unbundled successfully.
619 * Skip this by moving in the same direction as the
620 * previous step.
623 move:
624 /* Move in the specified direction and repeat. */
625 cur += move_direction;
629 * We succeed if the loop terminates because 'cur' drops below
630 * zero. The other case is that we terminate because 'cur'
631 * reaches the end of the list, so we have a failure no matter
632 * which bundles we apply from the list.
634 if (cur < 0) {
635 struct strbuf value = STRBUF_INIT;
636 strbuf_addf(&value, "%"PRIu64"", newMaxCreationToken);
637 if (repo_config_set_multivar_gently(ctx.r,
638 "fetch.bundleCreationToken",
639 value.buf, NULL, 0))
640 warning(_("failed to store maximum creation token"));
642 strbuf_release(&value);
645 free(bundles.items);
646 return cur >= 0;
649 static int download_bundle_list(struct repository *r,
650 struct bundle_list *local_list,
651 struct bundle_list *global_list,
652 int depth)
654 struct bundle_list_context ctx = {
655 .r = r,
656 .list = global_list,
657 .depth = depth + 1,
658 .mode = local_list->mode,
661 return for_all_bundles_in_list(local_list, download_bundle_to_file, &ctx);
664 static int fetch_bundle_list_in_config_format(struct repository *r,
665 struct bundle_list *global_list,
666 struct remote_bundle_info *bundle,
667 int depth)
669 int result;
670 struct bundle_list list_from_bundle;
672 init_bundle_list(&list_from_bundle);
674 if ((result = bundle_uri_parse_config_format(bundle->uri,
675 bundle->file,
676 &list_from_bundle)))
677 goto cleanup;
679 if (list_from_bundle.mode == BUNDLE_MODE_NONE) {
680 warning(_("unrecognized bundle mode from URI '%s'"),
681 bundle->uri);
682 result = -1;
683 goto cleanup;
687 * If this list uses the creationToken heuristic, then the URIs
688 * it advertises are expected to be bundles, not nested lists.
689 * We can drop 'global_list' and 'depth'.
691 if (list_from_bundle.heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) {
692 result = fetch_bundles_by_token(r, &list_from_bundle);
693 global_list->heuristic = BUNDLE_HEURISTIC_CREATIONTOKEN;
694 } else if ((result = download_bundle_list(r, &list_from_bundle,
695 global_list, depth)))
696 goto cleanup;
698 cleanup:
699 clear_bundle_list(&list_from_bundle);
700 return result;
704 * This limits the recursion on fetch_bundle_uri_internal() when following
705 * bundle lists.
707 static int max_bundle_uri_depth = 4;
710 * Recursively download all bundles advertised at the given URI
711 * to files. If the file is a bundle, then add it to the given
712 * 'list'. Otherwise, expect a bundle list and recurse on the
713 * URIs in that list according to the list mode (ANY or ALL).
715 static int fetch_bundle_uri_internal(struct repository *r,
716 struct remote_bundle_info *bundle,
717 int depth,
718 struct bundle_list *list)
720 int result = 0;
721 struct remote_bundle_info *bcopy;
723 if (depth >= max_bundle_uri_depth) {
724 warning(_("exceeded bundle URI recursion limit (%d)"),
725 max_bundle_uri_depth);
726 return -1;
729 if (!bundle->file &&
730 !(bundle->file = find_temp_filename())) {
731 result = -1;
732 goto cleanup;
735 if ((result = copy_uri_to_file(bundle->file, bundle->uri))) {
736 warning(_("failed to download bundle from URI '%s'"), bundle->uri);
737 goto cleanup;
740 if ((result = !is_bundle(bundle->file, 1))) {
741 result = fetch_bundle_list_in_config_format(
742 r, list, bundle, depth);
743 if (result)
744 warning(_("file at URI '%s' is not a bundle or bundle list"),
745 bundle->uri);
746 goto cleanup;
749 /* Copy the bundle and insert it into the global list. */
750 CALLOC_ARRAY(bcopy, 1);
751 bcopy->id = xstrdup(bundle->id);
752 bcopy->file = xstrdup(bundle->file);
753 hashmap_entry_init(&bcopy->ent, strhash(bcopy->id));
754 hashmap_add(&list->bundles, &bcopy->ent);
756 cleanup:
757 if (result && bundle->file)
758 unlink(bundle->file);
759 return result;
763 * This loop iterator breaks the loop with nonzero return code on the
764 * first successful unbundling of a bundle.
766 static int attempt_unbundle(struct remote_bundle_info *info, void *data)
768 struct repository *r = data;
770 if (!info->file || info->unbundled)
771 return 0;
773 if (!unbundle_from_file(r, info->file)) {
774 info->unbundled = 1;
775 return 1;
778 return 0;
781 static int unbundle_all_bundles(struct repository *r,
782 struct bundle_list *list)
785 * Iterate through all bundles looking for ones that can
786 * successfully unbundle. If any succeed, then perhaps another
787 * will succeed in the next attempt.
789 * Keep in mind that a non-zero result for the loop here means
790 * the loop terminated early on a successful unbundling, which
791 * signals that we can try again.
793 while (for_all_bundles_in_list(list, attempt_unbundle, r)) ;
795 return 0;
798 static int unlink_bundle(struct remote_bundle_info *info, void *data UNUSED)
800 if (info->file)
801 unlink_or_warn(info->file);
802 return 0;
805 int fetch_bundle_uri(struct repository *r, const char *uri,
806 int *has_heuristic)
808 int result;
809 struct bundle_list list;
810 struct remote_bundle_info bundle = {
811 .uri = xstrdup(uri),
812 .id = xstrdup(""),
815 trace2_region_enter("fetch", "fetch-bundle-uri", the_repository);
817 init_bundle_list(&list);
820 * Do not fetch an empty bundle URI. An empty bundle URI
821 * could signal that a configured bundle URI has been disabled.
823 if (!*uri) {
824 result = 0;
825 goto cleanup;
828 /* If a bundle is added to this global list, then it is required. */
829 list.mode = BUNDLE_MODE_ALL;
831 if ((result = fetch_bundle_uri_internal(r, &bundle, 0, &list)))
832 goto cleanup;
834 result = unbundle_all_bundles(r, &list);
836 cleanup:
837 if (has_heuristic)
838 *has_heuristic = (list.heuristic != BUNDLE_HEURISTIC_NONE);
839 for_all_bundles_in_list(&list, unlink_bundle, NULL);
840 clear_bundle_list(&list);
841 clear_remote_bundle_info(&bundle, NULL);
842 trace2_region_leave("fetch", "fetch-bundle-uri", the_repository);
843 return result;
846 int fetch_bundle_list(struct repository *r, struct bundle_list *list)
848 int result;
849 struct bundle_list global_list;
852 * If the creationToken heuristic is used, then the URIs
853 * advertised by 'list' are not nested lists and instead
854 * direct bundles. We do not need to use global_list.
856 if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN)
857 return fetch_bundles_by_token(r, list);
859 init_bundle_list(&global_list);
861 /* If a bundle is added to this global list, then it is required. */
862 global_list.mode = BUNDLE_MODE_ALL;
864 if ((result = download_bundle_list(r, list, &global_list, 0)))
865 goto cleanup;
867 if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN)
868 result = fetch_bundles_by_token(r, list);
869 else
870 result = unbundle_all_bundles(r, &global_list);
872 cleanup:
873 for_all_bundles_in_list(&global_list, unlink_bundle, NULL);
874 clear_bundle_list(&global_list);
875 return result;
879 * API for serve.c.
882 int bundle_uri_advertise(struct repository *r, struct strbuf *value UNUSED)
884 static int advertise_bundle_uri = -1;
886 if (advertise_bundle_uri != -1)
887 goto cached;
889 advertise_bundle_uri = 0;
890 repo_config_get_maybe_bool(r, "uploadpack.advertisebundleuris", &advertise_bundle_uri);
892 cached:
893 return advertise_bundle_uri;
896 static int config_to_packet_line(const char *key, const char *value,
897 const struct config_context *ctx UNUSED,
898 void *data)
900 struct packet_reader *writer = data;
902 if (starts_with(key, "bundle."))
903 packet_write_fmt(writer->fd, "%s=%s", key, value);
905 return 0;
908 int bundle_uri_command(struct repository *r,
909 struct packet_reader *request)
911 struct packet_writer writer;
912 packet_writer_init(&writer, 1);
914 while (packet_reader_read(request) == PACKET_READ_NORMAL)
915 die(_("bundle-uri: unexpected argument: '%s'"), request->line);
916 if (request->status != PACKET_READ_FLUSH)
917 die(_("bundle-uri: expected flush after arguments"));
920 * Read all "bundle.*" config lines to the client as key=value
921 * packet lines.
923 repo_config(r, config_to_packet_line, &writer);
925 packet_writer_flush(&writer);
927 return 0;
931 * General API for {transport,connect}.c etc.
933 int bundle_uri_parse_line(struct bundle_list *list, const char *line)
935 int result;
936 const char *equals;
937 struct strbuf key = STRBUF_INIT;
939 if (!strlen(line))
940 return error(_("bundle-uri: got an empty line"));
942 equals = strchr(line, '=');
944 if (!equals)
945 return error(_("bundle-uri: line is not of the form 'key=value'"));
946 if (line == equals || !*(equals + 1))
947 return error(_("bundle-uri: line has empty key or value"));
949 strbuf_add(&key, line, equals - line);
950 result = bundle_list_update(key.buf, equals + 1, list);
951 strbuf_release(&key);
953 return result;