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