commit: integrate commit graph with commit parsing
[git/git.git] / builtin / commit-graph.c
CommitLineData
4ce58ee3
DS
1#include "builtin.h"
2#include "config.h"
f237c8b6
DS
3#include "dir.h"
4#include "lockfile.h"
4ce58ee3 5#include "parse-options.h"
f237c8b6 6#include "commit-graph.h"
4ce58ee3
DS
7
8static char const * const builtin_commit_graph_usage[] = {
9 N_("git commit-graph [--object-dir <objdir>]"),
2a2e32bd 10 N_("git commit-graph read [--object-dir <objdir>]"),
f237c8b6
DS
11 N_("git commit-graph write [--object-dir <objdir>]"),
12 NULL
13};
14
2a2e32bd
DS
15static const char * const builtin_commit_graph_read_usage[] = {
16 N_("git commit-graph read [--object-dir <objdir>]"),
17 NULL
18};
19
f237c8b6
DS
20static const char * const builtin_commit_graph_write_usage[] = {
21 N_("git commit-graph write [--object-dir <objdir>]"),
4ce58ee3
DS
22 NULL
23};
24
25static struct opts_commit_graph {
26 const char *obj_dir;
27} opts;
28
2a2e32bd
DS
29static int graph_read(int argc, const char **argv)
30{
31 struct commit_graph *graph = NULL;
32 char *graph_name;
33
34 static struct option builtin_commit_graph_read_options[] = {
35 OPT_STRING(0, "object-dir", &opts.obj_dir,
36 N_("dir"),
37 N_("The object directory to store the graph")),
38 OPT_END(),
39 };
40
41 argc = parse_options(argc, argv, NULL,
42 builtin_commit_graph_read_options,
43 builtin_commit_graph_read_usage, 0);
44
45 if (!opts.obj_dir)
46 opts.obj_dir = get_object_directory();
47
48 graph_name = get_commit_graph_filename(opts.obj_dir);
49 graph = load_commit_graph_one(graph_name);
50
51 if (!graph)
52 die("graph file %s does not exist", graph_name);
53 FREE_AND_NULL(graph_name);
54
55 printf("header: %08x %d %d %d %d\n",
56 ntohl(*(uint32_t*)graph->data),
57 *(unsigned char*)(graph->data + 4),
58 *(unsigned char*)(graph->data + 5),
59 *(unsigned char*)(graph->data + 6),
60 *(unsigned char*)(graph->data + 7));
61 printf("num_commits: %u\n", graph->num_commits);
62 printf("chunks:");
63
64 if (graph->chunk_oid_fanout)
65 printf(" oid_fanout");
66 if (graph->chunk_oid_lookup)
67 printf(" oid_lookup");
68 if (graph->chunk_commit_data)
69 printf(" commit_metadata");
70 if (graph->chunk_large_edges)
71 printf(" large_edges");
72 printf("\n");
73
74 return 0;
75}
76
f237c8b6
DS
77static int graph_write(int argc, const char **argv)
78{
79 static struct option builtin_commit_graph_write_options[] = {
80 OPT_STRING(0, "object-dir", &opts.obj_dir,
81 N_("dir"),
82 N_("The object directory to store the graph")),
83 OPT_END(),
84 };
85
86 argc = parse_options(argc, argv, NULL,
87 builtin_commit_graph_write_options,
88 builtin_commit_graph_write_usage, 0);
89
90 if (!opts.obj_dir)
91 opts.obj_dir = get_object_directory();
92
93 write_commit_graph(opts.obj_dir);
94 return 0;
95}
4ce58ee3
DS
96
97int cmd_commit_graph(int argc, const char **argv, const char *prefix)
98{
99 static struct option builtin_commit_graph_options[] = {
100 OPT_STRING(0, "object-dir", &opts.obj_dir,
101 N_("dir"),
102 N_("The object directory to store the graph")),
103 OPT_END(),
104 };
105
106 if (argc == 2 && !strcmp(argv[1], "-h"))
107 usage_with_options(builtin_commit_graph_usage,
108 builtin_commit_graph_options);
109
110 git_config(git_default_config, NULL);
111 argc = parse_options(argc, argv, prefix,
112 builtin_commit_graph_options,
113 builtin_commit_graph_usage,
114 PARSE_OPT_STOP_AT_NON_OPTION);
115
f237c8b6 116 if (argc > 0) {
2a2e32bd
DS
117 if (!strcmp(argv[0], "read"))
118 return graph_read(argc, argv);
f237c8b6
DS
119 if (!strcmp(argv[0], "write"))
120 return graph_write(argc, argv);
121 }
122
4ce58ee3
DS
123 usage_with_options(builtin_commit_graph_usage,
124 builtin_commit_graph_options);
125}