struct commit_graph *g = load_commit_graph_one(graph_name);
free(graph_name);
+ if (g)
+ g->obj_dir = obj_dir;
+
return g;
}
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;
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->path, line.buf);
+ struct commit_graph *g = load_commit_graph_one(graph_name);
- if (g && add_graph_to_chain(g, graph_chain, oids, i))
- graph_chain = g;
- else
- valid = 0;
+ free(graph_name);
+
+ if (g) {
+ g->obj_dir = odb->path;
+
+ 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);
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);
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,
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;
return 0;
}
-static void init_commit_graph_chain(struct write_commit_graph_context *ctx)
-{
- struct commit_graph *g = ctx->r->objects->commit_graph;
- uint32_t i;
-
- ctx->new_base_graph = g;
- ctx->base_graph_name = xstrdup(g->filename);
- ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
-
- ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
-
- ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
- ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
-
- for (i = 0; i < ctx->num_commit_graphs_before - 1; i++)
- ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
-
- if (ctx->num_commit_graphs_before)
- ctx->commit_graph_filenames_after[ctx->num_commit_graphs_before - 1] =
- get_split_graph_filename(ctx->obj_dir, oid_to_hex(&g->oid));
-
- i = ctx->num_commit_graphs_before - 1;
-
- while (g) {
- ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
- i--;
- g = g->base_graph;
- }
-}
-
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
{
uint32_t i;
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->obj_dir, 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]);
}
if (ctx->base_graph_name) {
- result = rename(ctx->base_graph_name,
- ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
+ const char *dest = ctx->commit_graph_filenames_after[
+ ctx->num_commit_graphs_after - 2];
+
+ if (strcmp(ctx->base_graph_name, dest)) {
+ result = rename(ctx->base_graph_name, dest);
- if (result) {
- error(_("failed to rename base commit-graph file"));
- return -1;
+ if (result) {
+ error(_("failed to rename base commit-graph file"));
+ return -1;
+ }
}
} else {
char *graph_name = get_commit_graph_filename(ctx->obj_dir);
return 0;
}
+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 <= size_mult * num_commits ||
+ (max_commits && num_commits > max_commits))) {
+ if (strcmp(g->obj_dir, ctx->obj_dir))
+ break;
+
+ num_commits += g->num_commits;
+ g = g->base_graph;
+
+ ctx->num_commit_graphs_after--;
+ }
+
+ ctx->new_base_graph = g;
+
+ if (ctx->num_commit_graphs_after == 2) {
+ char *old_graph_name = get_commit_graph_filename(g->obj_dir);
+
+ if (!strcmp(g->filename, old_graph_name) &&
+ strcmp(g->obj_dir, ctx->obj_dir)) {
+ 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);
+
+ for (i = 0; i < ctx->num_commit_graphs_after &&
+ i < ctx->num_commit_graphs_before; i++)
+ ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
+
+ i = ctx->num_commit_graphs_before - 1;
+ g = ctx->r->objects->commit_graph;
+
+ while (g) {
+ if (i < ctx->num_commit_graphs_after)
+ ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
+
+ i--;
+ g = g->base_graph;
+ }
+}
+
+static void merge_commit_graph(struct write_commit_graph_context *ctx,
+ struct commit_graph *g)
+{
+ uint32_t i;
+ uint32_t offset = g->num_commits_in_base;
+
+ ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
+
+ for (i = 0; i < g->num_commits; i++) {
+ struct object_id oid;
+ struct commit *result;
+
+ display_progress(ctx->progress, i + 1);
+
+ load_oid_from_graph(g, i + offset, &oid);
+
+ /* only add commits if they still exist in the repo */
+ result = lookup_commit_reference_gently(ctx->r, &oid, 1);
+
+ if (result) {
+ ctx->commits.list[ctx->commits.nr] = result;
+ ctx->commits.nr++;
+ }
+ }
+}
+
+static int commit_compare(const void *_a, const void *_b)
+{
+ const struct commit *a = *(const struct commit **)_a;
+ const struct commit *b = *(const struct commit **)_b;
+ return oidcmp(&a->object.oid, &b->object.oid);
+}
+
+static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
+{
+ uint32_t i, num_parents;
+ struct commit_list *parent;
+
+ if (ctx->report_progress)
+ ctx->progress = start_delayed_progress(
+ _("Scanning merged commits"),
+ ctx->commits.nr);
+
+ QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
+
+ ctx->num_extra_edges = 0;
+ for (i = 0; i < ctx->commits.nr; i++) {
+ display_progress(ctx->progress, i);
+
+ if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
+ &ctx->commits.list[i]->object.oid)) {
+ 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++;
+
+ if (num_parents > 2)
+ ctx->num_extra_edges += num_parents - 2;
+ }
+ }
+
+ stop_progress(&ctx->progress);
+}
+
+static void merge_commit_graphs(struct write_commit_graph_context *ctx)
+{
+ struct commit_graph *g = ctx->r->objects->commit_graph;
+ uint32_t current_graph_number = ctx->num_commit_graphs_before;
+ struct strbuf progress_title = STRBUF_INIT;
+
+ while (g && current_graph_number >= ctx->num_commit_graphs_after) {
+ current_graph_number--;
+
+ if (ctx->report_progress) {
+ strbuf_addstr(&progress_title, _("Merging commit-graph"));
+ ctx->progress = start_delayed_progress(progress_title.buf, 0);
+ }
+
+ merge_commit_graph(ctx, g);
+ stop_progress(&ctx->progress);
+ strbuf_release(&progress_title);
+
+ g = g->base_graph;
+ }
+
+ if (g) {
+ ctx->new_base_graph = g;
+ ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
+ }
+
+ if (ctx->new_base_graph)
+ ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
+
+ 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;
+
+ 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;
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;
ctx->approx_nr_objects = approximate_object_count();
ctx->oids.alloc = ctx->approx_nr_objects / 32;
+ 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);
if (ctx->r->objects->commit_graph)
if (!ctx->commits.nr)
goto cleanup;
- if (ctx->split)
- init_commit_graph_chain(ctx);
- else
+ if (ctx->split) {
+ split_graph_merge_strategy(ctx);
+
+ merge_commit_graphs(ctx);
+ } else
ctx->num_commit_graphs_after = 1;
compute_generation_numbers(ctx);
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);
#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;
struct hashfile *f;
int devnull;
struct progress *progress = NULL;
+ int local_error = 0;
if (!g) {
graph_report("no commit-graph file loaded");
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),
}
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)