]> git.ipfire.org Git - thirdparty/git.git/blame - connected.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / connected.c
CommitLineData
4f6728d5 1#include "git-compat-util.h"
f394e093 2#include "gettext.h"
41771fa4 3#include "hex.h"
a034e910 4#include "object-store-ll.h"
f96400cb
JH
5#include "run-command.h"
6#include "sigchain.h"
7#include "connected.h"
c6807a40 8#include "transport.h"
0abe14f6 9#include "packfile.h"
b14ed5ad 10#include "promisor-remote.h"
f96400cb
JH
11
12/*
13 * If we feed all the commits we want to verify to this command
14 *
d21c463d 15 * $ git rev-list --objects --stdin --not --all
f96400cb
JH
16 *
17 * and if it does not error out, that means everything reachable from
d21c463d
JH
18 * these commits locally exists and is connected to our existing refs.
19 * Note that this does _not_ validate the individual objects.
f96400cb
JH
20 *
21 * Returns 0 if everything is connected, non-zero otherwise.
22 */
6ccac9ee 23int check_connected(oid_iterate_fn fn, void *cb_data,
7043c707 24 struct check_connected_options *opt)
f96400cb 25{
d3180279 26 struct child_process rev_list = CHILD_PROCESS_INIT;
24b75faf 27 FILE *rev_list_in;
7043c707 28 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
9fec7b21 29 const struct object_id *oid;
3be89f9b 30 int err = 0;
c6807a40 31 struct packed_git *new_pack = NULL;
7043c707 32 struct transport *transport;
26936bfd 33 size_t base_len;
f96400cb 34
7043c707
JK
35 if (!opt)
36 opt = &defaults;
37 transport = opt->transport;
38
9fec7b21
PS
39 oid = fn(cb_data);
40 if (!oid) {
e0331849
JK
41 if (opt->err_fd)
42 close(opt->err_fd);
f96400cb 43 return err;
e0331849 44 }
f96400cb 45
c6807a40
NTND
46 if (transport && transport->smart_options &&
47 transport->smart_options->self_contained_and_connected &&
9da69a65
JT
48 transport->pack_lockfiles.nr == 1 &&
49 strip_suffix(transport->pack_lockfiles.items[0].string,
50 ".keep", &base_len)) {
c6807a40 51 struct strbuf idx_file = STRBUF_INIT;
9da69a65
JT
52 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
53 base_len);
c6807a40
NTND
54 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
a5183d76 59 if (repo_has_promisor_remote(the_repository)) {
dfa33a29
JS
60 /*
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
50033772
JT
66 * received, in a promisor packfile, the objects pointed to by
67 * each wanted ref.
b739d971
DS
68 *
69 * Before checking for promisor packs, be sure we have the
70 * latest pack-files loaded into memory.
dfa33a29 71 */
b739d971 72 reprepare_packed_git(the_repository);
dfa33a29 73 do {
50033772
JT
74 struct packed_git *p;
75
76 for (p = get_all_packs(the_repository); p; p = p->next) {
77 if (!p->pack_promisor)
78 continue;
9fec7b21 79 if (find_pack_entry_one(oid->hash, p))
50033772
JT
80 goto promisor_pack_found;
81 }
2b98478c
JT
82 /*
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;
50033772
JT
87promisor_pack_found:
88 ;
9fec7b21 89 } while ((oid = fn(cb_data)) != NULL);
dd4143e7 90 free(new_pack);
dfa33a29
JS
91 return 0;
92 }
93
2b98478c 94no_promisor_pack_found:
7043c707 95 if (opt->shallow_file) {
ef8d7ac4
JK
96 strvec_push(&rev_list.args, "--shallow-file");
97 strvec_push(&rev_list.args, opt->shallow_file);
614db3e2 98 }
ef8d7ac4
JK
99 strvec_push(&rev_list.args,"rev-list");
100 strvec_push(&rev_list.args, "--objects");
101 strvec_push(&rev_list.args, "--stdin");
a5183d76 102 if (repo_has_promisor_remote(the_repository))
ef8d7ac4 103 strvec_push(&rev_list.args, "--exclude-promisor-objects");
cf1e7c07 104 if (!opt->is_deepening_fetch) {
ef8d7ac4 105 strvec_push(&rev_list.args, "--not");
bcec6780
PS
106 if (opt->exclude_hidden_refs_section)
107 strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
108 opt->exclude_hidden_refs_section);
ef8d7ac4 109 strvec_push(&rev_list.args, "--all");
cf1e7c07 110 }
ef8d7ac4
JK
111 strvec_push(&rev_list.args, "--quiet");
112 strvec_push(&rev_list.args, "--alternate-refs");
70d5e2d7 113 if (opt->progress)
ef8d7ac4 114 strvec_pushf(&rev_list.args, "--progress=%s",
f6d8942b 115 _("Checking connectivity"));
f96400cb 116
f96400cb 117 rev_list.git_cmd = 1;
c7c4bdec 118 if (opt->env)
29fda24d 119 strvec_pushv(&rev_list.env, opt->env);
f96400cb
JH
120 rev_list.in = -1;
121 rev_list.no_stdout = 1;
e0331849
JK
122 if (opt->err_fd)
123 rev_list.err = opt->err_fd;
124 else
125 rev_list.no_stderr = opt->quiet;
126
dd4143e7
ÆAB
127 if (start_command(&rev_list)) {
128 free(new_pack);
f96400cb 129 return error(_("Could not run 'git rev-list'"));
dd4143e7 130 }
f96400cb
JH
131
132 sigchain_push(SIGPIPE, SIG_IGN);
133
24b75faf
RS
134 rev_list_in = xfdopen(rev_list.in, "w");
135
f96400cb 136 do {
c6807a40
NTND
137 /*
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 */
9fec7b21 145 if (new_pack && find_pack_entry_one(oid->hash, new_pack))
c6807a40
NTND
146 continue;
147
9fec7b21 148 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
f96400cb 149 break;
9fec7b21 150 } while ((oid = fn(cb_data)) != NULL);
f96400cb 151
24b75faf
RS
152 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))
5cc026e2 159 err = error_errno(_("failed to close rev-list's stdin"));
f96400cb
JH
160
161 sigchain_pop(SIGPIPE);
dd4143e7 162 free(new_pack);
f96400cb
JH
163 return finish_command(&rev_list) || err;
164}