1 #include "git-compat-util.h"
2 #include "bundle-uri.h"
5 #include "environment.h"
7 #include "object-store-ll.h"
9 #include "run-command.h"
16 enum bundle_list_heuristic heuristic
;
18 } heuristics
[BUNDLE_HEURISTIC__COUNT
] = {
19 { BUNDLE_HEURISTIC_NONE
, ""},
20 { BUNDLE_HEURISTIC_CREATIONTOKEN
, "creationToken" },
23 static int compare_bundles(const void *hashmap_cmp_fn_data UNUSED
,
24 const struct hashmap_entry
*he1
,
25 const struct hashmap_entry
*he2
,
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
);
33 return strcmp(e1
->id
, id
? (const char *)id
: e2
->id
);
36 void init_bundle_list(struct bundle_list
*list
)
38 memset(list
, 0, sizeof(*list
));
40 /* Implied defaults. */
41 list
->mode
= BUNDLE_MODE_ALL
;
44 hashmap_init(&list
->bundles
, compare_bundles
, NULL
, 0);
47 static int clear_remote_bundle_info(struct remote_bundle_info
*bundle
,
50 FREE_AND_NULL(bundle
->id
);
51 FREE_AND_NULL(bundle
->uri
);
52 FREE_AND_NULL(bundle
->file
);
53 bundle
->unbundled
= 0;
57 void clear_bundle_list(struct bundle_list
*list
)
62 for_all_bundles_in_list(list
, clear_remote_bundle_info
, NULL
);
63 hashmap_clear_and_free(&list
->bundles
, struct remote_bundle_info
, ent
);
67 int for_all_bundles_in_list(struct bundle_list
*list
,
71 struct remote_bundle_info
*info
;
72 struct hashmap_iter i
;
74 hashmap_for_each_entry(&list
->bundles
, &i
, info
, ent
) {
75 int result
= iter(info
, data
);
84 static int summarize_bundle(struct remote_bundle_info
*info
, void *data
)
87 fprintf(fp
, "[bundle \"%s\"]\n", info
->id
);
88 fprintf(fp
, "\turi = %s\n", info
->uri
);
90 if (info
->creationToken
)
91 fprintf(fp
, "\tcreationToken = %"PRIu64
"\n", info
->creationToken
);
95 void print_bundle_list(FILE *fp
, struct bundle_list
*list
)
100 case BUNDLE_MODE_ALL
:
104 case BUNDLE_MODE_ANY
:
108 case BUNDLE_MODE_NONE
:
113 fprintf(fp
, "[bundle]\n");
114 fprintf(fp
, "\tversion = %d\n", list
->version
);
115 fprintf(fp
, "\tmode = %s\n", mode
);
117 if (list
->heuristic
) {
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
);
128 for_all_bundles_in_list(list
, summarize_bundle
, fp
);
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.
136 static int bundle_list_update(const char *key
, const char *value
,
137 struct bundle_list
*list
)
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
;
145 if (parse_config_key(key
, "bundle", &subsection
, &subsection_len
, &subkey
))
148 if (!subsection_len
) {
149 if (!strcmp(subkey
, "version")) {
151 if (!git_parse_int(value
, &version
))
156 list
->version
= version
;
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
;
170 if (!strcmp(subkey
, "heuristic")) {
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
;
181 /* Ignore unknown heuristics. */
185 /* Ignore other unknown global keys. */
189 strbuf_add(&id
, subsection
, subsection_len
);
192 * Check for an existing bundle with this <id>, or create one
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
);
205 if (!strcmp(subkey
, "uri")) {
208 bundle
->uri
= relative_url(list
->baseURI
, value
, NULL
);
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
);
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.
227 static int config_to_bundle_list(const char *key
, const char *value
,
228 const struct config_context
*ctx UNUSED
,
231 struct bundle_list
*list
= data
;
232 return bundle_list_update(key
, value
, list
);
235 int bundle_uri_parse_config_format(const char *uri
,
236 const char *filename
,
237 struct bundle_list
*list
)
240 struct config_options opts
= {
241 .error_action
= CONFIG_ERROR_ERROR
,
244 if (!list
->baseURI
) {
245 struct strbuf baseURI
= STRBUF_INIT
;
246 strbuf_addstr(&baseURI
, uri
);
249 * If the URI does not end with a trailing slash, then
250 * remove the filename portion of the path. This is
251 * important for relative URIs.
253 strbuf_strip_file_from_path(&baseURI
);
254 list
->baseURI
= strbuf_detach(&baseURI
, NULL
);
256 result
= git_config_from_file_with_options(config_to_bundle_list
,
258 CONFIG_SCOPE_UNKNOWN
,
261 if (!result
&& list
->mode
== BUNDLE_MODE_NONE
) {
262 warning(_("bundle list at '%s' has no mode"), uri
);
269 static char *find_temp_filename(void)
272 struct strbuf name
= STRBUF_INIT
;
274 * Find a temporary filename that is available. This is briefly
275 * racy, but unlikely to collide.
277 fd
= odb_mkstemp(&name
, "bundles/tmp_uri_XXXXXX");
279 warning(_("failed to create temporary file"));
285 return strbuf_detach(&name
, NULL
);
288 static int download_https_uri_to_file(const char *file
, const char *uri
)
291 struct child_process cp
= CHILD_PROCESS_INIT
;
292 FILE *child_in
= NULL
, *child_out
= NULL
;
293 struct strbuf line
= STRBUF_INIT
;
296 strvec_pushl(&cp
.args
, "git-remote-https", uri
, NULL
);
301 if (start_command(&cp
))
304 child_in
= fdopen(cp
.in
, "w");
310 child_out
= fdopen(cp
.out
, "r");
316 fprintf(child_in
, "capabilities\n");
319 while (!strbuf_getline(&line
, child_out
)) {
322 if (!strcmp(line
.buf
, "get"))
325 strbuf_release(&line
);
328 result
= error(_("insufficient capabilities"));
332 fprintf(child_in
, "get %s %s\n\n", uri
, file
);
337 if (finish_command(&cp
))
344 static int copy_uri_to_file(const char *filename
, const char *uri
)
348 if (starts_with(uri
, "https:") ||
349 starts_with(uri
, "http:"))
350 return download_https_uri_to_file(filename
, uri
);
352 if (skip_prefix(uri
, "file://", &out
))
356 return copy_file(filename
, uri
, 0);
359 static int unbundle_from_file(struct repository
*r
, const char *file
)
363 struct bundle_header header
= BUNDLE_HEADER_INIT
;
364 struct string_list_item
*refname
;
365 struct strbuf bundle_ref
= STRBUF_INIT
;
366 size_t bundle_prefix_len
;
368 if ((bundle_fd
= read_bundle_header(file
, &header
)) < 0)
372 * Skip the reachability walk here, since we will be adding
373 * a reachable ref pointing to the new tips, which will reach
374 * the prerequisite commits.
376 if ((result
= unbundle(r
, &header
, bundle_fd
, NULL
,
377 VERIFY_BUNDLE_QUIET
)))
381 * Convert all refs/heads/ from the bundle into refs/bundles/
382 * in the local repository.
384 strbuf_addstr(&bundle_ref
, "refs/bundles/");
385 bundle_prefix_len
= bundle_ref
.len
;
387 for_each_string_list_item(refname
, &header
.references
) {
388 struct object_id
*oid
= refname
->util
;
389 struct object_id old_oid
;
390 const char *branch_name
;
393 if (!skip_prefix(refname
->string
, "refs/heads/", &branch_name
))
396 strbuf_setlen(&bundle_ref
, bundle_prefix_len
);
397 strbuf_addstr(&bundle_ref
, branch_name
);
399 has_old
= !read_ref(bundle_ref
.buf
, &old_oid
);
400 update_ref("fetched bundle", bundle_ref
.buf
, oid
,
401 has_old
? &old_oid
: NULL
,
402 REF_SKIP_OID_VERIFICATION
,
403 UPDATE_REFS_MSG_ON_ERR
);
406 bundle_header_release(&header
);
410 struct bundle_list_context
{
411 struct repository
*r
;
412 struct bundle_list
*list
;
413 enum bundle_list_mode mode
;
419 * This early definition is necessary because we use indirect recursion:
421 * While iterating through a bundle list that was downloaded as part
422 * of fetch_bundle_uri_internal(), iterator methods eventually call it
423 * again, but with depth + 1.
425 static int fetch_bundle_uri_internal(struct repository
*r
,
426 struct remote_bundle_info
*bundle
,
428 struct bundle_list
*list
);
430 static int download_bundle_to_file(struct remote_bundle_info
*bundle
, void *data
)
433 struct bundle_list_context
*ctx
= data
;
435 if (ctx
->mode
== BUNDLE_MODE_ANY
&& ctx
->count
)
438 res
= fetch_bundle_uri_internal(ctx
->r
, bundle
, ctx
->depth
+ 1, ctx
->list
);
441 * Only increment count if the download succeeded. If our mode is
442 * BUNDLE_MODE_ANY, then we will want to try other URIs in the
443 * list in case they work instead.
449 * To be opportunistic as possible, we continue iterating and
450 * download as many bundles as we can, so we can apply the ones
451 * that work, even in BUNDLE_MODE_ALL mode.
456 struct bundles_for_sorting
{
457 struct remote_bundle_info
**items
;
462 static int append_bundle(struct remote_bundle_info
*bundle
, void *data
)
464 struct bundles_for_sorting
*list
= data
;
465 list
->items
[list
->nr
++] = bundle
;
470 * For use in QSORT() to get a list sorted by creationToken
471 * in decreasing order.
473 static int compare_creation_token_decreasing(const void *va
, const void *vb
)
475 const struct remote_bundle_info
* const *a
= va
;
476 const struct remote_bundle_info
* const *b
= vb
;
478 if ((*a
)->creationToken
> (*b
)->creationToken
)
480 if ((*a
)->creationToken
< (*b
)->creationToken
)
485 static int fetch_bundles_by_token(struct repository
*r
,
486 struct bundle_list
*list
)
489 int move_direction
= 0;
490 const char *creationTokenStr
;
491 uint64_t maxCreationToken
= 0, newMaxCreationToken
= 0;
492 struct bundle_list_context ctx
= {
497 struct bundles_for_sorting bundles
= {
498 .alloc
= hashmap_get_size(&list
->bundles
),
501 ALLOC_ARRAY(bundles
.items
, bundles
.alloc
);
503 for_all_bundles_in_list(list
, append_bundle
, &bundles
);
510 QSORT(bundles
.items
, bundles
.nr
, compare_creation_token_decreasing
);
513 * If fetch.bundleCreationToken exists, parses to a uint64t, and
514 * is not strictly smaller than the maximum creation token in the
515 * bundle list, then do not download any bundles.
517 if (!repo_config_get_value(r
,
518 "fetch.bundlecreationtoken",
519 &creationTokenStr
) &&
520 sscanf(creationTokenStr
, "%"PRIu64
, &maxCreationToken
) == 1 &&
521 bundles
.items
[0]->creationToken
<= maxCreationToken
) {
527 * Attempt to download and unbundle the minimum number of bundles by
528 * creationToken in decreasing order. If we fail to unbundle (after
529 * a successful download) then move to the next non-downloaded bundle
530 * and attempt downloading. Once we succeed in applying a bundle,
531 * move to the previous unapplied bundle and attempt to unbundle it
534 * In the case of a fresh clone, we will likely download all of the
535 * bundles before successfully unbundling the oldest one, then the
536 * rest of the bundles unbundle successfully in increasing order
539 * If there are existing objects, then this process may terminate
540 * early when all required commits from "new" bundles exist in the
541 * repo's object store.
544 while (cur
>= 0 && cur
< bundles
.nr
) {
545 struct remote_bundle_info
*bundle
= bundles
.items
[cur
];
548 * If we need to dig into bundles below the previous
549 * creation token value, then likely we are in an erroneous
550 * state due to missing or invalid bundles. Halt the process
551 * instead of continuing to download extra data.
553 if (bundle
->creationToken
<= maxCreationToken
)
558 * Not downloaded yet. Try downloading.
560 * Note that bundle->file is non-NULL if a download
561 * was attempted, even if it failed to download.
563 if (fetch_bundle_uri_internal(ctx
.r
, bundle
, ctx
.depth
+ 1, ctx
.list
)) {
564 /* Mark as unbundled so we do not retry. */
565 bundle
->unbundled
= 1;
567 /* Try looking deeper in the list. */
572 /* We expect bundles when using creationTokens. */
573 if (!is_bundle(bundle
->file
, 1)) {
574 warning(_("file downloaded from '%s' is not a bundle"),
580 if (bundle
->file
&& !bundle
->unbundled
) {
582 * This was downloaded, but not successfully
583 * unbundled. Try unbundling again.
585 if (unbundle_from_file(ctx
.r
, bundle
->file
)) {
586 /* Try looking deeper in the list. */
590 * Succeeded in unbundle. Retry bundles
591 * that previously failed to unbundle.
594 bundle
->unbundled
= 1;
596 if (bundle
->creationToken
> newMaxCreationToken
)
597 newMaxCreationToken
= bundle
->creationToken
;
602 * Else case: downloaded and unbundled successfully.
603 * Skip this by moving in the same direction as the
608 /* Move in the specified direction and repeat. */
609 cur
+= move_direction
;
613 * We succeed if the loop terminates because 'cur' drops below
614 * zero. The other case is that we terminate because 'cur'
615 * reaches the end of the list, so we have a failure no matter
616 * which bundles we apply from the list.
619 struct strbuf value
= STRBUF_INIT
;
620 strbuf_addf(&value
, "%"PRIu64
"", newMaxCreationToken
);
621 if (repo_config_set_multivar_gently(ctx
.r
,
622 "fetch.bundleCreationToken",
624 warning(_("failed to store maximum creation token"));
626 strbuf_release(&value
);
633 static int download_bundle_list(struct repository
*r
,
634 struct bundle_list
*local_list
,
635 struct bundle_list
*global_list
,
638 struct bundle_list_context ctx
= {
642 .mode
= local_list
->mode
,
645 return for_all_bundles_in_list(local_list
, download_bundle_to_file
, &ctx
);
648 static int fetch_bundle_list_in_config_format(struct repository
*r
,
649 struct bundle_list
*global_list
,
650 struct remote_bundle_info
*bundle
,
654 struct bundle_list list_from_bundle
;
656 init_bundle_list(&list_from_bundle
);
658 if ((result
= bundle_uri_parse_config_format(bundle
->uri
,
663 if (list_from_bundle
.mode
== BUNDLE_MODE_NONE
) {
664 warning(_("unrecognized bundle mode from URI '%s'"),
671 * If this list uses the creationToken heuristic, then the URIs
672 * it advertises are expected to be bundles, not nested lists.
673 * We can drop 'global_list' and 'depth'.
675 if (list_from_bundle
.heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
) {
676 result
= fetch_bundles_by_token(r
, &list_from_bundle
);
677 global_list
->heuristic
= BUNDLE_HEURISTIC_CREATIONTOKEN
;
678 } else if ((result
= download_bundle_list(r
, &list_from_bundle
,
679 global_list
, depth
)))
683 clear_bundle_list(&list_from_bundle
);
688 * This limits the recursion on fetch_bundle_uri_internal() when following
691 static int max_bundle_uri_depth
= 4;
694 * Recursively download all bundles advertised at the given URI
695 * to files. If the file is a bundle, then add it to the given
696 * 'list'. Otherwise, expect a bundle list and recurse on the
697 * URIs in that list according to the list mode (ANY or ALL).
699 static int fetch_bundle_uri_internal(struct repository
*r
,
700 struct remote_bundle_info
*bundle
,
702 struct bundle_list
*list
)
705 struct remote_bundle_info
*bcopy
;
707 if (depth
>= max_bundle_uri_depth
) {
708 warning(_("exceeded bundle URI recursion limit (%d)"),
709 max_bundle_uri_depth
);
714 !(bundle
->file
= find_temp_filename())) {
719 if ((result
= copy_uri_to_file(bundle
->file
, bundle
->uri
))) {
720 warning(_("failed to download bundle from URI '%s'"), bundle
->uri
);
724 if ((result
= !is_bundle(bundle
->file
, 1))) {
725 result
= fetch_bundle_list_in_config_format(
726 r
, list
, bundle
, depth
);
728 warning(_("file at URI '%s' is not a bundle or bundle list"),
733 /* Copy the bundle and insert it into the global list. */
734 CALLOC_ARRAY(bcopy
, 1);
735 bcopy
->id
= xstrdup(bundle
->id
);
736 bcopy
->file
= xstrdup(bundle
->file
);
737 hashmap_entry_init(&bcopy
->ent
, strhash(bcopy
->id
));
738 hashmap_add(&list
->bundles
, &bcopy
->ent
);
741 if (result
&& bundle
->file
)
742 unlink(bundle
->file
);
747 * This loop iterator breaks the loop with nonzero return code on the
748 * first successful unbundling of a bundle.
750 static int attempt_unbundle(struct remote_bundle_info
*info
, void *data
)
752 struct repository
*r
= data
;
754 if (!info
->file
|| info
->unbundled
)
757 if (!unbundle_from_file(r
, info
->file
)) {
765 static int unbundle_all_bundles(struct repository
*r
,
766 struct bundle_list
*list
)
769 * Iterate through all bundles looking for ones that can
770 * successfully unbundle. If any succeed, then perhaps another
771 * will succeed in the next attempt.
773 * Keep in mind that a non-zero result for the loop here means
774 * the loop terminated early on a successful unbundling, which
775 * signals that we can try again.
777 while (for_all_bundles_in_list(list
, attempt_unbundle
, r
)) ;
782 static int unlink_bundle(struct remote_bundle_info
*info
, void *data UNUSED
)
785 unlink_or_warn(info
->file
);
789 int fetch_bundle_uri(struct repository
*r
, const char *uri
,
793 struct bundle_list list
;
794 struct remote_bundle_info bundle
= {
799 init_bundle_list(&list
);
802 * Do not fetch an empty bundle URI. An empty bundle URI
803 * could signal that a configured bundle URI has been disabled.
810 /* If a bundle is added to this global list, then it is required. */
811 list
.mode
= BUNDLE_MODE_ALL
;
813 if ((result
= fetch_bundle_uri_internal(r
, &bundle
, 0, &list
)))
816 result
= unbundle_all_bundles(r
, &list
);
820 *has_heuristic
= (list
.heuristic
!= BUNDLE_HEURISTIC_NONE
);
821 for_all_bundles_in_list(&list
, unlink_bundle
, NULL
);
822 clear_bundle_list(&list
);
823 clear_remote_bundle_info(&bundle
, NULL
);
827 int fetch_bundle_list(struct repository
*r
, struct bundle_list
*list
)
830 struct bundle_list global_list
;
833 * If the creationToken heuristic is used, then the URIs
834 * advertised by 'list' are not nested lists and instead
835 * direct bundles. We do not need to use global_list.
837 if (list
->heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
)
838 return fetch_bundles_by_token(r
, list
);
840 init_bundle_list(&global_list
);
842 /* If a bundle is added to this global list, then it is required. */
843 global_list
.mode
= BUNDLE_MODE_ALL
;
845 if ((result
= download_bundle_list(r
, list
, &global_list
, 0)))
848 if (list
->heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
)
849 result
= fetch_bundles_by_token(r
, list
);
851 result
= unbundle_all_bundles(r
, &global_list
);
854 for_all_bundles_in_list(&global_list
, unlink_bundle
, NULL
);
855 clear_bundle_list(&global_list
);
863 int bundle_uri_advertise(struct repository
*r
, struct strbuf
*value UNUSED
)
865 static int advertise_bundle_uri
= -1;
867 if (advertise_bundle_uri
!= -1)
870 advertise_bundle_uri
= 0;
871 repo_config_get_maybe_bool(r
, "uploadpack.advertisebundleuris", &advertise_bundle_uri
);
874 return advertise_bundle_uri
;
877 static int config_to_packet_line(const char *key
, const char *value
,
878 const struct config_context
*ctx UNUSED
,
881 struct packet_reader
*writer
= data
;
883 if (starts_with(key
, "bundle."))
884 packet_write_fmt(writer
->fd
, "%s=%s", key
, value
);
889 int bundle_uri_command(struct repository
*r
,
890 struct packet_reader
*request
)
892 struct packet_writer writer
;
893 packet_writer_init(&writer
, 1);
895 while (packet_reader_read(request
) == PACKET_READ_NORMAL
)
896 die(_("bundle-uri: unexpected argument: '%s'"), request
->line
);
897 if (request
->status
!= PACKET_READ_FLUSH
)
898 die(_("bundle-uri: expected flush after arguments"));
901 * Read all "bundle.*" config lines to the client as key=value
904 repo_config(r
, config_to_packet_line
, &writer
);
906 packet_writer_flush(&writer
);
912 * General API for {transport,connect}.c etc.
914 int bundle_uri_parse_line(struct bundle_list
*list
, const char *line
)
918 struct strbuf key
= STRBUF_INIT
;
921 return error(_("bundle-uri: got an empty line"));
923 equals
= strchr(line
, '=');
926 return error(_("bundle-uri: line is not of the form 'key=value'"));
927 if (line
== equals
|| !*(equals
+ 1))
928 return error(_("bundle-uri: line has empty key or value"));
930 strbuf_add(&key
, line
, equals
- line
);
931 result
= bundle_list_update(key
.buf
, equals
+ 1, list
);
932 strbuf_release(&key
);