| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "gettext.h" |
| 5 | #include "hex.h" |
| 6 | #include "odb.h" |
| 7 | #include "run-command.h" |
| 8 | #include "sigchain.h" |
| 9 | #include "connected.h" |
| 10 | #include "transport.h" |
| 11 | #include "packfile.h" |
| 12 | #include "promisor-remote.h" |
| 13 | |
| 14 | static int promised_object_cb(const struct object_id *oid UNUSED, |
| 15 | struct object_info *oi UNUSED, |
| 16 | void *payload) |
| 17 | { |
| 18 | bool *found = payload; |
| 19 | *found = true; |
| 20 | return 1; |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * For partial clones, we don't want to have to do a regular connectivity check |
| 25 | * because we have to enumerate and exclude all promisor objects (slow), and |
| 26 | * then the connectivity check itself becomes a no-op because in a partial |
| 27 | * clone every object is a promisor object. Instead, just make sure we |
| 28 | * received, in a promisor packfile, the objects pointed to by each wanted ref. |
| 29 | * |
| 30 | * Before checking for promisor packs, be sure we have the latest pack-files |
| 31 | * loaded into memory. |
| 32 | * |
| 33 | * Returns 1 when all object IDs have been found in promisor packs, in which |
| 34 | * case we're fully connected and thus done. Returns 0 when we have found |
| 35 | * objects in non-promisor packs, in which case we'll have to fall back to the |
| 36 | * rev-list-based connectivity checks. Returns a negative error code on error. |
| 37 | */ |
| 38 | static int check_connected_promisor(oid_iterate_fn fn, |
| 39 | void *cb_data, |
| 40 | const struct object_id **oid) |
| 41 | { |
| 42 | struct odb_for_each_object_options opts = { |
| 43 | .flags = ODB_FOR_EACH_OBJECT_PROMISOR_ONLY, |
| 44 | .prefix_hex_len = the_repository->hash_algo->hexsz, |
| 45 | }; |
| 46 | int err; |
| 47 | |
| 48 | odb_reprepare(the_repository->objects); |
| 49 | do { |
| 50 | bool found = false; |
| 51 | |
| 52 | opts.prefix = *oid; |
| 53 | |
| 54 | err = odb_for_each_object_ext(the_repository->objects, NULL, |
| 55 | promised_object_cb, &found, &opts); |
| 56 | if (err < 0) |
| 57 | return err; |
| 58 | |
| 59 | /* |
| 60 | * We have found an object that is not part of a promisor pack, |
| 61 | * and thus we cannot skip the full connectivity check. |
| 62 | */ |
| 63 | if (!found) |
| 64 | return 0; |
| 65 | } while ((*oid = fn(cb_data)) != NULL); |
| 66 | |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * If we feed all the commits we want to verify to this command |
| 72 | * |
| 73 | * $ git rev-list --objects --stdin --not --all |
| 74 | * |
| 75 | * and if it does not error out, that means everything reachable from |
| 76 | * these commits locally exists and is connected to our existing refs. |
| 77 | * Note that this does _not_ validate the individual objects. |
| 78 | * |
| 79 | * Returns 0 if everything is connected, non-zero otherwise. |
| 80 | */ |
| 81 | int check_connected(oid_iterate_fn fn, void *cb_data, |
| 82 | struct check_connected_options *opt) |
| 83 | { |
| 84 | struct child_process rev_list = CHILD_PROCESS_INIT; |
| 85 | FILE *rev_list_in; |
| 86 | struct check_connected_options defaults = CHECK_CONNECTED_INIT; |
| 87 | const struct object_id *oid; |
| 88 | int err = 0; |
| 89 | struct packed_git *new_pack = NULL; |
| 90 | struct transport *transport; |
| 91 | size_t base_len; |
| 92 | |
| 93 | if (!opt) |
| 94 | opt = &defaults; |
| 95 | transport = opt->transport; |
| 96 | |
| 97 | oid = fn(cb_data); |
| 98 | if (!oid) { |
| 99 | if (opt->err_fd) |
| 100 | close(opt->err_fd); |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | if (repo_has_promisor_remote(the_repository)) { |
| 105 | err = check_connected_promisor(fn, cb_data, &oid); |
| 106 | if (err) { |
| 107 | if (opt->err_fd) |
| 108 | close(opt->err_fd); |
| 109 | if (err > 0) |
| 110 | err = 0; |
| 111 | return err; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (opt->shallow_file) { |
| 116 | strvec_push(&rev_list.args, "--shallow-file"); |
| 117 | strvec_push(&rev_list.args, opt->shallow_file); |
| 118 | } |
| 119 | strvec_push(&rev_list.args,"rev-list"); |
| 120 | strvec_push(&rev_list.args, "--objects"); |
| 121 | strvec_push(&rev_list.args, "--stdin"); |
| 122 | if (repo_has_promisor_remote(the_repository)) |
| 123 | strvec_push(&rev_list.args, "--exclude-promisor-objects"); |
| 124 | if (!opt->is_deepening_fetch) { |
| 125 | strvec_push(&rev_list.args, "--not"); |
| 126 | if (opt->exclude_hidden_refs_section) |
| 127 | strvec_pushf(&rev_list.args, "--exclude-hidden=%s", |
| 128 | opt->exclude_hidden_refs_section); |
| 129 | strvec_push(&rev_list.args, "--all"); |
| 130 | } |
| 131 | strvec_push(&rev_list.args, "--quiet"); |
| 132 | strvec_push(&rev_list.args, "--alternate-refs"); |
| 133 | if (opt->progress) |
| 134 | strvec_pushf(&rev_list.args, "--progress=%s", |
| 135 | _("Checking connectivity")); |
| 136 | |
| 137 | rev_list.git_cmd = 1; |
| 138 | if (opt->env) |
| 139 | strvec_pushv(&rev_list.env, opt->env); |
| 140 | rev_list.in = -1; |
| 141 | rev_list.no_stdout = 1; |
| 142 | if (opt->err_fd) |
| 143 | rev_list.err = opt->err_fd; |
| 144 | else |
| 145 | rev_list.no_stderr = opt->quiet; |
| 146 | |
| 147 | if (start_command(&rev_list)) |
| 148 | return error(_("Could not run 'git rev-list'")); |
| 149 | |
| 150 | sigchain_push(SIGPIPE, SIG_IGN); |
| 151 | |
| 152 | rev_list_in = xfdopen(rev_list.in, "w"); |
| 153 | |
| 154 | if (transport && transport->smart_options && |
| 155 | transport->smart_options->self_contained_and_connected && |
| 156 | transport->pack_lockfiles.nr == 1 && |
| 157 | strip_suffix(transport->pack_lockfiles.items[0].string, |
| 158 | ".keep", &base_len)) { |
| 159 | struct strbuf idx_file = STRBUF_INIT; |
| 160 | strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string, |
| 161 | base_len); |
| 162 | strbuf_addstr(&idx_file, ".idx"); |
| 163 | new_pack = add_packed_git(the_repository, idx_file.buf, |
| 164 | idx_file.len, 1); |
| 165 | strbuf_release(&idx_file); |
| 166 | } |
| 167 | |
| 168 | do { |
| 169 | /* |
| 170 | * If index-pack already checked that: |
| 171 | * - there are no dangling pointers in the new pack |
| 172 | * - the pack is self contained |
| 173 | * Then if the updated ref is in the new pack, then we |
| 174 | * are sure the ref is good and not sending it to |
| 175 | * rev-list for verification. |
| 176 | */ |
| 177 | if (new_pack && find_pack_entry_one(oid, new_pack)) |
| 178 | continue; |
| 179 | |
| 180 | if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0) |
| 181 | break; |
| 182 | } while ((oid = fn(cb_data)) != NULL); |
| 183 | |
| 184 | if (ferror(rev_list_in) || fflush(rev_list_in)) { |
| 185 | if (errno != EPIPE && errno != EINVAL) |
| 186 | error_errno(_("failed write to rev-list")); |
| 187 | err = -1; |
| 188 | } |
| 189 | |
| 190 | if (fclose(rev_list_in)) |
| 191 | err = error_errno(_("failed to close rev-list's stdin")); |
| 192 | |
| 193 | sigchain_pop(SIGPIPE); |
| 194 | if (new_pack) { |
| 195 | close_pack(new_pack); |
| 196 | free(new_pack); |
| 197 | } |
| 198 | return finish_command(&rev_list) || err; |
| 199 | } |