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 / tree-verify.c

433 lines · 12173 bytes Raw Blame History
1#include "git-compat-util.h"
2#include "commit.h"
3#include "gettext.h"
4#include "hex.h"
5#include "khash.h"
6#include "object.h"
7#include "odb.h"
8#include "oid-array.h"
9#include "oidset.h"
10#include "tree.h"
11#include "tree-walk.h"
12#include "tree-verify.h"
13#include "packfile.h"
14#include "trace2.h"
15
16enum tree_state {
17 TREE_UNTRUSTED = 0,
18 TREE_TRUSTED = 1,
19 TREE_EXPANDED = 2,
20};
21
22/* Values are enum tree_state, stored as a byte to keep the map small. */
23KHASH_INIT(oid_tree, struct object_id, unsigned char, 1,
24 oidhash_by_value, oideq_by_value)
25
26static enum tree_state tree_map_get(kh_oid_tree_t *m,
27 const struct object_id *oid)
28{
29 khint_t pos = kh_get_oid_tree(m, *oid);
30 if (pos == kh_end(m))
31 return TREE_UNTRUSTED;
32 return kh_val(m, pos);
33}
34
35static void tree_map_add(kh_oid_tree_t *m, const struct object_id *oid,
36 enum tree_state state)
37{
38 int added;
39 khint_t pos = kh_put_oid_tree(m, *oid, &added);
40 if (added)
41 kh_val(m, pos) = state;
42 else if (state > kh_val(m, pos))
43 kh_val(m, pos) = state;
44}
45
46struct work_item {
47 struct name_entry entry;
48 struct oid_array parent_trees;
49};
50
51/* The entries of a tree that still need to be verified. */
52struct work_list {
53 struct work_item *items;
54 size_t nr, alloc;
55 bool has_subtrees;
56};
57
58static void work_list_clear(struct work_list *list)
59{
60 for (size_t i = 0; i < list->nr; i++)
61 oid_array_clear(&list->items[i].parent_trees);
62 free(list->items);
63}
64
65struct verify_state {
66 kh_oid_tree_t *trees;
67 struct oidset trusted_blobs;
68 size_t trees_loaded;
69 size_t blobs_checked;
70 int exclude_promisor_objects;
71
72 /*
73 * Scratch space for reading one tree at a time. It lives here
74 * rather than on the stack to keep the frame of the recursive
75 * verify_tree() small.
76 */
77 struct tree_desc desc;
78 struct name_entry entry;
79};
80
81/*
82 * Merge-walk the work list against one base tree entry, recording
83 * same-path parent subtrees as recursive comparison bases.
84 * Returns the updated work-list cursor.
85 */
86static size_t collect_subtree_bases(struct work_list *list, size_t wi,
87 const struct name_entry *entry)
88{
89 while (wi < list->nr) {
90 struct work_item *item = &list->items[wi];
91 int cmp = base_name_compare(item->entry.path,
92 item->entry.pathlen,
93 item->entry.mode,
94 entry->path, entry->pathlen,
95 entry->mode);
96 if (cmp > 0)
97 break;
98 if (cmp < 0) {
99 wi++;
100 continue;
101 }
102 if (S_ISDIR(item->entry.mode) && S_ISDIR(entry->mode))
103 oid_array_append(&item->parent_trees, &entry->oid);
104 return wi + 1;
105 }
106 return wi;
107}
108
109/*
110 * Report an object that could not be read, telling a missing object
111 * apart from one that exists but cannot be read, e.g. because it is
112 * corrupt.
113 */
114static int unreadable_object_error(struct repository *repo,
115 const struct object_id *oid,
116 const char *type)
117{
118 if (!odb_has_object(repo->objects, oid, 0))
119 return error(_("missing %s object %s"), type, oid_to_hex(oid));
120 return error(_("unable to read %s object %s"), type, oid_to_hex(oid));
121}
122
123static int verify_blob(struct repository *repo,
124 const struct object_id *oid,
125 struct verify_state *vs)
126{
127 struct object_info oi = OBJECT_INFO_INIT;
128 enum object_type type;
129
130 if (oidset_contains(&vs->trusted_blobs, oid))
131 return 0;
132
133 vs->blobs_checked++;
134 oi.typep = &type;
135 if (odb_read_object_info_extended(repo->objects, oid, &oi,
136 OBJECT_INFO_LOOKUP_REPLACE |
137 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
138 if (vs->exclude_promisor_objects &&
139 is_promisor_object(repo, oid))
140 return 0;
141 return unreadable_object_error(repo, oid, "blob");
142 }
143 if (type != OBJ_BLOB)
144 return error(_("object %s is a %s, not a %s"),
145 oid_to_hex(oid), type_name(type), "blob");
146 oidset_insert(&vs->trusted_blobs, oid);
147 return 0;
148}
149
150/*
151 * Read a tree object into *bufp. Returns -1 if the object exists
152 * but is not a tree. Otherwise returns 0, leaving *bufp NULL if the
153 * object could not be read.
154 */
155static int read_tree_object(struct object_database *odb,
156 const struct object_id *oid,
157 void **bufp, size_t *sizep)
158{
159 struct object_info oi = OBJECT_INFO_INIT;
160 enum object_type type;
161
162 oi.typep = &type;
163 oi.sizep = sizep;
164 oi.contentp = bufp;
165 if (odb_read_object_info_extended(odb, oid, &oi,
166 OBJECT_INFO_LOOKUP_REPLACE |
167 OBJECT_INFO_SKIP_FETCH_OBJECT))
168 *bufp = NULL;
169 if (*bufp && type != OBJ_TREE) {
170 FREE_AND_NULL(*bufp);
171 return error(_("object %s is a %s, not a %s"),
172 oid_to_hex(oid), type_name(type), "tree");
173 }
174 return 0;
175}
176
177/* List the entries of a tree that are not yet known to be trusted. */
178static void list_untrusted_entries(struct verify_state *vs,
179 const struct object_id *tree_oid,
180 void *tree_buf, size_t tree_size,
181 struct work_list *list)
182{
183 struct name_entry *entry = &vs->entry;
184
185 init_tree_desc(&vs->desc, tree_oid, tree_buf, tree_size);
186 while (tree_entry(&vs->desc, entry)) {
187 if (S_ISGITLINK(entry->mode))
188 continue;
189 if (S_ISDIR(entry->mode)) {
190 if (tree_map_get(vs->trees, &entry->oid) >=
191 TREE_TRUSTED)
192 continue;
193 list->has_subtrees = true;
194 } else {
195 if (oidset_contains(&vs->trusted_blobs, &entry->oid))
196 continue;
197 }
198 ALLOC_GROW(list->items, list->nr + 1, list->alloc);
199 memset(&list->items[list->nr], 0, sizeof(list->items[0]));
200 list->items[list->nr++].entry = *entry;
201 }
202}
203
204/*
205 * Scan one trusted base tree: expand it if that has not happened yet,
206 * and collect its same-path subtrees as bases for untrusted subtrees.
207 */
208static int scan_base_tree(struct repository *repo, struct verify_state *vs,
209 const struct object_id *base_oid,
210 struct work_list *list)
211{
212 int expanded = tree_map_get(vs->trees, base_oid) >= TREE_EXPANDED;
213 struct name_entry *entry = &vs->entry;
214 size_t wi = 0, base_size;
215 void *base_buf;
216
217 if (expanded && !list->has_subtrees)
218 return 0;
219
220 if (read_tree_object(repo->objects, base_oid, &base_buf, &base_size))
221 return -1;
222 if (!base_buf) {
223 if (vs->exclude_promisor_objects &&
224 is_promisor_object(repo, base_oid))
225 return 0;
226 return unreadable_object_error(repo, base_oid, "tree");
227 }
228
229 vs->trees_loaded++;
230 init_tree_desc(&vs->desc, base_oid, base_buf, base_size);
231
232 while (tree_entry(&vs->desc, entry)) {
233 if (S_ISGITLINK(entry->mode))
234 continue;
235
236 /* Same-path subtree as a base for an untrusted one. */
237 if (list->has_subtrees)
238 wi = collect_subtree_bases(list, wi, entry);
239
240 /* Expand: the base is trusted, so are its entries. */
241 if (!expanded) {
242 if (S_ISDIR(entry->mode))
243 tree_map_add(vs->trees, &entry->oid,
244 TREE_TRUSTED);
245 else
246 oidset_insert(&vs->trusted_blobs,
247 &entry->oid);
248 }
249 }
250
251 if (!expanded)
252 tree_map_add(vs->trees, base_oid, TREE_EXPANDED);
253
254 free(base_buf);
255 return 0;
256}
257
258/*
259 * Verify a tree against the trusted base trees, loading as little as
260 * possible:
261 *
262 * First list the entries that are not yet known to be trusted. When
263 * the parent trees have been expanded before, only the changed entries
264 * remain.
265 *
266 * Then scan the trusted base trees (the same-path trees of the parent
267 * commits). A base that has not been expanded yet is read so that its
268 * entries can be marked trusted; this often shows that listed entries
269 * are trusted after all. An already expanded base is still read when
270 * there are untrusted subtrees, because its subtree at the same path is
271 * what lets the untrusted subtree skip its own unchanged entries in
272 * turn.
273 *
274 * Finally check that the remaining blobs exist, and recurse into the
275 * remaining subtrees with the same-path subtrees as their bases.
276 */
277static int verify_tree(struct repository *repo,
278 const struct object_id *tree_oid,
279 const struct oid_array *trusted_bases,
280 struct verify_state *vs, int depth)
281{
282 struct work_list list = { 0 };
283 size_t i, tree_size;
284 void *tree_buf;
285 int ret = 0;
286
287 /*
288 * Enforce core.maxTreeDepth like process_tree() in list-objects.c,
289 * so that the full and incremental checks reject the same trees.
290 */
291 if (depth > repo->settings.max_allowed_tree_depth)
292 return error(_("exceeded maximum allowed tree depth"));
293
294 if (tree_map_get(vs->trees, tree_oid) >= TREE_TRUSTED)
295 return 0;
296
297 if (read_tree_object(repo->objects, tree_oid, &tree_buf, &tree_size))
298 return -1;
299 if (!tree_buf) {
300 /*
301 * A missing tree that is promised can be fetched on demand,
302 * so it is not an error, as in the full check.
303 */
304 if (vs->exclude_promisor_objects &&
305 is_promisor_object(repo, tree_oid))
306 return 0;
307 return unreadable_object_error(repo, tree_oid, "tree");
308 }
309
310 vs->trees_loaded++;
311 list_untrusted_entries(vs, tree_oid, tree_buf, tree_size, &list);
312
313 /* Scan the base trees, unless nothing is left to verify. */
314 for (i = 0; list.nr && i < trusted_bases->nr; i++) {
315 ret = scan_base_tree(repo, vs, &trusted_bases->oid[i], &list);
316 if (ret)
317 goto done;
318 }
319
320 /*
321 * Check the remaining entries in tree order. Verifying a subtree
322 * can show that later entries are trusted, which then makes them
323 * cheap to skip.
324 */
325 for (i = 0; i < list.nr && !ret; i++) {
326 struct work_item *item = &list.items[i];
327
328 if (S_ISDIR(item->entry.mode))
329 ret = verify_tree(repo, &item->entry.oid,
330 &item->parent_trees, vs, depth + 1);
331 else
332 ret = verify_blob(repo, &item->entry.oid, vs);
333 }
334 if (!ret)
335 tree_map_add(vs->trees, tree_oid, TREE_EXPANDED);
336
337done:
338 free(tree_buf);
339 work_list_clear(&list);
340 return ret;
341}
342
343static int verify_commit_tree(struct repository *repo,
344 struct commit *commit,
345 struct verify_state *vs)
346{
347 struct oid_array parent_trees = OID_ARRAY_INIT;
348 const struct object_id *tree_oid;
349 struct commit_list *p;
350 int ret;
351
352 /*
353 * Parent trees are trusted: boundary parents are already
354 * connected, and earlier incoming parents were verified
355 * first due to the topological processing order.
356 *
357 * The revision walk skips missing parents that are promised
358 * (see process_parents()), so do the same here. Such a parent
359 * contributes no base, which only means less of the new tree
360 * can be skipped.
361 */
362 for (p = commit->parents; p; p = p->next) {
363 if (repo_parse_commit_gently(repo, p->item, 1) < 0) {
364 if (vs->exclude_promisor_objects &&
365 is_promisor_object(repo, &p->item->object.oid))
366 continue;
367 ret = error(_("unable to parse commit %s"),
368 oid_to_hex(&p->item->object.oid));
369 goto out;
370 }
371 tree_oid = get_commit_tree_oid(p->item);
372 if (!tree_oid) {
373 ret = error(_("unable to load root tree for commit %s"),
374 oid_to_hex(&p->item->object.oid));
375 goto out;
376 }
377 tree_map_add(vs->trees, tree_oid, TREE_TRUSTED);
378 oid_array_append(&parent_trees, tree_oid);
379 }
380
381 tree_oid = get_commit_tree_oid(commit);
382 if (!tree_oid) {
383 ret = error(_("unable to load root tree for commit %s"),
384 oid_to_hex(&commit->object.oid));
385 goto out;
386 }
387 ret = verify_tree(repo, tree_oid, &parent_trees, vs, 0);
388out:
389 oid_array_clear(&parent_trees);
390 return ret;
391}
392
393int verify_commits_incremental(struct repository *repo,
394 struct commit_list **commits,
395 int exclude_promisor_objects)
396{
397 struct verify_state vs = { 0 };
398 struct commit_list *iter;
399 unsigned nr_before;
400 int ret = 0;
401
402 vs.trees = kh_init_oid_tree();
403 vs.exclude_promisor_objects = exclude_promisor_objects;
404
405 /*
406 * Ancestors must be verified before descendants so that parent
407 * trees can be trusted without re-verification. Sort explicitly
408 * rather than relying on the caller's ordering.
409 *
410 * sort_in_topological_order() silently drops cycle members,
411 * so explicitly check if the size has changed.
412 */
413 nr_before = commit_list_count(*commits);
414 sort_in_topological_order(commits, REV_SORT_IN_GRAPH_ORDER);
415 if (commit_list_count(*commits) < nr_before) {
416 ret = error(_("cycle detected in incoming commit graph"));
417 goto out;
418 }
419
420 *commits = commit_list_reverse(*commits);
421
422 for (iter = *commits; iter && !ret; iter = iter->next)
423 ret = verify_commit_tree(repo, iter->item, &vs);
424
425out:
426 kh_destroy_oid_tree(vs.trees);
427 oidset_clear(&vs.trusted_blobs);
428 trace2_data_intmax("connectivity", repo,
429 "trees_loaded", (intmax_t)vs.trees_loaded);
430 trace2_data_intmax("connectivity", repo,
431 "blobs_checked", (intmax_t)vs.blobs_checked);
432 return ret;
433}