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