blob: 74a20cb32e7c7bfa4527addf0e83c2284b111745 [file] [log] [blame]
Junio C Hamanof96400c2011-09-02 16:33:22 -07001#include "cache.h"
Josh Steadmondfa33a22019-04-19 14:00:13 -07002#include "object-store.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -07003#include "run-command.h"
4#include "sigchain.h"
5#include "connected.h"
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07006#include "transport.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -07007#include "packfile.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +02008#include "promisor-remote.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -07009
10/*
11 * If we feed all the commits we want to verify to this command
12 *
Junio C Hamanod21c4632012-03-15 14:57:02 -070013 * $ git rev-list --objects --stdin --not --all
Junio C Hamanof96400c2011-09-02 16:33:22 -070014 *
15 * and if it does not error out, that means everything reachable from
Junio C Hamanod21c4632012-03-15 14:57:02 -070016 * these commits locally exists and is connected to our existing refs.
17 * Note that this does _not_ validate the individual objects.
Junio C Hamanof96400c2011-09-02 16:33:22 -070018 *
19 * Returns 0 if everything is connected, non-zero otherwise.
20 */
brian m. carlson6ccac9e2017-10-15 22:06:54 +000021int check_connected(oid_iterate_fn fn, void *cb_data,
Jeff King7043c702016-07-15 06:30:40 -040022 struct check_connected_options *opt)
Junio C Hamanof96400c2011-09-02 16:33:22 -070023{
René Scharfed3180272014-08-19 21:09:35 +020024 struct child_process rev_list = CHILD_PROCESS_INIT;
René Scharfe24b75fa2020-08-12 18:52:49 +020025 FILE *rev_list_in;
Jeff King7043c702016-07-15 06:30:40 -040026 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020027 const struct object_id *oid;
Jeff King3be89f92016-07-15 06:28:32 -040028 int err = 0;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070029 struct packed_git *new_pack = NULL;
Jeff King7043c702016-07-15 06:30:40 -040030 struct transport *transport;
Jeff King26936bf2014-06-30 12:58:51 -040031 size_t base_len;
Junio C Hamanof96400c2011-09-02 16:33:22 -070032
Jeff King7043c702016-07-15 06:30:40 -040033 if (!opt)
34 opt = &defaults;
35 transport = opt->transport;
36
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020037 oid = fn(cb_data);
38 if (!oid) {
Jeff Kinge0331842016-07-15 06:32:03 -040039 if (opt->err_fd)
40 close(opt->err_fd);
Junio C Hamanof96400c2011-09-02 16:33:22 -070041 return err;
Jeff Kinge0331842016-07-15 06:32:03 -040042 }
Junio C Hamanof96400c2011-09-02 16:33:22 -070043
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070044 if (transport && transport->smart_options &&
45 transport->smart_options->self_contained_and_connected &&
Jonathan Tan9da69a62020-06-10 13:57:22 -070046 transport->pack_lockfiles.nr == 1 &&
47 strip_suffix(transport->pack_lockfiles.items[0].string,
48 ".keep", &base_len)) {
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070049 struct strbuf idx_file = STRBUF_INIT;
Jonathan Tan9da69a62020-06-10 13:57:22 -070050 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
51 base_len);
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +070052 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 Tan2b984782020-03-20 15:00:45 -070057 if (has_promisor_remote()) {
Josh Steadmondfa33a22019-04-19 14:00:13 -070058 /*
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 Tan50033772020-01-11 20:15:24 -080064 * received, in a promisor packfile, the objects pointed to by
65 * each wanted ref.
Derrick Stoleeb739d972020-03-13 21:11:55 +000066 *
67 * Before checking for promisor packs, be sure we have the
68 * latest pack-files loaded into memory.
Josh Steadmondfa33a22019-04-19 14:00:13 -070069 */
Derrick Stoleeb739d972020-03-13 21:11:55 +000070 reprepare_packed_git(the_repository);
Josh Steadmondfa33a22019-04-19 14:00:13 -070071 do {
Jonathan Tan50033772020-01-11 20:15:24 -080072 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 Steinhardt9fec7b22021-09-01 15:09:50 +020077 if (find_pack_entry_one(oid->hash, p))
Jonathan Tan50033772020-01-11 20:15:24 -080078 goto promisor_pack_found;
79 }
Jonathan Tan2b984782020-03-20 15:00:45 -070080 /*
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 Tan50033772020-01-11 20:15:24 -080085promisor_pack_found:
86 ;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +020087 } while ((oid = fn(cb_data)) != NULL);
Josh Steadmondfa33a22019-04-19 14:00:13 -070088 return 0;
89 }
90
Jonathan Tan2b984782020-03-20 15:00:45 -070091no_promisor_pack_found:
Jeff King7043c702016-07-15 06:30:40 -040092 if (opt->shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -040093 strvec_push(&rev_list.args, "--shallow-file");
94 strvec_push(&rev_list.args, opt->shallow_file);
Nguyễn Thái Ngọc Duy614db3e2013-12-05 20:02:46 +070095 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -040096 strvec_push(&rev_list.args,"rev-list");
97 strvec_push(&rev_list.args, "--objects");
98 strvec_push(&rev_list.args, "--stdin");
Christian Couderb14ed5a2019-06-25 15:40:31 +020099 if (has_promisor_remote())
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400100 strvec_push(&rev_list.args, "--exclude-promisor-objects");
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700101 if (!opt->is_deepening_fetch) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400102 strvec_push(&rev_list.args, "--not");
103 strvec_push(&rev_list.args, "--all");
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700104 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400105 strvec_push(&rev_list.args, "--quiet");
106 strvec_push(&rev_list.args, "--alternate-refs");
Jeff King70d5e2d2016-07-15 06:32:28 -0400107 if (opt->progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400108 strvec_pushf(&rev_list.args, "--progress=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400109 _("Checking connectivity"));
Junio C Hamanof96400c2011-09-02 16:33:22 -0700110
Junio C Hamanof96400c2011-09-02 16:33:22 -0700111 rev_list.git_cmd = 1;
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100112 if (opt->env)
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200113 strvec_pushv(&rev_list.env, opt->env);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700114 rev_list.in = -1;
115 rev_list.no_stdout = 1;
Jeff Kinge0331842016-07-15 06:32:03 -0400116 if (opt->err_fd)
117 rev_list.err = opt->err_fd;
118 else
119 rev_list.no_stderr = opt->quiet;
120
Junio C Hamanof96400c2011-09-02 16:33:22 -0700121 if (start_command(&rev_list))
122 return error(_("Could not run 'git rev-list'"));
123
124 sigchain_push(SIGPIPE, SIG_IGN);
125
René Scharfe24b75fa2020-08-12 18:52:49 +0200126 rev_list_in = xfdopen(rev_list.in, "w");
127
Junio C Hamanof96400c2011-09-02 16:33:22 -0700128 do {
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700129 /*
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 Steinhardt9fec7b22021-09-01 15:09:50 +0200137 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700138 continue;
139
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200140 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
Junio C Hamanof96400c2011-09-02 16:33:22 -0700141 break;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200142 } while ((oid = fn(cb_data)) != NULL);
Junio C Hamanof96400c2011-09-02 16:33:22 -0700143
René Scharfe24b75fa2020-08-12 18:52:49 +0200144 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 Duy5cc026e2016-05-08 16:47:39 +0700151 err = error_errno(_("failed to close rev-list's stdin"));
Junio C Hamanof96400c2011-09-02 16:33:22 -0700152
153 sigchain_pop(SIGPIPE);
154 return finish_command(&rev_list) || err;
155}