Skip to content

Commit

Permalink
commit-graph: build graph from starting commits
Browse files Browse the repository at this point in the history
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 <dstolee@microsoft.com>
  • Loading branch information
derrickstolee committed Feb 19, 2018
1 parent 823ce1a commit c3435ea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
15 changes: 14 additions & 1 deletion Documentation/git-commit-graph.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ graph-latest file if it is updated by the `--set-latest` option.
+
With the `--stdin-packs` option, generate the new commit graph by
walking objects only in the specified packfiles and any commits in
the existing graph-head.
the existing graph-head. (Cannot be combined with --stdin-commits.)
+
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.)

'read'::

Expand Down Expand Up @@ -79,6 +84,14 @@ $ git commit-graph write --set-latest --delete-expired
$ echo <pack-index> | git commit-graph write --set-latest --delete-expired --stdin-packs
------------------------------------------------

* Write a graph file, extending the current graph file using all
* commits reachable from refs/heads/*, update graph-latest, and delete
* stale graph files.
+
------------------------------------------------
$ git show-ref -s | git commit-graph write --set-latest --delete-expired --stdin-commits
------------------------------------------------

* Read basic information from a graph file.
+
------------------------------------------------
Expand Down
27 changes: 21 additions & 6 deletions builtin/commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
static char const * const builtin_commit_graph_usage[] = {
N_("git commit-graph [--object-dir <objdir>]"),
N_("git commit-graph read [--object-dir <objdir>] [--file=<hash>]"),
N_("git commit-graph write [--object-dir <objdir>] [--set-latest] [--delete-expired] [--stdin-packs]"),
N_("git commit-graph write [--object-dir <objdir>] [--set-latest] [--delete-expired] [--stdin-packs|--stdin-commits]"),
NULL
};

Expand All @@ -18,7 +18,7 @@ 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>] [--set-latest] [--delete-expired] [--stdin-packs]"),
N_("git commit-graph write [--object-dir <objdir>] [--set-latest] [--delete-expired] [--stdin-packs|--stdin-commits]"),
NULL
};

Expand All @@ -28,6 +28,7 @@ static struct opts_commit_graph {
int set_latest;
int delete_expired;
int stdin_packs;
int stdin_commits;
} opts;

static int graph_read(int argc, const char **argv)
Expand Down Expand Up @@ -152,6 +153,8 @@ static int graph_write(int argc, const char **argv)
char *old_graph_name;
const char **pack_indexes = NULL;
int nr_packs = 0;
const char **commit_hex = NULL;
int nr_commits = 0;
const char **lines = NULL;
int nr_lines = 0;
int alloc_lines = 0;
Expand All @@ -166,19 +169,23 @@ static int graph_write(int argc, const char **argv)
N_("delete expired head graph file")),
OPT_BOOL('s', "stdin-packs", &opts.stdin_packs,
N_("only scan packfiles listed by stdin")),
OPT_BOOL('C', "stdin-commits", &opts.stdin_commits,
N_("start walk at commits listed by stdin")),
OPT_END(),
};

argc = parse_options(argc, argv, NULL,
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.obj_dir)
opts.obj_dir = get_object_directory();

old_graph_name = get_graph_latest_contents(opts.obj_dir);

if (opts.stdin_packs) {
if (opts.stdin_packs || opts.stdin_commits) {
struct strbuf buf = STRBUF_INIT;
nr_lines = 0;
alloc_lines = 128;
Expand All @@ -190,13 +197,21 @@ static int graph_write(int argc, const char **argv)
strbuf_detach(&buf, NULL);
}

pack_indexes = lines;
nr_packs = nr_lines;
if (opts.stdin_packs) {
pack_indexes = lines;
nr_packs = nr_lines;
}
if (opts.stdin_commits) {
commit_hex = lines;
nr_commits = nr_lines;
}
}

graph_name = write_commit_graph(opts.obj_dir,
pack_indexes,
nr_packs);
nr_packs,
commit_hex,
nr_commits);

if (graph_name) {
if (opts.set_latest)
Expand Down
26 changes: 24 additions & 2 deletions commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,9 @@ static void close_reachable(struct packed_oid_list *oids)

char *write_commit_graph(const char *obj_dir,
const char **pack_indexes,
int nr_packs)
int nr_packs,
const char **commit_hex,
int nr_commits)
{
struct packed_oid_list oids;
struct packed_commit_list commits;
Expand Down Expand Up @@ -599,7 +601,27 @@ char *write_commit_graph(const char *obj_dir,
close_pack(p);
}
}
else

if (commit_hex) {
for (i = 0; i < nr_commits; i++) {
const char *end;
struct object_id oid;
struct commit *result;

if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
continue;

result = lookup_commit_reference_gently(&oid, 1);

if (result) {
ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
oidcpy(&oids.list[oids.nr], &(result->object.oid));
oids.nr++;
}
}
}

if (!pack_indexes && !commit_hex)
for_each_packed_object(if_packed_commit_add_to_list, &oids, 0);

close_reachable(&oids);
Expand Down
4 changes: 3 additions & 1 deletion commit-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ extern void prepare_commit_graph(void);

extern char *write_commit_graph(const char *obj_dir,
const char **pack_indexes,
int nr_packs);
int nr_packs,
const char **commit_hex,
int nr_commits);

#endif

19 changes: 19 additions & 0 deletions t/t5318-commit-graph.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ test_expect_success 'build graph from latest pack with closure' '
graph_git_behavior 'graph from pack, commit 8 vs merge 1' commits/8 merge/1
graph_git_behavior 'graph from pack, commit 8 vs merge 2' commits/8 merge/2

test_expect_success 'build graph from commits with closure' '
git tag -a -m "merge" tag/merge merge/2 &&
git rev-parse tag/merge >commits-in &&
git rev-parse merge/1 >>commits-in &&
rm $objdir/info/graph-latest &&
graph6=$(cat commits-in | git commit-graph write --set-latest --delete-expired --stdin-commits) &&
test_path_is_file $objdir/info/$graph6 &&
test_path_is_missing $objdir/info/$graph5 &&
test_path_is_file $objdir/info/graph-latest &&
printf $graph6 >expect &&
test_cmp expect $objdir/info/graph-latest &&
git commit-graph read --file=$graph6 >output &&
graph_read_expect "6" &&
test_cmp expect output
'

graph_git_behavior 'graph from commits, commit 8 vs merge 1' commits/8 merge/1
graph_git_behavior 'graph from commits, commit 8 vs merge 2' commits/8 merge/2

test_expect_success 'setup bare repo' '
cd .. &&
git clone --bare --no-local full bare &&
Expand Down

0 comments on commit c3435ea

Please sign in to comment.