]> git.ipfire.org Git - thirdparty/git.git/commitdiff
maintenance: add auto condition for commit-graph task
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 17 Sep 2020 18:11:51 +0000 (18:11 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Sep 2020 18:30:05 +0000 (11:30 -0700)
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>
Documentation/config/maintenance.txt
builtin/gc.c
object.h

index 4402b8b49f2dc24ae7cd33c96f562db30973f26a..7cc6700d574d3208178d7d36e00d62f71d5b10bf 100644 (file)
@@ -4,3 +4,13 @@ maintenance.<task>.enabled::
        `git maintenance run`. These config values are ignored if a
        `--task` option exists. By default, only `maintenance.gc.enabled`
        is true.
+
+maintenance.commit-graph.auto::
+       This integer config option controls how often the `commit-graph` task
+       should be run as part of `git maintenance run --auto`. If zero, then
+       the `commit-graph` task will not run with the `--auto` option. A
+       negative value will force the task to run every time. Otherwise, a
+       positive value implies the command should run when the number of
+       reachable commits that are not in the commit-graph file is at least
+       the value of `maintenance.commit-graph.auto`. The default value is
+       100.
index 13c24bca7df771daaf4840985593fbf8d4bc4b1d..4db853c5619071463ee353066968d455915539c1 100644 (file)
@@ -28,6 +28,7 @@
 #include "blob.h"
 #include "tree.h"
 #include "promisor-remote.h"
+#include "refs.h"
 
 #define FAILED_RUN "failed to run %s"
 
@@ -710,6 +711,86 @@ struct maintenance_run_opts {
        int quiet;
 };
 
+/* Remember to update object flag allocation in object.h */
+#define SEEN           (1u<<0)
+
+struct cg_auto_data {
+       int num_not_in_graph;
+       int limit;
+};
+
+static int dfs_on_ref(const char *refname,
+                     const struct object_id *oid, int flags,
+                     void *cb_data)
+{
+       struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
+       int result = 0;
+       struct object_id peeled;
+       struct commit_list *stack = NULL;
+       struct commit *commit;
+
+       if (!peel_ref(refname, &peeled))
+               oid = &peeled;
+       if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
+               return 0;
+
+       commit = lookup_commit(the_repository, oid);
+       if (!commit)
+               return 0;
+       if (parse_commit(commit))
+               return 0;
+
+       commit_list_append(commit, &stack);
+
+       while (!result && stack) {
+               struct commit_list *parent;
+
+               commit = pop_commit(&stack);
+
+               for (parent = commit->parents; parent; parent = parent->next) {
+                       if (parse_commit(parent->item) ||
+                           commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
+                           parent->item->object.flags & SEEN)
+                               continue;
+
+                       parent->item->object.flags |= SEEN;
+                       data->num_not_in_graph++;
+
+                       if (data->num_not_in_graph >= data->limit) {
+                               result = 1;
+                               break;
+                       }
+
+                       commit_list_append(parent->item, &stack);
+               }
+       }
+
+       free_commit_list(stack);
+       return result;
+}
+
+static int should_write_commit_graph(void)
+{
+       int result;
+       struct cg_auto_data data;
+
+       data.num_not_in_graph = 0;
+       data.limit = 100;
+       git_config_get_int("maintenance.commit-graph.auto",
+                          &data.limit);
+
+       if (!data.limit)
+               return 0;
+       if (data.limit < 0)
+               return 1;
+
+       result = for_each_ref(dfs_on_ref, &data);
+
+       clear_commit_marks_all(SEEN);
+
+       return result;
+}
+
 static int run_write_commit_graph(struct maintenance_run_opts *opts)
 {
        struct child_process child = CHILD_PROCESS_INIT;
@@ -790,6 +871,7 @@ static struct maintenance_task tasks[] = {
        [TASK_COMMIT_GRAPH] = {
                "commit-graph",
                maintenance_task_commit_graph,
+               should_write_commit_graph,
        },
 };
 
index 96a2105859426f66729292db3c7462eadc4509dd..20b18805f08f6eb2d472abc377b3766594dc4e78 100644 (file)
--- a/object.h
+++ b/object.h
@@ -73,6 +73,7 @@ struct object_array {
  * sha1-name.c:                                              20
  * list-objects-filter.c:                                      21
  * builtin/fsck.c:           0--3
+ * builtin/gc.c:             0
  * builtin/index-pack.c:                                     2021
  * builtin/pack-objects.c:                                   20
  * builtin/reflog.c:                   10--12