2 #include "bundle-uri.h"
4 #include "object-store.h"
6 #include "run-command.h"
11 static int compare_bundles(const void *hashmap_cmp_fn_data
,
12 const struct hashmap_entry
*he1
,
13 const struct hashmap_entry
*he2
,
16 const struct remote_bundle_info
*e1
=
17 container_of(he1
, const struct remote_bundle_info
, ent
);
18 const struct remote_bundle_info
*e2
=
19 container_of(he2
, const struct remote_bundle_info
, ent
);
21 return strcmp(e1
->id
, id
? (const char *)id
: e2
->id
);
24 void init_bundle_list(struct bundle_list
*list
)
26 memset(list
, 0, sizeof(*list
));
28 /* Implied defaults. */
29 list
->mode
= BUNDLE_MODE_ALL
;
32 hashmap_init(&list
->bundles
, compare_bundles
, NULL
, 0);
35 static int clear_remote_bundle_info(struct remote_bundle_info
*bundle
,
38 FREE_AND_NULL(bundle
->id
);
39 FREE_AND_NULL(bundle
->uri
);
40 FREE_AND_NULL(bundle
->file
);
41 bundle
->unbundled
= 0;
45 void clear_bundle_list(struct bundle_list
*list
)
50 for_all_bundles_in_list(list
, clear_remote_bundle_info
, NULL
);
51 hashmap_clear_and_free(&list
->bundles
, struct remote_bundle_info
, ent
);
54 int for_all_bundles_in_list(struct bundle_list
*list
,
58 struct remote_bundle_info
*info
;
59 struct hashmap_iter i
;
61 hashmap_for_each_entry(&list
->bundles
, &i
, info
, ent
) {
62 int result
= iter(info
, data
);
71 static int summarize_bundle(struct remote_bundle_info
*info
, void *data
)
74 fprintf(fp
, "[bundle \"%s\"]\n", info
->id
);
75 fprintf(fp
, "\turi = %s\n", info
->uri
);
79 void print_bundle_list(FILE *fp
, struct bundle_list
*list
)
92 case BUNDLE_MODE_NONE
:
97 fprintf(fp
, "[bundle]\n");
98 fprintf(fp
, "\tversion = %d\n", list
->version
);
99 fprintf(fp
, "\tmode = %s\n", mode
);
101 for_all_bundles_in_list(list
, summarize_bundle
, fp
);
105 * Given a key-value pair, update the state of the given bundle list.
106 * Returns 0 if the key-value pair is understood. Returns -1 if the key
107 * is not understood or the value is malformed.
109 static int bundle_list_update(const char *key
, const char *value
,
110 struct bundle_list
*list
)
112 struct strbuf id
= STRBUF_INIT
;
113 struct remote_bundle_info lookup
= REMOTE_BUNDLE_INFO_INIT
;
114 struct remote_bundle_info
*bundle
;
115 const char *subsection
, *subkey
;
116 size_t subsection_len
;
118 if (parse_config_key(key
, "bundle", &subsection
, &subsection_len
, &subkey
))
121 if (!subsection_len
) {
122 if (!strcmp(subkey
, "version")) {
124 if (!git_parse_int(value
, &version
))
129 list
->version
= version
;
133 if (!strcmp(subkey
, "mode")) {
134 if (!strcmp(value
, "all"))
135 list
->mode
= BUNDLE_MODE_ALL
;
136 else if (!strcmp(value
, "any"))
137 list
->mode
= BUNDLE_MODE_ANY
;
143 /* Ignore other unknown global keys. */
147 strbuf_add(&id
, subsection
, subsection_len
);
150 * Check for an existing bundle with this <id>, or create one
154 hashmap_entry_init(&lookup
.ent
, strhash(lookup
.id
));
155 if (!(bundle
= hashmap_get_entry(&list
->bundles
, &lookup
, ent
, NULL
))) {
156 CALLOC_ARRAY(bundle
, 1);
157 bundle
->id
= strbuf_detach(&id
, NULL
);
158 hashmap_entry_init(&bundle
->ent
, strhash(bundle
->id
));
159 hashmap_add(&list
->bundles
, &bundle
->ent
);
163 if (!strcmp(subkey
, "uri")) {
166 bundle
->uri
= xstrdup(value
);
171 * At this point, we ignore any information that we don't
172 * understand, assuming it to be hints for a heuristic the client
173 * does not currently understand.
178 static int config_to_bundle_list(const char *key
, const char *value
, void *data
)
180 struct bundle_list
*list
= data
;
181 return bundle_list_update(key
, value
, list
);
184 int bundle_uri_parse_config_format(const char *uri
,
185 const char *filename
,
186 struct bundle_list
*list
)
189 struct config_options opts
= {
190 .error_action
= CONFIG_ERROR_ERROR
,
193 result
= git_config_from_file_with_options(config_to_bundle_list
,
197 if (!result
&& list
->mode
== BUNDLE_MODE_NONE
) {
198 warning(_("bundle list at '%s' has no mode"), uri
);
205 static char *find_temp_filename(void)
208 struct strbuf name
= STRBUF_INIT
;
210 * Find a temporary filename that is available. This is briefly
211 * racy, but unlikely to collide.
213 fd
= odb_mkstemp(&name
, "bundles/tmp_uri_XXXXXX");
215 warning(_("failed to create temporary file"));
221 return strbuf_detach(&name
, NULL
);
224 static int download_https_uri_to_file(const char *file
, const char *uri
)
227 struct child_process cp
= CHILD_PROCESS_INIT
;
228 FILE *child_in
= NULL
, *child_out
= NULL
;
229 struct strbuf line
= STRBUF_INIT
;
232 strvec_pushl(&cp
.args
, "git-remote-https", uri
, NULL
);
236 if (start_command(&cp
))
239 child_in
= fdopen(cp
.in
, "w");
245 child_out
= fdopen(cp
.out
, "r");
251 fprintf(child_in
, "capabilities\n");
254 while (!strbuf_getline(&line
, child_out
)) {
257 if (!strcmp(line
.buf
, "get"))
260 strbuf_release(&line
);
263 result
= error(_("insufficient capabilities"));
267 fprintf(child_in
, "get %s %s\n\n", uri
, file
);
272 if (finish_command(&cp
))
279 static int copy_uri_to_file(const char *filename
, const char *uri
)
283 if (starts_with(uri
, "https:") ||
284 starts_with(uri
, "http:"))
285 return download_https_uri_to_file(filename
, uri
);
287 if (skip_prefix(uri
, "file://", &out
))
291 return copy_file(filename
, uri
, 0);
294 static int unbundle_from_file(struct repository
*r
, const char *file
)
298 struct bundle_header header
= BUNDLE_HEADER_INIT
;
299 struct string_list_item
*refname
;
300 struct strbuf bundle_ref
= STRBUF_INIT
;
301 size_t bundle_prefix_len
;
303 if ((bundle_fd
= read_bundle_header(file
, &header
)) < 0)
307 * Skip the reachability walk here, since we will be adding
308 * a reachable ref pointing to the new tips, which will reach
309 * the prerequisite commits.
311 if ((result
= unbundle(r
, &header
, bundle_fd
, NULL
, 0)))
315 * Convert all refs/heads/ from the bundle into refs/bundles/
316 * in the local repository.
318 strbuf_addstr(&bundle_ref
, "refs/bundles/");
319 bundle_prefix_len
= bundle_ref
.len
;
321 for_each_string_list_item(refname
, &header
.references
) {
322 struct object_id
*oid
= refname
->util
;
323 struct object_id old_oid
;
324 const char *branch_name
;
327 if (!skip_prefix(refname
->string
, "refs/heads/", &branch_name
))
330 strbuf_setlen(&bundle_ref
, bundle_prefix_len
);
331 strbuf_addstr(&bundle_ref
, branch_name
);
333 has_old
= !read_ref(bundle_ref
.buf
, &old_oid
);
334 update_ref("fetched bundle", bundle_ref
.buf
, oid
,
335 has_old
? &old_oid
: NULL
,
336 REF_SKIP_OID_VERIFICATION
,
337 UPDATE_REFS_MSG_ON_ERR
);
340 bundle_header_release(&header
);
344 struct bundle_list_context
{
345 struct repository
*r
;
346 struct bundle_list
*list
;
347 enum bundle_list_mode mode
;
353 * This early definition is necessary because we use indirect recursion:
355 * While iterating through a bundle list that was downloaded as part
356 * of fetch_bundle_uri_internal(), iterator methods eventually call it
357 * again, but with depth + 1.
359 static int fetch_bundle_uri_internal(struct repository
*r
,
360 struct remote_bundle_info
*bundle
,
362 struct bundle_list
*list
);
364 static int download_bundle_to_file(struct remote_bundle_info
*bundle
, void *data
)
367 struct bundle_list_context
*ctx
= data
;
369 if (ctx
->mode
== BUNDLE_MODE_ANY
&& ctx
->count
)
372 res
= fetch_bundle_uri_internal(ctx
->r
, bundle
, ctx
->depth
+ 1, ctx
->list
);
375 * Only increment count if the download succeeded. If our mode is
376 * BUNDLE_MODE_ANY, then we will want to try other URIs in the
377 * list in case they work instead.
383 * To be opportunistic as possible, we continue iterating and
384 * download as many bundles as we can, so we can apply the ones
385 * that work, even in BUNDLE_MODE_ALL mode.
390 static int download_bundle_list(struct repository
*r
,
391 struct bundle_list
*local_list
,
392 struct bundle_list
*global_list
,
395 struct bundle_list_context ctx
= {
399 .mode
= local_list
->mode
,
402 return for_all_bundles_in_list(local_list
, download_bundle_to_file
, &ctx
);
405 static int fetch_bundle_list_in_config_format(struct repository
*r
,
406 struct bundle_list
*global_list
,
407 struct remote_bundle_info
*bundle
,
411 struct bundle_list list_from_bundle
;
413 init_bundle_list(&list_from_bundle
);
415 if ((result
= bundle_uri_parse_config_format(bundle
->uri
,
420 if (list_from_bundle
.mode
== BUNDLE_MODE_NONE
) {
421 warning(_("unrecognized bundle mode from URI '%s'"),
427 if ((result
= download_bundle_list(r
, &list_from_bundle
,
428 global_list
, depth
)))
432 clear_bundle_list(&list_from_bundle
);
437 * This limits the recursion on fetch_bundle_uri_internal() when following
440 static int max_bundle_uri_depth
= 4;
443 * Recursively download all bundles advertised at the given URI
444 * to files. If the file is a bundle, then add it to the given
445 * 'list'. Otherwise, expect a bundle list and recurse on the
446 * URIs in that list according to the list mode (ANY or ALL).
448 static int fetch_bundle_uri_internal(struct repository
*r
,
449 struct remote_bundle_info
*bundle
,
451 struct bundle_list
*list
)
454 struct remote_bundle_info
*bcopy
;
456 if (depth
>= max_bundle_uri_depth
) {
457 warning(_("exceeded bundle URI recursion limit (%d)"),
458 max_bundle_uri_depth
);
463 !(bundle
->file
= find_temp_filename())) {
468 if ((result
= copy_uri_to_file(bundle
->file
, bundle
->uri
))) {
469 warning(_("failed to download bundle from URI '%s'"), bundle
->uri
);
473 if ((result
= !is_bundle(bundle
->file
, 1))) {
474 result
= fetch_bundle_list_in_config_format(
475 r
, list
, bundle
, depth
);
477 warning(_("file at URI '%s' is not a bundle or bundle list"),
482 /* Copy the bundle and insert it into the global list. */
483 CALLOC_ARRAY(bcopy
, 1);
484 bcopy
->id
= xstrdup(bundle
->id
);
485 bcopy
->file
= xstrdup(bundle
->file
);
486 hashmap_entry_init(&bcopy
->ent
, strhash(bcopy
->id
));
487 hashmap_add(&list
->bundles
, &bcopy
->ent
);
490 if (result
&& bundle
->file
)
491 unlink(bundle
->file
);
496 * This loop iterator breaks the loop with nonzero return code on the
497 * first successful unbundling of a bundle.
499 static int attempt_unbundle(struct remote_bundle_info
*info
, void *data
)
501 struct repository
*r
= data
;
503 if (!info
->file
|| info
->unbundled
)
506 if (!unbundle_from_file(r
, info
->file
)) {
514 static int unbundle_all_bundles(struct repository
*r
,
515 struct bundle_list
*list
)
518 * Iterate through all bundles looking for ones that can
519 * successfully unbundle. If any succeed, then perhaps another
520 * will succeed in the next attempt.
522 * Keep in mind that a non-zero result for the loop here means
523 * the loop terminated early on a successful unbundling, which
524 * signals that we can try again.
526 while (for_all_bundles_in_list(list
, attempt_unbundle
, r
)) ;
531 static int unlink_bundle(struct remote_bundle_info
*info
, void *data
)
534 unlink_or_warn(info
->file
);
538 int fetch_bundle_uri(struct repository
*r
, const char *uri
)
541 struct bundle_list list
;
542 struct remote_bundle_info bundle
= {
547 init_bundle_list(&list
);
549 /* If a bundle is added to this global list, then it is required. */
550 list
.mode
= BUNDLE_MODE_ALL
;
552 if ((result
= fetch_bundle_uri_internal(r
, &bundle
, 0, &list
)))
555 result
= unbundle_all_bundles(r
, &list
);
558 for_all_bundles_in_list(&list
, unlink_bundle
, NULL
);
559 clear_bundle_list(&list
);
560 clear_remote_bundle_info(&bundle
, NULL
);
565 * General API for {transport,connect}.c etc.
567 int bundle_uri_parse_line(struct bundle_list
*list
, const char *line
)
571 struct strbuf key
= STRBUF_INIT
;
574 return error(_("bundle-uri: got an empty line"));
576 equals
= strchr(line
, '=');
579 return error(_("bundle-uri: line is not of the form 'key=value'"));
580 if (line
== equals
|| !*(equals
+ 1))
581 return error(_("bundle-uri: line has empty key or value"));
583 strbuf_add(&key
, line
, equals
- line
);
584 result
= bundle_list_update(key
.buf
, equals
+ 1, list
);
585 strbuf_release(&key
);