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

[PATCH v6 19/21] commit-graph: add '--reachable' option

From
Derrick Stolee <dstolee@microsoft.com>
Date
Jun 8, 2018, 13:56 UTC
Message-ID
<20180608135548.216405-20-dstolee@microsoft.com>
In-Reply-To
<20180608135548.216405-1-dstolee@microsoft.com>

When writing commit-graph files, it can be convenient to ask for all reachable commits (starting at the ref set) in the resulting file. This is particularly helpful when writing to stdin is complicated, such as a future integration with 'git gc'.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/git-commit-graph.txt |  8 ++++++--
 builtin/commit-graph.c             | 16 ++++++++++++----
 commit-graph.c                     | 18 ++++++++++++++++++
 commit-graph.h                     |  1 +
 t/t5318-commit-graph.sh            | 10 ++++++++++
 5 files changed, 47 insertions(+), 6 deletions(-)
diff --git a/Documentation/git-commit-graph.txt b/Documentation/git-commit-graph.txt
index a222cfab08..dececb79d7 100644
--- a/Documentation/git-commit-graph.txt
+++ b/Documentation/git-commit-graph.txt
@@ -38,12 +38,16 @@ Write a commit graph file based on the commits found in packfiles.
 +
 With the `--stdin-packs` option, generate the new commit graph by
 walking objects only in the specified pack-indexes. (Cannot be combined
-with --stdin-commits.)
+with `--stdin-commits` or `--reachable`.)
 +
 With the `--stdin-commits` option, generate the new commit graph by
 walking commits starting at the commits specified in stdin as a list
 of OIDs in hex, one OID per line. (Cannot be combined with
---stdin-packs.)
+`--stdin-packs` or `--reachable`.)
++
+With the `--reachable` option, generate the new commit graph by walking
+commits starting at all refs. (Cannot be combined with `--stdin-commits`
+or `--stdin-packs`.)
 +
 With the `--append` option, include all commits that are present in the
 existing commit-graph file.
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index ea28bc311a..c7d0db5ab4 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -10,7 +10,7 @@ static char const * const builtin_commit_graph_usage[] = {
 	N_("git commit-graph [--object-dir <objdir>]"),
 	N_("git commit-graph read [--object-dir <objdir>]"),
 	N_("git commit-graph verify [--object-dir <objdir>]"),
-	N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
+	N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
 	NULL
 };
 
@@ -25,12 +25,13 @@ static const char * const builtin_commit_graph_read_usage[] = {
 };
 
 static const char * const builtin_commit_graph_write_usage[] = {
-	N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
+	N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
 	NULL
 };
 
 static struct opts_commit_graph {
 	const char *obj_dir;
+	int reachable;
 	int stdin_packs;
 	int stdin_commits;
 	int append;
@@ -127,6 +128,8 @@ static int graph_write(int argc, const char **argv)
 		OPT_STRING(0, "object-dir", &opts.obj_dir,
 			N_("dir"),
 			N_("The object directory to store the graph")),
+		OPT_BOOL(0, "reachable", &opts.reachable,
+			N_("start walk at all refs")),
 		OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
 			N_("scan pack-indexes listed by stdin for commits")),
 		OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
@@ -140,11 +143,16 @@ static int graph_write(int argc, const char **argv)
 			     builtin_commit_graph_write_options,
 			     builtin_commit_graph_write_usage, 0);
 
-	if (opts.stdin_packs && opts.stdin_commits)
-		die(_("cannot use both --stdin-commits and --stdin-packs"));
+	if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
+		die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
 	if (!opts.obj_dir)
 		opts.obj_dir = get_object_directory();
 
+	if (opts.reachable) {
+		write_commit_graph_reachable(opts.obj_dir, opts.append);
+		return 0;
+	}
+
 	string_list_init(&lines, 0);
 	if (opts.stdin_packs || opts.stdin_commits) {
 		struct strbuf buf = STRBUF_INIT;
diff --git a/commit-graph.c b/commit-graph.c
index a06e7e9549..adf54e3fe7 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -7,6 +7,7 @@
 #include "packfile.h"
 #include "commit.h"
 #include "object.h"
+#include "refs.h"
 #include "revision.h"
 #include "sha1-lookup.h"
 #include "commit-graph.h"
@@ -656,6 +657,23 @@ static void compute_generation_numbers(struct packed_commit_list* commits)
 	}
 }
 
+static int add_ref_to_list(const char *refname,
+			   const struct object_id *oid,
+			   int flags, void *cb_data)
+{
+	struct string_list *list = (struct string_list*)cb_data;
+	string_list_append(list, oid_to_hex(oid));
+	return 0;
+}
+
+void write_commit_graph_reachable(const char *obj_dir, int append)
+{
+	struct string_list list;
+	string_list_init(&list, 1);
+	for_each_ref(add_ref_to_list, &list);
+	write_commit_graph(obj_dir, NULL, &list, append);
+}
+
 void write_commit_graph(const char *obj_dir,
 			struct string_list *pack_indexes,
 			struct string_list *commit_hex,
diff --git a/commit-graph.h b/commit-graph.h
index a79b854470..506cb45fb1 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -48,6 +48,7 @@ struct commit_graph {
 
 struct commit_graph *load_commit_graph_one(const char *graph_file);
 
+void write_commit_graph_reachable(const char *obj_dir, int append);
 void write_commit_graph(const char *obj_dir,
 			struct string_list *pack_indexes,
 			struct string_list *commit_hex,
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
index 0da5a51552..06ecbb3f4a 100755
--- a/t/t5318-commit-graph.sh
+++ b/t/t5318-commit-graph.sh
@@ -205,6 +205,16 @@ test_expect_success 'build graph from commits with append' '
 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
 
+test_expect_success 'build graph using --reachable' '
+	cd "$TRASH_DIRECTORY/full" &&
+	git commit-graph write --reachable &&
+	test_path_is_file $objdir/info/commit-graph &&
+	graph_read_expect "11" "large_edges"
+'
+
+graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
+
 test_expect_success 'setup bare repo' '
 	cd "$TRASH_DIRECTORY" &&
 	git clone --bare --no-local full bare &&
-- 
2.18.0.rc1
Previous: Derrick StoleeNext: Derrick Stolee
Message 70 of 125 in “Integrate commit-graph into 'fsck' and 'gc'”
  1. 00/21 Integrate commit-graph into 'fsck' and 'gc'Derrick Stolee, Jun 4, 2018
  2. 01/21 commit-graph: UNLEAK before die()Derrick Stolee, Jun 4, 2018
  3. 02/21 commit-graph: fix GRAPH_MIN_SIZEDerrick Stolee, Jun 4, 2018
  4. 03/21 commit-graph: parse commit from chosen graphDerrick Stolee, Jun 4, 2018
  5. 05/21 commit-graph: load a root tree from specific graphDerrick Stolee, Jun 4, 2018
  6. 07/21 commit-graph: verify catches corrupt signatureDerrick Stolee, Jun 4, 2018
  7. 06/21 commit-graph: add 'verify' subcommandDerrick Stolee, Jun 4, 2018
  8. 08/21 commit-graph: verify required chunks are presentDerrick Stolee, Jun 4, 2018
  9. 09/21 commit-graph: verify corrupt OID fanout and lookupDerrick Stolee, Jun 4, 2018
  10. 11/21 commit-graph: verify root tree OIDsDerrick Stolee, Jun 4, 2018
  11. 12/21 commit-graph: verify parent listDerrick Stolee, Jun 4, 2018
  12. 10/21 commit-graph: verify objects existDerrick Stolee, Jun 4, 2018
  13. 18/21 commit-graph: use string-list API for inputDerrick Stolee, Jun 4, 2018
  14. 20/21 gc: automatically write commit-graph filesDerrick Stolee, Jun 4, 2018
  15. Ævar Arnfjörð BjarmasonJun 6, 2018
  16. 19/21 commit-graph: add '--reachable' optionDerrick Stolee, Jun 4, 2018
  17. 21/21 commit-graph: update design documentDerrick Stolee, Jun 4, 2018
  18. 13/21 commit-graph: verify generation numberDerrick Stolee, Jun 4, 2018
  19. 17/21 fsck: verify commit-graphDerrick Stolee, Jun 4, 2018
  20. Ævar Arnfjörð BjarmasonJun 6, 2018
  21. Derrick StoleeJun 6, 2018
  22. 16/21 commit-graph: verify contents match checksumDerrick Stolee, Jun 4, 2018
  23. 14/21 commit-graph: verify commit dateDerrick Stolee, Jun 4, 2018
  24. 15/21 commit-graph: test for corrupted octopus edgeDerrick Stolee, Jun 4, 2018
  25. 04/21 commit: force commit to parse from object databaseDerrick Stolee, Jun 4, 2018
  26. Derrick StoleeJun 4, 2018
  27. Ævar Arnfjörð BjarmasonJun 5, 2018
  28. Derrick StoleeJun 5, 2018
  29. 00/21 Integrate commit-graph into 'fsck' and 'gc'Derrick Stolee, Jun 6, 2018
  30. 05/21 commit-graph: load a root tree from specific graphDerrick Stolee, Jun 6, 2018
  31. 17/21 fsck: verify commit-graphDerrick Stolee, Jun 6, 2018
  32. 15/21 commit-graph: test for corrupted octopus edgeDerrick Stolee, Jun 6, 2018
  33. 21/21 commit-graph: update design documentDerrick Stolee, Jun 6, 2018
  34. 13/21 commit-graph: verify generation numberDerrick Stolee, Jun 6, 2018
  35. 20/21 gc: automatically write commit-graph filesDerrick Stolee, Jun 6, 2018
  36. 10/21 commit-graph: verify objects existDerrick Stolee, Jun 6, 2018
  37. 19/21 commit-graph: add '--reachable' optionDerrick Stolee, Jun 6, 2018
  38. 03/21 commit-graph: parse commit from chosen graphDerrick Stolee, Jun 6, 2018
  39. 18/21 commit-graph: use string-list API for inputDerrick Stolee, Jun 6, 2018
  40. Ævar Arnfjörð BjarmasonJun 6, 2018
  41. Derrick StoleeJun 6, 2018
  42. Ævar Arnfjörð BjarmasonJun 6, 2018
  43. Derrick StoleeJun 6, 2018
  44. Derrick StoleeJun 8, 2018
  45. 06/21 commit-graph: add 'verify' subcommandDerrick Stolee, Jun 6, 2018
  46. 16/21 commit-graph: verify contents match checksumDerrick Stolee, Jun 6, 2018
  47. 09/21 commit-graph: verify corrupt OID fanout and lookupDerrick Stolee, Jun 6, 2018
  48. 04/21 commit: force commit to parse from object databaseDerrick Stolee, Jun 6, 2018
  49. 14/21 commit-graph: verify commit dateDerrick Stolee, Jun 6, 2018
  50. 07/21 commit-graph: verify catches corrupt signatureDerrick Stolee, Jun 6, 2018
  51. 08/21 commit-graph: verify required chunks are presentDerrick Stolee, Jun 6, 2018
  52. Ævar Arnfjörð BjarmasonJun 6, 2018
  53. 12/21 commit-graph: verify parent listDerrick Stolee, Jun 6, 2018
  54. 11/21 commit-graph: verify root tree OIDsDerrick Stolee, Jun 6, 2018
  55. 01/21 commit-graph: UNLEAK before die()Derrick Stolee, Jun 6, 2018
  56. 02/21 commit-graph: fix GRAPH_MIN_SIZEDerrick Stolee, Jun 6, 2018
  57. 00/21 Integrate commit-graph into 'fsck' and 'gc'Derrick Stolee, Jun 8, 2018
  58. 02/21 commit-graph: fix GRAPH_MIN_SIZEDerrick Stolee, Jun 8, 2018
  59. 03/21 commit-graph: parse commit from chosen graphDerrick Stolee, Jun 8, 2018
  60. 05/21 commit-graph: load a root tree from specific graphDerrick Stolee, Jun 8, 2018
  61. 06/21 commit-graph: add 'verify' subcommandDerrick Stolee, Jun 8, 2018
  62. 09/21 commit-graph: verify corrupt OID fanout and lookupDerrick Stolee, Jun 8, 2018
  63. 12/21 commit-graph: verify parent listDerrick Stolee, Jun 8, 2018
  64. Junio C HamanoJun 12, 2018
  65. 10/21 commit-graph: verify objects existDerrick Stolee, Jun 8, 2018
  66. 17/21 fsck: verify commit-graphDerrick Stolee, Jun 8, 2018
  67. 21/21 commit-graph: update design documentDerrick Stolee, Jun 8, 2018
  68. 16/21 commit-graph: verify contents match checksumDerrick Stolee, Jun 8, 2018
  69. 18/21 commit-graph: use string-list API for inputDerrick Stolee, Jun 8, 2018
  70. 19/21 commit-graph: add '--reachable' optionDerrick Stolee, Jun 8, 2018
  71. 15/21 commit-graph: test for corrupted octopus edgeDerrick Stolee, Jun 8, 2018
  72. 20/21 gc: automatically write commit-graph filesDerrick Stolee, Jun 8, 2018
  73. SZEDER GáborJun 8, 2018
  74. Derrick StoleeJun 25, 2018
  75. 11/21 commit-graph: verify root tree OIDsDerrick Stolee, Jun 8, 2018
  76. 14/21 commit-graph: verify commit dateDerrick Stolee, Jun 8, 2018
  77. 13/21 commit-graph: verify generation numberDerrick Stolee, Jun 8, 2018
  78. 08/21 commit-graph: verify required chunks are presentDerrick Stolee, Jun 8, 2018
  79. 07/21 commit-graph: verify catches corrupt signatureDerrick Stolee, Jun 8, 2018
  80. 04/21 commit: force commit to parse from object databaseDerrick Stolee, Jun 8, 2018
  81. 01/21 commit-graph: UNLEAK before die()Derrick Stolee, Jun 8, 2018
  82. Jakub NarębskiJun 8, 2018
  83. Derrick StoleeJun 8, 2018
  84. 00/22 Integrate commit-graph into 'fsck' and 'gc'Derrick Stolee, Jun 27, 2018
  85. 01/22 t5318-commit-graph.sh: use core.commitGraphDerrick Stolee, Jun 27, 2018
  86. Junio C HamanoJun 27, 2018
  87. 02/22 commit-graph: UNLEAK before die()Derrick Stolee, Jun 27, 2018
  88. 03/22 commit-graph: fix GRAPH_MIN_SIZEDerrick Stolee, Jun 27, 2018
  89. 05/22 commit: force commit to parse from object databaseDerrick Stolee, Jun 27, 2018
  90. 04/22 commit-graph: parse commit from chosen graphDerrick Stolee, Jun 27, 2018
  91. 06/22 commit-graph: load a root tree from specific graphDerrick Stolee, Jun 27, 2018
  92. SZEDER GáborJul 11, 2018
  93. coccinelle: update commit.cocciDerrick Stolee, Jul 13, 2018
  94. Eric SunshineJul 13, 2018
  95. 08/22 commit-graph: verify catches corrupt signatureDerrick Stolee, Jun 27, 2018
  96. 07/22 commit-graph: add 'verify' subcommandDerrick Stolee, Jun 27, 2018
  97. Jonathan TanJun 27, 2018
  98. Derrick StoleeJun 29, 2018
  99. 12/22 commit-graph: verify root tree OIDsDerrick Stolee, Jun 27, 2018
  100. 13/22 commit-graph: verify parent listDerrick Stolee, Jun 27, 2018
  101. 14/22 commit-graph: verify generation numberDerrick Stolee, Jun 27, 2018
  102. 16/22 commit-graph: test for corrupted octopus edgeDerrick Stolee, Jun 27, 2018
  103. 18/22 fsck: verify commit-graphDerrick Stolee, Jun 27, 2018
  104. 17/22 commit-graph: verify contents match checksumDerrick Stolee, Jun 27, 2018
  105. Junio C HamanoJun 27, 2018
  106. 11/22 commit-graph: verify objects existDerrick Stolee, Jun 27, 2018
  107. 10/22 commit-graph: verify corrupt OID fanout and lookupDerrick Stolee, Jun 27, 2018
  108. 22/22 commit-graph: update design documentDerrick Stolee, Jun 27, 2018
  109. Junio C HamanoJun 27, 2018
  110. 15/22 commit-graph: verify commit dateDerrick Stolee, Jun 27, 2018
  111. 21/22 gc: automatically write commit-graph filesDerrick Stolee, Jun 27, 2018
  112. Junio C HamanoJun 27, 2018
  113. Derrick StoleeJun 27, 2018
  114. SZEDER GáborAug 3, 2018
  115. t5318: use 'test_cmp_bin' to compare commit-graph filesSZEDER Gábor, Aug 12, 2018
  116. Derrick StoleeAug 13, 2018
  117. SZEDER GáborAug 13, 2018
  118. t5318: use 'test_cmp_bin' to compare commit-graph filesSZEDER Gábor, Aug 13, 2018
  119. 20/22 commit-graph: add '--reachable' optionDerrick Stolee, Jun 27, 2018
  120. Junio C HamanoJun 27, 2018
  121. Christian CouderSep 11, 2018
  122. Derrick StoleeSep 11, 2018
  123. Christian CouderSep 11, 2018
  124. 09/22 commit-graph: verify required chunks are presentDerrick Stolee, Jun 27, 2018
  125. 19/22 commit-graph: use string-list API for inputDerrick Stolee, Jun 27, 2018

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.