Merge branch 'di/readme-markup-fix'
[gitweb.git] / commit-graph.c
index fba705bc51e507368b75d671edd15c7eaf0fc5a7..b3c4de79b6da4502726dbec5e21b11734d76e28e 100644 (file)
 
 char *get_commit_graph_filename(const char *obj_dir)
 {
-       return xstrfmt("%s/info/commit-graph", obj_dir);
+       char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
+       char *normalized = xmalloc(strlen(filename) + 1);
+       normalize_path_copy(normalized, filename);
+       free(filename);
+       return normalized;
 }
 
 static char *get_split_graph_filename(const char *obj_dir,
                                      const char *oid_hex)
 {
-       return xstrfmt("%s/info/commit-graphs/graph-%s.graph",
-                      obj_dir,
-                      oid_hex);
+       char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
+                                obj_dir,
+                                oid_hex);
+       char *normalized = xmalloc(strlen(filename) + 1);
+       normalize_path_copy(normalized, filename);
+       free(filename);
+       return normalized;
 }
 
 static char *get_chain_filename(const char *obj_dir)
@@ -289,8 +297,10 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
 
        hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
 
-       if (verify_commit_graph_lite(graph))
+       if (verify_commit_graph_lite(graph)) {
+               free(graph);
                return NULL;
+       }
 
        return graph;
 }
@@ -428,7 +438,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
        return graph_chain;
 }
 
-static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
+struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
 {
        struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
 
@@ -580,6 +590,11 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
        item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
 }
 
+static inline void set_commit_tree(struct commit *c, struct tree *t)
+{
+       c->maybe_tree = t;
+}
+
 static int fill_commit_in_graph(struct repository *r,
                                struct commit *item,
                                struct commit_graph *g, uint32_t pos)
@@ -608,7 +623,7 @@ static int fill_commit_in_graph(struct repository *r,
 
        item->object.parsed = 1;
 
-       item->maybe_tree = NULL;
+       set_commit_tree(item, NULL);
 
        date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
        date_low = get_be32(commit_data + g->hash_len + 12);
@@ -710,7 +725,7 @@ static struct tree *load_tree_for_commit(struct repository *r,
                        GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
 
        hashcpy(oid.hash, commit_data);
-       c->maybe_tree = lookup_tree(r, &oid);
+       set_commit_tree(c, lookup_tree(r, &oid));
 
        return c->maybe_tree;
 }
@@ -746,7 +761,7 @@ struct packed_oid_list {
 
 struct write_commit_graph_context {
        struct repository *r;
-       const char *obj_dir;
+       char *obj_dir;
        char *graph_name;
        struct packed_oid_list oids;
        struct packed_commit_list commits;
@@ -768,6 +783,8 @@ struct write_commit_graph_context {
        unsigned append:1,
                 report_progress:1,
                 split:1;
+
+       const struct split_commit_graph_opts *split_opts;
 };
 
 static void write_graph_chunk_fanout(struct hashfile *f,
@@ -1116,14 +1133,15 @@ static int add_ref_to_list(const char *refname,
        return 0;
 }
 
-int write_commit_graph_reachable(const char *obj_dir, unsigned int flags)
+int write_commit_graph_reachable(const char *obj_dir, unsigned int flags,
+                                const struct split_commit_graph_opts *split_opts)
 {
        struct string_list list = STRING_LIST_INIT_DUP;
        int result;
 
        for_each_ref(add_ref_to_list, &list);
        result = write_commit_graph(obj_dir, NULL, &list,
-                                   flags);
+                                   flags, split_opts);
 
        string_list_clear(&list, 0);
        return result;
@@ -1498,20 +1516,25 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
        return 0;
 }
 
-static int split_strategy_max_commits = 64000;
-static float split_strategy_size_mult = 2.0f;
-
 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
 {
        struct commit_graph *g = ctx->r->objects->commit_graph;
        uint32_t num_commits = ctx->commits.nr;
        uint32_t i;
 
+       int max_commits = 0;
+       int size_mult = 2;
+
+       if (ctx->split_opts) {
+               max_commits = ctx->split_opts->max_commits;
+               size_mult = ctx->split_opts->size_multiple;
+       }
+
        g = ctx->r->objects->commit_graph;
        ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
 
-       while (g && (g->num_commits <= split_strategy_size_mult * num_commits ||
-                    num_commits > split_strategy_max_commits)) {
+       while (g && (g->num_commits <= size_mult * num_commits ||
+                   (max_commits && num_commits > max_commits))) {
                if (strcmp(g->obj_dir, ctx->obj_dir))
                        break;
 
@@ -1652,13 +1675,87 @@ static void merge_commit_graphs(struct write_commit_graph_context *ctx)
        sort_and_scan_merged_commits(ctx);
 }
 
+static void mark_commit_graphs(struct write_commit_graph_context *ctx)
+{
+       uint32_t i;
+       time_t now = time(NULL);
+
+       for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
+               struct stat st;
+               struct utimbuf updated_time;
+
+               stat(ctx->commit_graph_filenames_before[i], &st);
+
+               updated_time.actime = st.st_atime;
+               updated_time.modtime = now;
+               utime(ctx->commit_graph_filenames_before[i], &updated_time);
+       }
+}
+
+static void expire_commit_graphs(struct write_commit_graph_context *ctx)
+{
+       struct strbuf path = STRBUF_INIT;
+       DIR *dir;
+       struct dirent *de;
+       size_t dirnamelen;
+       timestamp_t expire_time = time(NULL);
+
+       if (ctx->split_opts && ctx->split_opts->expire_time)
+               expire_time -= ctx->split_opts->expire_time;
+       if (!ctx->split) {
+               char *chain_file_name = get_chain_filename(ctx->obj_dir);
+               unlink(chain_file_name);
+               free(chain_file_name);
+               ctx->num_commit_graphs_after = 0;
+       }
+
+       strbuf_addstr(&path, ctx->obj_dir);
+       strbuf_addstr(&path, "/info/commit-graphs");
+       dir = opendir(path.buf);
+
+       if (!dir) {
+               strbuf_release(&path);
+               return;
+       }
+
+       strbuf_addch(&path, '/');
+       dirnamelen = path.len;
+       while ((de = readdir(dir)) != NULL) {
+               struct stat st;
+               uint32_t i, found = 0;
+
+               strbuf_setlen(&path, dirnamelen);
+               strbuf_addstr(&path, de->d_name);
+
+               stat(path.buf, &st);
+
+               if (st.st_mtime > expire_time)
+                       continue;
+               if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
+                       continue;
+
+               for (i = 0; i < ctx->num_commit_graphs_after; i++) {
+                       if (!strcmp(ctx->commit_graph_filenames_after[i],
+                                   path.buf)) {
+                               found = 1;
+                               break;
+                       }
+               }
+
+               if (!found)
+                       unlink(path.buf);
+       }
+}
+
 int write_commit_graph(const char *obj_dir,
                       struct string_list *pack_indexes,
                       struct string_list *commit_hex,
-                      unsigned int flags)
+                      unsigned int flags,
+                      const struct split_commit_graph_opts *split_opts)
 {
        struct write_commit_graph_context *ctx;
        uint32_t i, count_distinct = 0;
+       size_t len;
        int res = 0;
 
        if (!commit_graph_compatible(the_repository))
@@ -1666,10 +1763,18 @@ int write_commit_graph(const char *obj_dir,
 
        ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
        ctx->r = the_repository;
-       ctx->obj_dir = obj_dir;
+
+       /* normalize object dir with no trailing slash */
+       ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
+       normalize_path_copy(ctx->obj_dir, obj_dir);
+       len = strlen(ctx->obj_dir);
+       if (len && ctx->obj_dir[len - 1] == '/')
+               ctx->obj_dir[len - 1] = 0;
+
        ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
        ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
        ctx->split = flags & COMMIT_GRAPH_SPLIT ? 1 : 0;
+       ctx->split_opts = split_opts;
 
        if (ctx->split) {
                struct commit_graph *g;
@@ -1697,8 +1802,8 @@ int write_commit_graph(const char *obj_dir,
        ctx->approx_nr_objects = approximate_object_count();
        ctx->oids.alloc = ctx->approx_nr_objects / 32;
 
-       if (ctx->split && ctx->oids.alloc > split_strategy_max_commits)
-               ctx->oids.alloc = split_strategy_max_commits;
+       if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
+               ctx->oids.alloc = split_opts->max_commits;
 
        if (ctx->append) {
                prepare_commit_graph_one(ctx->r, ctx->obj_dir);
@@ -1764,10 +1869,16 @@ int write_commit_graph(const char *obj_dir,
 
        res = write_commit_graph_file(ctx);
 
+       if (ctx->split)
+               mark_commit_graphs(ctx);
+
+       expire_commit_graphs(ctx);
+
 cleanup:
        free(ctx->graph_name);
        free(ctx->commits.list);
        free(ctx->oids.list);
+       free(ctx->obj_dir);
 
        if (ctx->commit_graph_filenames_after) {
                for (i = 0; i < ctx->num_commit_graphs_after; i++) {
@@ -1805,7 +1916,7 @@ static void graph_report(const char *fmt, ...)
 #define GENERATION_ZERO_EXISTS 1
 #define GENERATION_NUMBER_EXISTS 2
 
-int verify_commit_graph(struct repository *r, struct commit_graph *g)
+int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
 {
        uint32_t i, cur_fanout_pos = 0;
        struct object_id prev_oid, cur_oid, checksum;
@@ -1813,6 +1924,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        struct hashfile *f;
        int devnull;
        struct progress *progress = NULL;
+       int local_error = 0;
 
        if (!g) {
                graph_report("no commit-graph file loaded");
@@ -1883,7 +1995,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(r, &cur_oid);
-               odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
+               odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
                if (parse_commit_internal(odb_commit, 0, 0)) {
                        graph_report(_("failed to parse commit %s from object database for commit-graph"),
                                     oid_to_hex(&cur_oid));
@@ -1907,6 +2019,9 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                                break;
                        }
 
+                       /* parse parent in case it is in a base graph */
+                       parse_commit_in_graph_one(r, g, graph_parents->item);
+
                        if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
                                graph_report(_("commit-graph parent for %s is %s != %s"),
                                             oid_to_hex(&cur_oid),
@@ -1958,7 +2073,12 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        }
        stop_progress(&progress);
 
-       return verify_commit_graph_error;
+       local_error = verify_commit_graph_error;
+
+       if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
+               local_error |= verify_commit_graph(r, g->base_graph, flags);
+
+       return local_error;
 }
 
 void free_commit_graph(struct commit_graph *g)