Skip to content

Commit 663b2b1

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3103e98 commit 663b2b1

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

Documentation/git-maintenance.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ run::
3535
TASKS
3636
-----
3737

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+
3846
gc::
3947
Clean up unnecessary files and optimize the local repository. "GC"
4048
stands for "garbage collection," but this task performs many

builtin/gc.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,31 @@ struct maintenance_run_opts {
710710
int quiet;
711711
};
712712

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+
713738
static int maintenance_task_gc(struct maintenance_run_opts *opts)
714739
{
715740
struct child_process child = CHILD_PROCESS_INIT;
@@ -738,6 +763,7 @@ struct maintenance_task {
738763

739764
enum maintenance_task_label {
740765
TASK_GC,
766+
TASK_COMMIT_GRAPH,
741767

742768
/* Leave as final value */
743769
TASK__COUNT
@@ -749,6 +775,10 @@ static struct maintenance_task tasks[] = {
749775
maintenance_task_gc,
750776
1,
751777
},
778+
[TASK_COMMIT_GRAPH] = {
779+
"commit-graph",
780+
maintenance_task_commit_graph,
781+
},
752782
};
753783

754784
static int maintenance_run_tasks(struct maintenance_run_opts *opts)

commit-graph.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static char *get_split_graph_filename(struct object_directory *odb,
172172
oid_hex);
173173
}
174174

175-
static char *get_chain_filename(struct object_directory *odb)
175+
char *get_commit_graph_chain_filename(struct object_directory *odb)
176176
{
177177
return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
178178
}
@@ -516,7 +516,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
516516
struct stat st;
517517
struct object_id *oids;
518518
int i = 0, valid = 1, count;
519-
char *chain_name = get_chain_filename(odb);
519+
char *chain_name = get_commit_graph_chain_filename(odb);
520520
FILE *fp;
521521
int stat_res;
522522

@@ -1668,7 +1668,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
16681668
}
16691669

16701670
if (ctx->split) {
1671-
char *lock_name = get_chain_filename(ctx->odb);
1671+
char *lock_name = get_commit_graph_chain_filename(ctx->odb);
16721672

16731673
hold_lock_file_for_update_mode(&lk, lock_name,
16741674
LOCK_DIE_ON_ERROR, 0444);
@@ -2038,7 +2038,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
20382038
if (ctx->split_opts && ctx->split_opts->expire_time)
20392039
expire_time = ctx->split_opts->expire_time;
20402040
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);
20422042
unlink(chain_file_name);
20432043
free(chain_file_name);
20442044
ctx->num_commit_graphs_after = 0;

commit-graph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct raw_object_store;
2525
struct string_list;
2626

2727
char *get_commit_graph_filename(struct object_directory *odb);
28+
char *get_commit_graph_chain_filename(struct object_directory *odb);
2829
int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
2930

3031
/*

t/t7900-maintenance.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ test_description='git maintenance builtin'
44

55
. ./test-lib.sh
66

7+
GIT_TEST_COMMIT_GRAPH=0
8+
79
test_expect_success 'help text' '
810
test_expect_code 129 git maintenance -h 2>err &&
911
test_i18ngrep "usage: git maintenance run" err &&

0 commit comments

Comments
 (0)