]> git.ipfire.org Git - thirdparty/git.git/commitdiff
maintenance: add 'unregister --force'
authorDerrick Stolee <derrickstolee@github.com>
Tue, 27 Sep 2022 13:56:58 +0000 (13:56 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 27 Sep 2022 16:32:25 +0000 (09:32 -0700)
The 'git maintenance unregister' subcommand has a step that removes the
current repository from the multi-valued maitenance.repo config key.
This fails if the repository is not listed in that key. This makes
running 'git maintenance unregister' twice result in a failure in the
second instance.

This failure exit code is helpful, but its message is not. Add a new
die() message that explicitly calls out the failure due to the
repository not being registered.

In some cases, users may want to run 'git maintenance unregister' just
to make sure that background jobs will not start on this repository, but
they do not want to check to see if it is registered first. Add a new
'--force' option that will siltently succeed if the repository is not
already registered.

Also add an extra test of 'git maintenance unregister' at a point where
there are no registered repositories. This should fail without --force.

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

index 9c630efe19c68ec4b25fee1cf716c5a2073e2f51..bb888690e4dbd2642fc46d3c8775fa1638741cf4 100644 (file)
@@ -11,7 +11,7 @@ SYNOPSIS
 [verse]
 'git maintenance' run [<options>]
 'git maintenance' start [--scheduler=<scheduler>]
-'git maintenance' (stop|register|unregister)
+'git maintenance' (stop|register|unregister) [<options>]
 
 
 DESCRIPTION
@@ -79,6 +79,10 @@ 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.
++
+The `unregister` subcommand will report an error if the current repository
+is not already registered. Use the `--force` option to return success even
+when the current repository is not registered.
 
 TASKS
 -----
index 2753bd15a5e85f584a0e73943828ce8cd1d0fe5c..dc0ba9e3648bae0e840d3ea4aef37fa0fc663768 100644 (file)
@@ -1519,18 +1519,26 @@ done:
 }
 
 static char const * const builtin_maintenance_unregister_usage[] = {
-       "git maintenance unregister",
+       "git maintenance unregister [--force]",
        NULL
 };
 
 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
 {
+       int force = 0;
        struct option options[] = {
+               OPT__FORCE(&force,
+                          N_("return success even if repository was not registered"),
+                          PARSE_OPT_NOCOMPLETE),
                OPT_END(),
        };
-       int rc;
+       const char *key = "maintenance.repo";
+       int rc = 0;
        struct child_process config_unset = CHILD_PROCESS_INIT;
        char *maintpath = get_maintpath();
+       int found = 0;
+       struct string_list_item *item;
+       const struct string_list *list;
 
        argc = parse_options(argc, argv, prefix, options,
                             builtin_maintenance_unregister_usage, 0);
@@ -1538,11 +1546,26 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
                usage_with_options(builtin_maintenance_unregister_usage,
                                   options);
 
-       config_unset.git_cmd = 1;
-       strvec_pushl(&config_unset.args, "config", "--global", "--unset",
-                    "--fixed-value", "maintenance.repo", maintpath, NULL);
+       list = git_config_get_value_multi(key);
+       if (list) {
+               for_each_string_list_item(item, list) {
+                       if (!strcmp(maintpath, item->string)) {
+                               found = 1;
+                               break;
+                       }
+               }
+       }
+
+       if (found) {
+               config_unset.git_cmd = 1;
+               strvec_pushl(&config_unset.args, "config", "--global", "--unset",
+                            "--fixed-value", key, maintpath, NULL);
+
+               rc = run_command(&config_unset);
+       } else if (!force) {
+               die(_("repository '%s' is not registered"), maintpath);
+       }
 
-       rc = run_command(&config_unset);
        free(maintpath);
        return rc;
 }
index 2724a44fe3ef240aa09077f770706ed022a99b7e..96bdd4204560c58ec2fe97f9898e698a2379549f 100755 (executable)
@@ -480,6 +480,11 @@ test_expect_success 'maintenance.strategy inheritance' '
 
 test_expect_success 'register and unregister' '
        test_when_finished git config --global --unset-all maintenance.repo &&
+
+       test_must_fail git maintenance unregister 2>err &&
+       grep "is not registered" err &&
+       git maintenance unregister --force &&
+
        git config --global --add maintenance.repo /existing1 &&
        git config --global --add maintenance.repo /existing2 &&
        git config --global --get-all maintenance.repo >before &&
@@ -493,7 +498,11 @@ test_expect_success 'register and unregister' '
 
        git maintenance unregister &&
        git config --global --get-all maintenance.repo >actual &&
-       test_cmp before actual
+       test_cmp before actual &&
+
+       test_must_fail git maintenance unregister 2>err &&
+       grep "is not registered" err &&
+       git maintenance unregister --force
 '
 
 test_expect_success !MINGW 'register and unregister with regex metacharacters' '