commit-graph: implement 'git-commit-graph write'
[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         char *graph_name;
26
27         static struct option builtin_commit_graph_write_options[] = {
28                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
29                         N_("dir"),
30                         N_("The object directory to store the graph") },
31                 OPT_END(),
32         };
33
34         argc = parse_options(argc, argv, NULL,
35                              builtin_commit_graph_write_options,
36                              builtin_commit_graph_write_usage, 0);
37
38         if (!opts.obj_dir)
39                 opts.obj_dir = get_object_directory();
40
41         graph_name = write_commit_graph(opts.obj_dir);
42
43         if (graph_name) {
44                 printf("%s\n", graph_name);
45                 FREE_AND_NULL(graph_name);
46         }
47
48         return 0;
49 }
50
51 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
52 {
53         static struct option builtin_commit_graph_options[] = {
54                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
55                         N_("dir"),
56                         N_("The object directory to store the graph") },
57                 OPT_END(),
58         };
59
60         if (argc == 2 && !strcmp(argv[1], "-h"))
61                 usage_with_options(builtin_commit_graph_usage,
62                                    builtin_commit_graph_options);
63
64         git_config(git_default_config, NULL);
65         argc = parse_options(argc, argv, prefix,
66                              builtin_commit_graph_options,
67                              builtin_commit_graph_usage,
68                              PARSE_OPT_STOP_AT_NON_OPTION);
69
70         if (argc > 0) {
71                 if (!strcmp(argv[0], "write"))
72                         return graph_write(argc, argv);
73         }
74
75         usage_with_options(builtin_commit_graph_usage,
76                            builtin_commit_graph_options);
77 }