Skip to content

Commit 481c808

Browse files
committed
gc: add git maintenance list command
List all repositories registered for background maintenance. This displays the paths of all repositories that are configured in the maintenance.repo config variable. By default, it reads from the global config, but you can specify a different config file with the --config-file option. Signed-off-by: Rémy Léone <rleone@scaleway.com>
1 parent 852829b commit 481c808

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

Documentation/git-maintenance.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git maintenance' run [<options>]
1313
'git maintenance' start [--scheduler=<scheduler>]
14-
'git maintenance' (stop|register|unregister) [<options>]
14+
'git maintenance' (stop|register|unregister|list) [<options>]
1515
'git maintenance' is-needed [<options>]
1616

1717

@@ -85,6 +85,13 @@ The `unregister` subcommand will report an error if the current repository
8585
is not already registered. Use the `--force` option to return success even
8686
when the current repository is not registered.
8787

88+
list::
89+
List all repositories registered for background maintenance. This
90+
displays the paths of all repositories that are configured in the
91+
`maintenance.repo` config variable. By default, it reads from the
92+
global config, but you can specify a different config file with
93+
the `--config-file` option.
94+
8895
is-needed::
8996
Check whether maintenance needs to be run without actually running it.
9097
Exits with a 0 status code if maintenance needs to be run, 1 otherwise.

builtin/gc.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,64 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
22392239
return 0;
22402240
}
22412241

2242+
static const char * const builtin_maintenance_list_usage[] = {
2243+
"git maintenance list [--config-file <path>]",
2244+
NULL
2245+
};
2246+
2247+
static int maintenance_list(int argc, const char **argv, const char *prefix,
2248+
struct repository *repo UNUSED)
2249+
{
2250+
char *config_file = NULL;
2251+
struct option options[] = {
2252+
OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
2253+
OPT_END(),
2254+
};
2255+
const char *key = "maintenance.repo";
2256+
const struct string_list *list;
2257+
struct config_set cs = { { 0 } };
2258+
char *global_config_file = NULL;
2259+
2260+
argc = parse_options(argc, argv, prefix, options,
2261+
builtin_maintenance_list_usage, 0);
2262+
if (argc)
2263+
usage_with_options(builtin_maintenance_list_usage,
2264+
options);
2265+
2266+
if (config_file) {
2267+
git_configset_init(&cs);
2268+
git_configset_add_file(&cs, config_file);
2269+
if (git_configset_get_string_multi(&cs, key, &list)) {
2270+
/* No repositories registered in custom config */
2271+
git_configset_clear(&cs);
2272+
return 0;
2273+
}
2274+
} else {
2275+
global_config_file = git_global_config();
2276+
if (!global_config_file)
2277+
die(_("$HOME not set"));
2278+
git_configset_init(&cs);
2279+
git_configset_add_file(&cs, global_config_file);
2280+
if (git_configset_get_string_multi(&cs, key, &list)) {
2281+
/* No repositories registered in global config */
2282+
free(global_config_file);
2283+
git_configset_clear(&cs);
2284+
return 0;
2285+
}
2286+
}
2287+
2288+
{
2289+
struct string_list_item *item;
2290+
for_each_string_list_item(item, list) {
2291+
printf("%s\n", item->string);
2292+
}
2293+
}
2294+
2295+
free(global_config_file);
2296+
git_configset_clear(&cs);
2297+
return 0;
2298+
}
2299+
22422300
static const char *get_frequency(enum schedule_priority schedule)
22432301
{
22442302
switch (schedule) {
@@ -3535,6 +3593,7 @@ int cmd_maintenance(int argc,
35353593
OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
35363594
OPT_SUBCOMMAND("register", &fn, maintenance_register),
35373595
OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
3596+
OPT_SUBCOMMAND("list", &fn, maintenance_list),
35383597
OPT_SUBCOMMAND("is-needed", &fn, maintenance_is_needed),
35393598
OPT_END(),
35403599
};

t/t7900-maintenance.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,4 +1482,37 @@ test_expect_success 'maintenance aborts with existing lock file' '
14821482
test_grep "Another scheduled git-maintenance(1) process seems to be running" err
14831483
'
14841484

1485+
test_expect_success 'maintenance list shows registered repositories' '
1486+
test_when_finished "rm -rf repo1 repo2" &&
1487+
test_when_finished git config --global --unset-all maintenance.repo &&
1488+
git init repo1 &&
1489+
git init repo2 &&
1490+
(
1491+
cd repo1 &&
1492+
git maintenance register &&
1493+
cd ../repo2 &&
1494+
git maintenance register
1495+
) &&
1496+
git config --global --get-all maintenance.repo >expect &&
1497+
git maintenance list >actual &&
1498+
test_cmp expect actual
1499+
'
1500+
1501+
test_expect_success 'maintenance list with --config-file' '
1502+
CUSTOM_CONFIG="./custom-maintenance-config" &&
1503+
test_when_finished "rm -rf repo3 repo4" &&
1504+
test_when_finished rm -f "$CUSTOM_CONFIG" &&
1505+
git init repo3 &&
1506+
git init repo4 &&
1507+
(
1508+
cd repo3 &&
1509+
git maintenance register --config-file "../$CUSTOM_CONFIG" &&
1510+
cd ../repo4 &&
1511+
git maintenance register --config-file "../$CUSTOM_CONFIG"
1512+
) &&
1513+
git config --file="$CUSTOM_CONFIG" --get-all maintenance.repo >expect &&
1514+
git maintenance list --config-file "$CUSTOM_CONFIG" >actual &&
1515+
test_cmp expect actual
1516+
'
1517+
14851518
test_done

0 commit comments

Comments
 (0)