Skip to content

Commit 0c18b70

Browse files
derrickstoleegitster
authored andcommitted
maintenance: add [un]register subcommands
In preparation for launching background maintenance from the 'git maintenance' builtin, create register/unregister subcommands. These commands update the new 'maintenance.repos' config option in the global config so the background maintenance job knows which repositories to maintain. These commands allow users to add a repository to the background maintenance list without disrupting the actual maintenance mechanism. For example, a user can run 'git maintenance register' when no background maintenance is running and it will not start the background maintenance. A later update to start running background maintenance will then pick up this repository automatically. The opposite example is that a user can run 'git maintenance unregister' to remove the current repository from background maintenance without halting maintenance for other repositories. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4950b2a commit 0c18b70

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

Documentation/git-maintenance.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,27 @@ Git repository.
2929
SUBCOMMANDS
3030
-----------
3131

32+
register::
33+
Initialize Git config values so any scheduled maintenance will
34+
start running on this repository. This adds the repository to the
35+
`maintenance.repo` config variable in the current user's global
36+
config and enables some recommended configuration values for
37+
`maintenance.<task>.schedule`. The tasks that are enabled are safe
38+
for running in the background without disrupting foreground
39+
processes.
40+
3241
run::
3342
Run one or more maintenance tasks. If one or more `--task` options
3443
are specified, then those tasks are run in that order. Otherwise,
3544
the tasks are determined by which `maintenance.<task>.enabled`
3645
config options are true. By default, only `maintenance.gc.enabled`
3746
is true.
3847

48+
unregister::
49+
Remove the current repository from background maintenance. This
50+
only removes the repository from the configured list. It does not
51+
stop the background maintenance processes from running.
52+
3953
TASKS
4054
-----
4155

builtin/gc.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,56 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
14071407
return maintenance_run_tasks(&opts);
14081408
}
14091409

1410-
static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");
1410+
static int maintenance_register(void)
1411+
{
1412+
struct child_process config_set = CHILD_PROCESS_INIT;
1413+
struct child_process config_get = CHILD_PROCESS_INIT;
1414+
1415+
/* There is no current repository, so skip registering it */
1416+
if (!the_repository || !the_repository->gitdir)
1417+
return 0;
1418+
1419+
config_get.git_cmd = 1;
1420+
strvec_pushl(&config_get.args, "config", "--global", "--get", "maintenance.repo",
1421+
the_repository->worktree ? the_repository->worktree
1422+
: the_repository->gitdir,
1423+
NULL);
1424+
config_get.out = -1;
1425+
1426+
if (start_command(&config_get))
1427+
return error(_("failed to run 'git config'"));
1428+
1429+
/* We already have this value in our config! */
1430+
if (!finish_command(&config_get))
1431+
return 0;
1432+
1433+
config_set.git_cmd = 1;
1434+
strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
1435+
the_repository->worktree ? the_repository->worktree
1436+
: the_repository->gitdir,
1437+
NULL);
1438+
1439+
return run_command(&config_set);
1440+
}
1441+
1442+
static int maintenance_unregister(void)
1443+
{
1444+
struct child_process config_unset = CHILD_PROCESS_INIT;
1445+
1446+
if (!the_repository || !the_repository->gitdir)
1447+
return error(_("no current repository to unregister"));
1448+
1449+
config_unset.git_cmd = 1;
1450+
strvec_pushl(&config_unset.args, "config", "--global", "--unset",
1451+
"maintenance.repo",
1452+
the_repository->worktree ? the_repository->worktree
1453+
: the_repository->gitdir,
1454+
NULL);
1455+
1456+
return run_command(&config_unset);
1457+
}
1458+
1459+
static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
14111460

14121461
int cmd_maintenance(int argc, const char **argv, const char *prefix)
14131462
{
@@ -1417,6 +1466,10 @@ int cmd_maintenance(int argc, const char **argv, const char *prefix)
14171466

14181467
if (!strcmp(argv[1], "run"))
14191468
return maintenance_run(argc - 1, argv + 1, prefix);
1469+
if (!strcmp(argv[1], "register"))
1470+
return maintenance_register();
1471+
if (!strcmp(argv[1], "unregister"))
1472+
return maintenance_unregister();
14201473

14211474
die(_("invalid subcommand: %s"), argv[1]);
14221475
}

t/t7900-maintenance.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ GIT_TEST_MULTI_PACK_INDEX=0
99

1010
test_expect_success 'help text' '
1111
test_expect_code 129 git maintenance -h 2>err &&
12-
test_i18ngrep "usage: git maintenance run" err &&
12+
test_i18ngrep "usage: git maintenance <subcommand>" err &&
1313
test_expect_code 128 git maintenance barf 2>err &&
1414
test_i18ngrep "invalid subcommand: barf" err &&
1515
test_expect_code 129 git maintenance 2>err &&
@@ -300,4 +300,19 @@ test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
300300
test_subcommand git multi-pack-index write --no-progress <weekly.txt
301301
'
302302

303+
test_expect_success 'register and unregister' '
304+
test_when_finished git config --global --unset-all maintenance.repo &&
305+
git config --global --add maintenance.repo /existing1 &&
306+
git config --global --add maintenance.repo /existing2 &&
307+
git config --global --get-all maintenance.repo >before &&
308+
git maintenance register &&
309+
git config --global --get-all maintenance.repo >actual &&
310+
cp before after &&
311+
pwd >>after &&
312+
test_cmp after actual &&
313+
git maintenance unregister &&
314+
git config --global --get-all maintenance.repo >actual &&
315+
test_cmp before actual
316+
'
317+
303318
test_done

0 commit comments

Comments
 (0)