Teach git-commit-graph to write graph files. Create new test script to verify
this command succeeds without failure.

Signed-off-by: Derrick Stolee <dsto...@microsoft.com>
---
 Documentation/git-commit-graph.txt | 18 +++++++
 builtin/commit-graph.c             | 30 ++++++++++++
 t/t5318-commit-graph.sh            | 96 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100755 t/t5318-commit-graph.sh

diff --git a/Documentation/git-commit-graph.txt 
b/Documentation/git-commit-graph.txt
index c8ea548dfb..3f3790d9a8 100644
--- a/Documentation/git-commit-graph.txt
+++ b/Documentation/git-commit-graph.txt
@@ -5,3 +5,21 @@ NAME
 ----
 git-commit-graph - Write and verify Git commit graphs (.graph files)
 
+
+SYNOPSIS
+--------
+[verse]
+'git commit-graph' --write <options> [--pack-dir <pack_dir>]
+
+EXAMPLES
+--------
+
+* Write a commit graph file for the packed commits in your local .git folder.
++
+------------------------------------------------
+$ git commit-graph --write
+------------------------------------------------
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 2104550d25..7affd512f1 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -6,22 +6,38 @@
 #include "lockfile.h"
 #include "packfile.h"
 #include "parse-options.h"
+#include "commit-graph.h"
 
 static char const * const builtin_commit_graph_usage[] = {
        N_("git commit-graph [--pack-dir <packdir>]"),
+       N_("git commit-graph --write [--pack-dir <packdir>]"),
        NULL
 };
 
 static struct opts_commit_graph {
        const char *pack_dir;
+       int write;
 } opts;
 
+static int graph_write(void)
+{
+       struct object_id *graph_hash = construct_commit_graph(opts.pack_dir);
+
+       if (graph_hash)
+               printf("%s\n", oid_to_hex(graph_hash));
+
+       free(graph_hash);
+       return 0;
+}
+
 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
 {
        static struct option builtin_commit_graph_options[] = {
                { OPTION_STRING, 'p', "pack-dir", &opts.pack_dir,
                        N_("dir"),
                        N_("The pack directory to store the graph") },
+               OPT_BOOL('w', "write", &opts.write,
+                       N_("write commit graph file")),
                OPT_END(),
        };
 
@@ -29,5 +45,19 @@ int cmd_commit_graph(int argc, const char **argv, const char 
*prefix)
                usage_with_options(builtin_commit_graph_usage,
                                   builtin_commit_graph_options);
 
+       argc = parse_options(argc, argv, prefix,
+                            builtin_commit_graph_options,
+                            builtin_commit_graph_usage, 0);
+
+       if (!opts.pack_dir) {
+               struct strbuf path = STRBUF_INIT;
+               strbuf_addstr(&path, get_object_directory());
+               strbuf_addstr(&path, "/pack");
+               opts.pack_dir = strbuf_detach(&path, NULL);
+       }
+
+       if (opts.write)
+               return graph_write();
+
        return 0;
 }
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
new file mode 100755
index 0000000000..6bcd1cc264
--- /dev/null
+++ b/t/t5318-commit-graph.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='commit graph'
+. ./test-lib.sh
+
+test_expect_success 'setup full repo' \
+    'rm -rf .git &&
+     mkdir full &&
+     cd full &&
+     git init &&
+     git config core.commitgraph true &&
+     git config pack.threads 1 &&
+     packdir=".git/objects/pack"'
+
+test_expect_success 'write graph with no packs' \
+    'git commit-graph --write --pack-dir .'
+
+test_expect_success 'create commits and repack' \
+    'for i in $(test_seq 5)
+     do
+        echo $i >$i.txt &&
+        git add $i.txt &&
+        git commit -m "commit $i" &&
+        git branch commits/$i
+     done &&
+     git repack'
+
+test_expect_success 'write graph' \
+    'graph1=$(git commit-graph --write) &&
+     test_path_is_file ${packdir}/graph-${graph1}.graph'
+
+t_expect_success 'Add more commits' \
+    'git reset --hard commits/3 &&
+     for i in $(test_seq 6 10)
+     do
+        echo $i >$i.txt &&
+        git add $i.txt &&
+        git commit -m "commit $i" &&
+        git branch commits/$i
+     done &&
+     git reset --hard commits/3 &&
+     for i in $(test_seq 11 15)
+     do
+        echo $i >$i.txt &&
+        git add $i.txt &&
+        git commit -m "commit $i" &&
+        git branch commits/$i
+     done &&
+     git reset --hard commits/7 &&
+     git merge commits/11 &&
+     git branch merge/1 &&
+     git reset --hard commits/8 &&
+     git merge commits/12 &&
+     git branch merge/2 &&
+     git reset --hard commits/5 &&
+     git merge commits/10 commits/15 &&
+     git branch merge/3 &&
+     git repack'
+
+# Current graph structure:
+#
+#      M3
+#     / |\_____
+#    / 10      15
+#   /   |      |
+#  /    9 M2   14
+# |     |/  \  |
+# |     8 M1 | 13
+# |     |/ | \_|
+# 5     7  |   12
+# |     |   \__|
+# 4     6      11
+# |____/______/
+# 3
+# |
+# 2
+# |
+# 1
+
+test_expect_success 'write graph with merges' \
+    'graph2=$(git commit-graph --write) &&
+     test_path_is_file ${packdir}/graph-${graph2}.graph'
+
+test_expect_success 'setup bare repo' \
+    'cd .. &&
+     git clone --bare full bare &&
+     cd bare &&
+     git config core.graph true &&
+     git config pack.threads 1 &&
+     baredir="objects/pack"'
+
+test_expect_success 'write graph in bare repo' \
+    'graphbare=$(git commit-graph --write) &&
+     test_path_is_file ${baredir}/graph-${graphbare}.graph'
+
+test_done
-- 
2.16.0.15.g9c3cf44.dirty

Reply via email to