git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/7] maintenance: add [un]register subcommands

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 25, 2020, 18:40 UTC
Message-ID
<c0ce1267a99606e7632c8930c3b4d6c33f901101.1598380805.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.680.v2.git.1598380805.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

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>
---
 Documentation/git-maintenance.txt | 14 ++++++++
 builtin/gc.c                      | 55 ++++++++++++++++++++++++++++++-
 t/t7900-maintenance.sh            | 17 +++++++++-
 3 files changed, 84 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt
index 2bc02c65e4..c42a176a95 100644
--- a/Documentation/git-maintenance.txt
+++ b/Documentation/git-maintenance.txt
@@ -29,6 +29,15 @@ Git repository.
 SUBCOMMANDS
 -----------
 
+register::
+	Initialize Git config values so any scheduled maintenance will
+	start running on this repository. This adds the repository to the
+	`maintenance.repo` config variable in the current user's global
+	config and enables some recommended configuration values for
+	`maintenance.<task>.schedule`. The tasks that are enabled are safe
+	for running in the background without disrupting foreground
+	processes.
+
 run::
 	Run one or more maintenance tasks. If one or more `--task` options
 	are specified, then those tasks are run in that order. Otherwise,
@@ -36,6 +45,11 @@ run::
 	config options are true. By default, only `maintenance.gc.enabled`
 	is true.
 
+unregister::
+	Remove the current repository from background maintenance. This
+	only removes the repository from the configured list. It does not
+	stop the background maintenance processes from running.
+
 TASKS
 -----
 
diff --git a/builtin/gc.c b/builtin/gc.c
index 5726a9a3b3..5218d52cb7 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1414,7 +1414,56 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
 	return maintenance_run_tasks(&opts);
 }
 
-static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");
+static int maintenance_register(void)
+{
+	struct child_process config_set = CHILD_PROCESS_INIT;
+	struct child_process config_get = CHILD_PROCESS_INIT;
+
+	/* There is no current repository, so skip registering it */
+	if (!the_repository || !the_repository->gitdir)
+		return 0;
+
+	config_get.git_cmd = 1;
+	strvec_pushl(&config_get.args, "config", "--global", "--get", "maintenance.repo",
+		     the_repository->worktree ? the_repository->worktree
+					      : the_repository->gitdir,
+			 NULL);
+	config_get.out = -1;
+
+	if (start_command(&config_get))
+		return error(_("failed to run 'git config'"));
+
+	/* We already have this value in our config! */
+	if (!finish_command(&config_get))
+		return 0;
+
+	config_set.git_cmd = 1;
+	strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
+		     the_repository->worktree ? the_repository->worktree
+					      : the_repository->gitdir,
+		     NULL);
+
+	return run_command(&config_set);
+}
+
+static int maintenance_unregister(void)
+{
+	struct child_process config_unset = CHILD_PROCESS_INIT;
+
+	if (!the_repository || !the_repository->gitdir)
+		return error(_("no current repository to unregister"));
+
+	config_unset.git_cmd = 1;
+	strvec_pushl(&config_unset.args, "config", "--global", "--unset",
+		     "maintenance.repo",
+		     the_repository->worktree ? the_repository->worktree
+					      : the_repository->gitdir,
+		     NULL);
+
+	return run_command(&config_unset);
+}
+
+static const char builtin_maintenance_usage[] =	N_("git maintenance <subcommand> [<options>]");
 
 int cmd_maintenance(int argc, const char **argv, const char *prefix)
 {
@@ -1423,6 +1472,10 @@ int cmd_maintenance(int argc, const char **argv, const char *prefix)
 
 	if (!strcmp(argv[1], "run"))
 		return maintenance_run(argc - 1, argv + 1, prefix);
+	if (!strcmp(argv[1], "register"))
+		return maintenance_register();
+	if (!strcmp(argv[1], "unregister"))
+		return maintenance_unregister();
 
 	die(_("invalid subcommand: %s"), argv[1]);
 }
diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
index 3e0c5f1ca8..b20ee2d542 100755
--- a/t/t7900-maintenance.sh
+++ b/t/t7900-maintenance.sh
@@ -9,7 +9,7 @@ GIT_TEST_MULTI_PACK_INDEX=0
 
 test_expect_success 'help text' '
 	test_expect_code 129 git maintenance -h 2>err &&
-	test_i18ngrep "usage: git maintenance run" err &&
+	test_i18ngrep "usage: git maintenance <subcommand>" err &&
 	test_expect_code 128 git maintenance barf 2>err &&
 	test_i18ngrep "invalid subcommand: barf" err
 '
@@ -294,4 +294,19 @@ test_expect_success '--scheduled with specific time' '
 	test_cmp_config 1595000100 maintenance.commit-graph.lastrun
 '
 
+test_expect_success 'register and unregister' '
+	test_when_finished git config --global --unset-all maintenance.repo &&
+	git config --global --add maintenance.repo /existing1 &&
+	git config --global --add maintenance.repo /existing2 &&
+	git config --global --get-all maintenance.repo >before &&
+	git maintenance register &&
+	git config --global --get-all maintenance.repo >actual &&
+	cp before after &&
+	pwd >>after &&
+	test_cmp after actual &&
+	git maintenance unregister &&
+	git config --global --get-all maintenance.repo >actual &&
+	test_cmp before actual
+'
+
 test_done
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee via GitGitGadget
Message 32 of 41 in “[RFC] Maintenance III: background maintenance”
  1. 0/7 [RFC] Maintenance III: background maintenanceDerrick Stolee via GitGitGadget, Aug 19, 2020
  2. 1/7 maintenance: optionally skip --auto processDerrick Stolee via GitGitGadget, Aug 19, 2020
  3. Đoàn Trần Công DanhAug 20, 2020
  4. Derrick StoleeAug 20, 2020
  5. 4/7 for-each-repo: run subcommands on configured reposDerrick Stolee via GitGitGadget, Aug 19, 2020
  6. Đoàn Trần Công DanhAug 20, 2020
  7. 3/7 maintenance: add --scheduled option and configDerrick Stolee via GitGitGadget, Aug 19, 2020
  8. Đoàn Trần Công DanhAug 20, 2020
  9. Derrick StoleeAug 24, 2020
  10. 5/7 maintenance: add [un]register subcommandsDerrick Stolee via GitGitGadget, Aug 19, 2020
  11. 6/7 maintenance: add start/stop subcommandsDerrick Stolee via GitGitGadget, Aug 19, 2020
  12. 7/7 maintenance: recommended schedule in register/startDerrick Stolee via GitGitGadget, Aug 19, 2020
  13. 0/7 [RFC] Maintenance III: background maintenanceDerrick Stolee via GitGitGadget, Aug 25, 2020
  14. 1/7 maintenance: optionally skip --auto processDerrick Stolee via GitGitGadget, Aug 25, 2020
  15. Junio C HamanoAug 25, 2020
  16. Derrick StoleeAug 26, 2020
  17. Junio C HamanoAug 26, 2020
  18. 2/7 maintenance: store the "last run" time in configDerrick Stolee via GitGitGadget, Aug 25, 2020
  19. Junio C HamanoAug 25, 2020
  20. Derrick StoleeAug 26, 2020
  21. Junio C HamanoAug 26, 2020
  22. Derrick StoleeAug 27, 2020
  23. 3/7 maintenance: add --scheduled option and configDerrick Stolee via GitGitGadget, Aug 25, 2020
  24. Junio C HamanoAug 25, 2020
  25. Derrick StoleeAug 26, 2020
  26. Derrick StoleeAug 27, 2020
  27. 4/7 for-each-repo: run subcommands on configured reposDerrick Stolee via GitGitGadget, Aug 25, 2020
  28. Junio C HamanoAug 25, 2020
  29. Derrick StoleeAug 26, 2020
  30. 6/7 maintenance: add start/stop subcommandsDerrick Stolee via GitGitGadget, Aug 25, 2020
  31. 7/7 maintenance: recommended schedule in register/startDerrick Stolee via GitGitGadget, Aug 25, 2020
  32. 5/7 maintenance: add [un]register subcommandsDerrick Stolee via GitGitGadget, Aug 25, 2020
  33. 0/6 [RFC] Maintenance III: background maintenanceDerrick Stolee via GitGitGadget, Aug 28, 2020
  34. 2/6 maintenance: add --schedule option and configDerrick Stolee via GitGitGadget, Aug 28, 2020
  35. 4/6 maintenance: add [un]register subcommandsDerrick Stolee via GitGitGadget, Aug 28, 2020
  36. 1/6 maintenance: optionally skip --auto processDerrick Stolee via GitGitGadget, Aug 28, 2020
  37. 5/6 maintenance: add start/stop subcommandsDerrick Stolee via GitGitGadget, Aug 28, 2020
  38. 6/6 maintenance: recommended schedule in register/startDerrick Stolee via GitGitGadget, Aug 28, 2020
  39. 3/6 for-each-repo: run subcommands on configured reposDerrick Stolee via GitGitGadget, Aug 28, 2020
  40. Michal SuchánekAug 26, 2020
  41. Đoàn Trần Công DanhAug 20, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.