Skip to content

Commit 3d5df01

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: build graph from starting commits
Teach git-commit-graph to read commits from stdin when the --stdin-commits flag is specified. Commits reachable from these commits are added to the graph. This is a much faster way to construct the graph than inspecting all packed objects, but is restricted to known tips. For the Linux repository, 700,000+ commits were added to the graph file starting from 'master' in 7-9 seconds, depending on the number of packfiles in the repo (1, 24, or 120). Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 049d51a commit 3d5df01

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

Documentation/git-commit-graph.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ COMMANDS
3636
Write a commit graph file based on the commits found in packfiles.
3737
+
3838
With the `--stdin-packs` option, generate the new commit graph by
39-
walking objects only in the specified pack-indexes.
39+
walking objects only in the specified pack-indexes. (Cannot be combined
40+
with --stdin-commits.)
41+
+
42+
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.)
4046

4147
'read'::
4248

@@ -60,6 +66,12 @@ $ git commit-graph write
6066
$ echo <pack-index> | git commit-graph write --stdin-packs
6167
------------------------------------------------
6268

69+
* Write a graph file containing all reachable commits.
70+
+
71+
------------------------------------------------
72+
$ git show-ref -s | git commit-graph write --stdin-commits
73+
------------------------------------------------
74+
6375
* Read basic information from the commit-graph file.
6476
+
6577
------------------------------------------------

builtin/commit-graph.c

Lines changed: 21 additions & 6 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>] [--stdin-packs]"),
11+
N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
1212
NULL
1313
};
1414

@@ -18,13 +18,14 @@ 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>] [--stdin-packs]"),
21+
N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
2222
NULL
2323
};
2424

2525
static struct opts_commit_graph {
2626
const char *obj_dir;
2727
int stdin_packs;
28+
int stdin_commits;
2829
} opts;
2930

3031
static int graph_read(int argc, const char **argv)
@@ -79,6 +80,8 @@ static int graph_write(int argc, const char **argv)
7980
{
8081
const char **pack_indexes = NULL;
8182
int packs_nr = 0;
83+
const char **commit_hex = NULL;
84+
int commits_nr = 0;
8285
const char **lines = NULL;
8386
int lines_nr = 0;
8487
int lines_alloc = 0;
@@ -89,17 +92,21 @@ static int graph_write(int argc, const char **argv)
8992
N_("The object directory to store the graph")),
9093
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
9194
N_("scan pack-indexes listed by stdin for commits")),
95+
OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
96+
N_("start walk at commits listed by stdin")),
9297
OPT_END(),
9398
};
9499

95100
argc = parse_options(argc, argv, NULL,
96101
builtin_commit_graph_write_options,
97102
builtin_commit_graph_write_usage, 0);
98103

104+
if (opts.stdin_packs && opts.stdin_commits)
105+
die(_("cannot use both --stdin-commits and --stdin-packs"));
99106
if (!opts.obj_dir)
100107
opts.obj_dir = get_object_directory();
101108

102-
if (opts.stdin_packs) {
109+
if (opts.stdin_packs || opts.stdin_commits) {
103110
struct strbuf buf = STRBUF_INIT;
104111
lines_nr = 0;
105112
lines_alloc = 128;
@@ -110,13 +117,21 @@ static int graph_write(int argc, const char **argv)
110117
lines[lines_nr++] = strbuf_detach(&buf, NULL);
111118
}
112119

113-
pack_indexes = lines;
114-
packs_nr = lines_nr;
120+
if (opts.stdin_packs) {
121+
pack_indexes = lines;
122+
packs_nr = lines_nr;
123+
}
124+
if (opts.stdin_commits) {
125+
commit_hex = lines;
126+
commits_nr = lines_nr;
127+
}
115128
}
116129

117130
write_commit_graph(opts.obj_dir,
118131
pack_indexes,
119-
packs_nr);
132+
packs_nr,
133+
commit_hex,
134+
commits_nr);
120135

121136
return 0;
122137
}

commit-graph.c

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

552552
void write_commit_graph(const char *obj_dir,
553553
const char **pack_indexes,
554-
int nr_packs)
554+
int nr_packs,
555+
const char **commit_hex,
556+
int nr_commits)
555557
{
556558
struct packed_oid_list oids;
557559
struct packed_commit_list commits;
@@ -591,7 +593,28 @@ void write_commit_graph(const char *obj_dir,
591593
close_pack(p);
592594
}
593595
strbuf_release(&packname);
594-
} else
596+
}
597+
598+
if (commit_hex) {
599+
for (i = 0; i < nr_commits; i++) {
600+
const char *end;
601+
struct object_id oid;
602+
struct commit *result;
603+
604+
if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
605+
continue;
606+
607+
result = lookup_commit_reference_gently(&oid, 1);
608+
609+
if (result) {
610+
ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
611+
oidcpy(&oids.list[oids.nr], &(result->object.oid));
612+
oids.nr++;
613+
}
614+
}
615+
}
616+
617+
if (!pack_indexes && !commit_hex)
595618
for_each_packed_object(add_packed_commits, &oids, 0);
596619

597620
close_reachable(&oids);

commit-graph.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct commit_graph *load_commit_graph_one(const char *graph_file);
3838

3939
void write_commit_graph(const char *obj_dir,
4040
const char **pack_indexes,
41-
int nr_packs);
41+
int nr_packs,
42+
const char **commit_hex,
43+
int nr_commits);
4244

4345
#endif

t/t5318-commit-graph.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ test_expect_success 'build graph from latest pack with closure' '
177177
graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
178178
graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
179179

180+
test_expect_success 'build graph from commits with closure' '
181+
cd "$TRASH_DIRECTORY/full" &&
182+
git tag -a -m "merge" tag/merge merge/2 &&
183+
git rev-parse tag/merge >commits-in &&
184+
git rev-parse merge/1 >>commits-in &&
185+
cat commits-in | git commit-graph write --stdin-commits &&
186+
test_path_is_file $objdir/info/commit-graph &&
187+
graph_read_expect "6"
188+
'
189+
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+
180193
test_expect_success 'setup bare repo' '
181194
cd "$TRASH_DIRECTORY" &&
182195
git clone --bare --no-local full bare &&

0 commit comments

Comments
 (0)