fsck: verify commit-graph
[gitweb.git] / commit-graph.c
index a79b06f37a674ff800393bbebf9aac682a1015b9..aca9e5e2dc994fdd4febce79982efd323f6558db 100644 (file)
@@ -833,6 +833,7 @@ void write_commit_graph(const char *obj_dir,
        oids.nr = 0;
 }
 
+#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
 static int verify_commit_graph_error;
 
 static void graph_report(const char *fmt, ...)
@@ -846,10 +847,16 @@ static void graph_report(const char *fmt, ...)
        va_end(ap);
 }
 
+#define GENERATION_ZERO_EXISTS 1
+#define GENERATION_NUMBER_EXISTS 2
+
 int verify_commit_graph(struct repository *r, struct commit_graph *g)
 {
        uint32_t i, cur_fanout_pos = 0;
-       struct object_id prev_oid, cur_oid;
+       struct object_id prev_oid, cur_oid, checksum;
+       int generation_zero = 0;
+       struct hashfile *f;
+       int devnull;
 
        if (!g) {
                graph_report("no commit-graph file loaded");
@@ -868,6 +875,15 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        if (verify_commit_graph_error)
                return verify_commit_graph_error;
 
+       devnull = open("/dev/null", O_WRONLY);
+       f = hashfd(devnull, NULL);
+       hashwrite(f, g->data, g->data_len - g->hash_len);
+       finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
+       if (hashcmp(checksum.hash, g->data + g->data_len - g->hash_len)) {
+               graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
+               verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
+       }
+
        for (i = 0; i < g->num_commits; i++) {
                struct commit *graph_commit;
 
@@ -905,12 +921,13 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                cur_fanout_pos++;
        }
 
-       if (verify_commit_graph_error)
+       if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
                return verify_commit_graph_error;
 
        for (i = 0; i < g->num_commits; i++) {
                struct commit *graph_commit, *odb_commit;
                struct commit_list *graph_parents, *odb_parents;
+               uint32_t max_generation = 0;
 
                hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
 
@@ -945,6 +962,9 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                                             oid_to_hex(&graph_parents->item->object.oid),
                                             oid_to_hex(&odb_parents->item->object.oid));
 
+                       if (graph_parents->item->generation > max_generation)
+                               max_generation = graph_parents->item->generation;
+
                        graph_parents = graph_parents->next;
                        odb_parents = odb_parents->next;
                }
@@ -952,6 +972,38 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                if (odb_parents != NULL)
                        graph_report("commit-graph parent list for commit %s terminates early",
                                     oid_to_hex(&cur_oid));
+
+               if (!graph_commit->generation) {
+                       if (generation_zero == GENERATION_NUMBER_EXISTS)
+                               graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
+                                            oid_to_hex(&cur_oid));
+                       generation_zero = GENERATION_ZERO_EXISTS;
+               } else if (generation_zero == GENERATION_ZERO_EXISTS)
+                       graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
+                                    oid_to_hex(&cur_oid));
+
+               if (generation_zero == GENERATION_ZERO_EXISTS)
+                       continue;
+
+               /*
+                * If one of our parents has generation GENERATION_NUMBER_MAX, then
+                * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
+                * extra logic in the following condition.
+                */
+               if (max_generation == GENERATION_NUMBER_MAX)
+                       max_generation--;
+
+               if (graph_commit->generation != max_generation + 1)
+                       graph_report("commit-graph generation for commit %s is %u != %u",
+                                    oid_to_hex(&cur_oid),
+                                    graph_commit->generation,
+                                    max_generation + 1);
+
+               if (graph_commit->date != odb_commit->date)
+                       graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
+                                    oid_to_hex(&cur_oid),
+                                    graph_commit->date,
+                                    odb_commit->date);
        }
 
        return verify_commit_graph_error;