commit-graph: implement "--append" option

Teach git-commit-graph to add all commits from the existing commit-graph file to the file about to be written. This should be used when adding new commits without performing garbage collection. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 10, 2018 at 08:56 UTC 7547b95b4fbb8591726b1d9381c176cc27fc6aea
5 files changed +45 -5
Documentation/git-commit-graph.txt
+10
@@ -43,6 +43,9 @@ With the `--stdin-commits` option, generate the new commit graph by
43 walking commits starting at the commits specified in stdin as a list
44 of OIDs in hex, one OID per line. (Cannot be combined with
45 --stdin-packs.)
46 ++
47 +With the `--append` option, include all commits that are present in the
48 +existing commit-graph file.
49
50 'read'::
51
@@ -72,6 +75,13 @@ $ echo <pack-index> | git commit-graph write --stdin-packs
75 $ git show-ref -s | git commit-graph write --stdin-commits
76 ------------------------------------------------
77
78 +* Write a graph file containing all commits in the current
79 +* commit-graph file along with those reachable from HEAD.
80 ++
81 +------------------------------------------------
82 +$ git rev-parse HEAD | git commit-graph write --stdin-commits --append
83 +------------------------------------------------
84 +
85 * Read basic information from the commit-graph file.
86 +
87 ------------------------------------------------
builtin/commit-graph.c
+7 -3
@@ -8,7 +8,7 @@
8 static char const * const builtin_commit_graph_usage[] = {
9 N_("git commit-graph [--object-dir <objdir>]"),
10 N_("git commit-graph read [--object-dir <objdir>]"),
11 - N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
11 + N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
12 NULL
13 };
14
@@ -18,7 +18,7 @@ static const char * const builtin_commit_graph_read_usage[] = {
18 };
19
20 static const char * const builtin_commit_graph_write_usage[] = {
21 - N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
21 + N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
22 NULL
23 };
24
@@ -26,6 +26,7 @@ static struct opts_commit_graph {
26 const char *obj_dir;
27 int stdin_packs;
28 int stdin_commits;
29 + int append;
30 } opts;
31
32 static int graph_read(int argc, const char **argv)
@@ -94,6 +95,8 @@ static int graph_write(int argc, const char **argv)
95 N_("scan pack-indexes listed by stdin for commits")),
96 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
97 N_("start walk at commits listed by stdin")),
98 + OPT_BOOL(0, "append", &opts.append,
99 + N_("include all commits already in the commit-graph file")),
100 OPT_END(),
101 };
102
@@ -131,7 +134,8 @@ static int graph_write(int argc, const char **argv)
134 pack_indexes,
135 packs_nr,
136 commit_hex,
134 - commits_nr);
137 + commits_nr,
138 + opts.append);
139
140 return 0;
141 }
commit-graph.c
+16 -1
@@ -553,7 +553,8 @@ void write_commit_graph(const char *obj_dir,
553 const char **pack_indexes,
554 int nr_packs,
555 const char **commit_hex,
556 - int nr_commits)
556 + int nr_commits,
557 + int append)
558 {
559 struct packed_oid_list oids;
560 struct packed_commit_list commits;
@@ -571,10 +572,24 @@ void write_commit_graph(const char *obj_dir,
572 oids.nr = 0;
573 oids.alloc = approximate_object_count() / 4;
574
575 + if (append) {
576 + prepare_commit_graph_one(obj_dir);
577 + if (commit_graph)
578 + oids.alloc += commit_graph->num_commits;
579 + }
580 +
581 if (oids.alloc < 1024)
582 oids.alloc = 1024;
583 ALLOC_ARRAY(oids.list, oids.alloc);
584
585 + if (append && commit_graph) {
586 + for (i = 0; i < commit_graph->num_commits; i++) {
587 + const unsigned char *hash = commit_graph->chunk_oid_lookup +
588 + commit_graph->hash_len * i;
589 + hashcpy(oids.list[oids.nr++].hash, hash);
590 + }
591 + }
592 +
593 if (pack_indexes) {
594 struct strbuf packname = STRBUF_INIT;
595 int dirlen;
commit-graph.h
+2 -1
@@ -40,6 +40,7 @@ void write_commit_graph(const char *obj_dir,
40 const char **pack_indexes,
41 int nr_packs,
42 const char **commit_hex,
43 - int nr_commits);
43 + int nr_commits,
44 + int append);
45
46 #endif
t/t5318-commit-graph.sh
+10
@@ -190,6 +190,16 @@ test_expect_success 'build graph from commits with closure' '
190 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
191 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
192
193 +test_expect_success 'build graph from commits with append' '
194 + cd "$TRASH_DIRECTORY/full" &&
195 + git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
196 + test_path_is_file $objdir/info/commit-graph &&
197 + graph_read_expect "10" "large_edges"
198 +'
199 +
200 +graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
201 +graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
202 +
203 test_expect_success 'setup bare repo' '
204 cd "$TRASH_DIRECTORY" &&
205 git clone --bare --no-local full bare &&