]> git.ipfire.org Git - thirdparty/git.git/blame - connected.c
Merge branch 'ab/fewer-the-index-macros'
[thirdparty/git.git] / connected.c
CommitLineData
f96400cb 1#include "cache.h"
dfa33a29 2#include "object-store.h"
f96400cb
JH
3#include "run-command.h"
4#include "sigchain.h"
5#include "connected.h"
c6807a40 6#include "transport.h"
0abe14f6 7#include "packfile.h"
b14ed5ad 8#include "promisor-remote.h"
f96400cb
JH
9
10/*
11 * If we feed all the commits we want to verify to this command
12 *
d21c463d 13 * $ git rev-list --objects --stdin --not --all
f96400cb
JH
14 *
15 * and if it does not error out, that means everything reachable from
d21c463d
JH
16 * these commits locally exists and is connected to our existing refs.
17 * Note that this does _not_ validate the individual objects.
f96400cb
JH
18 *
19 * Returns 0 if everything is connected, non-zero otherwise.
20 */
6ccac9ee 21int check_connected(oid_iterate_fn fn, void *cb_data,
7043c707 22 struct check_connected_options *opt)
f96400cb 23{
d3180279 24 struct child_process rev_list = CHILD_PROCESS_INIT;
24b75faf 25 FILE *rev_list_in;
7043c707 26 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
9fec7b21 27 const struct object_id *oid;
3be89f9b 28 int err = 0;
c6807a40 29 struct packed_git *new_pack = NULL;
7043c707 30 struct transport *transport;
26936bfd 31 size_t base_len;
f96400cb 32
7043c707
JK
33 if (!opt)
34 opt = &defaults;
35 transport = opt->transport;
36
9fec7b21
PS
37 oid = fn(cb_data);
38 if (!oid) {
e0331849
JK
39 if (opt->err_fd)
40 close(opt->err_fd);
f96400cb 41 return err;
e0331849 42 }
f96400cb 43
c6807a40
NTND
44 if (transport && transport->smart_options &&
45 transport->smart_options->self_contained_and_connected &&
9da69a65
JT
46 transport->pack_lockfiles.nr == 1 &&
47 strip_suffix(transport->pack_lockfiles.items[0].string,
48 ".keep", &base_len)) {
c6807a40 49 struct strbuf idx_file = STRBUF_INIT;
9da69a65
JT
50 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
51 base_len);
c6807a40
NTND
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
2b98478c 57 if (has_promisor_remote()) {
dfa33a29
JS
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
50033772
JT
64 * received, in a promisor packfile, the objects pointed to by
65 * each wanted ref.
b739d971
DS
66 *
67 * Before checking for promisor packs, be sure we have the
68 * latest pack-files loaded into memory.
dfa33a29 69 */
b739d971 70 reprepare_packed_git(the_repository);
dfa33a29 71 do {
50033772
JT
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;
9fec7b21 77 if (find_pack_entry_one(oid->hash, p))
50033772
JT
78 goto promisor_pack_found;
79 }
2b98478c
JT
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;
50033772
JT
85promisor_pack_found:
86 ;
9fec7b21 87 } while ((oid = fn(cb_data)) != NULL);
dfa33a29
JS
88 return 0;
89 }
90
2b98478c 91no_promisor_pack_found:
7043c707 92 if (opt->shallow_file) {
ef8d7ac4
JK
93 strvec_push(&rev_list.args, "--shallow-file");
94 strvec_push(&rev_list.args, opt->shallow_file);
614db3e2 95 }
ef8d7ac4
JK
96 strvec_push(&rev_list.args,"rev-list");
97 strvec_push(&rev_list.args, "--objects");
98 strvec_push(&rev_list.args, "--stdin");
b14ed5ad 99 if (has_promisor_remote())
ef8d7ac4 100 strvec_push(&rev_list.args, "--exclude-promisor-objects");
cf1e7c07 101 if (!opt->is_deepening_fetch) {
ef8d7ac4 102 strvec_push(&rev_list.args, "--not");
bcec6780
PS
103 if (opt->exclude_hidden_refs_section)
104 strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
105 opt->exclude_hidden_refs_section);
ef8d7ac4 106 strvec_push(&rev_list.args, "--all");
cf1e7c07 107 }
ef8d7ac4
JK
108 strvec_push(&rev_list.args, "--quiet");
109 strvec_push(&rev_list.args, "--alternate-refs");
70d5e2d7 110 if (opt->progress)
ef8d7ac4 111 strvec_pushf(&rev_list.args, "--progress=%s",
f6d8942b 112 _("Checking connectivity"));
f96400cb 113
f96400cb 114 rev_list.git_cmd = 1;
c7c4bdec 115 if (opt->env)
29fda24d 116 strvec_pushv(&rev_list.env, opt->env);
f96400cb
JH
117 rev_list.in = -1;
118 rev_list.no_stdout = 1;
e0331849
JK
119 if (opt->err_fd)
120 rev_list.err = opt->err_fd;
121 else
122 rev_list.no_stderr = opt->quiet;
123
f96400cb
JH
124 if (start_command(&rev_list))
125 return error(_("Could not run 'git rev-list'"));
126
127 sigchain_push(SIGPIPE, SIG_IGN);
128
24b75faf
RS
129 rev_list_in = xfdopen(rev_list.in, "w");
130
f96400cb 131 do {
c6807a40
NTND
132 /*
133 * If index-pack already checked that:
134 * - there are no dangling pointers in the new pack
135 * - the pack is self contained
136 * Then if the updated ref is in the new pack, then we
137 * are sure the ref is good and not sending it to
138 * rev-list for verification.
139 */
9fec7b21 140 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
c6807a40
NTND
141 continue;
142
9fec7b21 143 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
f96400cb 144 break;
9fec7b21 145 } while ((oid = fn(cb_data)) != NULL);
f96400cb 146
24b75faf
RS
147 if (ferror(rev_list_in) || fflush(rev_list_in)) {
148 if (errno != EPIPE && errno != EINVAL)
149 error_errno(_("failed write to rev-list"));
150 err = -1;
151 }
152
153 if (fclose(rev_list_in))
5cc026e2 154 err = error_errno(_("failed to close rev-list's stdin"));
f96400cb
JH
155
156 sigchain_pop(SIGPIPE);
157 return finish_command(&rev_list) || err;
158}