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>

Ronan Pigott committed Nov 9, 2022 at 12:07 UTC 1f80129d61bcc9a77b34b30bc42b2770b4b96deb
3 files changed +59 -21
Documentation/git-maintenance.txt
+7 -7
@@ -50,13 +50,13 @@ stop::
50 the background maintenance is restarted later.
51
52 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.
60 +
61 The `register` subcommand will also set the `maintenance.strategy` config
62 value to `incremental`, if this value is not previously set. The
builtin/gc.c
+32 -13
@@ -1454,13 +1454,15 @@ static char *get_maintpath(void)
1454 }
1455
1456 static char const * const builtin_maintenance_register_usage[] = {
1457 - "git maintenance register",
1457 + "git maintenance register [--config-file <path>]",
1458 NULL
1459 };
1460
1461 static int maintenance_register(int argc, const char **argv, const char *prefix)
1462 {
1463 + char *config_file = NULL;
1464 struct option options[] = {
1465 + OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1466 OPT_END(),
1467 };
1468 int found = 0;
@@ -1497,12 +1499,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
1499
1500 if (!found) {
1501 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 + }
1510 rc = git_config_set_multivar_in_file_gently(
1505 - user_config, "maintenance.repo", maintpath,
1511 + config_file, "maintenance.repo", maintpath,
1512 CONFIG_REGEX_NONE, 0);
1513 free(user_config);
1514 free(xdg_config);
@@ -1517,14 +1523,16 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
1523 }
1524
1525 static char const * const builtin_maintenance_unregister_usage[] = {
1520 - "git maintenance unregister [--force]",
1526 + "git maintenance unregister [--config-file <path>] [--force]",
1527 NULL
1528 };
1529
1530 static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1531 {
1532 int force = 0;
1533 + char *config_file = NULL;
1534 struct option options[] = {
1535 + OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1536 OPT__FORCE(&force,
1537 N_("return success even if repository was not registered"),
1538 PARSE_OPT_NOCOMPLETE),
@@ -1542,7 +1550,14 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
1550 usage_with_options(builtin_maintenance_unregister_usage,
1551 options);
1552
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 + }
1561 if (list) {
1562 for_each_string_list_item(item, list) {
1563 if (!strcmp(maintpath, item->string)) {
@@ -1554,12 +1569,15 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
1569
1570 if (found) {
1571 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 + }
1579 rc = git_config_set_multivar_in_file_gently(
1562 - user_config, key, NULL, maintpath,
1580 + config_file, key, NULL, maintpath,
1581 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1582 free(user_config);
1583 free(xdg_config);
@@ -1572,6 +1590,7 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
1590 die(_("repository '%s' is not registered"), maintpath);
1591 }
1592
1593 + git_configset_clear(&cs);
1594 free(maintpath);
1595 return 0;
1596 }
t/t7900-maintenance.sh
+20 -1
@@ -500,9 +500,28 @@ test_expect_success 'register and unregister' '
500 git config --global --get-all maintenance.repo >actual &&
501 test_cmp before actual &&
502
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 +
518 test_must_fail git maintenance unregister 2>err &&
519 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
525 '
526
527 test_expect_success !MINGW 'register and unregister with regex metacharacters' '