Skip to content

Commit 96af91d

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: verify objects exist
In the 'verify' subcommand, load commits directly from the object database to ensure they exist. Parse by skipping the commit-graph. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bda846 commit 96af91d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

commit-graph.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "sha1-lookup.h"
1212
#include "commit-graph.h"
1313
#include "object-store.h"
14+
#include "alloc.h"
1415

1516
#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
1617
#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
@@ -242,6 +243,7 @@ static struct commit_list **insert_parent_or_die(struct commit_graph *g,
242243
{
243244
struct commit *c;
244245
struct object_id oid;
246+
245247
hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
246248
c = lookup_commit(&oid);
247249
if (!c)
@@ -893,5 +895,21 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
893895
cur_fanout_pos++;
894896
}
895897

898+
if (verify_commit_graph_error)
899+
return verify_commit_graph_error;
900+
901+
for (i = 0; i < g->num_commits; i++) {
902+
struct commit *odb_commit;
903+
904+
hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
905+
906+
odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
907+
if (parse_commit_internal(odb_commit, 0, 0)) {
908+
graph_report("failed to parse %s from object database",
909+
oid_to_hex(&cur_oid));
910+
continue;
911+
}
912+
}
913+
896914
return verify_commit_graph_error;
897915
}

t/t5318-commit-graph.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ test_expect_success 'git commit-graph verify' '
247247
git commit-graph verify >output
248248
'
249249

250+
NUM_COMMITS=9
250251
HASH_LEN=20
251252
GRAPH_BYTE_VERSION=4
252253
GRAPH_BYTE_HASH=5
@@ -265,6 +266,7 @@ GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
265266
GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
266267
GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
267268
GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
269+
GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
268270

269271
# usage: corrupt_graph_and_verify <position> <data> <string>
270272
# Manipulates the commit-graph file at the position
@@ -334,4 +336,9 @@ test_expect_success 'detect incorrect OID order' '
334336
"incorrect OID order"
335337
'
336338

339+
test_expect_success 'detect OID not in object database' '
340+
corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
341+
"from object database"
342+
'
343+
337344
test_done

0 commit comments

Comments
 (0)