Skip to content

Commit 049d51a

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 177722b commit 049d51a

File tree

7 files changed

+81
-9
lines changed

7 files changed

+81
-9
lines changed

Documentation/git-commit-graph.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ COMMANDS
3434
'write'::
3535

3636
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.
3840

3941
'read'::
4042

@@ -51,6 +53,13 @@ EXAMPLES
5153
$ git commit-graph write
5254
------------------------------------------------
5355

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+
5463
* Read basic information from the commit-graph file.
5564
+
5665
------------------------------------------------

builtin/commit-graph.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
static char const * const builtin_commit_graph_usage[] = {
99
N_("git commit-graph [--object-dir <objdir>]"),
1010
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]"),
1212
NULL
1313
};
1414

@@ -18,12 +18,13 @@ static const char * const builtin_commit_graph_read_usage[] = {
1818
};
1919

2020
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]"),
2222
NULL
2323
};
2424

2525
static struct opts_commit_graph {
2626
const char *obj_dir;
27+
int stdin_packs;
2728
} opts;
2829

2930
static int graph_read(int argc, const char **argv)
@@ -76,10 +77,18 @@ static int graph_read(int argc, const char **argv)
7677

7778
static int graph_write(int argc, const char **argv)
7879
{
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+
7986
static struct option builtin_commit_graph_write_options[] = {
8087
OPT_STRING(0, "object-dir", &opts.obj_dir,
8188
N_("dir"),
8289
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")),
8392
OPT_END(),
8493
};
8594

@@ -90,7 +99,25 @@ static int graph_write(int argc, const char **argv)
9099
if (!opts.obj_dir)
91100
opts.obj_dir = get_object_directory();
92101

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+
94121
return 0;
95122
}
96123

commit-graph.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,9 @@ static void close_reachable(struct packed_oid_list *oids)
549549
}
550550
}
551551

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)
553555
{
554556
struct packed_oid_list oids;
555557
struct packed_commit_list commits;
@@ -571,7 +573,27 @@ void write_commit_graph(const char *obj_dir)
571573
oids.alloc = 1024;
572574
ALLOC_ARRAY(oids.list, oids.alloc);
573575

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+
575597
close_reachable(&oids);
576598

577599
QSORT(oids.list, oids.nr, commit_compare);

commit-graph.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct commit_graph {
3636

3737
struct commit_graph *load_commit_graph_one(const char *graph_file);
3838

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);
4042

4143
#endif

packfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void close_pack_index(struct packed_git *p)
304304
}
305305
}
306306

307-
static void close_pack(struct packed_git *p)
307+
void close_pack(struct packed_git *p)
308308
{
309309
close_pack_windows(p);
310310
close_pack_fd(p);
@@ -1850,7 +1850,7 @@ int has_pack_index(const unsigned char *sha1)
18501850
return 1;
18511851
}
18521852

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)
18541854
{
18551855
uint32_t i;
18561856
int r = 0;

packfile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extern void close_pack_index(struct packed_git *);
6363

6464
extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
6565
extern void close_pack_windows(struct packed_git *);
66+
extern void close_pack(struct packed_git *);
6667
extern void close_all_packs(void);
6768
extern void unuse_pack(struct pack_window **);
6869
extern void clear_delta_base_cache(void);
@@ -140,6 +141,7 @@ typedef int each_packed_object_fn(const struct object_id *oid,
140141
struct packed_git *pack,
141142
uint32_t pos,
142143
void *data);
144+
extern int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn, void *data);
143145
extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
144146

145147
/*

t/t5318-commit-graph.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ test_expect_success 'write graph with nothing new' '
167167
graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
168168
graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
169169

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+
170180
test_expect_success 'setup bare repo' '
171181
cd "$TRASH_DIRECTORY" &&
172182
git clone --bare --no-local full bare &&

0 commit comments

Comments
 (0)