Skip to content

Commit 1f80129

Browse files
rpigottttaylorr
authored andcommitted
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 <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 13d5bbd commit 1f80129

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

Documentation/git-maintenance.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ stop::
5050
the background maintenance is restarted later.
5151

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

builtin/gc.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,13 +1454,15 @@ static char *get_maintpath(void)
14541454
}
14551455

14561456
static char const * const builtin_maintenance_register_usage[] = {
1457-
"git maintenance register",
1457+
"git maintenance register [--config-file <path>]",
14581458
NULL
14591459
};
14601460

14611461
static int maintenance_register(int argc, const char **argv, const char *prefix)
14621462
{
1463+
char *config_file = NULL;
14631464
struct option options[] = {
1465+
OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
14641466
OPT_END(),
14651467
};
14661468
int found = 0;
@@ -1497,12 +1499,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
14971499

14981500
if (!found) {
14991501
int rc;
1500-
char *user_config, *xdg_config;
1501-
git_global_config(&user_config, &xdg_config);
1502-
if (!user_config)
1503-
die(_("$HOME not set"));
1502+
char *user_config = NULL, *xdg_config = NULL;
1503+
1504+
if (!config_file) {
1505+
git_global_config(&user_config, &xdg_config);
1506+
config_file = user_config;
1507+
if (!user_config)
1508+
die(_("$HOME not set"));
1509+
}
15041510
rc = git_config_set_multivar_in_file_gently(
1505-
user_config, "maintenance.repo", maintpath,
1511+
config_file, "maintenance.repo", maintpath,
15061512
CONFIG_REGEX_NONE, 0);
15071513
free(user_config);
15081514
free(xdg_config);
@@ -1517,14 +1523,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
15171523
}
15181524

15191525
static char const * const builtin_maintenance_unregister_usage[] = {
1520-
"git maintenance unregister [--force]",
1526+
"git maintenance unregister [--config-file <path>] [--force]",
15211527
NULL
15221528
};
15231529

15241530
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
15251531
{
15261532
int force = 0;
1533+
char *config_file = NULL;
15271534
struct option options[] = {
1535+
OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
15281536
OPT__FORCE(&force,
15291537
N_("return success even if repository was not registered"),
15301538
PARSE_OPT_NOCOMPLETE),
@@ -1542,7 +1550,14 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
15421550
usage_with_options(builtin_maintenance_unregister_usage,
15431551
options);
15441552

1545-
list = git_config_get_value_multi(key);
1553+
struct config_set cs;
1554+
if (config_file) {
1555+
git_configset_init(&cs);
1556+
git_configset_add_file(&cs, config_file);
1557+
list = git_configset_get_value_multi(&cs, key);
1558+
} else {
1559+
list = git_config_get_value_multi(key);
1560+
}
15461561
if (list) {
15471562
for_each_string_list_item(item, list) {
15481563
if (!strcmp(maintpath, item->string)) {
@@ -1554,12 +1569,15 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
15541569

15551570
if (found) {
15561571
int rc;
1557-
char *user_config, *xdg_config;
1558-
git_global_config(&user_config, &xdg_config);
1559-
if (!user_config)
1560-
die(_("$HOME not set"));
1572+
char *user_config = NULL, *xdg_config = NULL;
1573+
if (!config_file) {
1574+
git_global_config(&user_config, &xdg_config);
1575+
config_file = user_config;
1576+
if (!user_config)
1577+
die(_("$HOME not set"));
1578+
}
15611579
rc = git_config_set_multivar_in_file_gently(
1562-
user_config, key, NULL, maintpath,
1580+
config_file, key, NULL, maintpath,
15631581
CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
15641582
free(user_config);
15651583
free(xdg_config);
@@ -1572,6 +1590,7 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
15721590
die(_("repository '%s' is not registered"), maintpath);
15731591
}
15741592

1593+
git_configset_clear(&cs);
15751594
free(maintpath);
15761595
return 0;
15771596
}

t/t7900-maintenance.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,28 @@ test_expect_success 'register and unregister' '
500500
git config --global --get-all maintenance.repo >actual &&
501501
test_cmp before actual &&
502502
503+
git config --file ./other --add maintenance.repo /existing1 &&
504+
git config --file ./other --add maintenance.repo /existing2 &&
505+
git config --file ./other --get-all maintenance.repo >before &&
506+
507+
git maintenance register --config-file ./other &&
508+
test_cmp_config false maintenance.auto &&
509+
git config --file ./other --get-all maintenance.repo >between &&
510+
cp before expect &&
511+
pwd >>expect &&
512+
test_cmp expect between &&
513+
514+
git maintenance unregister --config-file ./other &&
515+
git config --file ./other --get-all maintenance.repo >actual &&
516+
test_cmp before actual &&
517+
503518
test_must_fail git maintenance unregister 2>err &&
504519
grep "is not registered" err &&
505-
git maintenance unregister --force
520+
git maintenance unregister --force &&
521+
522+
test_must_fail git maintenance unregister --config-file ./other 2>err &&
523+
grep "is not registered" err &&
524+
git maintenance unregister --config-file ./other --force
506525
'
507526

508527
test_expect_success !MINGW 'register and unregister with regex metacharacters' '

0 commit comments

Comments
 (0)