commit-graph: read only from specific pack-indexes

Teach git-commit-graph to inspect the objects only in a certain list of pack-indexes within the given pack directory. This allows updating the commit graph iteratively. 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 049d51a2bb9a03d2f2c2cce1ae41e57dbbf42244
7 files changed +81 -9
Documentation/git-commit-graph.txt
+10 -1
@@ -34,7 +34,9 @@ COMMANDS
34 'write'::
35
36 Write a commit graph file based on the commits found in packfiles.
37 -Includes all commits from the existing commit graph file.
37 ++
38 +With the `--stdin-packs` option, generate the new commit graph by
39 +walking objects only in the specified pack-indexes.
40
41 'read'::
42
@@ -51,6 +53,13 @@ EXAMPLES
53 $ git commit-graph write
54 ------------------------------------------------
55
56 +* Write a graph file, extending the current graph file using commits
57 +* in <pack-index>.
58 ++
59 +------------------------------------------------
60 +$ echo <pack-index> | git commit-graph write --stdin-packs
61 +------------------------------------------------
62 +
63 * Read basic information from the commit-graph file.
64 +
65 ------------------------------------------------
builtin/commit-graph.c
+30 -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>]"),
11 + N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
12 NULL
13 };
14
@@ -18,12 +18,13 @@ 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>]"),
21 + N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
22 NULL
23 };
24
25 static struct opts_commit_graph {
26 const char *obj_dir;
27 + int stdin_packs;
28 } opts;
29
30 static int graph_read(int argc, const char **argv)
@@ -76,10 +77,18 @@ static int graph_read(int argc, const char **argv)
77
78 static int graph_write(int argc, const char **argv)
79 {
80 + const char **pack_indexes = NULL;
81 + int packs_nr = 0;
82 + const char **lines = NULL;
83 + int lines_nr = 0;
84 + int lines_alloc = 0;
85 +
86 static struct option builtin_commit_graph_write_options[] = {
87 OPT_STRING(0, "object-dir", &opts.obj_dir,
88 N_("dir"),
89 N_("The object directory to store the graph")),
90 + OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
91 + N_("scan pack-indexes listed by stdin for commits")),
92 OPT_END(),
93 };
94
@@ -90,7 +99,25 @@ static int graph_write(int argc, const char **argv)
99 if (!opts.obj_dir)
100 opts.obj_dir = get_object_directory();
101
93 - write_commit_graph(opts.obj_dir);
102 + if (opts.stdin_packs) {
103 + struct strbuf buf = STRBUF_INIT;
104 + lines_nr = 0;
105 + lines_alloc = 128;
106 + ALLOC_ARRAY(lines, lines_alloc);
107 +
108 + while (strbuf_getline(&buf, stdin) != EOF) {
109 + ALLOC_GROW(lines, lines_nr + 1, lines_alloc);
110 + lines[lines_nr++] = strbuf_detach(&buf, NULL);
111 + }
112 +
113 + pack_indexes = lines;
114 + packs_nr = lines_nr;
115 + }
116 +
117 + write_commit_graph(opts.obj_dir,
118 + pack_indexes,
119 + packs_nr);
120 +
121 return 0;
122 }
123
commit-graph.c
+24 -2
@@ -549,7 +549,9 @@ static void close_reachable(struct packed_oid_list *oids)
549 }
550 }
551
552 -void write_commit_graph(const char *obj_dir)
552 +void write_commit_graph(const char *obj_dir,
553 + const char **pack_indexes,
554 + int nr_packs)
555 {
556 struct packed_oid_list oids;
557 struct packed_commit_list commits;
@@ -571,7 +573,27 @@ void write_commit_graph(const char *obj_dir)
573 oids.alloc = 1024;
574 ALLOC_ARRAY(oids.list, oids.alloc);
575
574 - for_each_packed_object(add_packed_commits, &oids, 0);
576 + if (pack_indexes) {
577 + struct strbuf packname = STRBUF_INIT;
578 + int dirlen;
579 + strbuf_addf(&packname, "%s/pack/", obj_dir);
580 + dirlen = packname.len;
581 + for (i = 0; i < nr_packs; i++) {
582 + struct packed_git *p;
583 + strbuf_setlen(&packname, dirlen);
584 + strbuf_addstr(&packname, pack_indexes[i]);
585 + p = add_packed_git(packname.buf, packname.len, 1);
586 + if (!p)
587 + die("error adding pack %s", packname.buf);
588 + if (open_pack_index(p))
589 + die("error opening index for %s", packname.buf);
590 + for_each_object_in_pack(p, add_packed_commits, &oids);
591 + close_pack(p);
592 + }
593 + strbuf_release(&packname);
594 + } else
595 + for_each_packed_object(add_packed_commits, &oids, 0);
596 +
597 close_reachable(&oids);
598
599 QSORT(oids.list, oids.nr, commit_compare);
commit-graph.h
+3 -1
@@ -36,6 +36,8 @@ struct commit_graph {
36
37 struct commit_graph *load_commit_graph_one(const char *graph_file);
38
39 -void write_commit_graph(const char *obj_dir);
39 +void write_commit_graph(const char *obj_dir,
40 + const char **pack_indexes,
41 + int nr_packs);
42
43 #endif
packfile.c
+2 -2
@@ -304,7 +304,7 @@ void close_pack_index(struct packed_git *p)
304 }
305 }
306
307 -static void close_pack(struct packed_git *p)
307 +void close_pack(struct packed_git *p)
308 {
309 close_pack_windows(p);
310 close_pack_fd(p);
@@ -1850,7 +1850,7 @@ int has_pack_index(const unsigned char *sha1)
1850 return 1;
1851 }
1852
1853 -static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
1853 +int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
1854 {
1855 uint32_t i;
1856 int r = 0;
packfile.h
+2
@@ -63,6 +63,7 @@ extern void close_pack_index(struct packed_git *);
63
64 extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
65 extern void close_pack_windows(struct packed_git *);
66 +extern void close_pack(struct packed_git *);
67 extern void close_all_packs(void);
68 extern void unuse_pack(struct pack_window **);
69 extern void clear_delta_base_cache(void);
@@ -140,6 +141,7 @@ typedef int each_packed_object_fn(const struct object_id *oid,
141 struct packed_git *pack,
142 uint32_t pos,
143 void *data);
144 +extern int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn, void *data);
145 extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
146
147 /*
t/t5318-commit-graph.sh
+10
@@ -167,6 +167,16 @@ test_expect_success 'write graph with nothing new' '
167 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
168 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
169
170 +test_expect_success 'build graph from latest pack with closure' '
171 + cd "$TRASH_DIRECTORY/full" &&
172 + cat new-idx | git commit-graph write --stdin-packs &&
173 + test_path_is_file $objdir/info/commit-graph &&
174 + graph_read_expect "9" "large_edges"
175 +'
176 +
177 +graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
178 +graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
179 +
180 test_expect_success 'setup bare repo' '
181 cd "$TRASH_DIRECTORY" &&
182 git clone --bare --no-local full bare &&