maintenance: add 'unregister --force'

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>
This commit is contained in:
Derrick Stolee 2022-09-27 13:56:58 +00:00 committed by Junio C Hamano
parent 1b3d6e17fe
commit 1ebe6b0297
3 changed files with 44 additions and 8 deletions

View 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
-----

View file

@ -1519,18 +1519,26 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
}
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;
}

View file

@ -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' '