]> git.ipfire.org Git - thirdparty/git.git/blobdiff - commit-graph.c
Merge branch 'ds/default-pack-use-sparse-to-true'
[thirdparty/git.git] / commit-graph.c
index e1673789ba1646222c97dd7c3aac7db1d792c1dd..f013a84e294b13b552b62257bbf2fa7e1c353a82 100644 (file)
 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
                        + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
 
-char *get_commit_graph_filename(const char *obj_dir)
+/* Remember to update object flag allocation in object.h */
+#define REACHABLE       (1u<<15)
+
+char *get_commit_graph_filename(struct object_directory *odb)
 {
-       return xstrfmt("%s/info/commit-graph", obj_dir);
+       return xstrfmt("%s/info/commit-graph", odb->path);
 }
 
-static char *get_split_graph_filename(const char *obj_dir,
+static char *get_split_graph_filename(struct object_directory *odb,
                                      const char *oid_hex)
 {
-       return xstrfmt("%s/info/commit-graphs/graph-%s.graph",
-                      obj_dir,
+       return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
                       oid_hex);
 }
 
-static char *get_chain_filename(const char *obj_dir)
+static char *get_chain_filename(struct object_directory *odb)
 {
-       return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
+       return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
 }
 
 static uint8_t oid_version(void)
@@ -106,7 +108,8 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
        return 1;
 }
 
-struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
+struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
+                                                struct object_directory *odb)
 {
        void *graph_map;
        size_t graph_size;
@@ -122,7 +125,9 @@ struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
        graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
        ret = parse_commit_graph(graph_map, fd, graph_size);
 
-       if (!ret) {
+       if (ret)
+               ret->odb = odb;
+       else {
                munmap(graph_map, graph_size);
                close(fd);
        }
@@ -289,13 +294,16 @@ 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;
 }
 
-static struct commit_graph *load_commit_graph_one(const char *graph_file)
+static struct commit_graph *load_commit_graph_one(const char *graph_file,
+                                                 struct object_directory *odb)
 {
 
        struct stat st;
@@ -306,7 +314,7 @@ static struct commit_graph *load_commit_graph_one(const char *graph_file)
        if (!open_ok)
                return NULL;
 
-       g = load_commit_graph_one_fd_st(fd, &st);
+       g = load_commit_graph_one_fd_st(fd, &st, odb);
 
        if (g)
                g->filename = xstrdup(graph_file);
@@ -314,10 +322,11 @@ static struct commit_graph *load_commit_graph_one(const char *graph_file)
        return g;
 }
 
-static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
+static struct commit_graph *load_commit_graph_v1(struct repository *r,
+                                                struct object_directory *odb)
 {
-       char *graph_name = get_commit_graph_filename(obj_dir);
-       struct commit_graph *g = load_commit_graph_one(graph_name);
+       char *graph_name = get_commit_graph_filename(odb);
+       struct commit_graph *g = load_commit_graph_one(graph_name, odb);
        free(graph_name);
 
        return g;
@@ -356,14 +365,15 @@ static int add_graph_to_chain(struct commit_graph *g,
        return 1;
 }
 
-static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
+static struct commit_graph *load_commit_graph_chain(struct repository *r,
+                                                   struct object_directory *odb)
 {
        struct commit_graph *graph_chain = NULL;
        struct strbuf line = STRBUF_INIT;
        struct stat st;
        struct object_id *oids;
        int i = 0, valid = 1, count;
-       char *chain_name = get_chain_filename(obj_dir);
+       char *chain_name = get_chain_filename(odb);
        FILE *fp;
        int stat_res;
 
@@ -379,9 +389,10 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
        count = st.st_size / (the_hash_algo->hexsz + 1);
        oids = xcalloc(count, sizeof(struct object_id));
 
-       for (i = 0; i < count && valid; i++) {
-               char *graph_name;
-               struct commit_graph *g;
+       prepare_alt_odb(r);
+
+       for (i = 0; i < count; i++) {
+               struct object_directory *odb;
 
                if (strbuf_getline_lf(&line, fp) == EOF)
                        break;
@@ -393,63 +404,86 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
                        break;
                }
 
-               graph_name = get_split_graph_filename(obj_dir, line.buf);
-               g = load_commit_graph_one(graph_name);
-               free(graph_name);
+               valid = 0;
+               for (odb = r->objects->odb; odb; odb = odb->next) {
+                       char *graph_name = get_split_graph_filename(odb, line.buf);
+                       struct commit_graph *g = load_commit_graph_one(graph_name, odb);
 
-               if (g && add_graph_to_chain(g, graph_chain, oids, i))
-                       graph_chain = g;
-               else
-                       valid = 0;
+                       free(graph_name);
+
+                       if (g) {
+                               if (add_graph_to_chain(g, graph_chain, oids, i)) {
+                                       graph_chain = g;
+                                       valid = 1;
+                               }
+
+                               break;
+                       }
+               }
+
+               if (!valid) {
+                       warning(_("unable to find all commit-graph files"));
+                       break;
+               }
        }
 
        free(oids);
        fclose(fp);
+       strbuf_release(&line);
 
        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,
+                                          struct object_directory *odb)
 {
-       struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
+       struct commit_graph *g = load_commit_graph_v1(r, odb);
 
        if (!g)
-               g = load_commit_graph_chain(r, obj_dir);
+               g = load_commit_graph_chain(r, odb);
 
        return g;
 }
 
-static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
+static void prepare_commit_graph_one(struct repository *r,
+                                    struct object_directory *odb)
 {
 
        if (r->objects->commit_graph)
                return;
 
-       r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
+       r->objects->commit_graph = read_commit_graph_one(r, odb);
 }
 
 /*
  * Return 1 if commit_graph is non-NULL, and 0 otherwise.
  *
- * On the first invocation, this function attemps to load the commit
+ * On the first invocation, this function attempts to load the commit
  * graph if the_repository is configured to have one.
  */
 static int prepare_commit_graph(struct repository *r)
 {
        struct object_directory *odb;
-       int config_value;
 
-       if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
-               die("dying as requested by the '%s' variable on commit-graph load!",
-                   GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
+       /*
+        * This must come before the "already attempted?" check below, because
+        * we want to disable even an already-loaded graph file.
+        */
+       if (r->commit_graph_disabled)
+               return 0;
 
        if (r->objects->commit_graph_attempted)
                return !!r->objects->commit_graph;
        r->objects->commit_graph_attempted = 1;
 
+       if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
+               die("dying as requested by the '%s' variable on commit-graph load!",
+                   GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
+
+       prepare_repo_settings(r);
+
        if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
-           (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
-           !config_value))
+           r->settings.core_commit_graph != 1)
                /*
                 * This repository is not configured to use commit graphs, so
                 * do not load one. (But report commit_graph_attempted anyway
@@ -465,7 +499,7 @@ static int prepare_commit_graph(struct repository *r)
        for (odb = r->objects->odb;
             !r->objects->commit_graph && odb;
             odb = odb->next)
-               prepare_commit_graph_one(r, odb->path);
+               prepare_commit_graph_one(r, odb);
        return !!r->objects->commit_graph;
 }
 
@@ -561,6 +595,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)
@@ -589,7 +628,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);
@@ -691,7 +730,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;
 }
@@ -727,7 +766,7 @@ struct packed_oid_list {
 
 struct write_commit_graph_context {
        struct repository *r;
-       const char *obj_dir;
+       struct object_directory *odb;
        char *graph_name;
        struct packed_oid_list oids;
        struct packed_commit_list commits;
@@ -748,7 +787,10 @@ struct write_commit_graph_context {
 
        unsigned append:1,
                 report_progress:1,
-                split:1;
+                split:1,
+                check_oids:1;
+
+       const struct split_commit_graph_opts *split_opts;
 };
 
 static void write_graph_chunk_fanout(struct hashfile *f,
@@ -801,12 +843,16 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
 
        while (list < last) {
                struct commit_list *parent;
+               struct object_id *tree;
                int edge_value;
                uint32_t packedDate[2];
                display_progress(ctx->progress, ++ctx->progress_cnt);
 
-               parse_commit_no_graph(*list);
-               hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
+               if (parse_commit_no_graph(*list))
+                       die(_("unable to parse commit %s"),
+                               oid_to_hex(&(*list)->object.oid));
+               tree = get_commit_tree_oid(*list);
+               hashwrite(f, tree->hash, hash_len);
 
                parent = (*list)->parents;
 
@@ -978,11 +1024,11 @@ static void add_missing_parents(struct write_commit_graph_context *ctx, struct c
 {
        struct commit_list *parent;
        for (parent = commit->parents; parent; parent = parent->next) {
-               if (!(parent->item->object.flags & UNINTERESTING)) {
+               if (!(parent->item->object.flags & REACHABLE)) {
                        ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
                        oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
                        ctx->oids.nr++;
-                       parent->item->object.flags |= UNINTERESTING;
+                       parent->item->object.flags |= REACHABLE;
                }
        }
 }
@@ -1000,7 +1046,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
                display_progress(ctx->progress, i + 1);
                commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
                if (commit)
-                       commit->object.flags |= UNINTERESTING;
+                       commit->object.flags |= REACHABLE;
        }
        stop_progress(&ctx->progress);
 
@@ -1012,7 +1058,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
        if (ctx->report_progress)
                ctx->progress = start_delayed_progress(
                                        _("Expanding reachable commits in commit graph"),
-                                       ctx->oids.nr);
+                                       0);
        for (i = 0; i < ctx->oids.nr; i++) {
                display_progress(ctx->progress, i + 1);
                commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
@@ -1037,7 +1083,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
                commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
 
                if (commit)
-                       commit->object.flags &= ~UNINTERESTING;
+                       commit->object.flags &= ~REACHABLE;
        }
        stop_progress(&ctx->progress);
 }
@@ -1048,7 +1094,7 @@ static void compute_generation_numbers(struct write_commit_graph_context *ctx)
        struct commit_list *list = NULL;
 
        if (ctx->report_progress)
-               ctx->progress = start_progress(
+               ctx->progress = start_delayed_progress(
                                        _("Computing commit graph generation numbers"),
                                        ctx->commits.nr);
        for (i = 0; i < ctx->commits.nr; i++) {
@@ -1097,14 +1143,16 @@ 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(struct object_directory *odb,
+                                enum commit_graph_write_flags 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);
+       result = write_commit_graph(odb, NULL, &list,
+                                   flags, split_opts);
 
        string_list_clear(&list, 0);
        return result;
@@ -1118,7 +1166,7 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
        struct strbuf packname = STRBUF_INIT;
        int dirlen;
 
-       strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
+       strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
        dirlen = packname.len;
        if (ctx->report_progress) {
                strbuf_addf(&progress_title,
@@ -1149,14 +1197,14 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
        }
 
        stop_progress(&ctx->progress);
-       strbuf_reset(&progress_title);
+       strbuf_release(&progress_title);
        strbuf_release(&packname);
 
        return 0;
 }
 
-static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
-                                     struct string_list *commit_hex)
+static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
+                                    struct string_list *commit_hex)
 {
        uint32_t i;
        struct strbuf progress_title = STRBUF_INIT;
@@ -1177,20 +1225,21 @@ static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
                struct commit *result;
 
                display_progress(ctx->progress, i + 1);
-               if (commit_hex->items[i].string &&
-                   parse_oid_hex(commit_hex->items[i].string, &oid, &end))
-                       continue;
-
-               result = lookup_commit_reference_gently(ctx->r, &oid, 1);
-
-               if (result) {
+               if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
+                   (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
                        ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
                        oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
                        ctx->oids.nr++;
+               } else if (ctx->check_oids) {
+                       error(_("invalid commit object id: %s"),
+                           commit_hex->items[i].string);
+                       return -1;
                }
        }
        stop_progress(&ctx->progress);
        strbuf_release(&progress_title);
+
+       return 0;
 }
 
 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
@@ -1238,7 +1287,6 @@ static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
 {
        uint32_t i;
-       struct commit_list *parent;
 
        ctx->num_extra_edges = 0;
        if (ctx->report_progress)
@@ -1246,7 +1294,8 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
                        _("Finding extra edges in commit graph"),
                        ctx->oids.nr);
        for (i = 0; i < ctx->oids.nr; i++) {
-               int num_parents = 0;
+               unsigned int num_parents;
+
                display_progress(ctx->progress, i + 1);
                if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
                        continue;
@@ -1260,10 +1309,7 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
 
                parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
 
-               for (parent = ctx->commits.list[ctx->commits.nr]->parents;
-                    parent; parent = parent->next)
-                       num_parents++;
-
+               num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
                if (num_parents > 2)
                        ctx->num_extra_edges += num_parents - 1;
 
@@ -1316,10 +1362,10 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
                strbuf_addf(&tmp_file,
                            "%s/info/commit-graphs/tmp_graph_XXXXXX",
-                           ctx->obj_dir);
+                           ctx->odb->path);
                ctx->graph_name = strbuf_detach(&tmp_file, NULL);
        } else {
-               ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
+               ctx->graph_name = get_commit_graph_filename(ctx->odb);
        }
 
        if (safe_create_leading_directories(ctx->graph_name)) {
@@ -1330,7 +1376,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
        }
 
        if (ctx->split) {
-               char *lock_name = get_chain_filename(ctx->obj_dir);
+               char *lock_name = get_chain_filename(ctx->odb);
 
                hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
 
@@ -1418,7 +1464,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
        if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
                char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
-               char *new_base_name = get_split_graph_filename(ctx->obj_dir, new_base_hash);
+               char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
 
                free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
                free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
@@ -1454,12 +1500,12 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
                                }
                        }
                } else {
-                       char *graph_name = get_commit_graph_filename(ctx->obj_dir);
+                       char *graph_name = get_commit_graph_filename(ctx->odb);
                        unlink(graph_name);
                }
 
                ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
-               final_graph_name = get_split_graph_filename(ctx->obj_dir,
+               final_graph_name = get_split_graph_filename(ctx->odb,
                                        ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
                ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
 
@@ -1479,20 +1525,31 @@ 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;
+       struct commit_graph *g;
+       uint32_t num_commits;
        uint32_t i;
 
+       int max_commits = 0;
+       int size_mult = 2;
+
+       if (ctx->split_opts) {
+               max_commits = ctx->split_opts->max_commits;
+
+               if (ctx->split_opts->size_multiple)
+                       size_mult = ctx->split_opts->size_multiple;
+       }
+
        g = ctx->r->objects->commit_graph;
+       num_commits = ctx->commits.nr;
        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 (g->odb != ctx->odb)
+                       break;
+
                num_commits += g->num_commits;
                g = g->base_graph;
 
@@ -1501,6 +1558,18 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
 
        ctx->new_base_graph = g;
 
+       if (ctx->num_commit_graphs_after == 2) {
+               char *old_graph_name = get_commit_graph_filename(g->odb);
+
+               if (!strcmp(g->filename, old_graph_name) &&
+                   g->odb != ctx->odb) {
+                       ctx->num_commit_graphs_after = 1;
+                       ctx->new_base_graph = NULL;
+               }
+
+               free(old_graph_name);
+       }
+
        ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
        ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
 
@@ -1555,8 +1624,7 @@ static int commit_compare(const void *_a, const void *_b)
 
 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
 {
-       uint32_t i, num_parents;
-       struct commit_list *parent;
+       uint32_t i;
 
        if (ctx->report_progress)
                ctx->progress = start_delayed_progress(
@@ -1574,12 +1642,11 @@ static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
                        die(_("unexpected duplicate commit id %s"),
                            oid_to_hex(&ctx->commits.list[i]->object.oid));
                } else {
-                       num_parents = 0;
-                       for (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)
-                               num_parents++;
+                       unsigned int num_parents;
 
+                       num_parents = commit_list_count(ctx->commits.list[i]->parents);
                        if (num_parents > 2)
-                               ctx->num_extra_edges += num_parents - 2;
+                               ctx->num_extra_edges += num_parents - 1;
                }
        }
 
@@ -1614,10 +1681,84 @@ static void merge_commit_graphs(struct write_commit_graph_context *ctx)
        sort_and_scan_merged_commits(ctx);
 }
 
-int write_commit_graph(const char *obj_dir,
+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->odb);
+               unlink(chain_file_name);
+               free(chain_file_name);
+               ctx->num_commit_graphs_after = 0;
+       }
+
+       strbuf_addstr(&path, ctx->odb->path);
+       strbuf_addstr(&path, "/info/commit-graphs");
+       dir = opendir(path.buf);
+
+       if (!dir)
+               goto out;
+
+       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);
+       }
+
+out:
+       strbuf_release(&path);
+}
+
+int write_commit_graph(struct object_directory *odb,
                       struct string_list *pack_indexes,
                       struct string_list *commit_hex,
-                      unsigned int flags)
+                      enum commit_graph_write_flags flags,
+                      const struct split_commit_graph_opts *split_opts)
 {
        struct write_commit_graph_context *ctx;
        uint32_t i, count_distinct = 0;
@@ -1628,10 +1769,12 @@ 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;
-       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->odb = odb;
+       ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
+       ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
+       ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
+       ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
+       ctx->split_opts = split_opts;
 
        if (ctx->split) {
                struct commit_graph *g;
@@ -1659,11 +1802,11 @@ 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);
+               prepare_commit_graph_one(ctx->r, ctx->odb);
                if (ctx->r->objects->commit_graph)
                        ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
        }
@@ -1685,8 +1828,10 @@ int write_commit_graph(const char *obj_dir,
                        goto cleanup;
        }
 
-       if (commit_hex)
-               fill_oids_from_commit_hex(ctx, commit_hex);
+       if (commit_hex) {
+               if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
+                       goto cleanup;
+       }
 
        if (!pack_indexes && !commit_hex)
                fill_oids_from_all_packs(ctx);
@@ -1726,6 +1871,11 @@ 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);
@@ -1767,7 +1917,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;
@@ -1775,6 +1925,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");
@@ -1834,8 +1985,10 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
                return verify_commit_graph_error;
 
-       progress = start_progress(_("Verifying commits in commit graph"),
-                                 g->num_commits);
+       if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
+               progress = start_progress(_("Verifying commits in commit graph"),
+                                       g->num_commits);
+
        for (i = 0; i < g->num_commits; i++) {
                struct commit *graph_commit, *odb_commit;
                struct commit_list *graph_parents, *odb_parents;
@@ -1845,7 +1998,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));
@@ -1869,6 +2022,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),
@@ -1920,7 +2076,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)
@@ -1935,3 +2096,8 @@ void free_commit_graph(struct commit_graph *g)
        free(g->filename);
        free(g);
 }
+
+void disable_commit_graph(struct repository *r)
+{
+       r->commit_graph_disabled = 1;
+}