commit-graph: implement git-commit-graph write
[git/git.git] / builtin / commit-graph.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "lockfile.h"
5 #include "parse-options.h"
6 #include "commit-graph.h"
7
8 static char const * const builtin_commit_graph_usage[] = {
9 N_("git commit-graph [--object-dir <objdir>]"),
10 N_("git commit-graph write [--object-dir <objdir>]"),
11 NULL
12 };
13
14 static const char * const builtin_commit_graph_write_usage[] = {
15 N_("git commit-graph write [--object-dir <objdir>]"),
16 NULL
17 };
18
19 static struct opts_commit_graph {
20 const char *obj_dir;
21 } opts;
22
23 static int graph_write(int argc, const char **argv)
24 {
25 static struct option builtin_commit_graph_write_options[] = {
26 OPT_STRING(0, "object-dir", &opts.obj_dir,
27 N_("dir"),
28 N_("The object directory to store the graph")),
29 OPT_END(),
30 };
31
32 argc = parse_options(argc, argv, NULL,
33 builtin_commit_graph_write_options,
34 builtin_commit_graph_write_usage, 0);
35
36 if (!opts.obj_dir)
37 opts.obj_dir = get_object_directory();
38
39 write_commit_graph(opts.obj_dir);
40 return 0;
41 }
42
43 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
44 {
45 static struct option builtin_commit_graph_options[] = {
46 OPT_STRING(0, "object-dir", &opts.obj_dir,
47 N_("dir"),
48 N_("The object directory to store the graph")),
49 OPT_END(),
50 };
51
52 if (argc == 2 && !strcmp(argv[1], "-h"))
53 usage_with_options(builtin_commit_graph_usage,
54 builtin_commit_graph_options);
55
56 git_config(git_default_config, NULL);
57 argc = parse_options(argc, argv, prefix,
58 builtin_commit_graph_options,
59 builtin_commit_graph_usage,
60 PARSE_OPT_STOP_AT_NON_OPTION);
61
62 if (argc > 0) {
63 if (!strcmp(argv[0], "write"))
64 return graph_write(argc, argv);
65 }
66
67 usage_with_options(builtin_commit_graph_usage,
68 builtin_commit_graph_options);
69 }