t5318-commit-graph.sh: use core.commitGraph
[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>]"),
7547b95b 11 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
f237c8b6
DS
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 20static const char * const builtin_commit_graph_write_usage[] = {
7547b95b 21 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
4ce58ee3
DS
22 NULL
23};
24
25static struct opts_commit_graph {
26 const char *obj_dir;
049d51a2 27 int stdin_packs;
3d5df01b 28 int stdin_commits;
7547b95b 29 int append;
4ce58ee3
DS
30} opts;
31
2a2e32bd
DS
32static int graph_read(int argc, const char **argv)
33{
34 struct commit_graph *graph = NULL;
35 char *graph_name;
36
37 static struct option builtin_commit_graph_read_options[] = {
38 OPT_STRING(0, "object-dir", &opts.obj_dir,
39 N_("dir"),
40 N_("The object directory to store the graph")),
41 OPT_END(),
42 };
43
44 argc = parse_options(argc, argv, NULL,
45 builtin_commit_graph_read_options,
46 builtin_commit_graph_read_usage, 0);
47
48 if (!opts.obj_dir)
49 opts.obj_dir = get_object_directory();
50
51 graph_name = get_commit_graph_filename(opts.obj_dir);
52 graph = load_commit_graph_one(graph_name);
53
54 if (!graph)
55 die("graph file %s does not exist", graph_name);
56 FREE_AND_NULL(graph_name);
57
58 printf("header: %08x %d %d %d %d\n",
59 ntohl(*(uint32_t*)graph->data),
60 *(unsigned char*)(graph->data + 4),
61 *(unsigned char*)(graph->data + 5),
62 *(unsigned char*)(graph->data + 6),
63 *(unsigned char*)(graph->data + 7));
64 printf("num_commits: %u\n", graph->num_commits);
65 printf("chunks:");
66
67 if (graph->chunk_oid_fanout)
68 printf(" oid_fanout");
69 if (graph->chunk_oid_lookup)
70 printf(" oid_lookup");
71 if (graph->chunk_commit_data)
72 printf(" commit_metadata");
73 if (graph->chunk_large_edges)
74 printf(" large_edges");
75 printf("\n");
76
77 return 0;
78}
79
f237c8b6
DS
80static int graph_write(int argc, const char **argv)
81{
049d51a2
DS
82 const char **pack_indexes = NULL;
83 int packs_nr = 0;
3d5df01b
DS
84 const char **commit_hex = NULL;
85 int commits_nr = 0;
049d51a2
DS
86 const char **lines = NULL;
87 int lines_nr = 0;
88 int lines_alloc = 0;
89
f237c8b6
DS
90 static struct option builtin_commit_graph_write_options[] = {
91 OPT_STRING(0, "object-dir", &opts.obj_dir,
92 N_("dir"),
93 N_("The object directory to store the graph")),
049d51a2
DS
94 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
95 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
96 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
97 N_("start walk at commits listed by stdin")),
7547b95b
DS
98 OPT_BOOL(0, "append", &opts.append,
99 N_("include all commits already in the commit-graph file")),
f237c8b6
DS
100 OPT_END(),
101 };
102
103 argc = parse_options(argc, argv, NULL,
104 builtin_commit_graph_write_options,
105 builtin_commit_graph_write_usage, 0);
106
3d5df01b
DS
107 if (opts.stdin_packs && opts.stdin_commits)
108 die(_("cannot use both --stdin-commits and --stdin-packs"));
f237c8b6
DS
109 if (!opts.obj_dir)
110 opts.obj_dir = get_object_directory();
111
3d5df01b 112 if (opts.stdin_packs || opts.stdin_commits) {
049d51a2
DS
113 struct strbuf buf = STRBUF_INIT;
114 lines_nr = 0;
115 lines_alloc = 128;
116 ALLOC_ARRAY(lines, lines_alloc);
117
118 while (strbuf_getline(&buf, stdin) != EOF) {
119 ALLOC_GROW(lines, lines_nr + 1, lines_alloc);
120 lines[lines_nr++] = strbuf_detach(&buf, NULL);
121 }
122
3d5df01b
DS
123 if (opts.stdin_packs) {
124 pack_indexes = lines;
125 packs_nr = lines_nr;
126 }
127 if (opts.stdin_commits) {
128 commit_hex = lines;
129 commits_nr = lines_nr;
130 }
049d51a2
DS
131 }
132
133 write_commit_graph(opts.obj_dir,
134 pack_indexes,
3d5df01b
DS
135 packs_nr,
136 commit_hex,
7547b95b
DS
137 commits_nr,
138 opts.append);
049d51a2 139
f237c8b6
DS
140 return 0;
141}
4ce58ee3
DS
142
143int cmd_commit_graph(int argc, const char **argv, const char *prefix)
144{
145 static struct option builtin_commit_graph_options[] = {
146 OPT_STRING(0, "object-dir", &opts.obj_dir,
147 N_("dir"),
148 N_("The object directory to store the graph")),
149 OPT_END(),
150 };
151
152 if (argc == 2 && !strcmp(argv[1], "-h"))
153 usage_with_options(builtin_commit_graph_usage,
154 builtin_commit_graph_options);
155
156 git_config(git_default_config, NULL);
157 argc = parse_options(argc, argv, prefix,
158 builtin_commit_graph_options,
159 builtin_commit_graph_usage,
160 PARSE_OPT_STOP_AT_NON_OPTION);
161
f237c8b6 162 if (argc > 0) {
2a2e32bd
DS
163 if (!strcmp(argv[0], "read"))
164 return graph_read(argc, argv);
f237c8b6
DS
165 if (!strcmp(argv[0], "write"))
166 return graph_write(argc, argv);
167 }
168
4ce58ee3
DS
169 usage_with_options(builtin_commit_graph_usage,
170 builtin_commit_graph_options);
171}