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

[PATCH 05/21] maintenance: create basic maintenance runner

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 7, 2020, 14:21 UTC
Message-ID
<5f89e0ec9b9c9a02deed5b79b35253e0462dc87c.1594131695.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.671.git.1594131695.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

The 'gc' builtin is our current entrypoint for automatically maintaining a repository. This one tool does many operations, such as repacking the repository, packing refs, and rewriting the commit-graph file. The name implies it performs "garbage collection" which means several different things, and some users may not want to use this operation that rewrites the entire object database.

Create a new 'maintenance' builtin that will become a more general- purpose command. To start, it will only support the 'run' subcommand, but will later expand to add subcommands for scheduling maintenance in the background.

For now, the 'maintenance' builtin is a thin shim over the 'gc' builtin. In fact, the only option is the '--auto' toggle, which is handed directly to the 'gc' builtin. The current change is isolated to this simple operation to prevent more interesting logic from being lost in all of the boilerplate of adding a new builtin.

Use existing builtin/gc.c file because we want to share code between the two builtins. It is possible that we will have 'maintenance' replace the 'gc' builtin entirely at some point, leaving 'git gc' as an alias for some specific arguments to 'git maintenance run'.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 .gitignore                        |  1 +
 Documentation/git-maintenance.txt | 57 +++++++++++++++++++++++++++++
 builtin.h                         |  1 +
 builtin/gc.c                      | 61 +++++++++++++++++++++++++++++++
 git.c                             |  1 +
 t/t7900-maintenance.sh            | 22 +++++++++++
 6 files changed, 143 insertions(+)
 create mode 100644 Documentation/git-maintenance.txt
 create mode 100755 t/t7900-maintenance.sh
diff --git a/.gitignore b/.gitignore
index ee509a2ad2..a5808fa30d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -90,6 +90,7 @@
 /git-ls-tree
 /git-mailinfo
 /git-mailsplit
+/git-maintenance
 /git-merge
 /git-merge-base
 /git-merge-index
diff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt
new file mode 100644
index 0000000000..34cd2b4417
--- /dev/null
+++ b/Documentation/git-maintenance.txt
@@ -0,0 +1,57 @@
+git-maintenance(1)
+==================
+
+NAME
+----
+git-maintenance - Run tasks to optimize Git repository data
+
+
+SYNOPSIS
+--------
+[verse]
+'git maintenance' run [<options>]
+
+
+DESCRIPTION
+-----------
+Run tasks to optimize Git repository data, speeding up other Git commands
+and reducing storage requirements for the repository.
++
+Git commands that add repository data, such as `git add` or `git fetch`,
+are optimized for a responsive user experience. These commands do not take
+time to optimize the Git data, since such optimizations scale with the full
+size of the repository while these user commands each perform a relatively
+small action.
++
+The `git maintenance` command provides flexibility for how to optimize the
+Git repository.
+
+SUBCOMMANDS
+-----------
+
+run::
+	Run one or more maintenance tasks.
+
+TASKS
+-----
+
+gc::
+	Cleanup unnecessary files and optimize the local repository. "GC"
+	stands for "garbage collection," but this task performs many
+	smaller tasks. This task can be rather expensive for large
+	repositories, as it repacks all Git objects into a single pack-file.
+	It can also be disruptive in some situations, as it deletes stale
+	data.
+
+OPTIONS
+-------
+--auto::
+	When combined with the `run` subcommand, run maintenance tasks
+	only if certain thresholds are met. For example, the `gc` task
+	runs when the number of loose objects exceeds the number stored
+	in the `gc.auto` config setting, or when the number of pack-files
+	exceeds the `gc.autoPackLimit` config setting.
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/builtin.h b/builtin.h
index a5ae15bfe5..17c1c0ce49 100644
--- a/builtin.h
+++ b/builtin.h
@@ -167,6 +167,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix);
 int cmd_ls_remote(int argc, const char **argv, const char *prefix);
 int cmd_mailinfo(int argc, const char **argv, const char *prefix);
 int cmd_mailsplit(int argc, const char **argv, const char *prefix);
+int cmd_maintenance(int argc, const char **argv, const char *prefix);
 int cmd_merge(int argc, const char **argv, const char *prefix);
 int cmd_merge_base(int argc, const char **argv, const char *prefix);
 int cmd_merge_index(int argc, const char **argv, const char *prefix);
diff --git a/builtin/gc.c b/builtin/gc.c
index 58a00be5af..07cc5f46ae 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -704,3 +704,64 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 
 	return 0;
 }
+
+static const char * const builtin_maintenance_usage[] = {
+	N_("git maintenance run [<options>]"),
+	NULL
+};
+
+struct maintenance_opts {
+	int auto_flag;
+} opts;
+
+static int maintenance_task_gc(struct repository *r)
+{
+	int result;
+	struct argv_array cmd = ARGV_ARRAY_INIT;
+
+	argv_array_pushl(&cmd, "gc", NULL);
+
+	if (opts.auto_flag)
+		argv_array_pushl(&cmd, "--auto", NULL);
+
+	close_object_store(r->objects);
+	result = run_command_v_opt(cmd.argv, RUN_GIT_CMD);
+	argv_array_clear(&cmd);
+
+	return result;
+}
+
+static int maintenance_run(struct repository *r)
+{
+	return maintenance_task_gc(r);
+}
+
+int cmd_maintenance(int argc, const char **argv, const char *prefix)
+{
+	struct repository *r = the_repository;
+
+	static struct option builtin_maintenance_options[] = {
+		OPT_BOOL(0, "auto", &opts.auto_flag,
+			 N_("run tasks based on the state of the repository")),
+		OPT_END()
+	};
+
+	memset(&opts, 0, sizeof(opts));
+
+	if (argc == 2 && !strcmp(argv[1], "-h"))
+		usage_with_options(builtin_maintenance_usage,
+				   builtin_maintenance_options);
+
+	argc = parse_options(argc, argv, prefix,
+			     builtin_maintenance_options,
+			     builtin_maintenance_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+
+	if (argc == 1) {
+		if (!strcmp(argv[0], "run"))
+			return maintenance_run(r);
+	}
+
+	usage_with_options(builtin_maintenance_usage,
+			   builtin_maintenance_options);
+}
diff --git a/git.c b/git.c
index 2f021b97f3..ff56d1df24 100644
--- a/git.c
+++ b/git.c
@@ -527,6 +527,7 @@ static struct cmd_struct commands[] = {
 	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
 	{ "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT },
 	{ "mailsplit", cmd_mailsplit, NO_PARSEOPT },
+	{ "maintenance", cmd_maintenance, RUN_SETUP_GENTLY | NO_PARSEOPT },
 	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
 	{ "merge-base", cmd_merge_base, RUN_SETUP },
 	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
new file mode 100755
index 0000000000..d00641c4dd
--- /dev/null
+++ b/t/t7900-maintenance.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='git maintenance builtin'
+
+GIT_TEST_COMMIT_GRAPH=0
+GIT_TEST_MULTI_PACK_INDEX=0
+
+. ./test-lib.sh
+
+test_expect_success 'help text' '
+	test_must_fail git maintenance -h 2>err &&
+	test_i18ngrep "usage: git maintenance run" err
+'
+
+test_expect_success 'gc [--auto]' '
+	GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" git maintenance run &&
+	GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" git maintenance run --auto &&
+	grep ",\"gc\"]" run-no-auto.txt  &&
+	grep ",\"gc\",\"--auto\"]" run-auto.txt
+'
+
+test_done
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee via GitGitGadget
Message 5 of 164 in “Maintenance builtin, allowing 'gc --auto' customization”
  1. 00/21 Maintenance builtin, allowing 'gc --auto' customizationDerrick Stolee via GitGitGadget, Jul 7, 2020
  2. 01/21 gc: use the_repository less oftenDerrick Stolee via GitGitGadget, Jul 7, 2020
  3. 02/21 gc: use repository in too_many_loose_objects()Derrick Stolee via GitGitGadget, Jul 7, 2020
  4. 03/21 gc: use repo configDerrick Stolee via GitGitGadget, Jul 7, 2020
  5. 05/21 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Jul 7, 2020
  6. 06/21 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Jul 7, 2020
  7. 08/21 maintenance: initialize task array and hashmapDerrick Stolee via GitGitGadget, Jul 7, 2020
  8. Jonathan TanJul 9, 2020
  9. Derrick StoleeJul 9, 2020
  10. Junio C HamanoJul 9, 2020
  11. 07/21 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Jul 7, 2020
  12. 09/21 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Jul 7, 2020
  13. Jonathan TanJul 9, 2020
  14. Derrick StoleeJul 9, 2020
  15. Jeff KingJul 9, 2020
  16. Derrick StoleeJul 9, 2020
  17. 10/21 maintenance: add --task optionDerrick Stolee via GitGitGadget, Jul 7, 2020
  18. 11/21 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Jul 7, 2020
  19. 17/21 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Jul 7, 2020
  20. 19/21 maintenance: create auto condition for loose-objectsDerrick Stolee via GitGitGadget, Jul 7, 2020
  21. 20/21 maintenance: add pack-files auto conditionDerrick Stolee via GitGitGadget, Jul 7, 2020
  22. 21/21 midx: use start_delayed_progress()Derrick Stolee via GitGitGadget, Jul 7, 2020
  23. 16/21 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Jul 7, 2020
  24. 18/21 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Jul 7, 2020
  25. 13/21 maintenance: add loose-objects taskDerrick Stolee via GitGitGadget, Jul 7, 2020
  26. 15/21 maintenance: auto-size pack-files batchDerrick Stolee via GitGitGadget, Jul 7, 2020
  27. 14/21 maintenance: add pack-files taskDerrick Stolee via GitGitGadget, Jul 7, 2020
  28. 04/21 gc: drop the_repository in log locationDerrick Stolee via GitGitGadget, Jul 7, 2020
  29. Jonathan TanJul 9, 2020
  30. Derrick StoleeJul 9, 2020
  31. 12/21 maintenance: add fetch taskDerrick Stolee via GitGitGadget, Jul 7, 2020
  32. Jonathan TanJul 9, 2020
  33. Emily ShafferJul 8, 2020
  34. Derrick StoleeJul 9, 2020
  35. Derrick StoleeJul 9, 2020
  36. Jeff KingJul 9, 2020
  37. Derrick StoleeJul 9, 2020
  38. Emily ShafferJul 10, 2020
  39. Son Luong NgocJul 10, 2020
  40. Junio C HamanoJul 9, 2020
  41. Derrick StoleeJul 9, 2020
  42. Junio C HamanoJul 9, 2020
  43. Derrick StoleeJul 9, 2020
  44. 00/18 Maintenance builtin, allowing 'gc --auto' customizationDerrick Stolee via GitGitGadget, Jul 23, 2020
  45. 01/18 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Jul 23, 2020
  46. Taylor BlauJul 25, 2020
  47. Đoàn Trần Công DanhJul 25, 2020
  48. Jonathan NiederJul 29, 2020
  49. Derrick StoleeJul 30, 2020
  50. Jonathan NiederJul 31, 2020
  51. Derrick StoleeAug 3, 2020
  52. Jonathan NiederAug 3, 2020
  53. Taylor BlauAug 3, 2020
  54. Jonathan NiederAug 3, 2020
  55. Taylor BlauAug 3, 2020
  56. Jonathan NiederAug 3, 2020
  57. Junio C HamanoAug 4, 2020
  58. Derrick StoleeAug 4, 2020
  59. Jonathan NiederAug 4, 2020
  60. Derrick StoleeAug 4, 2020
  61. Jonathan NiederAug 4, 2020
  62. Derrick StoleeAug 4, 2020
  63. Derrick StoleeAug 5, 2020
  64. Jonathan NiederJul 31, 2020
  65. Jonathan NiederAug 3, 2020
  66. 02/18 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Jul 23, 2020
  67. 03/18 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Jul 23, 2020
  68. Junio C HamanoJul 23, 2020
  69. Taylor BlauJul 25, 2020
  70. Derrick StoleeJul 30, 2020
  71. Derrick StoleeJul 30, 2020
  72. Eric SunshineJul 30, 2020
  73. Derrick StoleeJul 30, 2020
  74. 04/18 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Jul 23, 2020
  75. Junio C HamanoJul 23, 2020
  76. Derrick StoleeJul 24, 2020
  77. Derrick StoleeJul 24, 2020
  78. Junio C HamanoJul 24, 2020
  79. Taylor BlauJul 25, 2020
  80. Emily ShafferJul 29, 2020
  81. 05/18 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Jul 23, 2020
  82. Junio C HamanoJul 23, 2020
  83. Derrick StoleeJul 24, 2020
  84. Junio C HamanoJul 24, 2020
  85. Taylor BlauJul 25, 2020
  86. Derrick StoleeJul 30, 2020
  87. Jeff KingJul 29, 2020
  88. 07/18 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Jul 23, 2020
  89. 06/18 maintenance: add --task optionDerrick Stolee via GitGitGadget, Jul 23, 2020
  90. Junio C HamanoJul 23, 2020
  91. Junio C HamanoJul 23, 2020
  92. Derrick StoleeJul 24, 2020
  93. Junio C HamanoJul 24, 2020
  94. 08/18 maintenance: add prefetch taskDerrick Stolee via GitGitGadget, Jul 23, 2020
  95. Junio C HamanoJul 23, 2020
  96. Derrick StoleeJul 24, 2020
  97. Junio C HamanoJul 24, 2020
  98. Đoàn Trần Công DanhJul 25, 2020
  99. Junio C HamanoJul 25, 2020
  100. Derrick StoleeJul 27, 2020
  101. Junio C HamanoJul 27, 2020
  102. Derrick StoleeJul 27, 2020
  103. fetch: optionally allow disabling FETCH_HEAD updateJunio C Hamano, Jul 28, 2020
  104. Phillip WoodJul 29, 2020
  105. Phillip WoodJul 29, 2020
  106. Derrick StoleeJul 30, 2020
  107. 09/18 maintenance: add loose-objects taskDerrick Stolee via GitGitGadget, Jul 23, 2020
  108. Junio C HamanoJul 23, 2020
  109. Derrick StoleeJul 24, 2020
  110. Junio C HamanoJul 24, 2020
  111. Emily ShafferJul 29, 2020
  112. Derrick StoleeJul 30, 2020
  113. 10/18 maintenance: add incremental-repack taskDerrick Stolee via GitGitGadget, Jul 23, 2020
  114. Junio C HamanoJul 23, 2020
  115. Derrick StoleeJul 24, 2020
  116. Emily ShafferJul 29, 2020
  117. 11/18 maintenance: auto-size incremental-repack batchDerrick Stolee via GitGitGadget, Jul 23, 2020
  118. Junio C HamanoJul 23, 2020
  119. Eric SunshineJul 23, 2020
  120. Junio C HamanoJul 23, 2020
  121. Derrick StoleeJul 24, 2020
  122. Derrick StoleeJul 24, 2020
  123. Junio C HamanoJul 24, 2020
  124. Emily ShafferJul 29, 2020
  125. Derrick StoleeJul 30, 2020
  126. Derrick StoleeJul 30, 2020
  127. Chris TorekJul 30, 2020
  128. Đoàn Trần Công DanhAug 5, 2020
  129. Derrick StoleeAug 6, 2020
  130. 13/18 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Jul 23, 2020
  131. 14/18 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Jul 23, 2020
  132. 12/18 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Jul 23, 2020
  133. 18/18 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Jul 23, 2020
  134. 17/18 midx: use start_delayed_progress()Derrick Stolee via GitGitGadget, Jul 23, 2020
  135. 16/18 maintenance: add incremental-repack auto conditionDerrick Stolee via GitGitGadget, Jul 23, 2020
  136. 15/18 maintenance: create auto condition for loose-objectsDerrick Stolee via GitGitGadget, Jul 23, 2020
  137. Emily ShafferJul 29, 2020
  138. 00/20 Maintenance builtin, allowing 'gc --auto' customizationDerrick Stolee via GitGitGadget, Jul 30, 2020
  139. 01/20 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Jul 30, 2020
  140. 02/20 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Jul 30, 2020
  141. 04/20 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Jul 30, 2020
  142. 03/20 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Jul 30, 2020
  143. 05/20 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Jul 30, 2020
  144. 06/20 maintenance: add --task optionDerrick Stolee via GitGitGadget, Jul 30, 2020
  145. 08/20 fetch: optionally allow disabling FETCH_HEAD updateJunio C Hamano via GitGitGadget, Jul 30, 2020
  146. 07/20 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Jul 30, 2020
  147. 10/20 maintenance: add loose-objects taskDerrick Stolee via GitGitGadget, Jul 30, 2020
  148. 09/20 maintenance: add prefetch taskDerrick Stolee via GitGitGadget, Jul 30, 2020
  149. 11/20 midx: enable core.multiPackIndex by defaultDerrick Stolee via GitGitGadget, Jul 30, 2020
  150. 14/20 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Jul 30, 2020
  151. 15/20 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Jul 30, 2020
  152. 13/20 maintenance: auto-size incremental-repack batchDerrick Stolee via GitGitGadget, Jul 30, 2020
  153. Chris TorekJul 30, 2020
  154. Derrick StoleeAug 3, 2020
  155. 12/20 maintenance: add incremental-repack taskDerrick Stolee via GitGitGadget, Jul 30, 2020
  156. 20/20 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Jul 30, 2020
  157. 18/20 maintenance: add incremental-repack auto conditionDerrick Stolee via GitGitGadget, Jul 30, 2020
  158. 17/20 maintenance: create auto condition for loose-objectsDerrick Stolee via GitGitGadget, Jul 30, 2020
  159. 19/20 midx: use start_delayed_progress()Derrick Stolee via GitGitGadget, Jul 30, 2020
  160. 16/20 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Jul 30, 2020
  161. Junio C HamanoJul 30, 2020
  162. Junio C HamanoJul 30, 2020
  163. Junio C HamanoJul 31, 2020
  164. Derrick StoleeAug 6, 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.