|
| 1 | +#include "cache.h" |
| 2 | +#include "bundle-uri.h" |
| 3 | +#include "bundle.h" |
| 4 | +#include "config.h" |
| 5 | +#include "object-store.h" |
| 6 | +#include "refs.h" |
| 7 | +#include "run-command.h" |
| 8 | + |
| 9 | +static void find_temp_filename(struct strbuf *name) |
| 10 | +{ |
| 11 | + int fd; |
| 12 | + /* |
| 13 | + * Find a temporray filename that is available. This is briefly |
| 14 | + * racy, but unlikely to collide. |
| 15 | + */ |
| 16 | + fd = odb_mkstemp(name, "bundles/tmp_uri_XXXXXX"); |
| 17 | + if (fd < 0) |
| 18 | + die(_("failed to create temporary file")); |
| 19 | + close(fd); |
| 20 | + unlink(name->buf); |
| 21 | +} |
| 22 | + |
| 23 | +static int download_https_uri_to_file(const char *uri, const char *file) |
| 24 | +{ |
| 25 | + int result = 0; |
| 26 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 27 | + FILE *child_in = NULL, *child_out = NULL; |
| 28 | + struct strbuf line = STRBUF_INIT; |
| 29 | + int found_get = 0; |
| 30 | + |
| 31 | + strvec_pushl(&cp.args, "git-remote-https", "origin", uri, NULL); |
| 32 | + cp.in = -1; |
| 33 | + cp.out = -1; |
| 34 | + |
| 35 | + if (start_command(&cp)) |
| 36 | + return 1; |
| 37 | + |
| 38 | + child_in = fdopen(cp.in, "w"); |
| 39 | + if (!child_in) { |
| 40 | + result = 1; |
| 41 | + goto cleanup; |
| 42 | + } |
| 43 | + |
| 44 | + child_out = fdopen(cp.out, "r"); |
| 45 | + if (!child_out) { |
| 46 | + result = 1; |
| 47 | + goto cleanup; |
| 48 | + } |
| 49 | + |
| 50 | + fprintf(child_in, "capabilities\n"); |
| 51 | + fflush(child_in); |
| 52 | + |
| 53 | + while (!strbuf_getline(&line, child_out)) { |
| 54 | + if (!line.len) |
| 55 | + break; |
| 56 | + if (!strcmp(line.buf, "get")) |
| 57 | + found_get = 1; |
| 58 | + } |
| 59 | + strbuf_release(&line); |
| 60 | + |
| 61 | + if (!found_get) { |
| 62 | + result = error(_("insufficient capabilities")); |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + |
| 66 | + fprintf(child_in, "get %s %s\n\n", uri, file); |
| 67 | + |
| 68 | +cleanup: |
| 69 | + if (child_in) |
| 70 | + fclose(child_in); |
| 71 | + if (finish_command(&cp)) |
| 72 | + return 1; |
| 73 | + if (child_out) |
| 74 | + fclose(child_out); |
| 75 | + return result; |
| 76 | +} |
| 77 | + |
| 78 | +static int copy_uri_to_file(const char *uri, const char *file) |
| 79 | +{ |
| 80 | + const char *out; |
| 81 | + if (skip_prefix(uri, "https:", &out) || |
| 82 | + skip_prefix(uri, "http:", &out)) |
| 83 | + return download_https_uri_to_file(uri, file); |
| 84 | + |
| 85 | + if (!skip_prefix(uri, "file://", &out)) |
| 86 | + out = uri; |
| 87 | + |
| 88 | + /* Copy as a file */ |
| 89 | + return !!copy_file(file, out, 0); |
| 90 | +} |
| 91 | + |
| 92 | +static int unbundle_from_file(struct repository *r, const char *file) |
| 93 | +{ |
| 94 | + int result = 0; |
| 95 | + int bundle_fd; |
| 96 | + struct bundle_header header = BUNDLE_HEADER_INIT; |
| 97 | + struct strvec extra_index_pack_args = STRVEC_INIT; |
| 98 | + struct string_list_item *refname; |
| 99 | + struct strbuf bundle_ref = STRBUF_INIT; |
| 100 | + size_t bundle_prefix_len; |
| 101 | + |
| 102 | + if ((bundle_fd = read_bundle_header(file, &header)) < 0) |
| 103 | + return 1; |
| 104 | + |
| 105 | + result = unbundle(r, &header, bundle_fd, &extra_index_pack_args); |
| 106 | + |
| 107 | + /* |
| 108 | + * Convert all refs/heads/ from the bundle into refs/bundles/ |
| 109 | + * in the local repository. |
| 110 | + */ |
| 111 | + strbuf_addstr(&bundle_ref, "refs/bundles/"); |
| 112 | + bundle_prefix_len = bundle_ref.len; |
| 113 | + |
| 114 | + for_each_string_list_item(refname, &header.references) { |
| 115 | + struct object_id *oid = refname->util; |
| 116 | + struct object_id old_oid; |
| 117 | + const char *branch_name; |
| 118 | + int has_old; |
| 119 | + |
| 120 | + if (!skip_prefix(refname->string, "refs/heads/", &branch_name)) |
| 121 | + continue; |
| 122 | + |
| 123 | + strbuf_setlen(&bundle_ref, bundle_prefix_len); |
| 124 | + strbuf_addstr(&bundle_ref, branch_name); |
| 125 | + |
| 126 | + has_old = !read_ref(bundle_ref.buf, &old_oid); |
| 127 | + update_ref("fetched bundle", bundle_ref.buf, oid, |
| 128 | + has_old ? &old_oid : NULL, |
| 129 | + REF_SKIP_OID_VERIFICATION, |
| 130 | + UPDATE_REFS_MSG_ON_ERR); |
| 131 | + } |
| 132 | + |
| 133 | + bundle_header_release(&header); |
| 134 | + return result; |
| 135 | +} |
| 136 | + |
| 137 | +int fetch_bundle_uri(struct repository *r, const char *uri) |
| 138 | +{ |
| 139 | + int result = 0; |
| 140 | + struct strbuf filename = STRBUF_INIT; |
| 141 | + |
| 142 | + find_temp_filename(&filename); |
| 143 | + if ((result = copy_uri_to_file(uri, filename.buf))) { |
| 144 | + error(_("failed to download bundle from URI '%s'"), uri); |
| 145 | + goto cleanup; |
| 146 | + } |
| 147 | + |
| 148 | + if ((result = !is_bundle(filename.buf, 0))) { |
| 149 | + error(_("file at URI '%s' is not a bundle"), uri); |
| 150 | + goto cleanup; |
| 151 | + } |
| 152 | + |
| 153 | + if ((result = unbundle_from_file(r, filename.buf))) |
| 154 | + goto cleanup; |
| 155 | + |
| 156 | + git_config_set_multivar_gently("log.excludedecoration", |
| 157 | + "refs/bundle/", |
| 158 | + "refs/bundle/", |
| 159 | + CONFIG_FLAGS_FIXED_VALUE | |
| 160 | + CONFIG_FLAGS_MULTI_REPLACE); |
| 161 | + |
| 162 | +cleanup: |
| 163 | + unlink(filename.buf); |
| 164 | + strbuf_release(&filename); |
| 165 | + return result; |
| 166 | +} |
0 commit comments