Skip to content

Commit 2e3c073

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: verify root tree OIDs
The 'verify' subcommand must compare the commit content parsed from the commit-graph against the content in the object database. Use lookup_commit() and parse_commit_in_graph_one() to parse the commits from the graph and compare against a commit that is loaded separately and parsed directly from the object database. Add checks for the root tree OID. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96af91d commit 2e3c073

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

commit-graph.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
866866
return verify_commit_graph_error;
867867

868868
for (i = 0; i < g->num_commits; i++) {
869+
struct commit *graph_commit;
870+
869871
hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
870872

871873
if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
@@ -883,6 +885,11 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
883885
cur_fanout_pos, fanout_value, i);
884886
cur_fanout_pos++;
885887
}
888+
889+
graph_commit = lookup_commit(&cur_oid);
890+
if (!parse_commit_in_graph_one(g, graph_commit))
891+
graph_report("failed to parse %s from commit-graph",
892+
oid_to_hex(&cur_oid));
886893
}
887894

888895
while (cur_fanout_pos < 256) {
@@ -899,16 +906,24 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
899906
return verify_commit_graph_error;
900907

901908
for (i = 0; i < g->num_commits; i++) {
902-
struct commit *odb_commit;
909+
struct commit *graph_commit, *odb_commit;
903910

904911
hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
905912

913+
graph_commit = lookup_commit(&cur_oid);
906914
odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
907915
if (parse_commit_internal(odb_commit, 0, 0)) {
908916
graph_report("failed to parse %s from object database",
909917
oid_to_hex(&cur_oid));
910918
continue;
911919
}
920+
921+
if (oidcmp(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
922+
get_commit_tree_oid(odb_commit)))
923+
graph_report("root tree OID for commit %s in commit-graph is %s != %s",
924+
oid_to_hex(&cur_oid),
925+
oid_to_hex(get_commit_tree_oid(graph_commit)),
926+
oid_to_hex(get_commit_tree_oid(odb_commit)));
912927
}
913928

914929
return verify_commit_graph_error;

t/t5318-commit-graph.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
267267
GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
268268
GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
269269
GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
270+
GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
271+
GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
270272

271273
# usage: corrupt_graph_and_verify <position> <data> <string>
272274
# Manipulates the commit-graph file at the position
@@ -341,4 +343,9 @@ test_expect_success 'detect OID not in object database' '
341343
"from object database"
342344
'
343345

346+
test_expect_success 'detect incorrect tree OID' '
347+
corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
348+
"root tree OID for commit"
349+
'
350+
344351
test_done

0 commit comments

Comments
 (0)