Merge branch 'rs/oidtree-alignment-fix'
[git/debian.git] / connected.c
blobb18299fdf0e5224924915810d66196a1d38cbe56
1 #include "cache.h"
2 #include "object-store.h"
3 #include "run-command.h"
4 #include "sigchain.h"
5 #include "connected.h"
6 #include "transport.h"
7 #include "packfile.h"
8 #include "promisor-remote.h"
11 * If we feed all the commits we want to verify to this command
13 * $ git rev-list --objects --stdin --not --all
15 * and if it does not error out, that means everything reachable from
16 * these commits locally exists and is connected to our existing refs.
17 * Note that this does _not_ validate the individual objects.
19 * Returns 0 if everything is connected, non-zero otherwise.
21 int check_connected(oid_iterate_fn fn, void *cb_data,
22 struct check_connected_options *opt)
24 struct child_process rev_list = CHILD_PROCESS_INIT;
25 FILE *rev_list_in;
26 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
27 struct object_id oid;
28 int err = 0;
29 struct packed_git *new_pack = NULL;
30 struct transport *transport;
31 size_t base_len;
33 if (!opt)
34 opt = &defaults;
35 transport = opt->transport;
37 if (fn(cb_data, &oid)) {
38 if (opt->err_fd)
39 close(opt->err_fd);
40 return err;
43 if (transport && transport->smart_options &&
44 transport->smart_options->self_contained_and_connected &&
45 transport->pack_lockfiles.nr == 1 &&
46 strip_suffix(transport->pack_lockfiles.items[0].string,
47 ".keep", &base_len)) {
48 struct strbuf idx_file = STRBUF_INIT;
49 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
50 base_len);
51 strbuf_addstr(&idx_file, ".idx");
52 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
53 strbuf_release(&idx_file);
56 if (has_promisor_remote()) {
58 * For partial clones, we don't want to have to do a regular
59 * connectivity check because we have to enumerate and exclude
60 * all promisor objects (slow), and then the connectivity check
61 * itself becomes a no-op because in a partial clone every
62 * object is a promisor object. Instead, just make sure we
63 * received, in a promisor packfile, the objects pointed to by
64 * each wanted ref.
66 * Before checking for promisor packs, be sure we have the
67 * latest pack-files loaded into memory.
69 reprepare_packed_git(the_repository);
70 do {
71 struct packed_git *p;
73 for (p = get_all_packs(the_repository); p; p = p->next) {
74 if (!p->pack_promisor)
75 continue;
76 if (find_pack_entry_one(oid.hash, p))
77 goto promisor_pack_found;
80 * Fallback to rev-list with oid and the rest of the
81 * object IDs provided by fn.
83 goto no_promisor_pack_found;
84 promisor_pack_found:
86 } while (!fn(cb_data, &oid));
87 return 0;
90 no_promisor_pack_found:
91 if (opt->shallow_file) {
92 strvec_push(&rev_list.args, "--shallow-file");
93 strvec_push(&rev_list.args, opt->shallow_file);
95 strvec_push(&rev_list.args,"rev-list");
96 strvec_push(&rev_list.args, "--objects");
97 strvec_push(&rev_list.args, "--stdin");
98 if (has_promisor_remote())
99 strvec_push(&rev_list.args, "--exclude-promisor-objects");
100 if (!opt->is_deepening_fetch) {
101 strvec_push(&rev_list.args, "--not");
102 strvec_push(&rev_list.args, "--all");
104 strvec_push(&rev_list.args, "--quiet");
105 strvec_push(&rev_list.args, "--alternate-refs");
106 if (opt->progress)
107 strvec_pushf(&rev_list.args, "--progress=%s",
108 _("Checking connectivity"));
110 rev_list.git_cmd = 1;
111 rev_list.env = opt->env;
112 rev_list.in = -1;
113 rev_list.no_stdout = 1;
114 if (opt->err_fd)
115 rev_list.err = opt->err_fd;
116 else
117 rev_list.no_stderr = opt->quiet;
119 if (start_command(&rev_list))
120 return error(_("Could not run 'git rev-list'"));
122 sigchain_push(SIGPIPE, SIG_IGN);
124 rev_list_in = xfdopen(rev_list.in, "w");
126 do {
128 * If index-pack already checked that:
129 * - there are no dangling pointers in the new pack
130 * - the pack is self contained
131 * Then if the updated ref is in the new pack, then we
132 * are sure the ref is good and not sending it to
133 * rev-list for verification.
135 if (new_pack && find_pack_entry_one(oid.hash, new_pack))
136 continue;
138 if (fprintf(rev_list_in, "%s\n", oid_to_hex(&oid)) < 0)
139 break;
140 } while (!fn(cb_data, &oid));
142 if (ferror(rev_list_in) || fflush(rev_list_in)) {
143 if (errno != EPIPE && errno != EINVAL)
144 error_errno(_("failed write to rev-list"));
145 err = -1;
148 if (fclose(rev_list_in))
149 err = error_errno(_("failed to close rev-list's stdin"));
151 sigchain_pop(SIGPIPE);
152 return finish_command(&rev_list) || err;