Skip to content

Commit 4ddc79b

Browse files
derrickstoleegitster
authored andcommitted
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 SEEN 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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 916d062 commit 4ddc79b

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

Documentation/config/maintenance.txt

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

builtin/gc.c

Lines changed: 82 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,86 @@ struct maintenance_run_opts {
710711
int quiet;
711712
};
712713

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

object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct object_array {
7373
* sha1-name.c: 20
7474
* list-objects-filter.c: 21
7575
* builtin/fsck.c: 0--3
76+
* builtin/gc.c: 0
7677
* builtin/index-pack.c: 2021
7778
* builtin/pack-objects.c: 20
7879
* builtin/reflog.c: 10--12

0 commit comments

Comments
 (0)