@matheusbsbs / matheus-bagliano / commits / 663b2b1b90
Fork 0

maintenance: add commit-graph task

The first new task in the 'git maintenance' builtin is the 'commit-graph' task. This updates the commit-graph file incrementally with the command git commit-graph write --reachable --split By writing an incremental commit-graph file using the "--split" option we minimize the disruption from this operation. The default behavior is to merge layers until the new "top" layer is less than half the size of the layer below. This provides quick writes most of the time, with the longer writes following a power law distribution. Most importantly, concurrent Git processes only look at the commit-graph-chain file for a very short amount of time, so they will verly likely not be holding a handle to the file when we try to replace it. (This only matters on Windows.) If a concurrent process reads the old commit-graph-chain file, but our job expires some of the .graph files before they can be read, then those processes will see a warning message (but not fail). This could be avoided by a future update to use the --expire-time argument when writing the commit-graph. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 17, 2020 at 18:11 UTC 663b2b1b90bf76275044824ddeca96aaec240f09
5 files changed +45 -4
Documentation/git-maintenance.txt
+8
@@ -35,6 +35,14 @@ run::
35 TASKS
36 -----
37
38 +commit-graph::
39 + The `commit-graph` job updates the `commit-graph` files incrementally,
40 + then verifies that the written data is correct. The incremental
41 + write is safe to run alongside concurrent Git processes since it
42 + will not expire `.graph` files that were in the previous
43 + `commit-graph-chain` file. They will be deleted by a later run based
44 + on the expiration delay.
45 +
46 gc::
47 Clean up unnecessary files and optimize the local repository. "GC"
48 stands for "garbage collection," but this task performs many
builtin/gc.c
+30
@@ -710,6 +710,31 @@ struct maintenance_run_opts {
710 int quiet;
711 };
712
713 +static int run_write_commit_graph(struct maintenance_run_opts *opts)
714 +{
715 + struct child_process child = CHILD_PROCESS_INIT;
716 +
717 + child.git_cmd = 1;
718 + strvec_pushl(&child.args, "commit-graph", "write",
719 + "--split", "--reachable", NULL);
720 +
721 + if (opts->quiet)
722 + strvec_push(&child.args, "--no-progress");
723 +
724 + return !!run_command(&child);
725 +}
726 +
727 +static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
728 +{
729 + close_object_store(the_repository->objects);
730 + if (run_write_commit_graph(opts)) {
731 + error(_("failed to write commit-graph"));
732 + return 1;
733 + }
734 +
735 + return 0;
736 +}
737 +
738 static int maintenance_task_gc(struct maintenance_run_opts *opts)
739 {
740 struct child_process child = CHILD_PROCESS_INIT;
@@ -738,6 +763,7 @@ struct maintenance_task {
763
764 enum maintenance_task_label {
765 TASK_GC,
766 + TASK_COMMIT_GRAPH,
767
768 /* Leave as final value */
769 TASK__COUNT
@@ -749,6 +775,10 @@ static struct maintenance_task tasks[] = {
775 maintenance_task_gc,
776 1,
777 },
778 + [TASK_COMMIT_GRAPH] = {
779 + "commit-graph",
780 + maintenance_task_commit_graph,
781 + },
782 };
783
784 static int maintenance_run_tasks(struct maintenance_run_opts *opts)
commit-graph.c
+4 -4
@@ -172,7 +172,7 @@ static char *get_split_graph_filename(struct object_directory *odb,
172 oid_hex);
173 }
174
175 -static char *get_chain_filename(struct object_directory *odb)
175 +char *get_commit_graph_chain_filename(struct object_directory *odb)
176 {
177 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
178 }
@@ -516,7 +516,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
516 struct stat st;
517 struct object_id *oids;
518 int i = 0, valid = 1, count;
519 - char *chain_name = get_chain_filename(odb);
519 + char *chain_name = get_commit_graph_chain_filename(odb);
520 FILE *fp;
521 int stat_res;
522
@@ -1668,7 +1668,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1668 }
1669
1670 if (ctx->split) {
1671 - char *lock_name = get_chain_filename(ctx->odb);
1671 + char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1672
1673 hold_lock_file_for_update_mode(&lk, lock_name,
1674 LOCK_DIE_ON_ERROR, 0444);
@@ -2038,7 +2038,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2038 if (ctx->split_opts && ctx->split_opts->expire_time)
2039 expire_time = ctx->split_opts->expire_time;
2040 if (!ctx->split) {
2041 - char *chain_file_name = get_chain_filename(ctx->odb);
2041 + char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2042 unlink(chain_file_name);
2043 free(chain_file_name);
2044 ctx->num_commit_graphs_after = 0;
commit-graph.h
+1
@@ -25,6 +25,7 @@ struct raw_object_store;
25 struct string_list;
26
27 char *get_commit_graph_filename(struct object_directory *odb);
28 +char *get_commit_graph_chain_filename(struct object_directory *odb);
29 int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
30
31 /*
t/t7900-maintenance.sh
+2
@@ -4,6 +4,8 @@ test_description='git maintenance builtin'
4
5 . ./test-lib.sh
6
7 +GIT_TEST_COMMIT_GRAPH=0
8 +
9 test_expect_success 'help text' '
10 test_expect_code 129 git maintenance -h 2>err &&
11 test_i18ngrep "usage: git maintenance run" err &&