interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / connected.c

246 lines · 6993 bytes Raw Blame History
1#include "git-compat-util.h"
2#include "config.h"
3#include "gettext.h"
4#include "hex.h"
5#include "odb.h"
6#include "parse.h"
7#include "run-command.h"
8#include "sigchain.h"
9#include "connected.h"
10#include "transport.h"
11#include "packfile.h"
12#include "promisor-remote.h"
13
14static int promised_object_cb(const struct object_id *oid UNUSED,
15 struct object_info *oi UNUSED,
16 void *payload)
17{
18 bool *found = payload;
19 *found = true;
20 return 1;
21}
22
23/*
24 * For partial clones, we don't want to have to do a regular connectivity check
25 * because we have to enumerate and exclude all promisor objects (slow), and
26 * then the connectivity check itself becomes a no-op because in a partial
27 * clone every object is a promisor object. Instead, just make sure we
28 * received, in a promisor packfile, the objects pointed to by each wanted ref.
29 *
30 * Before checking for promisor packs, be sure we have the latest pack-files
31 * loaded into memory.
32 *
33 * Returns 1 when all object IDs have been found in promisor packs, in which
34 * case we're fully connected and thus done. Returns 0 when we have found
35 * objects in non-promisor packs, in which case we'll have to fall back to the
36 * rev-list-based connectivity checks. Returns a negative error code on error.
37 */
38static int check_connected_promisor(struct repository *repo,
39 oid_iterate_fn fn,
40 void *cb_data,
41 const struct object_id **oid)
42{
43 struct odb_for_each_object_options opts = {
44 .flags = ODB_FOR_EACH_OBJECT_PROMISOR_ONLY,
45 .prefix_hex_len = repo->hash_algo->hexsz,
46 };
47 int err;
48
49 odb_reprepare(repo->objects);
50 do {
51 bool found = false;
52
53 opts.prefix = *oid;
54
55 err = odb_for_each_object_ext(repo->objects, NULL,
56 promised_object_cb, &found, &opts);
57 if (err < 0)
58 return err;
59
60 /*
61 * We have found an object that is not part of a promisor pack,
62 * and thus we cannot skip the full connectivity check.
63 */
64 if (!found)
65 return 0;
66 } while ((*oid = fn(cb_data)) != NULL);
67
68 return 1;
69}
70
71/*
72 * Fills in the --connectivity-check mode to pass to rev-list.
73 */
74static void connectivity_check_mode(struct repository *repo,
75 struct check_connected_options *opt,
76 struct strbuf *mode)
77{
78 const char *algorithm = NULL, *cutoff;
79 unsigned long max_commits;
80
81 /*
82 * Deepening fetches do not have the usual boundary of
83 * existing references, so use the full check for them.
84 */
85 if (opt->is_deepening_fetch) {
86 strbuf_addstr(mode, "full");
87 return;
88 }
89
90 if (repo_config_get_string_tmp(repo,
91 "transfer.connectivitycheck",
92 &algorithm))
93 algorithm = "full";
94 if (!strcasecmp(algorithm, "full"))
95 strbuf_addstr(mode, "full");
96 else if (!strcasecmp(algorithm, "incremental"))
97 strbuf_addstr(mode, "incremental");
98 else if (!strcasecmp(algorithm, "auto"))
99 strbuf_addstr(mode, "auto");
100 else if (skip_iprefix(algorithm, "auto=", &cutoff) &&
101 git_parse_ulong(cutoff, &max_commits))
102 strbuf_addf(mode, "auto=%lu", max_commits);
103 else {
104 warning(_("unknown transfer.connectivityCheck algorithm '%s', "
105 "using 'full'"), algorithm);
106 strbuf_addstr(mode, "full");
107 }
108}
109
110/*
111 * If we feed all the commits we want to verify to this command
112 *
113 * $ git rev-list --objects --stdin --not --all
114 *
115 * and if it does not error out, that means everything reachable from
116 * these commits locally exists and is connected to our existing refs.
117 * Note that this does _not_ validate the individual objects.
118 *
119 * Returns 0 if everything is connected, non-zero otherwise.
120 */
121int check_connected(struct repository *repo,
122 oid_iterate_fn fn, void *cb_data,
123 struct check_connected_options *opt)
124{
125 struct child_process rev_list = CHILD_PROCESS_INIT;
126 FILE *rev_list_in;
127 struct check_connected_options defaults = CHECK_CONNECTED_INIT;
128 struct strbuf mode = STRBUF_INIT;
129 const struct object_id *oid;
130 int err = 0;
131 struct packed_git *new_pack = NULL;
132 struct transport *transport;
133 size_t base_len;
134
135 if (!opt)
136 opt = &defaults;
137 transport = opt->transport;
138
139 oid = fn(cb_data);
140 if (!oid) {
141 if (opt->err_fd)
142 close(opt->err_fd);
143 return err;
144 }
145
146 if (repo_has_promisor_remote(repo)) {
147 err = check_connected_promisor(repo, fn, cb_data, &oid);
148 if (err) {
149 if (opt->err_fd)
150 close(opt->err_fd);
151 if (err > 0)
152 err = 0;
153 return err;
154 }
155 }
156
157 if (opt->shallow_file) {
158 strvec_push(&rev_list.args, "--shallow-file");
159 strvec_push(&rev_list.args, opt->shallow_file);
160 }
161 strvec_push(&rev_list.args,"rev-list");
162 strvec_push(&rev_list.args, "--objects");
163 strvec_push(&rev_list.args, "--stdin");
164 if (repo_has_promisor_remote(repo))
165 strvec_push(&rev_list.args, "--exclude-promisor-objects");
166
167 /* define where the traversal should stop */
168 strvec_push(&rev_list.args, "--not");
169 if (!opt->is_deepening_fetch) {
170 if (opt->exclude_hidden_refs_section)
171 strvec_pushf(&rev_list.args, "--exclude-hidden=%s",
172 opt->exclude_hidden_refs_section);
173 strvec_push(&rev_list.args, "--all");
174 }
175 strvec_push(&rev_list.args, "--alternate-refs");
176 strvec_push(&rev_list.args, "--quiet");
177 if (opt->progress)
178 strvec_pushf(&rev_list.args, "--progress=%s",
179 _("Checking connectivity"));
180 connectivity_check_mode(repo, opt, &mode);
181 strvec_pushf(&rev_list.args, "--connectivity-check=%s", mode.buf);
182 strbuf_release(&mode);
183
184 rev_list.git_cmd = 1;
185 if (opt->env)
186 strvec_pushv(&rev_list.env, opt->env);
187 rev_list.in = -1;
188 rev_list.no_stdout = 1;
189 if (opt->err_fd)
190 rev_list.err = opt->err_fd;
191 else
192 rev_list.no_stderr = opt->quiet;
193
194 if (start_command(&rev_list))
195 return error(_("Could not run 'git rev-list'"));
196
197 sigchain_push(SIGPIPE, SIG_IGN);
198
199 rev_list_in = xfdopen(rev_list.in, "w");
200
201 if (transport && transport->smart_options &&
202 transport->smart_options->self_contained_and_connected &&
203 transport->pack_lockfiles.nr == 1 &&
204 strip_suffix(transport->pack_lockfiles.items[0].string,
205 ".keep", &base_len)) {
206 struct strbuf idx_file = STRBUF_INIT;
207 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
208 base_len);
209 strbuf_addstr(&idx_file, ".idx");
210 new_pack = add_packed_git(repo, idx_file.buf,
211 idx_file.len, 1);
212 strbuf_release(&idx_file);
213 }
214
215 do {
216 /*
217 * If index-pack already checked that:
218 * - there are no dangling pointers in the new pack
219 * - the pack is self contained
220 * Then if the updated ref is in the new pack, then we
221 * are sure the ref is good and not sending it to
222 * rev-list for verification.
223 */
224 if (new_pack && find_pack_entry_one(oid, new_pack))
225 continue;
226
227 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
228 break;
229 } while ((oid = fn(cb_data)) != NULL);
230
231 if (ferror(rev_list_in) || fflush(rev_list_in)) {
232 if (errno != EPIPE && errno != EINVAL)
233 error_errno(_("failed write to rev-list"));
234 err = -1;
235 }
236
237 if (fclose(rev_list_in))
238 err = error_errno(_("failed to close rev-list's stdin"));
239
240 sigchain_pop(SIGPIPE);
241 if (new_pack) {
242 close_pack(new_pack);
243 free(new_pack);
244 }
245 return finish_command(&rev_list) || err;
246}