Skip to content

Commit

Permalink
maintenance: make unregister idempotent
Browse files Browse the repository at this point in the history
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.

Make this task idempotent, since the end result is the same in both
cases: maintenance will no longer run on this repository.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
  • Loading branch information
derrickstolee authored and dscho committed Sep 27, 2022
1 parent 6f44f23 commit a9b6d9e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 19 additions & 5 deletions builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,21 +1528,35 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
struct option options[] = {
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 = git_config_get_value_multi(key);

argc = parse_options(argc, argv, prefix, options,
builtin_maintenance_unregister_usage, 0);
if (argc)
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);
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);
}

rc = run_command(&config_unset);
free(maintpath);
return rc;
}
Expand Down
5 changes: 4 additions & 1 deletion t/t7900-maintenance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ 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 &&
# Expect unregister to be idempotent.
git maintenance unregister
'

test_expect_success !MINGW 'register and unregister with regex metacharacters' '
Expand Down

0 comments on commit a9b6d9e

Please sign in to comment.