@matheusbsbs / matheus-bagliano / commits / 4ddc79b2da
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 17, 2020 at 18:11 UTC 4ddc79b2dac18189e7ff18e99a6fabdd161f866c
3 files changed +93
Documentation/config/maintenance.txt
+10
@@ -4,3 +4,13 @@ maintenance.<task>.enabled::
4 `git maintenance run`. These config values are ignored if a
5 `--task` option exists. By default, only `maintenance.gc.enabled`
6 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
+82
@@ -28,6 +28,7 @@
28 #include "blob.h"
29 #include "tree.h"
30 #include "promisor-remote.h"
31 +#include "refs.h"
32
33 #define FAILED_RUN "failed to run %s"
34
@@ -710,6 +711,86 @@ struct maintenance_run_opts {
711 int quiet;
712 };
713
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 +
794 static int run_write_commit_graph(struct maintenance_run_opts *opts)
795 {
796 struct child_process child = CHILD_PROCESS_INIT;
@@ -790,6 +871,7 @@ static struct maintenance_task tasks[] = {
871 [TASK_COMMIT_GRAPH] = {
872 "commit-graph",
873 maintenance_task_commit_graph,
874 + should_write_commit_graph,
875 },
876 };
877
object.h
+1
@@ -73,6 +73,7 @@ struct object_array {
73 * sha1-name.c: 20
74 * list-objects-filter.c: 21
75 * builtin/fsck.c: 0--3
76 + * builtin/gc.c: 0
77 * builtin/index-pack.c: 2021
78 * builtin/pack-objects.c: 20
79 * builtin/reflog.c: 10--12