]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-graph.c
commit-graph: create options for split files
[thirdparty/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"
283e68c7 6#include "repository.h"
f237c8b6 7#include "commit-graph.h"
4ce58ee3
DS
8
9static char const * const builtin_commit_graph_usage[] = {
10 N_("git commit-graph [--object-dir <objdir>]"),
2a2e32bd 11 N_("git commit-graph read [--object-dir <objdir>]"),
283e68c7 12 N_("git commit-graph verify [--object-dir <objdir>]"),
c2bc6e6a 13 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
f237c8b6
DS
14 NULL
15};
16
283e68c7
DS
17static const char * const builtin_commit_graph_verify_usage[] = {
18 N_("git commit-graph verify [--object-dir <objdir>]"),
19 NULL
20};
21
2a2e32bd
DS
22static const char * const builtin_commit_graph_read_usage[] = {
23 N_("git commit-graph read [--object-dir <objdir>]"),
24 NULL
25};
26
f237c8b6 27static const char * const builtin_commit_graph_write_usage[] = {
c2bc6e6a 28 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
4ce58ee3
DS
29 NULL
30};
31
32static struct opts_commit_graph {
33 const char *obj_dir;
59fb8770 34 int reachable;
049d51a2 35 int stdin_packs;
3d5df01b 36 int stdin_commits;
7547b95b 37 int append;
135a7123 38 int split;
4ce58ee3
DS
39} opts;
40
283e68c7
DS
41static int graph_verify(int argc, const char **argv)
42{
43 struct commit_graph *graph = NULL;
44 char *graph_name;
61df89c8
ÆAB
45 int open_ok;
46 int fd;
47 struct stat st;
283e68c7
DS
48
49 static struct option builtin_commit_graph_verify_options[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
51 N_("dir"),
52 N_("The object directory to store the graph")),
53 OPT_END(),
54 };
55
56 argc = parse_options(argc, argv, NULL,
57 builtin_commit_graph_verify_options,
58 builtin_commit_graph_verify_usage, 0);
59
60 if (!opts.obj_dir)
61 opts.obj_dir = get_object_directory();
62
63 graph_name = get_commit_graph_filename(opts.obj_dir);
61df89c8 64 open_ok = open_commit_graph(graph_name, &fd, &st);
7b8ce9c6 65 if (!open_ok && errno == ENOENT)
61df89c8 66 return 0;
7b8ce9c6
ÆAB
67 if (!open_ok)
68 die_errno(_("Could not open commit-graph '%s'"), graph_name);
67a530fa 69 graph = load_commit_graph_one_fd_st(fd, &st);
283e68c7
DS
70 FREE_AND_NULL(graph_name);
71
72 if (!graph)
61df89c8 73 return 1;
283e68c7 74
0bfb48e6 75 UNLEAK(graph);
283e68c7
DS
76 return verify_commit_graph(the_repository, graph);
77}
78
2a2e32bd
DS
79static int graph_read(int argc, const char **argv)
80{
81 struct commit_graph *graph = NULL;
82 char *graph_name;
61df89c8
ÆAB
83 int open_ok;
84 int fd;
85 struct stat st;
2a2e32bd
DS
86
87 static struct option builtin_commit_graph_read_options[] = {
88 OPT_STRING(0, "object-dir", &opts.obj_dir,
89 N_("dir"),
90 N_("The object directory to store the graph")),
91 OPT_END(),
92 };
93
94 argc = parse_options(argc, argv, NULL,
95 builtin_commit_graph_read_options,
96 builtin_commit_graph_read_usage, 0);
97
98 if (!opts.obj_dir)
99 opts.obj_dir = get_object_directory();
100
101 graph_name = get_commit_graph_filename(opts.obj_dir);
2a2e32bd 102
61df89c8
ÆAB
103 open_ok = open_commit_graph(graph_name, &fd, &st);
104 if (!open_ok)
105 die_errno(_("Could not open commit-graph '%s'"), graph_name);
106
67a530fa 107 graph = load_commit_graph_one_fd_st(fd, &st);
0bfb48e6 108 if (!graph)
61df89c8 109 return 1;
883e5c7f 110
2a2e32bd
DS
111 FREE_AND_NULL(graph_name);
112
113 printf("header: %08x %d %d %d %d\n",
114 ntohl(*(uint32_t*)graph->data),
115 *(unsigned char*)(graph->data + 4),
116 *(unsigned char*)(graph->data + 5),
117 *(unsigned char*)(graph->data + 6),
118 *(unsigned char*)(graph->data + 7));
119 printf("num_commits: %u\n", graph->num_commits);
120 printf("chunks:");
121
122 if (graph->chunk_oid_fanout)
123 printf(" oid_fanout");
124 if (graph->chunk_oid_lookup)
125 printf(" oid_lookup");
126 if (graph->chunk_commit_data)
127 printf(" commit_metadata");
5af7417b
SG
128 if (graph->chunk_extra_edges)
129 printf(" extra_edges");
2a2e32bd
DS
130 printf("\n");
131
0bfb48e6 132 UNLEAK(graph);
c3756d5b 133
2a2e32bd
DS
134 return 0;
135}
136
d6538246 137extern int read_replace_refs;
c2bc6e6a 138static struct split_commit_graph_opts split_opts;
d6538246 139
f237c8b6
DS
140static int graph_write(int argc, const char **argv)
141{
d88b14b3
DS
142 struct string_list *pack_indexes = NULL;
143 struct string_list *commit_hex = NULL;
144 struct string_list lines;
e103f727 145 int result = 0;
5af80394 146 unsigned int flags = COMMIT_GRAPH_PROGRESS;
049d51a2 147
f237c8b6
DS
148 static struct option builtin_commit_graph_write_options[] = {
149 OPT_STRING(0, "object-dir", &opts.obj_dir,
150 N_("dir"),
151 N_("The object directory to store the graph")),
59fb8770
DS
152 OPT_BOOL(0, "reachable", &opts.reachable,
153 N_("start walk at all refs")),
049d51a2
DS
154 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
155 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
156 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
157 N_("start walk at commits listed by stdin")),
7547b95b
DS
158 OPT_BOOL(0, "append", &opts.append,
159 N_("include all commits already in the commit-graph file")),
135a7123
DS
160 OPT_BOOL(0, "split", &opts.split,
161 N_("allow writing an incremental commit-graph file")),
c2bc6e6a
DS
162 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
163 N_("maximum number of commits in a non-base split commit-graph")),
164 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
165 N_("maximum ratio between two levels of a split commit-graph")),
166 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
167 N_("maximum number of commits in a non-base split commit-graph")),
f237c8b6
DS
168 OPT_END(),
169 };
170
c2bc6e6a
DS
171 split_opts.size_multiple = 2;
172 split_opts.max_commits = 0;
173 split_opts.expire_time = 0;
174
f237c8b6
DS
175 argc = parse_options(argc, argv, NULL,
176 builtin_commit_graph_write_options,
177 builtin_commit_graph_write_usage, 0);
178
59fb8770
DS
179 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
180 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
f237c8b6
DS
181 if (!opts.obj_dir)
182 opts.obj_dir = get_object_directory();
5af80394
DS
183 if (opts.append)
184 flags |= COMMIT_GRAPH_APPEND;
135a7123
DS
185 if (opts.split)
186 flags |= COMMIT_GRAPH_SPLIT;
f237c8b6 187
d6538246
DS
188 read_replace_refs = 0;
189
c2bc6e6a
DS
190 if (opts.reachable) {
191 if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts))
192 return 1;
193 return 0;
194 }
59fb8770 195
d88b14b3 196 string_list_init(&lines, 0);
3d5df01b 197 if (opts.stdin_packs || opts.stdin_commits) {
049d51a2 198 struct strbuf buf = STRBUF_INIT;
d88b14b3
DS
199
200 while (strbuf_getline(&buf, stdin) != EOF)
201 string_list_append(&lines, strbuf_detach(&buf, NULL));
202
203 if (opts.stdin_packs)
204 pack_indexes = &lines;
205 if (opts.stdin_commits)
206 commit_hex = &lines;
0bfb48e6
207
208 UNLEAK(buf);
049d51a2
DS
209 }
210
e103f727
DS
211 if (write_commit_graph(opts.obj_dir,
212 pack_indexes,
213 commit_hex,
c2bc6e6a
DS
214 flags,
215 &split_opts))
e103f727 216 result = 1;
049d51a2 217
0bfb48e6 218 UNLEAK(lines);
e103f727 219 return result;
f237c8b6 220}
4ce58ee3
DS
221
222int cmd_commit_graph(int argc, const char **argv, const char *prefix)
223{
224 static struct option builtin_commit_graph_options[] = {
225 OPT_STRING(0, "object-dir", &opts.obj_dir,
226 N_("dir"),
227 N_("The object directory to store the graph")),
228 OPT_END(),
229 };
230
231 if (argc == 2 && !strcmp(argv[1], "-h"))
232 usage_with_options(builtin_commit_graph_usage,
233 builtin_commit_graph_options);
234
235 git_config(git_default_config, NULL);
236 argc = parse_options(argc, argv, prefix,
237 builtin_commit_graph_options,
238 builtin_commit_graph_usage,
239 PARSE_OPT_STOP_AT_NON_OPTION);
240
f237c8b6 241 if (argc > 0) {
2a2e32bd
DS
242 if (!strcmp(argv[0], "read"))
243 return graph_read(argc, argv);
283e68c7
DS
244 if (!strcmp(argv[0], "verify"))
245 return graph_verify(argc, argv);
f237c8b6
DS
246 if (!strcmp(argv[0], "write"))
247 return graph_write(argc, argv);
248 }
249
4ce58ee3
DS
250 usage_with_options(builtin_commit_graph_usage,
251 builtin_commit_graph_options);
252}