| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 1 | #include "cache.h" |
| Josh Steadmon | dfa33a2 | 2019-04-19 21:00:13 | [diff] [blame] | 2 | #include "object-store.h" |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 3 | #include "run-command.h" |
| 4 | #include "sigchain.h" |
| 5 | #include "connected.h" |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 6 | #include "transport.h" |
| Jonathan Tan | 0abe14f | 2017-08-18 22:20:26 | [diff] [blame] | 7 | #include "packfile.h" |
| Christian Couder | b14ed5a | 2019-06-25 13:40:31 | [diff] [blame] | 8 | #include "promisor-remote.h" |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * If we feed all the commits we want to verify to this command |
| 12 | * |
| Junio C Hamano | d21c463 | 2012-03-15 21:57:02 | [diff] [blame] | 13 | * $ git rev-list --objects --stdin --not --all |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 14 | * |
| 15 | * and if it does not error out, that means everything reachable from |
| Junio C Hamano | d21c463 | 2012-03-15 21:57:02 | [diff] [blame] | 16 | * these commits locally exists and is connected to our existing refs. |
| 17 | * Note that this does _not_ validate the individual objects. |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 18 | * |
| 19 | * Returns 0 if everything is connected, non-zero otherwise. |
| 20 | */ |
| brian m. carlson | 6ccac9e | 2017-10-15 22:06:54 | [diff] [blame] | 21 | int check_connected(oid_iterate_fn fn, void *cb_data, |
| Jeff King | 7043c70 | 2016-07-15 10:30:40 | [diff] [blame] | 22 | struct check_connected_options *opt) |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 23 | { |
| René Scharfe | d318027 | 2014-08-19 19:09:35 | [diff] [blame] | 24 | struct child_process rev_list = CHILD_PROCESS_INIT; |
| René Scharfe | 24b75fa | 2020-08-12 16:52:49 | [diff] [blame] | 25 | FILE *rev_list_in; |
| Jeff King | 7043c70 | 2016-07-15 10:30:40 | [diff] [blame] | 26 | struct check_connected_options defaults = CHECK_CONNECTED_INIT; |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 27 | const struct object_id *oid; |
| Jeff King | 3be89f9 | 2016-07-15 10:28:32 | [diff] [blame] | 28 | int err = 0; |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 29 | struct packed_git *new_pack = NULL; |
| Jeff King | 7043c70 | 2016-07-15 10:30:40 | [diff] [blame] | 30 | struct transport *transport; |
| Jeff King | 26936bf | 2014-06-30 16:58:51 | [diff] [blame] | 31 | size_t base_len; |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 32 | |
| Jeff King | 7043c70 | 2016-07-15 10:30:40 | [diff] [blame] | 33 | if (!opt) |
| 34 | opt = &defaults; |
| 35 | transport = opt->transport; |
| 36 | |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 37 | oid = fn(cb_data); |
| 38 | if (!oid) { |
| Jeff King | e033184 | 2016-07-15 10:32:03 | [diff] [blame] | 39 | if (opt->err_fd) |
| 40 | close(opt->err_fd); |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 41 | return err; |
| Jeff King | e033184 | 2016-07-15 10:32:03 | [diff] [blame] | 42 | } |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 43 | |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 44 | if (transport && transport->smart_options && |
| 45 | transport->smart_options->self_contained_and_connected && |
| Jonathan Tan | 9da69a6 | 2020-06-10 20:57:22 | [diff] [blame] | 46 | transport->pack_lockfiles.nr == 1 && |
| 47 | strip_suffix(transport->pack_lockfiles.items[0].string, |
| 48 | ".keep", &base_len)) { |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 49 | struct strbuf idx_file = STRBUF_INIT; |
| Jonathan Tan | 9da69a6 | 2020-06-10 20:57:22 | [diff] [blame] | 50 | strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string, |
| 51 | base_len); |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 52 | strbuf_addstr(&idx_file, ".idx"); |
| 53 | new_pack = add_packed_git(idx_file.buf, idx_file.len, 1); |
| 54 | strbuf_release(&idx_file); |
| 55 | } |
| 56 | |
| Jonathan Tan | 2b98478 | 2020-03-20 22:00:45 | [diff] [blame] | 57 | if (has_promisor_remote()) { |
| Josh Steadmon | dfa33a2 | 2019-04-19 21:00:13 | [diff] [blame] | 58 | /* |
| 59 | * For partial clones, we don't want to have to do a regular |
| 60 | * connectivity check because we have to enumerate and exclude |
| 61 | * all promisor objects (slow), and then the connectivity check |
| 62 | * itself becomes a no-op because in a partial clone every |
| 63 | * object is a promisor object. Instead, just make sure we |
| Jonathan Tan | 5003377 | 2020-01-12 04:15:24 | [diff] [blame] | 64 | * received, in a promisor packfile, the objects pointed to by |
| 65 | * each wanted ref. |
| Derrick Stolee | b739d97 | 2020-03-13 21:11:55 | [diff] [blame] | 66 | * |
| 67 | * Before checking for promisor packs, be sure we have the |
| 68 | * latest pack-files loaded into memory. |
| Josh Steadmon | dfa33a2 | 2019-04-19 21:00:13 | [diff] [blame] | 69 | */ |
| Derrick Stolee | b739d97 | 2020-03-13 21:11:55 | [diff] [blame] | 70 | reprepare_packed_git(the_repository); |
| Josh Steadmon | dfa33a2 | 2019-04-19 21:00:13 | [diff] [blame] | 71 | do { |
| Jonathan Tan | 5003377 | 2020-01-12 04:15:24 | [diff] [blame] | 72 | struct packed_git *p; |
| 73 | |
| 74 | for (p = get_all_packs(the_repository); p; p = p->next) { |
| 75 | if (!p->pack_promisor) |
| 76 | continue; |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 77 | if (find_pack_entry_one(oid->hash, p)) |
| Jonathan Tan | 5003377 | 2020-01-12 04:15:24 | [diff] [blame] | 78 | goto promisor_pack_found; |
| 79 | } |
| Jonathan Tan | 2b98478 | 2020-03-20 22:00:45 | [diff] [blame] | 80 | /* |
| 81 | * Fallback to rev-list with oid and the rest of the |
| 82 | * object IDs provided by fn. |
| 83 | */ |
| 84 | goto no_promisor_pack_found; |
| Jonathan Tan | 5003377 | 2020-01-12 04:15:24 | [diff] [blame] | 85 | promisor_pack_found: |
| 86 | ; |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 87 | } while ((oid = fn(cb_data)) != NULL); |
| Josh Steadmon | dfa33a2 | 2019-04-19 21:00:13 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| Jonathan Tan | 2b98478 | 2020-03-20 22:00:45 | [diff] [blame] | 91 | no_promisor_pack_found: |
| Jeff King | 7043c70 | 2016-07-15 10:30:40 | [diff] [blame] | 92 | if (opt->shallow_file) { |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 93 | strvec_push(&rev_list.args, "--shallow-file"); |
| 94 | strvec_push(&rev_list.args, opt->shallow_file); |
| Nguyễn Thái Ngọc Duy | 614db3e | 2013-12-05 13:02:46 | [diff] [blame] | 95 | } |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 96 | strvec_push(&rev_list.args,"rev-list"); |
| 97 | strvec_push(&rev_list.args, "--objects"); |
| 98 | strvec_push(&rev_list.args, "--stdin"); |
| Christian Couder | b14ed5a | 2019-06-25 13:40:31 | [diff] [blame] | 99 | if (has_promisor_remote()) |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 100 | strvec_push(&rev_list.args, "--exclude-promisor-objects"); |
| Jonathan Tan | cf1e7c0 | 2018-07-02 22:08:43 | [diff] [blame] | 101 | if (!opt->is_deepening_fetch) { |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 102 | strvec_push(&rev_list.args, "--not"); |
| 103 | strvec_push(&rev_list.args, "--all"); |
| Jonathan Tan | cf1e7c0 | 2018-07-02 22:08:43 | [diff] [blame] | 104 | } |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 105 | strvec_push(&rev_list.args, "--quiet"); |
| 106 | strvec_push(&rev_list.args, "--alternate-refs"); |
| Jeff King | 70d5e2d | 2016-07-15 10:32:28 | [diff] [blame] | 107 | if (opt->progress) |
| Jeff King | ef8d7ac | 2020-07-28 20:24:53 | [diff] [blame] | 108 | strvec_pushf(&rev_list.args, "--progress=%s", |
| Jeff King | f6d8942 | 2020-07-28 20:26:31 | [diff] [blame] | 109 | _("Checking connectivity")); |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 110 | |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 111 | rev_list.git_cmd = 1; |
| Ævar Arnfjörð Bjarmason | c7c4bde | 2021-11-25 22:52:24 | [diff] [blame] | 112 | if (opt->env) |
| 113 | strvec_pushv(&rev_list.env_array, opt->env); |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 114 | rev_list.in = -1; |
| 115 | rev_list.no_stdout = 1; |
| Jeff King | e033184 | 2016-07-15 10:32:03 | [diff] [blame] | 116 | if (opt->err_fd) |
| 117 | rev_list.err = opt->err_fd; |
| 118 | else |
| 119 | rev_list.no_stderr = opt->quiet; |
| 120 | |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 121 | if (start_command(&rev_list)) |
| 122 | return error(_("Could not run 'git rev-list'")); |
| 123 | |
| 124 | sigchain_push(SIGPIPE, SIG_IGN); |
| 125 | |
| René Scharfe | 24b75fa | 2020-08-12 16:52:49 | [diff] [blame] | 126 | rev_list_in = xfdopen(rev_list.in, "w"); |
| 127 | |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 128 | do { |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 129 | /* |
| 130 | * If index-pack already checked that: |
| 131 | * - there are no dangling pointers in the new pack |
| 132 | * - the pack is self contained |
| 133 | * Then if the updated ref is in the new pack, then we |
| 134 | * are sure the ref is good and not sending it to |
| 135 | * rev-list for verification. |
| 136 | */ |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 137 | if (new_pack && find_pack_entry_one(oid->hash, new_pack)) |
| Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 01:16:17 | [diff] [blame] | 138 | continue; |
| 139 | |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 140 | if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0) |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 141 | break; |
| Patrick Steinhardt | 9fec7b2 | 2021-09-01 13:09:50 | [diff] [blame] | 142 | } while ((oid = fn(cb_data)) != NULL); |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 143 | |
| René Scharfe | 24b75fa | 2020-08-12 16:52:49 | [diff] [blame] | 144 | if (ferror(rev_list_in) || fflush(rev_list_in)) { |
| 145 | if (errno != EPIPE && errno != EINVAL) |
| 146 | error_errno(_("failed write to rev-list")); |
| 147 | err = -1; |
| 148 | } |
| 149 | |
| 150 | if (fclose(rev_list_in)) |
| Nguyễn Thái Ngọc Duy | 5cc026e | 2016-05-08 09:47:39 | [diff] [blame] | 151 | err = error_errno(_("failed to close rev-list's stdin")); |
| Junio C Hamano | f96400c | 2011-09-02 23:33:22 | [diff] [blame] | 152 | |
| 153 | sigchain_pop(SIGPIPE); |
| 154 | return finish_command(&rev_list) || err; |
| 155 | } |