Merge branch 'jm/send-email-tls-auth-on-batch'
[gitweb.git] / commit-graph.c
index de656ebbaff88d692fc26b3f8a57d3d8798070ba..41a0133ff7506131d4966bd6bdee9cf6ea7614a2 100644 (file)
@@ -7,6 +7,7 @@
 #include "packfile.h"
 #include "commit.h"
 #include "object.h"
+#include "refs.h"
 #include "revision.h"
 #include "sha1-lookup.h"
 #include "commit-graph.h"
@@ -248,7 +249,7 @@ static struct commit_list **insert_parent_or_die(struct commit_graph *g,
                die("invalid parent position %"PRIu64, pos);
 
        hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
-       c = lookup_commit(&oid);
+       c = lookup_commit(the_repository, &oid);
        if (!c)
                die("could not find commit %s", oid_to_hex(&oid));
        c->graph_pos = pos;
@@ -362,7 +363,7 @@ static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *
                                           GRAPH_DATA_WIDTH * (c->graph_pos);
 
        hashcpy(oid.hash, commit_data);
-       c->maybe_tree = lookup_tree(&oid);
+       c->maybe_tree = lookup_tree(the_repository, &oid);
 
        return c->maybe_tree;
 }
@@ -592,7 +593,7 @@ static void close_reachable(struct packed_oid_list *oids)
        struct commit *commit;
 
        for (i = 0; i < oids->nr; i++) {
-               commit = lookup_commit(&oids->list[i]);
+               commit = lookup_commit(the_repository, &oids->list[i]);
                if (commit)
                        commit->object.flags |= UNINTERESTING;
        }
@@ -603,14 +604,14 @@ static void close_reachable(struct packed_oid_list *oids)
         * closure.
         */
        for (i = 0; i < oids->nr; i++) {
-               commit = lookup_commit(&oids->list[i]);
+               commit = lookup_commit(the_repository, &oids->list[i]);
 
                if (commit && !parse_commit(commit))
                        add_missing_parents(oids, commit);
        }
 
        for (i = 0; i < oids->nr; i++) {
-               commit = lookup_commit(&oids->list[i]);
+               commit = lookup_commit(the_repository, &oids->list[i]);
 
                if (commit)
                        commit->object.flags &= ~UNINTERESTING;
@@ -656,11 +657,28 @@ static void compute_generation_numbers(struct packed_commit_list* commits)
        }
 }
 
+static int add_ref_to_list(const char *refname,
+                          const struct object_id *oid,
+                          int flags, void *cb_data)
+{
+       struct string_list *list = (struct string_list *)cb_data;
+
+       string_list_append(list, oid_to_hex(oid));
+       return 0;
+}
+
+void write_commit_graph_reachable(const char *obj_dir, int append)
+{
+       struct string_list list;
+
+       string_list_init(&list, 1);
+       for_each_ref(add_ref_to_list, &list);
+       write_commit_graph(obj_dir, NULL, &list, append);
+}
+
 void write_commit_graph(const char *obj_dir,
-                       const char **pack_indexes,
-                       int nr_packs,
-                       const char **commit_hex,
-                       int nr_commits,
+                       struct string_list *pack_indexes,
+                       struct string_list *commit_hex,
                        int append)
 {
        struct packed_oid_list oids;
@@ -701,10 +719,10 @@ void write_commit_graph(const char *obj_dir,
                int dirlen;
                strbuf_addf(&packname, "%s/pack/", obj_dir);
                dirlen = packname.len;
-               for (i = 0; i < nr_packs; i++) {
+               for (i = 0; i < pack_indexes->nr; i++) {
                        struct packed_git *p;
                        strbuf_setlen(&packname, dirlen);
-                       strbuf_addstr(&packname, pack_indexes[i]);
+                       strbuf_addstr(&packname, pack_indexes->items[i].string);
                        p = add_packed_git(packname.buf, packname.len, 1);
                        if (!p)
                                die("error adding pack %s", packname.buf);
@@ -717,15 +735,16 @@ void write_commit_graph(const char *obj_dir,
        }
 
        if (commit_hex) {
-               for (i = 0; i < nr_commits; i++) {
+               for (i = 0; i < commit_hex->nr; i++) {
                        const char *end;
                        struct object_id oid;
                        struct commit *result;
 
-                       if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
+                       if (commit_hex->items[i].string &&
+                           parse_oid_hex(commit_hex->items[i].string, &oid, &end))
                                continue;
 
-                       result = lookup_commit_reference_gently(&oid, 1);
+                       result = lookup_commit_reference_gently(the_repository, &oid, 1);
 
                        if (result) {
                                ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
@@ -761,7 +780,7 @@ void write_commit_graph(const char *obj_dir,
                if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
                        continue;
 
-               commits.list[commits.nr] = lookup_commit(&oids.list[i]);
+               commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
                parse_commit(commits.list[commits.nr]);
 
                for (parent = commits.list[commits.nr]->parents;
@@ -833,6 +852,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, ...)
@@ -852,8 +872,10 @@ static void graph_report(const char *fmt, ...)
 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");
@@ -872,6 +894,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;
 
@@ -893,7 +924,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                        cur_fanout_pos++;
                }
 
-               graph_commit = lookup_commit(&cur_oid);
+               graph_commit = lookup_commit(r, &cur_oid);
                if (!parse_commit_in_graph_one(g, graph_commit))
                        graph_report("failed to parse %s from commit-graph",
                                     oid_to_hex(&cur_oid));
@@ -909,7 +940,7 @@ 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++) {
@@ -919,7 +950,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
 
                hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
 
-               graph_commit = lookup_commit(&cur_oid);
+               graph_commit = lookup_commit(r, &cur_oid);
                odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
                if (parse_commit_internal(odb_commit, 0, 0)) {
                        graph_report("failed to parse %s from object database",
@@ -986,6 +1017,12 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                                     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;