]> git.ipfire.org Git - thirdparty/git.git/commitdiff
maintenance: add [un]register subcommands
authorDerrick Stolee <dstolee@microsoft.com>
Fri, 11 Sep 2020 17:49:17 +0000 (17:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 25 Sep 2020 17:59:44 +0000 (10:59 -0700)
In preparation for launching background maintenance from the 'git
maintenance' builtin, create register/unregister subcommands. These
commands update the new 'maintenance.repos' config option in the global
config so the background maintenance job knows which repositories to
maintain.

These commands allow users to add a repository to the background
maintenance list without disrupting the actual maintenance mechanism.

For example, a user can run 'git maintenance register' when no
background maintenance is running and it will not start the background
maintenance. A later update to start running background maintenance will
then pick up this repository automatically.

The opposite example is that a user can run 'git maintenance unregister'
to remove the current repository from background maintenance without
halting maintenance for other repositories.

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

index ed94f66e368fc9229cd5b7e43b5b84e9db1572f2..1c59fd0cb55fafcfa2be3e6e0ecac32fe7106b39 100644 (file)
@@ -29,6 +29,15 @@ Git repository.
 SUBCOMMANDS
 -----------
 
+register::
+       Initialize Git config values so any scheduled maintenance will
+       start running on this repository. This adds the repository to the
+       `maintenance.repo` config variable in the current user's global
+       config and enables some recommended configuration values for
+       `maintenance.<task>.schedule`. The tasks that are enabled are safe
+       for running in the background without disrupting foreground
+       processes.
+
 run::
        Run one or more maintenance tasks. If one or more `--task` options
        are specified, then those tasks are run in that order. Otherwise,
@@ -36,6 +45,11 @@ run::
        config options are true. By default, only `maintenance.gc.enabled`
        is true.
 
+unregister::
+       Remove the current repository from background maintenance. This
+       only removes the repository from the configured list. It does not
+       stop the background maintenance processes from running.
+
 TASKS
 -----
 
index 03b24ea0dbb8920e95abd3eadc3a070ac03d2d80..edf1d35ce5f38cba2a74ce64f23d241c2786ac62 100644 (file)
@@ -1407,7 +1407,56 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
        return maintenance_run_tasks(&opts);
 }
 
-static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");
+static int maintenance_register(void)
+{
+       struct child_process config_set = CHILD_PROCESS_INIT;
+       struct child_process config_get = CHILD_PROCESS_INIT;
+
+       /* There is no current repository, so skip registering it */
+       if (!the_repository || !the_repository->gitdir)
+               return 0;
+
+       config_get.git_cmd = 1;
+       strvec_pushl(&config_get.args, "config", "--global", "--get", "maintenance.repo",
+                    the_repository->worktree ? the_repository->worktree
+                                             : the_repository->gitdir,
+                        NULL);
+       config_get.out = -1;
+
+       if (start_command(&config_get))
+               return error(_("failed to run 'git config'"));
+
+       /* We already have this value in our config! */
+       if (!finish_command(&config_get))
+               return 0;
+
+       config_set.git_cmd = 1;
+       strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
+                    the_repository->worktree ? the_repository->worktree
+                                             : the_repository->gitdir,
+                    NULL);
+
+       return run_command(&config_set);
+}
+
+static int maintenance_unregister(void)
+{
+       struct child_process config_unset = CHILD_PROCESS_INIT;
+
+       if (!the_repository || !the_repository->gitdir)
+               return error(_("no current repository to unregister"));
+
+       config_unset.git_cmd = 1;
+       strvec_pushl(&config_unset.args, "config", "--global", "--unset",
+                    "maintenance.repo",
+                    the_repository->worktree ? the_repository->worktree
+                                             : the_repository->gitdir,
+                    NULL);
+
+       return run_command(&config_unset);
+}
+
+static const char builtin_maintenance_usage[] =        N_("git maintenance <subcommand> [<options>]");
 
 int cmd_maintenance(int argc, const char **argv, const char *prefix)
 {
@@ -1417,6 +1466,10 @@ int cmd_maintenance(int argc, const char **argv, const char *prefix)
 
        if (!strcmp(argv[1], "run"))
                return maintenance_run(argc - 1, argv + 1, prefix);
+       if (!strcmp(argv[1], "register"))
+               return maintenance_register();
+       if (!strcmp(argv[1], "unregister"))
+               return maintenance_unregister();
 
        die(_("invalid subcommand: %s"), argv[1]);
 }
index 33d73cd01c5df4fb1868522262d7de707c62f420..8f383d01d9c53e154ee9a08b09510d045478f0b0 100755 (executable)
@@ -9,7 +9,7 @@ GIT_TEST_MULTI_PACK_INDEX=0
 
 test_expect_success 'help text' '
        test_expect_code 129 git maintenance -h 2>err &&
-       test_i18ngrep "usage: git maintenance run" err &&
+       test_i18ngrep "usage: git maintenance <subcommand>" err &&
        test_expect_code 128 git maintenance barf 2>err &&
        test_i18ngrep "invalid subcommand: barf" err &&
        test_expect_code 129 git maintenance 2>err &&
@@ -300,4 +300,19 @@ test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
        test_subcommand git multi-pack-index write --no-progress <weekly.txt
 '
 
+test_expect_success 'register and unregister' '
+       test_when_finished git config --global --unset-all maintenance.repo &&
+       git config --global --add maintenance.repo /existing1 &&
+       git config --global --add maintenance.repo /existing2 &&
+       git config --global --get-all maintenance.repo >before &&
+       git maintenance register &&
+       git config --global --get-all maintenance.repo >actual &&
+       cp before after &&
+       pwd >>after &&
+       test_cmp after actual &&
+       git maintenance unregister &&
+       git config --global --get-all maintenance.repo >actual &&
+       test_cmp before actual
+'
+
 test_done