Skip to content

Commit eb74e4a

Browse files
committed
maintenance: add auto condition for commit-graph task
Instead of writing a new commit-graph in every 'git maintenance run --auto' process (when maintenance.commit-graph.enalbed is configured to be true), only write when there are "enough" commits not in a commit-graph file. This count is controlled by the maintenance.commit-graph.auto config option. To compute the count, use a depth-first search starting at each ref, and leaving markers using the PARENT1 flag. If this count reaches the limit, then terminate early and start the task. Otherwise, this operation will peel every ref and parse the commit it points to. If these are all in the commit-graph, then this is typically a very fast operation. Users with many refs might feel a slow-down, and hence could consider updating their limit to be very small. A negative value will force the step to run every time. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 67fe2e7 commit eb74e4a

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

Documentation/config/maintenance.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ maintenance.<task>.enabled::
22
This boolean config option controls whether the maintenance task
33
with name `<task>` is run when no `--task` option is specified.
44
By default, only `maintenance.gc.enabled` is true.
5+
6+
maintenance.commit-graph.auto::
7+
This integer config option controls how often the `commit-graph` task
8+
should be run as part of `git maintenance run --auto`. If zero, then
9+
the `commit-graph` task will not run with the `--auto` option. A
10+
negative value will force the task to run every time. Otherwise, a
11+
positive value implies the command should run when the number of
12+
reachable commits that are not in the commit-graph file is at least
13+
the value of `maintenance.commit-graph.auto`. The default value is
14+
100.

builtin/gc.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "blob.h"
2929
#include "tree.h"
3030
#include "promisor-remote.h"
31+
#include "refs.h"
3132

3233
#define FAILED_RUN "failed to run %s"
3334

@@ -710,6 +711,80 @@ struct maintenance_opts {
710711
int quiet;
711712
};
712713

714+
/* Remember to update object flag allocation in object.h */
715+
#define PARENT1 (1u<<16)
716+
717+
static int num_commits_not_in_graph = 0;
718+
static int limit_commits_not_in_graph = 100;
719+
720+
static int dfs_on_ref(const char *refname,
721+
const struct object_id *oid, int flags,
722+
void *cb_data)
723+
{
724+
int result = 0;
725+
struct object_id peeled;
726+
struct commit_list *stack = NULL;
727+
struct commit *commit;
728+
729+
if (!peel_ref(refname, &peeled))
730+
oid = &peeled;
731+
if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
732+
return 0;
733+
734+
commit = lookup_commit(the_repository, oid);
735+
if (!commit)
736+
return 0;
737+
if (parse_commit(commit))
738+
return 0;
739+
740+
commit_list_append(commit, &stack);
741+
742+
while (!result && stack) {
743+
struct commit_list *parent;
744+
745+
commit = pop_commit(&stack);
746+
747+
for (parent = commit->parents; parent; parent = parent->next) {
748+
if (parse_commit(parent->item) ||
749+
commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
750+
parent->item->object.flags & PARENT1)
751+
continue;
752+
753+
parent->item->object.flags |= PARENT1;
754+
num_commits_not_in_graph++;
755+
756+
if (num_commits_not_in_graph >= limit_commits_not_in_graph) {
757+
result = 1;
758+
break;
759+
}
760+
761+
commit_list_append(parent->item, &stack);
762+
}
763+
}
764+
765+
free_commit_list(stack);
766+
return result;
767+
}
768+
769+
static int should_write_commit_graph(void)
770+
{
771+
int result;
772+
773+
git_config_get_int("maintenance.commit-graph.auto",
774+
&limit_commits_not_in_graph);
775+
776+
if (!limit_commits_not_in_graph)
777+
return 0;
778+
if (limit_commits_not_in_graph < 0)
779+
return 1;
780+
781+
result = for_each_ref(dfs_on_ref, NULL);
782+
783+
clear_commit_marks_all(PARENT1);
784+
785+
return result;
786+
}
787+
713788
static int run_write_commit_graph(struct maintenance_opts *opts)
714789
{
715790
struct child_process child = CHILD_PROCESS_INIT;
@@ -822,6 +897,7 @@ static struct maintenance_task tasks[] = {
822897
[TASK_COMMIT_GRAPH] = {
823898
"commit-graph",
824899
maintenance_task_commit_graph,
900+
should_write_commit_graph,
825901
},
826902
};
827903

object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct object_array {
7474
* list-objects-filter.c: 21
7575
* builtin/fsck.c: 0--3
7676
* builtin/index-pack.c: 2021
77+
* builtin/maintenance.c: 16
7778
* builtin/pack-objects.c: 20
7879
* builtin/reflog.c: 10--12
7980
* builtin/show-branch.c: 0-------------------------------------------26

0 commit comments

Comments
 (0)