@matheusbsbs / matheus-bagliano / commits / 0c18b70081
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 11, 2020 at 17:49 UTC 0c18b700810ab2e2a4fac0d5b54a817141198a27
3 files changed +84 -2
Documentation/git-maintenance.txt
+14
@@ -29,6 +29,15 @@ Git repository.
29 SUBCOMMANDS
30 -----------
31
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 +
41 run::
42 Run one or more maintenance tasks. If one or more `--task` options
43 are specified, then those tasks are run in that order. Otherwise,
@@ -36,6 +45,11 @@ run::
45 config options are true. By default, only `maintenance.gc.enabled`
46 is true.
47
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 +
53 TASKS
54 -----
55
builtin/gc.c
+54 -1
@@ -1407,7 +1407,56 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
1407 return maintenance_run_tasks(&opts);
1408 }
1409
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>]");
1460
1461 int cmd_maintenance(int argc, const char **argv, const char *prefix)
1462 {
@@ -1417,6 +1466,10 @@ int cmd_maintenance(int argc, const char **argv, const char *prefix)
1466
1467 if (!strcmp(argv[1], "run"))
1468 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();
1473
1474 die(_("invalid subcommand: %s"), argv[1]);
1475 }
t/t7900-maintenance.sh
+16 -1
@@ -9,7 +9,7 @@ GIT_TEST_MULTI_PACK_INDEX=0
9
10 test_expect_success 'help text' '
11 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 &&
13 test_expect_code 128 git maintenance barf 2>err &&
14 test_i18ngrep "invalid subcommand: barf" err &&
15 test_expect_code 129 git maintenance 2>err &&
@@ -300,4 +300,19 @@ test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
300 test_subcommand git multi-pack-index write --no-progress <weekly.txt
301 '
302
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 +
318 test_done