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

[PATCH 10/11] maintenance: add auto condition for commit-graph task

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 6, 2020, 15:48 UTC
Message-ID
<e67e259aefddab76503be0963b5833ff2aa5cb43.1596728921.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.695.git.1596728921.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

Instead of writing a new commit-graph in every 'git maintenance run --auto' process (when maintenance.commit-graph.enalbed is configured to be true), only write when there are "enough" commits not in a commit-graph file.

This count is controlled by the maintenance.commit-graph.auto config option.

To compute the count, use a depth-first search starting at each ref, and leaving markers using the PARENT1 flag. If this count reaches the limit, then terminate early and start the task. Otherwise, this operation will peel every ref and parse the commit it points to. If these are all in the commit-graph, then this is typically a very fast operation. Users with many refs might feel a slow-down, and hence could consider updating their limit to be very small. A negative value will force the step to run every time.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/config/maintenance.txt | 10 ++++
 builtin/gc.c                         | 76 ++++++++++++++++++++++++++++
 object.h                             |  1 +
 3 files changed, 87 insertions(+)
diff --git a/Documentation/config/maintenance.txt b/Documentation/config/maintenance.txt
index 370cbfb42f..9bd69b9df3 100644
--- a/Documentation/config/maintenance.txt
+++ b/Documentation/config/maintenance.txt
@@ -2,3 +2,13 @@ maintenance.<task>.enabled::
 	This boolean config option controls whether the maintenance task
 	with name `<task>` is run when no `--task` option is specified.
 	By default, only `maintenance.gc.enabled` is true.
+
+maintenance.commit-graph.auto::
+	This integer config option controls how often the `commit-graph` task
+	should be run as part of `git maintenance run --auto`. If zero, then
+	the `commit-graph` task will not run with the `--auto` option. A
+	negative value will force the task to run every time. Otherwise, a
+	positive value implies the command should run when the number of
+	reachable commits that are not in the commit-graph file is at least
+	the value of `maintenance.commit-graph.auto`. The default value is
+	100.
diff --git a/builtin/gc.c b/builtin/gc.c
index 2b3e45eb71..20d85f6f4e 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -28,6 +28,7 @@
 #include "blob.h"
 #include "tree.h"
 #include "promisor-remote.h"
+#include "refs.h"
 
 #define FAILED_RUN "failed to run %s"
 
@@ -710,6 +711,80 @@ struct maintenance_opts {
 	int quiet;
 };
 
+/* Remember to update object flag allocation in object.h */
+#define PARENT1		(1u<<16)
+
+static int num_commits_not_in_graph = 0;
+static int limit_commits_not_in_graph = 100;
+
+static int dfs_on_ref(const char *refname,
+		      const struct object_id *oid, int flags,
+		      void *cb_data)
+{
+	int result = 0;
+	struct object_id peeled;
+	struct commit_list *stack = NULL;
+	struct commit *commit;
+
+	if (!peel_ref(refname, &peeled))
+		oid = &peeled;
+	if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
+		return 0;
+
+	commit = lookup_commit(the_repository, oid);
+	if (!commit)
+		return 0;
+	if (parse_commit(commit))
+		return 0;
+
+	commit_list_append(commit, &stack);
+
+	while (!result && stack) {
+		struct commit_list *parent;
+
+		commit = pop_commit(&stack);
+
+		for (parent = commit->parents; parent; parent = parent->next) {
+			if (parse_commit(parent->item) ||
+			    commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
+			    parent->item->object.flags & PARENT1)
+				continue;
+
+			parent->item->object.flags |= PARENT1;
+			num_commits_not_in_graph++;
+
+			if (num_commits_not_in_graph >= limit_commits_not_in_graph) {
+				result = 1;
+				break;
+			}
+
+			commit_list_append(parent->item, &stack);
+		}
+	}
+
+	free_commit_list(stack);
+	return result;
+}
+
+static int should_write_commit_graph(void)
+{
+	int result;
+
+	git_config_get_int("maintenance.commit-graph.auto",
+			   &limit_commits_not_in_graph);
+
+	if (!limit_commits_not_in_graph)
+		return 0;
+	if (limit_commits_not_in_graph < 0)
+		return 1;
+
+	result = for_each_ref(dfs_on_ref, NULL);
+
+	clear_commit_marks_all(PARENT1);
+
+	return result;
+}
+
 static int run_write_commit_graph(struct maintenance_opts *opts)
 {
 	struct child_process child = CHILD_PROCESS_INIT;
@@ -822,6 +897,7 @@ static struct maintenance_task tasks[] = {
 	[TASK_COMMIT_GRAPH] = {
 		"commit-graph",
 		maintenance_task_commit_graph,
+		should_write_commit_graph,
 	},
 };
 
diff --git a/object.h b/object.h
index 96a2105859..30d4e0f6a0 100644
--- a/object.h
+++ b/object.h
@@ -74,6 +74,7 @@ struct object_array {
  * list-objects-filter.c:                                      21
  * builtin/fsck.c:           0--3
  * builtin/index-pack.c:                                     2021
+ * builtin/maintenance.c:                           16
  * builtin/pack-objects.c:                                   20
  * builtin/reflog.c:                   10--12
  * builtin/show-branch.c:    0-------------------------------------------26
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee via GitGitGadget
Message 9 of 91 in “Maintenance I: Command, gc and commit-graph tasks”
  1. 00/11 Maintenance I: Command, gc and commit-graph tasksDerrick Stolee via GitGitGadget, Aug 6, 2020
  2. 06/11 maintenance: add --task optionDerrick Stolee via GitGitGadget, Aug 6, 2020
  3. 08/11 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Aug 6, 2020
  4. 05/11 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Aug 6, 2020
  5. Martin ÅgrenAug 7, 2020
  6. Derrick StoleeAug 12, 2020
  7. Martin ÅgrenAug 14, 2020
  8. 04/11 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Aug 6, 2020
  9. 10/11 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Aug 6, 2020
  10. 11/11 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Aug 6, 2020
  11. 02/11 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Aug 6, 2020
  12. 01/11 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Aug 6, 2020
  13. Martin ÅgrenAug 7, 2020
  14. Jonathan NiederAug 12, 2020
  15. Junio C HamanoAug 12, 2020
  16. Jonathan NiederAug 12, 2020
  17. Derrick StoleeAug 14, 2020
  18. 03/11 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Aug 6, 2020
  19. 07/11 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Aug 6, 2020
  20. 09/11 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Aug 6, 2020
  21. 00/11 Maintenance I: Command, gc and commit-graph tasksDerrick Stolee via GitGitGadget, Aug 18, 2020
  22. 02/11 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Aug 18, 2020
  23. 01/11 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Aug 18, 2020
  24. 03/11 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Aug 18, 2020
  25. 04/11 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Aug 18, 2020
  26. Jonathan TanAug 18, 2020
  27. 06/11 maintenance: add --task optionDerrick Stolee via GitGitGadget, Aug 18, 2020
  28. Jonathan TanAug 19, 2020
  29. Junio C HamanoAug 19, 2020
  30. Derrick StoleeAug 19, 2020
  31. Jonathan TanAug 19, 2020
  32. 05/11 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Aug 18, 2020
  33. Jonathan TanAug 18, 2020
  34. Derrick StoleeAug 19, 2020
  35. Jonathan TanAug 19, 2020
  36. 08/11 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Aug 18, 2020
  37. 10/11 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Aug 18, 2020
  38. Jonathan TanAug 19, 2020
  39. Derrick StoleeAug 19, 2020
  40. 11/11 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Aug 18, 2020
  41. Jonathan TanAug 19, 2020
  42. 09/11 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Aug 18, 2020
  43. 07/11 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Aug 18, 2020
  44. Jonathan TanAug 19, 2020
  45. Derrick StoleeAug 19, 2020
  46. Junio C HamanoAug 18, 2020
  47. Derrick StoleeAug 19, 2020
  48. 00/11 Maintenance I: Command, gc and commit-graph tasksDerrick Stolee via GitGitGadget, Aug 25, 2020
  49. 01/11 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Aug 25, 2020
  50. 03/11 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Aug 25, 2020
  51. 02/11 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Aug 25, 2020
  52. 04/11 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Aug 25, 2020
  53. 06/11 maintenance: add --task optionDerrick Stolee via GitGitGadget, Aug 25, 2020
  54. 05/11 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Aug 25, 2020
  55. 07/11 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Aug 25, 2020
  56. Jonathan TanAug 26, 2020
  57. 08/11 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Aug 25, 2020
  58. 10/11 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Aug 25, 2020
  59. Jonathan TanAug 26, 2020
  60. Junio C HamanoAug 26, 2020
  61. 11/11 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Aug 25, 2020
  62. 09/11 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Aug 25, 2020
  63. 00/11 Maintenance I: Command, gc and commit-graph tasksDerrick Stolee via GitGitGadget, Sep 4, 2020
  64. 04/11 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Sep 4, 2020
  65. 05/11 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Sep 4, 2020
  66. 03/11 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Sep 4, 2020
  67. 07/11 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Sep 4, 2020
  68. 11/11 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Sep 4, 2020
  69. 09/11 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Sep 4, 2020
  70. 10/11 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Sep 4, 2020
  71. 06/11 maintenance: add --task optionDerrick Stolee via GitGitGadget, Sep 4, 2020
  72. 08/11 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Sep 4, 2020
  73. 01/11 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Sep 4, 2020
  74. 02/11 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Sep 4, 2020
  75. 00/11 Maintenance I: Command, gc and commit-graph tasksDerrick Stolee via GitGitGadget, Sep 17, 2020
  76. 04/11 maintenance: initialize task arrayDerrick Stolee via GitGitGadget, Sep 17, 2020
  77. 05/11 maintenance: add commit-graph taskDerrick Stolee via GitGitGadget, Sep 17, 2020
  78. 06/11 maintenance: add --task optionDerrick Stolee via GitGitGadget, Sep 17, 2020
  79. 01/11 maintenance: create basic maintenance runnerDerrick Stolee via GitGitGadget, Sep 17, 2020
  80. 10/11 maintenance: add auto condition for commit-graph taskDerrick Stolee via GitGitGadget, Sep 17, 2020
  81. 11/11 maintenance: add trace2 regions for task executionDerrick Stolee via GitGitGadget, Sep 17, 2020
  82. 03/11 maintenance: replace run_auto_gc()Derrick Stolee via GitGitGadget, Sep 17, 2020
  83. 08/11 maintenance: create maintenance.<task>.enabled configDerrick Stolee via GitGitGadget, Sep 17, 2020
  84. 09/11 maintenance: use pointers to check --autoDerrick Stolee via GitGitGadget, Sep 17, 2020
  85. 07/11 maintenance: take a lock on the objects directoryDerrick Stolee via GitGitGadget, Sep 17, 2020
  86. Ævar Arnfjörð BjarmasonSep 21, 2020
  87. Derrick StoleeSep 21, 2020
  88. Junio C HamanoSep 21, 2020
  89. 02/11 maintenance: add --quiet optionDerrick Stolee via GitGitGadget, Sep 17, 2020
  90. Junio C HamanoSep 17, 2020
  91. Johannes SchindelinSep 18, 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.