maintenance: add option to register in a specific config

maintenance register currently records the maintenance repo exclusively
within the user's global configuration, but other configuration files
may be relevant when running maintenance if they are included from the
global config. This option allows the user to choose where maintenance
repos are recorded.

Signed-off-by: Ronan Pigott <ronan@rjp.ie>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Ronan Pigott 2022-11-09 12:07:08 -07:00 committed by Taylor Blau
parent 13d5bbdf72
commit 1f80129d61
3 changed files with 59 additions and 21 deletions

View File

@ -50,13 +50,13 @@ stop::
the background maintenance is restarted later. the background maintenance is restarted later.
register:: register::
Initialize Git config values so any scheduled maintenance will Initialize Git config values so any scheduled maintenance will start
start running on this repository. This adds the repository to the running on this repository. This adds the repository to the
`maintenance.repo` config variable in the current user's global `maintenance.repo` config variable in the current user's global config,
config and enables some recommended configuration values for or the config specified by --config-file option, and enables some
`maintenance.<task>.schedule`. The tasks that are enabled are safe recommended configuration values for `maintenance.<task>.schedule`. The
for running in the background without disrupting foreground tasks that are enabled are safe for running in the background without
processes. disrupting foreground processes.
+ +
The `register` subcommand will also set the `maintenance.strategy` config The `register` subcommand will also set the `maintenance.strategy` config
value to `incremental`, if this value is not previously set. The value to `incremental`, if this value is not previously set. The

View File

@ -1454,13 +1454,15 @@ static char *get_maintpath(void)
} }
static char const * const builtin_maintenance_register_usage[] = { static char const * const builtin_maintenance_register_usage[] = {
"git maintenance register", "git maintenance register [--config-file <path>]",
NULL NULL
}; };
static int maintenance_register(int argc, const char **argv, const char *prefix) static int maintenance_register(int argc, const char **argv, const char *prefix)
{ {
char *config_file = NULL;
struct option options[] = { struct option options[] = {
OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
OPT_END(), OPT_END(),
}; };
int found = 0; int found = 0;
@ -1497,12 +1499,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
if (!found) { if (!found) {
int rc; int rc;
char *user_config, *xdg_config; char *user_config = NULL, *xdg_config = NULL;
if (!config_file) {
git_global_config(&user_config, &xdg_config); git_global_config(&user_config, &xdg_config);
config_file = user_config;
if (!user_config) if (!user_config)
die(_("$HOME not set")); die(_("$HOME not set"));
}
rc = git_config_set_multivar_in_file_gently( rc = git_config_set_multivar_in_file_gently(
user_config, "maintenance.repo", maintpath, config_file, "maintenance.repo", maintpath,
CONFIG_REGEX_NONE, 0); CONFIG_REGEX_NONE, 0);
free(user_config); free(user_config);
free(xdg_config); free(xdg_config);
@ -1517,14 +1523,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
} }
static char const * const builtin_maintenance_unregister_usage[] = { static char const * const builtin_maintenance_unregister_usage[] = {
"git maintenance unregister [--force]", "git maintenance unregister [--config-file <path>] [--force]",
NULL NULL
}; };
static int maintenance_unregister(int argc, const char **argv, const char *prefix) static int maintenance_unregister(int argc, const char **argv, const char *prefix)
{ {
int force = 0; int force = 0;
char *config_file = NULL;
struct option options[] = { struct option options[] = {
OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
OPT__FORCE(&force, OPT__FORCE(&force,
N_("return success even if repository was not registered"), N_("return success even if repository was not registered"),
PARSE_OPT_NOCOMPLETE), PARSE_OPT_NOCOMPLETE),
@ -1542,7 +1550,14 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
usage_with_options(builtin_maintenance_unregister_usage, usage_with_options(builtin_maintenance_unregister_usage,
options); options);
struct config_set cs;
if (config_file) {
git_configset_init(&cs);
git_configset_add_file(&cs, config_file);
list = git_configset_get_value_multi(&cs, key);
} else {
list = git_config_get_value_multi(key); list = git_config_get_value_multi(key);
}
if (list) { if (list) {
for_each_string_list_item(item, list) { for_each_string_list_item(item, list) {
if (!strcmp(maintpath, item->string)) { if (!strcmp(maintpath, item->string)) {
@ -1554,12 +1569,15 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
if (found) { if (found) {
int rc; int rc;
char *user_config, *xdg_config; char *user_config = NULL, *xdg_config = NULL;
if (!config_file) {
git_global_config(&user_config, &xdg_config); git_global_config(&user_config, &xdg_config);
config_file = user_config;
if (!user_config) if (!user_config)
die(_("$HOME not set")); die(_("$HOME not set"));
}
rc = git_config_set_multivar_in_file_gently( rc = git_config_set_multivar_in_file_gently(
user_config, key, NULL, maintpath, config_file, key, NULL, maintpath,
CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE); CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
free(user_config); free(user_config);
free(xdg_config); free(xdg_config);
@ -1572,6 +1590,7 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
die(_("repository '%s' is not registered"), maintpath); die(_("repository '%s' is not registered"), maintpath);
} }
git_configset_clear(&cs);
free(maintpath); free(maintpath);
return 0; return 0;
} }

View File

@ -500,9 +500,28 @@ test_expect_success 'register and unregister' '
git config --global --get-all maintenance.repo >actual && git config --global --get-all maintenance.repo >actual &&
test_cmp before actual && test_cmp before actual &&
git config --file ./other --add maintenance.repo /existing1 &&
git config --file ./other --add maintenance.repo /existing2 &&
git config --file ./other --get-all maintenance.repo >before &&
git maintenance register --config-file ./other &&
test_cmp_config false maintenance.auto &&
git config --file ./other --get-all maintenance.repo >between &&
cp before expect &&
pwd >>expect &&
test_cmp expect between &&
git maintenance unregister --config-file ./other &&
git config --file ./other --get-all maintenance.repo >actual &&
test_cmp before actual &&
test_must_fail git maintenance unregister 2>err && test_must_fail git maintenance unregister 2>err &&
grep "is not registered" err && grep "is not registered" err &&
git maintenance unregister --force git maintenance unregister --force &&
test_must_fail git maintenance unregister --config-file ./other 2>err &&
grep "is not registered" err &&
git maintenance unregister --config-file ./other --force
' '
test_expect_success !MINGW 'register and unregister with regex metacharacters' ' test_expect_success !MINGW 'register and unregister with regex metacharacters' '