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). If a commit graph file
already exists (and core.commitGraph=true), then this operation takes
only 1.8 seconds due to less time spent parsing commits.

Signed-off-by: Derrick Stolee <dsto...@microsoft.com>
---
 Documentation/git-commit-graph.txt | 15 ++++++++++++++-
 builtin/commit-graph.c             | 27 +++++++++++++++++++++------
 commit-graph.c                     | 26 ++++++++++++++++++++++++--
 commit-graph.h                     |  4 +++-
 t/t5318-commit-graph.sh            | 19 +++++++++++++++++++
 5 files changed, 81 insertions(+), 10 deletions(-)

diff --git a/Documentation/git-commit-graph.txt 
b/Documentation/git-commit-graph.txt
index 93d50d1..43ac74b 100644
--- a/Documentation/git-commit-graph.txt
+++ b/Documentation/git-commit-graph.txt
@@ -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'::
 
@@ -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.
 +
 ------------------------------------------------
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 5f08c40..9b92549 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -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
 };
 
@@ -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
 };
 
@@ -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)
@@ -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;
@@ -166,6 +169,8 @@ 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(),
        };
 
@@ -173,12 +178,14 @@ static int graph_write(int argc, const char **argv)
                             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;
@@ -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)
diff --git a/commit-graph.c b/commit-graph.c
index 943192c..b9e938c 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -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;
@@ -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);
diff --git a/commit-graph.h b/commit-graph.h
index 5617842..2582a3c 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -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
 
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
index 5bd1f77..2ed6b19 100755
--- a/t/t5318-commit-graph.sh
+++ b/t/t5318-commit-graph.sh
@@ -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 &&
-- 
2.7.4

Reply via email to