summaryrefslogtreecommitdiff
path: root/commit-graph.c
diff options
context:
space:
mode:
authorDerrick Stolee <dstolee@microsoft.com>2019-06-18 18:14:25 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-06-20 03:46:26 (GMT)
commit5c84b3396c73382eb49285b4e4bbb1bf82be3a9c (patch)
treec7a49e92ec1657183c5b51ddc4bde481d3a378f9 /commit-graph.c
parent3cbc6ed3ee02ab7c8254d51528ca75644c52d52d (diff)
downloadgit-5c84b3396c73382eb49285b4e4bbb1bf82be3a9c.zip
git-5c84b3396c73382eb49285b4e4bbb1bf82be3a9c.tar.gz
git-5c84b3396c73382eb49285b4e4bbb1bf82be3a9c.tar.bz2
commit-graph: load commit-graph chains
Prepare the logic for reading a chain of commit-graphs. First, look for a file at $OBJDIR/info/commit-graph. If it exists, then use that file and stop. Next, look for the chain file at $OBJDIR/info/commit-graphs/commit-graph-chain. If this file exists, then load the hash values as line-separated values in that file and load $OBJDIR/info/commit-graphs/graph-{hash[i]}.graph for each hash[i] in that file. The file is given in order, so the first hash corresponds to the "base" file and the final hash corresponds to the "tip" file. This implementation assumes that all of the graph-{hash}.graph files are in the same object directory as the commit-graph-chain file. This will be updated in a future change. This change is purposefully simple so we can isolate the different concerns. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'commit-graph.c')
-rw-r--r--commit-graph.c112
1 files changed, 106 insertions, 6 deletions
diff --git a/commit-graph.c b/commit-graph.c
index 96b07a6..f7dfc6a 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -45,6 +45,19 @@ char *get_commit_graph_filename(const char *obj_dir)
return xstrfmt("%s/info/commit-graph", obj_dir);
}
+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);
+}
+
+static char *get_chain_filename(const char *obj_dir)
+{
+ return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
+}
+
static uint8_t oid_version(void)
{
return 1;
@@ -286,18 +299,105 @@ static struct commit_graph *load_commit_graph_one(const char *graph_file)
return load_commit_graph_one_fd_st(fd, &st);
}
+static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
+{
+ char *graph_name = get_commit_graph_filename(obj_dir);
+ struct commit_graph *g = load_commit_graph_one(graph_name);
+ free(graph_name);
+
+ return g;
+}
+
+static int add_graph_to_chain(struct commit_graph *g,
+ struct commit_graph *chain,
+ struct object_id *oids,
+ int n)
+{
+ struct commit_graph *cur_g = chain;
+
+ while (n) {
+ n--;
+ cur_g = cur_g->base_graph;
+ }
+
+ g->base_graph = chain;
+
+ if (chain)
+ g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
+
+ return 1;
+}
+
+static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
+{
+ 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);
+ FILE *fp;
+ int stat_res;
+
+ fp = fopen(chain_name, "r");
+ stat_res = stat(chain_name, &st);
+ free(chain_name);
+
+ if (!fp ||
+ stat_res ||
+ st.st_size <= the_hash_algo->hexsz)
+ return NULL;
+
+ 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;
+
+ if (strbuf_getline_lf(&line, fp) == EOF)
+ break;
+
+ if (get_oid_hex(line.buf, &oids[i])) {
+ warning(_("invalid commit-graph chain: line '%s' not a hash"),
+ line.buf);
+ valid = 0;
+ break;
+ }
+
+ graph_name = get_split_graph_filename(obj_dir, line.buf);
+ g = load_commit_graph_one(graph_name);
+ free(graph_name);
+
+ if (g && add_graph_to_chain(g, graph_chain, oids, i))
+ graph_chain = g;
+ else
+ valid = 0;
+ }
+
+ free(oids);
+ fclose(fp);
+
+ return graph_chain;
+}
+
+static 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);
+
+ if (!g)
+ g = load_commit_graph_chain(r, obj_dir);
+
+ return g;
+}
+
static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
{
- char *graph_name;
if (r->objects->commit_graph)
return;
- graph_name = get_commit_graph_filename(obj_dir);
- r->objects->commit_graph =
- load_commit_graph_one(graph_name);
-
- FREE_AND_NULL(graph_name);
+ r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
}
/*