]> git.ipfire.org Git - thirdparty/git.git/commitdiff
maintenance: use pointers to check --auto
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 17 Sep 2020 18:11:50 +0000 (18:11 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Sep 2020 18:30:05 +0000 (11:30 -0700)
The 'git maintenance run' command has an '--auto' option. This is used
by other Git commands such as 'git commit' or 'git fetch' to check if
maintenance should be run after adding data to the repository.

Previously, this --auto option was only used to add the argument to the
'git gc' command as part of the 'gc' task. We will be expanding the
other tasks to perform a check to see if they should do work as part of
the --auto flag, when they are enabled by config.

First, update the 'gc' task to perform the auto check inside the
maintenance process. This prevents running an extra 'git gc --auto'
command when not needed. It also shows a model for other tasks.

Second, use the 'auto_condition' function pointer as a signal for
whether we enable the maintenance task under '--auto'. For instance, we
do not want to enable the 'fetch' task in '--auto' mode, so that
function pointer will remain NULL.

Now that we are not automatically calling 'git gc', a test in
t5514-fetch-multiple.sh must be changed to watch for 'git maintenance'
instead.

We continue to pass the '--auto' option to the 'git gc' command when
necessary, because of the gc.autoDetach config option changes behavior.
Likely, we will want to absorb the daemonizing behavior implied by
gc.autoDetach as a maintenance.autoDetach config option.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/gc.c
t/t5514-fetch-multiple.sh
t/t7900-maintenance.sh

index 55a3d836f0a476531fa41fef977be91d2f52e64d..13c24bca7df771daaf4840985593fbf8d4bc4b1d 100644 (file)
@@ -755,9 +755,17 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)
 
 typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
 
+/*
+ * An auto condition function returns 1 if the task should run
+ * and 0 if the task should NOT run. See needs_to_gc() for an
+ * example.
+ */
+typedef int maintenance_auto_fn(void);
+
 struct maintenance_task {
        const char *name;
        maintenance_task_fn *fn;
+       maintenance_auto_fn *auto_condition;
        unsigned enabled:1;
 
        /* -1 if not selected. */
@@ -776,6 +784,7 @@ static struct maintenance_task tasks[] = {
        [TASK_GC] = {
                "gc",
                maintenance_task_gc,
+               need_to_gc,
                1,
        },
        [TASK_COMMIT_GRAPH] = {
@@ -831,6 +840,11 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
                if (!found_selected && !tasks[i].enabled)
                        continue;
 
+               if (opts->auto_flag &&
+                   (!tasks[i].auto_condition ||
+                    !tasks[i].auto_condition()))
+                       continue;
+
                if (tasks[i].fn(opts)) {
                        error(_("task '%s' failed"), tasks[i].name);
                        result = 1;
@@ -845,6 +859,8 @@ static void initialize_task_config(void)
 {
        int i;
        struct strbuf config_name = STRBUF_INIT;
+       gc_config();
+
        for (i = 0; i < TASK__COUNT; i++) {
                int config_value;
 
index de8e2f15319b4f9d7ab2b189d52dd013f79033a8..bd202ec6f31e4b4197d89deda06cb1485fdbc1f3 100755 (executable)
@@ -108,7 +108,7 @@ test_expect_success 'git fetch --multiple (two remotes)' '
         GIT_TRACE=1 git fetch --multiple one two 2>trace &&
         git branch -r > output &&
         test_cmp ../expect output &&
-        grep "built-in: git gc" trace >gc &&
+        grep "built-in: git maintenance" trace >gc &&
         test_line_count = 1 gc
        )
 '
index 8a162a18ba32fdc09403d65b5344bdd3d6d0df73..53c883531e440c0d55db95134bab5a17e96a71e1 100755 (executable)
@@ -23,7 +23,7 @@ test_expect_success 'run [--auto|--quiet]' '
        GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
                git maintenance run --no-quiet 2>/dev/null &&
        test_subcommand git gc --quiet <run-no-auto.txt &&
-       test_subcommand git gc --auto --quiet <run-auto.txt &&
+       test_subcommand git gc --auto --quiet <run-auto.txt &&
        test_subcommand git gc --no-quiet <run-no-quiet.txt
 '