blob: 8f89376dbcf30cd2cf69c3d2eaa446e47c9f4ae8 [file] [log] [blame]
Elijah Newren4f6728d2023-03-21 06:25:56 +00001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00003#include "hex.h"
Elijah Newrena034e912023-05-16 06:34:06 +00004#include "object-store-ll.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -07005#include "run-command.h"
6#include "sigchain.h"
7#include "connected.h"
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07008#include "transport.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -07009#include "packfile.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020010#include "promisor-remote.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -070011
12/*
13 * If we feed all the commits we want to verify to this command
14 *
Junio C Hamanod21c4632012-03-15 14:57:02 -070015 * $ git rev-list --objects --stdin --not --all
Junio C Hamanof96400c2011-09-02 16:33:22 -070016 *
17 * and if it does not error out, that means everything reachable from
Junio C Hamanod21c4632012-03-15 14:57:02 -070018 * these commits locally exists and is connected to our existing refs.
19 * Note that this does _not_ validate the individual objects.
Junio C Hamanof96400c2011-09-02 16:33:22 -070020 *
21 * Returns 0 if everything is connected, non-zero otherwise.
22 */
brian m. carlson6ccac9e2017-10-15 22:06:54 +000023int check_connected(oid_iterate_fn fn, void *cb_data,
Jeff King7043c702016-07-15 06:30:40 -040024 struct check_connected_options *opt)
Junio C Hamanof96400c2011-09-02 16:33:22 -070025{
René Scharfed3180272014-08-19 21:09:35 +020026 struct child_process rev_list = CHILD_PROCESS_INIT;
René Scharfe24b75fa2020-08-12 18:52:49 +020027 FILE *rev_list_in;
Jeff King7043c702016-07-15 06:30:40 -040028 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020029 const struct object_id *oid;
Jeff King3be89f92016-07-15 06:28:32 -040030 int err = 0;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070031 struct packed_git *new_pack = NULL;
Jeff King7043c702016-07-15 06:30:40 -040032 struct transport *transport;
Jeff King26936bf2014-06-30 12:58:51 -040033 size_t base_len;
Junio C Hamanof96400c2011-09-02 16:33:22 -070034
Jeff King7043c702016-07-15 06:30:40 -040035 if (!opt)
36 opt = &defaults;
37 transport = opt->transport;
38
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020039 oid = fn(cb_data);
40 if (!oid) {
Jeff Kinge0331842016-07-15 06:32:03 -040041 if (opt->err_fd)
42 close(opt->err_fd);
Junio C Hamanof96400c2011-09-02 16:33:22 -070043 return err;
Jeff Kinge0331842016-07-15 06:32:03 -040044 }
Junio C Hamanof96400c2011-09-02 16:33:22 -070045
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070046 if (transport && transport->smart_options &&
47 transport->smart_options->self_contained_and_connected &&
Jonathan Tan9da69a62020-06-10 13:57:22 -070048 transport->pack_lockfiles.nr == 1 &&
49 strip_suffix(transport->pack_lockfiles.items[0].string,
50 ".keep", &base_len)) {
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070051 struct strbuf idx_file = STRBUF_INIT;
Jonathan Tan9da69a62020-06-10 13:57:22 -070052 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
53 base_len);
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070054 strbuf_addstr(&idx_file, ".idx");
55 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
56 strbuf_release(&idx_file);
57 }
58
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +020059 if (repo_has_promisor_remote(the_repository)) {
Josh Steadmondfa33a22019-04-19 14:00:13 -070060 /*
61 * For partial clones, we don't want to have to do a regular
62 * connectivity check because we have to enumerate and exclude
63 * all promisor objects (slow), and then the connectivity check
64 * itself becomes a no-op because in a partial clone every
65 * object is a promisor object. Instead, just make sure we
Jonathan Tan50033772020-01-11 20:15:24 -080066 * received, in a promisor packfile, the objects pointed to by
67 * each wanted ref.
Derrick Stoleeb739d972020-03-13 21:11:55 +000068 *
69 * Before checking for promisor packs, be sure we have the
70 * latest pack-files loaded into memory.
Josh Steadmondfa33a22019-04-19 14:00:13 -070071 */
Derrick Stoleeb739d972020-03-13 21:11:55 +000072 reprepare_packed_git(the_repository);
Josh Steadmondfa33a22019-04-19 14:00:13 -070073 do {
Jonathan Tan50033772020-01-11 20:15:24 -080074 struct packed_git *p;
75
76 for (p = get_all_packs(the_repository); p; p = p->next) {
77 if (!p->pack_promisor)
78 continue;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020079 if (find_pack_entry_one(oid->hash, p))
Jonathan Tan50033772020-01-11 20:15:24 -080080 goto promisor_pack_found;
81 }
Jonathan Tan2b984782020-03-20 15:00:45 -070082 /*
83 * Fallback to rev-list with oid and the rest of the
84 * object IDs provided by fn.
85 */
86 goto no_promisor_pack_found;
Jonathan Tan50033772020-01-11 20:15:24 -080087promisor_pack_found:
88 ;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020089 } while ((oid = fn(cb_data)) != NULL);
Ævar Arnfjörð Bjarmasondd4143e2022-11-08 19:17:47 +010090 free(new_pack);
Josh Steadmondfa33a22019-04-19 14:00:13 -070091 return 0;
92 }
93
Jonathan Tan2b984782020-03-20 15:00:45 -070094no_promisor_pack_found:
Jeff King7043c702016-07-15 06:30:40 -040095 if (opt->shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -040096 strvec_push(&rev_list.args, "--shallow-file");
97 strvec_push(&rev_list.args, opt->shallow_file);
Nguyễn Thái Ngọc Duy614db3e2013-12-05 20:02:46 +070098 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -040099 strvec_push(&rev_list.args,"rev-list");
100 strvec_push(&rev_list.args, "--objects");
101 strvec_push(&rev_list.args, "--stdin");
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200102 if (repo_has_promisor_remote(the_repository))
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400103 strvec_push(&rev_list.args, "--exclude-promisor-objects");
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700104 if (!opt->is_deepening_fetch) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400105 strvec_push(&rev_list.args, "--not");
Patrick Steinhardtbcec6782022-11-17 06:47:04 +0100106 if (opt->exclude_hidden_refs_section)
107 strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
108 opt->exclude_hidden_refs_section);
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400109 strvec_push(&rev_list.args, "--all");
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700110 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400111 strvec_push(&rev_list.args, "--quiet");
112 strvec_push(&rev_list.args, "--alternate-refs");
Jeff King70d5e2d2016-07-15 06:32:28 -0400113 if (opt->progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400114 strvec_pushf(&rev_list.args, "--progress=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400115 _("Checking connectivity"));
Junio C Hamanof96400c2011-09-02 16:33:22 -0700116
Junio C Hamanof96400c2011-09-02 16:33:22 -0700117 rev_list.git_cmd = 1;
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100118 if (opt->env)
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200119 strvec_pushv(&rev_list.env, opt->env);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700120 rev_list.in = -1;
121 rev_list.no_stdout = 1;
Jeff Kinge0331842016-07-15 06:32:03 -0400122 if (opt->err_fd)
123 rev_list.err = opt->err_fd;
124 else
125 rev_list.no_stderr = opt->quiet;
126
Ævar Arnfjörð Bjarmasondd4143e2022-11-08 19:17:47 +0100127 if (start_command(&rev_list)) {
128 free(new_pack);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700129 return error(_("Could not run 'git rev-list'"));
Ævar Arnfjörð Bjarmasondd4143e2022-11-08 19:17:47 +0100130 }
Junio C Hamanof96400c2011-09-02 16:33:22 -0700131
132 sigchain_push(SIGPIPE, SIG_IGN);
133
René Scharfe24b75fa2020-08-12 18:52:49 +0200134 rev_list_in = xfdopen(rev_list.in, "w");
135
Junio C Hamanof96400c2011-09-02 16:33:22 -0700136 do {
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700137 /*
138 * If index-pack already checked that:
139 * - there are no dangling pointers in the new pack
140 * - the pack is self contained
141 * Then if the updated ref is in the new pack, then we
142 * are sure the ref is good and not sending it to
143 * rev-list for verification.
144 */
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200145 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700146 continue;
147
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200148 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
Junio C Hamanof96400c2011-09-02 16:33:22 -0700149 break;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200150 } while ((oid = fn(cb_data)) != NULL);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700151
René Scharfe24b75fa2020-08-12 18:52:49 +0200152 if (ferror(rev_list_in) || fflush(rev_list_in)) {
153 if (errno != EPIPE && errno != EINVAL)
154 error_errno(_("failed write to rev-list"));
155 err = -1;
156 }
157
158 if (fclose(rev_list_in))
Nguyễn Thái Ngọc Duy5cc026e2016-05-08 16:47:39 +0700159 err = error_errno(_("failed to close rev-list's stdin"));
Junio C Hamanof96400c2011-09-02 16:33:22 -0700160
161 sigchain_pop(SIGPIPE);
Ævar Arnfjörð Bjarmasondd4143e2022-11-08 19:17:47 +0100162 free(new_pack);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700163 return finish_command(&rev_list) || err;
164}