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