1 #include "git-compat-util.h"
2 #include "bundle-uri.h"
5 #include "environment.h"
8 #include "run-command.h"
15 enum bundle_list_heuristic heuristic
;
17 } heuristics
[BUNDLE_HEURISTIC__COUNT
] = {
18 { BUNDLE_HEURISTIC_NONE
, ""},
19 { BUNDLE_HEURISTIC_CREATIONTOKEN
, "creationToken" },
22 static int compare_bundles(const void *hashmap_cmp_fn_data UNUSED
,
23 const struct hashmap_entry
*he1
,
24 const struct hashmap_entry
*he2
,
27 const struct remote_bundle_info
*e1
=
28 container_of(he1
, const struct remote_bundle_info
, ent
);
29 const struct remote_bundle_info
*e2
=
30 container_of(he2
, const struct remote_bundle_info
, ent
);
32 return strcmp(e1
->id
, id
? (const char *)id
: e2
->id
);
35 void init_bundle_list(struct bundle_list
*list
)
37 memset(list
, 0, sizeof(*list
));
39 /* Implied defaults. */
40 list
->mode
= BUNDLE_MODE_ALL
;
43 hashmap_init(&list
->bundles
, compare_bundles
, NULL
, 0);
46 static int clear_remote_bundle_info(struct remote_bundle_info
*bundle
,
49 FREE_AND_NULL(bundle
->id
);
50 FREE_AND_NULL(bundle
->uri
);
51 FREE_AND_NULL(bundle
->file
);
52 bundle
->unbundled
= 0;
56 void clear_bundle_list(struct bundle_list
*list
)
61 for_all_bundles_in_list(list
, clear_remote_bundle_info
, NULL
);
62 hashmap_clear_and_free(&list
->bundles
, struct remote_bundle_info
, ent
);
66 int for_all_bundles_in_list(struct bundle_list
*list
,
70 struct remote_bundle_info
*info
;
71 struct hashmap_iter i
;
73 hashmap_for_each_entry(&list
->bundles
, &i
, info
, ent
) {
74 int result
= iter(info
, data
);
83 static int summarize_bundle(struct remote_bundle_info
*info
, void *data
)
86 fprintf(fp
, "[bundle \"%s\"]\n", info
->id
);
87 fprintf(fp
, "\turi = %s\n", info
->uri
);
89 if (info
->creationToken
)
90 fprintf(fp
, "\tcreationToken = %"PRIu64
"\n", info
->creationToken
);
94 void print_bundle_list(FILE *fp
, struct bundle_list
*list
)
103 case BUNDLE_MODE_ANY
:
107 case BUNDLE_MODE_NONE
:
112 fprintf(fp
, "[bundle]\n");
113 fprintf(fp
, "\tversion = %d\n", list
->version
);
114 fprintf(fp
, "\tmode = %s\n", mode
);
116 if (list
->heuristic
) {
118 for (i
= 0; i
< BUNDLE_HEURISTIC__COUNT
; i
++) {
119 if (heuristics
[i
].heuristic
== list
->heuristic
) {
120 printf("\theuristic = %s\n",
121 heuristics
[list
->heuristic
].name
);
127 for_all_bundles_in_list(list
, summarize_bundle
, fp
);
131 * Given a key-value pair, update the state of the given bundle list.
132 * Returns 0 if the key-value pair is understood. Returns -1 if the key
133 * is not understood or the value is malformed.
135 static int bundle_list_update(const char *key
, const char *value
,
136 struct bundle_list
*list
)
138 struct strbuf id
= STRBUF_INIT
;
139 struct remote_bundle_info lookup
= REMOTE_BUNDLE_INFO_INIT
;
140 struct remote_bundle_info
*bundle
;
141 const char *subsection
, *subkey
;
142 size_t subsection_len
;
144 if (parse_config_key(key
, "bundle", &subsection
, &subsection_len
, &subkey
))
147 if (!subsection_len
) {
148 if (!strcmp(subkey
, "version")) {
150 if (!git_parse_int(value
, &version
))
155 list
->version
= version
;
159 if (!strcmp(subkey
, "mode")) {
160 if (!strcmp(value
, "all"))
161 list
->mode
= BUNDLE_MODE_ALL
;
162 else if (!strcmp(value
, "any"))
163 list
->mode
= BUNDLE_MODE_ANY
;
169 if (!strcmp(subkey
, "heuristic")) {
171 for (i
= 0; i
< BUNDLE_HEURISTIC__COUNT
; i
++) {
172 if (heuristics
[i
].heuristic
&&
173 heuristics
[i
].name
&&
174 !strcmp(value
, heuristics
[i
].name
)) {
175 list
->heuristic
= heuristics
[i
].heuristic
;
180 /* Ignore unknown heuristics. */
184 /* Ignore other unknown global keys. */
188 strbuf_add(&id
, subsection
, subsection_len
);
191 * Check for an existing bundle with this <id>, or create one
195 hashmap_entry_init(&lookup
.ent
, strhash(lookup
.id
));
196 if (!(bundle
= hashmap_get_entry(&list
->bundles
, &lookup
, ent
, NULL
))) {
197 CALLOC_ARRAY(bundle
, 1);
198 bundle
->id
= strbuf_detach(&id
, NULL
);
199 hashmap_entry_init(&bundle
->ent
, strhash(bundle
->id
));
200 hashmap_add(&list
->bundles
, &bundle
->ent
);
204 if (!strcmp(subkey
, "uri")) {
207 bundle
->uri
= relative_url(list
->baseURI
, value
, NULL
);
211 if (!strcmp(subkey
, "creationtoken")) {
212 if (sscanf(value
, "%"PRIu64
, &bundle
->creationToken
) != 1)
213 warning(_("could not parse bundle list key %s with value '%s'"),
214 "creationToken", value
);
219 * At this point, we ignore any information that we don't
220 * understand, assuming it to be hints for a heuristic the client
221 * does not currently understand.
226 static int config_to_bundle_list(const char *key
, const char *value
,
227 const struct config_context
*ctx UNUSED
,
230 struct bundle_list
*list
= data
;
231 return bundle_list_update(key
, value
, list
);
234 int bundle_uri_parse_config_format(const char *uri
,
235 const char *filename
,
236 struct bundle_list
*list
)
239 struct config_options opts
= {
240 .error_action
= CONFIG_ERROR_ERROR
,
243 if (!list
->baseURI
) {
244 struct strbuf baseURI
= STRBUF_INIT
;
245 strbuf_addstr(&baseURI
, uri
);
248 * If the URI does not end with a trailing slash, then
249 * remove the filename portion of the path. This is
250 * important for relative URIs.
252 strbuf_strip_file_from_path(&baseURI
);
253 list
->baseURI
= strbuf_detach(&baseURI
, NULL
);
255 result
= git_config_from_file_with_options(config_to_bundle_list
,
257 CONFIG_SCOPE_UNKNOWN
,
260 if (!result
&& list
->mode
== BUNDLE_MODE_NONE
) {
261 warning(_("bundle list at '%s' has no mode"), uri
);
268 static char *find_temp_filename(void)
271 struct strbuf name
= STRBUF_INIT
;
273 * Find a temporary filename that is available. This is briefly
274 * racy, but unlikely to collide.
276 fd
= odb_mkstemp(&name
, "bundles/tmp_uri_XXXXXX");
278 warning(_("failed to create temporary file"));
284 return strbuf_detach(&name
, NULL
);
287 static int download_https_uri_to_file(const char *file
, const char *uri
)
290 struct child_process cp
= CHILD_PROCESS_INIT
;
291 FILE *child_in
= NULL
, *child_out
= NULL
;
292 struct strbuf line
= STRBUF_INIT
;
295 strvec_pushl(&cp
.args
, "git-remote-https", uri
, NULL
);
300 if (start_command(&cp
))
303 child_in
= fdopen(cp
.in
, "w");
309 child_out
= fdopen(cp
.out
, "r");
315 fprintf(child_in
, "capabilities\n");
318 while (!strbuf_getline(&line
, child_out
)) {
321 if (!strcmp(line
.buf
, "get"))
324 strbuf_release(&line
);
327 result
= error(_("insufficient capabilities"));
331 fprintf(child_in
, "get %s %s\n\n", uri
, file
);
336 if (finish_command(&cp
))
343 static int copy_uri_to_file(const char *filename
, const char *uri
)
347 if (starts_with(uri
, "https:") ||
348 starts_with(uri
, "http:"))
349 return download_https_uri_to_file(filename
, uri
);
351 if (skip_prefix(uri
, "file://", &out
))
355 return copy_file(filename
, uri
, 0);
358 static int unbundle_from_file(struct repository
*r
, const char *file
)
362 struct bundle_header header
= BUNDLE_HEADER_INIT
;
363 struct string_list_item
*refname
;
364 struct strbuf bundle_ref
= STRBUF_INIT
;
365 size_t bundle_prefix_len
;
367 if ((bundle_fd
= read_bundle_header(file
, &header
)) < 0)
371 * Skip the reachability walk here, since we will be adding
372 * a reachable ref pointing to the new tips, which will reach
373 * the prerequisite commits.
375 if ((result
= unbundle(r
, &header
, bundle_fd
, NULL
,
376 VERIFY_BUNDLE_QUIET
)))
380 * Convert all refs/heads/ from the bundle into refs/bundles/
381 * in the local repository.
383 strbuf_addstr(&bundle_ref
, "refs/bundles/");
384 bundle_prefix_len
= bundle_ref
.len
;
386 for_each_string_list_item(refname
, &header
.references
) {
387 struct object_id
*oid
= refname
->util
;
388 struct object_id old_oid
;
389 const char *branch_name
;
392 if (!skip_prefix(refname
->string
, "refs/heads/", &branch_name
))
395 strbuf_setlen(&bundle_ref
, bundle_prefix_len
);
396 strbuf_addstr(&bundle_ref
, branch_name
);
398 has_old
= !refs_read_ref(get_main_ref_store(the_repository
),
399 bundle_ref
.buf
, &old_oid
);
400 refs_update_ref(get_main_ref_store(the_repository
),
401 "fetched bundle", bundle_ref
.buf
, oid
,
402 has_old
? &old_oid
: NULL
,
403 REF_SKIP_OID_VERIFICATION
,
404 UPDATE_REFS_MSG_ON_ERR
);
407 bundle_header_release(&header
);
411 struct bundle_list_context
{
412 struct repository
*r
;
413 struct bundle_list
*list
;
414 enum bundle_list_mode mode
;
420 * This early definition is necessary because we use indirect recursion:
422 * While iterating through a bundle list that was downloaded as part
423 * of fetch_bundle_uri_internal(), iterator methods eventually call it
424 * again, but with depth + 1.
426 static int fetch_bundle_uri_internal(struct repository
*r
,
427 struct remote_bundle_info
*bundle
,
429 struct bundle_list
*list
);
431 static int download_bundle_to_file(struct remote_bundle_info
*bundle
, void *data
)
434 struct bundle_list_context
*ctx
= data
;
436 if (ctx
->mode
== BUNDLE_MODE_ANY
&& ctx
->count
)
439 res
= fetch_bundle_uri_internal(ctx
->r
, bundle
, ctx
->depth
+ 1, ctx
->list
);
442 * Only increment count if the download succeeded. If our mode is
443 * BUNDLE_MODE_ANY, then we will want to try other URIs in the
444 * list in case they work instead.
450 * To be opportunistic as possible, we continue iterating and
451 * download as many bundles as we can, so we can apply the ones
452 * that work, even in BUNDLE_MODE_ALL mode.
457 struct bundles_for_sorting
{
458 struct remote_bundle_info
**items
;
463 static int append_bundle(struct remote_bundle_info
*bundle
, void *data
)
465 struct bundles_for_sorting
*list
= data
;
466 list
->items
[list
->nr
++] = bundle
;
471 * For use in QSORT() to get a list sorted by creationToken
472 * in decreasing order.
474 static int compare_creation_token_decreasing(const void *va
, const void *vb
)
476 const struct remote_bundle_info
* const *a
= va
;
477 const struct remote_bundle_info
* const *b
= vb
;
479 if ((*a
)->creationToken
> (*b
)->creationToken
)
481 if ((*a
)->creationToken
< (*b
)->creationToken
)
486 static int fetch_bundles_by_token(struct repository
*r
,
487 struct bundle_list
*list
)
490 int move_direction
= 0;
491 const char *creationTokenStr
;
492 uint64_t maxCreationToken
= 0, newMaxCreationToken
= 0;
493 struct bundle_list_context ctx
= {
498 struct bundles_for_sorting bundles
= {
499 .alloc
= hashmap_get_size(&list
->bundles
),
502 ALLOC_ARRAY(bundles
.items
, bundles
.alloc
);
504 for_all_bundles_in_list(list
, append_bundle
, &bundles
);
511 QSORT(bundles
.items
, bundles
.nr
, compare_creation_token_decreasing
);
514 * If fetch.bundleCreationToken exists, parses to a uint64t, and
515 * is not strictly smaller than the maximum creation token in the
516 * bundle list, then do not download any bundles.
518 if (!repo_config_get_value(r
,
519 "fetch.bundlecreationtoken",
520 &creationTokenStr
) &&
521 sscanf(creationTokenStr
, "%"PRIu64
, &maxCreationToken
) == 1 &&
522 bundles
.items
[0]->creationToken
<= maxCreationToken
) {
528 * Attempt to download and unbundle the minimum number of bundles by
529 * creationToken in decreasing order. If we fail to unbundle (after
530 * a successful download) then move to the next non-downloaded bundle
531 * and attempt downloading. Once we succeed in applying a bundle,
532 * move to the previous unapplied bundle and attempt to unbundle it
535 * In the case of a fresh clone, we will likely download all of the
536 * bundles before successfully unbundling the oldest one, then the
537 * rest of the bundles unbundle successfully in increasing order
540 * If there are existing objects, then this process may terminate
541 * early when all required commits from "new" bundles exist in the
542 * repo's object store.
545 while (cur
>= 0 && cur
< bundles
.nr
) {
546 struct remote_bundle_info
*bundle
= bundles
.items
[cur
];
549 * If we need to dig into bundles below the previous
550 * creation token value, then likely we are in an erroneous
551 * state due to missing or invalid bundles. Halt the process
552 * instead of continuing to download extra data.
554 if (bundle
->creationToken
<= maxCreationToken
)
559 * Not downloaded yet. Try downloading.
561 * Note that bundle->file is non-NULL if a download
562 * was attempted, even if it failed to download.
564 if (fetch_bundle_uri_internal(ctx
.r
, bundle
, ctx
.depth
+ 1, ctx
.list
)) {
565 /* Mark as unbundled so we do not retry. */
566 bundle
->unbundled
= 1;
568 /* Try looking deeper in the list. */
573 /* We expect bundles when using creationTokens. */
574 if (!is_bundle(bundle
->file
, 1)) {
575 warning(_("file downloaded from '%s' is not a bundle"),
581 if (bundle
->file
&& !bundle
->unbundled
) {
583 * This was downloaded, but not successfully
584 * unbundled. Try unbundling again.
586 if (unbundle_from_file(ctx
.r
, bundle
->file
)) {
587 /* Try looking deeper in the list. */
591 * Succeeded in unbundle. Retry bundles
592 * that previously failed to unbundle.
595 bundle
->unbundled
= 1;
597 if (bundle
->creationToken
> newMaxCreationToken
)
598 newMaxCreationToken
= bundle
->creationToken
;
603 * Else case: downloaded and unbundled successfully.
604 * Skip this by moving in the same direction as the
609 /* Move in the specified direction and repeat. */
610 cur
+= move_direction
;
614 * We succeed if the loop terminates because 'cur' drops below
615 * zero. The other case is that we terminate because 'cur'
616 * reaches the end of the list, so we have a failure no matter
617 * which bundles we apply from the list.
620 struct strbuf value
= STRBUF_INIT
;
621 strbuf_addf(&value
, "%"PRIu64
"", newMaxCreationToken
);
622 if (repo_config_set_multivar_gently(ctx
.r
,
623 "fetch.bundleCreationToken",
625 warning(_("failed to store maximum creation token"));
627 strbuf_release(&value
);
634 static int download_bundle_list(struct repository
*r
,
635 struct bundle_list
*local_list
,
636 struct bundle_list
*global_list
,
639 struct bundle_list_context ctx
= {
643 .mode
= local_list
->mode
,
646 return for_all_bundles_in_list(local_list
, download_bundle_to_file
, &ctx
);
649 static int fetch_bundle_list_in_config_format(struct repository
*r
,
650 struct bundle_list
*global_list
,
651 struct remote_bundle_info
*bundle
,
655 struct bundle_list list_from_bundle
;
657 init_bundle_list(&list_from_bundle
);
659 if ((result
= bundle_uri_parse_config_format(bundle
->uri
,
664 if (list_from_bundle
.mode
== BUNDLE_MODE_NONE
) {
665 warning(_("unrecognized bundle mode from URI '%s'"),
672 * If this list uses the creationToken heuristic, then the URIs
673 * it advertises are expected to be bundles, not nested lists.
674 * We can drop 'global_list' and 'depth'.
676 if (list_from_bundle
.heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
) {
677 result
= fetch_bundles_by_token(r
, &list_from_bundle
);
678 global_list
->heuristic
= BUNDLE_HEURISTIC_CREATIONTOKEN
;
679 } else if ((result
= download_bundle_list(r
, &list_from_bundle
,
680 global_list
, depth
)))
684 clear_bundle_list(&list_from_bundle
);
689 * This limits the recursion on fetch_bundle_uri_internal() when following
692 static int max_bundle_uri_depth
= 4;
695 * Recursively download all bundles advertised at the given URI
696 * to files. If the file is a bundle, then add it to the given
697 * 'list'. Otherwise, expect a bundle list and recurse on the
698 * URIs in that list according to the list mode (ANY or ALL).
700 static int fetch_bundle_uri_internal(struct repository
*r
,
701 struct remote_bundle_info
*bundle
,
703 struct bundle_list
*list
)
706 struct remote_bundle_info
*bcopy
;
708 if (depth
>= max_bundle_uri_depth
) {
709 warning(_("exceeded bundle URI recursion limit (%d)"),
710 max_bundle_uri_depth
);
715 !(bundle
->file
= find_temp_filename())) {
720 if ((result
= copy_uri_to_file(bundle
->file
, bundle
->uri
))) {
721 warning(_("failed to download bundle from URI '%s'"), bundle
->uri
);
725 if ((result
= !is_bundle(bundle
->file
, 1))) {
726 result
= fetch_bundle_list_in_config_format(
727 r
, list
, bundle
, depth
);
729 warning(_("file at URI '%s' is not a bundle or bundle list"),
734 /* Copy the bundle and insert it into the global list. */
735 CALLOC_ARRAY(bcopy
, 1);
736 bcopy
->id
= xstrdup(bundle
->id
);
737 bcopy
->file
= xstrdup(bundle
->file
);
738 hashmap_entry_init(&bcopy
->ent
, strhash(bcopy
->id
));
739 hashmap_add(&list
->bundles
, &bcopy
->ent
);
742 if (result
&& bundle
->file
)
743 unlink(bundle
->file
);
748 * This loop iterator breaks the loop with nonzero return code on the
749 * first successful unbundling of a bundle.
751 static int attempt_unbundle(struct remote_bundle_info
*info
, void *data
)
753 struct repository
*r
= data
;
755 if (!info
->file
|| info
->unbundled
)
758 if (!unbundle_from_file(r
, info
->file
)) {
766 static int unbundle_all_bundles(struct repository
*r
,
767 struct bundle_list
*list
)
770 * Iterate through all bundles looking for ones that can
771 * successfully unbundle. If any succeed, then perhaps another
772 * will succeed in the next attempt.
774 * Keep in mind that a non-zero result for the loop here means
775 * the loop terminated early on a successful unbundling, which
776 * signals that we can try again.
778 while (for_all_bundles_in_list(list
, attempt_unbundle
, r
)) ;
783 static int unlink_bundle(struct remote_bundle_info
*info
, void *data UNUSED
)
786 unlink_or_warn(info
->file
);
790 int fetch_bundle_uri(struct repository
*r
, const char *uri
,
794 struct bundle_list list
;
795 struct remote_bundle_info bundle
= {
800 init_bundle_list(&list
);
803 * Do not fetch an empty bundle URI. An empty bundle URI
804 * could signal that a configured bundle URI has been disabled.
811 /* If a bundle is added to this global list, then it is required. */
812 list
.mode
= BUNDLE_MODE_ALL
;
814 if ((result
= fetch_bundle_uri_internal(r
, &bundle
, 0, &list
)))
817 result
= unbundle_all_bundles(r
, &list
);
821 *has_heuristic
= (list
.heuristic
!= BUNDLE_HEURISTIC_NONE
);
822 for_all_bundles_in_list(&list
, unlink_bundle
, NULL
);
823 clear_bundle_list(&list
);
824 clear_remote_bundle_info(&bundle
, NULL
);
828 int fetch_bundle_list(struct repository
*r
, struct bundle_list
*list
)
831 struct bundle_list global_list
;
834 * If the creationToken heuristic is used, then the URIs
835 * advertised by 'list' are not nested lists and instead
836 * direct bundles. We do not need to use global_list.
838 if (list
->heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
)
839 return fetch_bundles_by_token(r
, list
);
841 init_bundle_list(&global_list
);
843 /* If a bundle is added to this global list, then it is required. */
844 global_list
.mode
= BUNDLE_MODE_ALL
;
846 if ((result
= download_bundle_list(r
, list
, &global_list
, 0)))
849 if (list
->heuristic
== BUNDLE_HEURISTIC_CREATIONTOKEN
)
850 result
= fetch_bundles_by_token(r
, list
);
852 result
= unbundle_all_bundles(r
, &global_list
);
855 for_all_bundles_in_list(&global_list
, unlink_bundle
, NULL
);
856 clear_bundle_list(&global_list
);
864 int bundle_uri_advertise(struct repository
*r
, struct strbuf
*value UNUSED
)
866 static int advertise_bundle_uri
= -1;
868 if (advertise_bundle_uri
!= -1)
871 advertise_bundle_uri
= 0;
872 repo_config_get_maybe_bool(r
, "uploadpack.advertisebundleuris", &advertise_bundle_uri
);
875 return advertise_bundle_uri
;
878 static int config_to_packet_line(const char *key
, const char *value
,
879 const struct config_context
*ctx UNUSED
,
882 struct packet_reader
*writer
= data
;
884 if (starts_with(key
, "bundle."))
885 packet_write_fmt(writer
->fd
, "%s=%s", key
, value
);
890 int bundle_uri_command(struct repository
*r
,
891 struct packet_reader
*request
)
893 struct packet_writer writer
;
894 packet_writer_init(&writer
, 1);
896 while (packet_reader_read(request
) == PACKET_READ_NORMAL
)
897 die(_("bundle-uri: unexpected argument: '%s'"), request
->line
);
898 if (request
->status
!= PACKET_READ_FLUSH
)
899 die(_("bundle-uri: expected flush after arguments"));
902 * Read all "bundle.*" config lines to the client as key=value
905 repo_config(r
, config_to_packet_line
, &writer
);
907 packet_writer_flush(&writer
);
913 * General API for {transport,connect}.c etc.
915 int bundle_uri_parse_line(struct bundle_list
*list
, const char *line
)
919 struct strbuf key
= STRBUF_INIT
;
922 return error(_("bundle-uri: got an empty line"));
924 equals
= strchr(line
, '=');
927 return error(_("bundle-uri: line is not of the form 'key=value'"));
928 if (line
== equals
|| !*(equals
+ 1))
929 return error(_("bundle-uri: line has empty key or value"));
931 strbuf_add(&key
, line
, equals
- line
);
932 result
= bundle_list_update(key
.buf
, equals
+ 1, list
);
933 strbuf_release(&key
);