]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-graph.c
commit-graph: remove redundant handling of -h
[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"
5b6653e5 9#include "progress.h"
2f00c355 10#include "tag.h"
4ce58ee3 11
8757b35d
ÆAB
12#define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
13 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]")
14
15#define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
16 N_("git commit-graph write [--object-dir <objdir>] [--append] " \
17 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " \
18 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] " \
19 "<split options>")
20
21static const char * builtin_commit_graph_verify_usage[] = {
22 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
f237c8b6
DS
23 NULL
24};
25
8757b35d
ÆAB
26static const char * builtin_commit_graph_write_usage[] = {
27 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
283e68c7
DS
28 NULL
29};
30
8757b35d
ÆAB
31static char const * const builtin_commit_graph_usage[] = {
32 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
34 NULL,
4ce58ee3
DS
35};
36
37static struct opts_commit_graph {
38 const char *obj_dir;
59fb8770 39 int reachable;
049d51a2 40 int stdin_packs;
3d5df01b 41 int stdin_commits;
7547b95b 42 int append;
135a7123 43 int split;
3da4b609 44 int shallow;
73716122 45 int progress;
d38e07b8 46 int enable_changed_paths;
4ce58ee3
DS
47} opts;
48
0bd52e27
TB
49static struct object_directory *find_odb(struct repository *r,
50 const char *obj_dir)
51{
52 struct object_directory *odb;
53 char *obj_dir_real = real_pathdup(obj_dir, 1);
3d7747e3 54 struct strbuf odb_path_real = STRBUF_INIT;
0bd52e27
TB
55
56 prepare_alt_odb(r);
57 for (odb = r->objects->odb; odb; odb = odb->next) {
3d7747e3
AM
58 strbuf_realpath(&odb_path_real, odb->path, 1);
59 if (!strcmp(obj_dir_real, odb_path_real.buf))
0bd52e27
TB
60 break;
61 }
62
63 free(obj_dir_real);
3d7747e3 64 strbuf_release(&odb_path_real);
0bd52e27
TB
65
66 if (!odb)
67 die(_("could not find object directory matching %s"), obj_dir);
68 return odb;
69}
70
283e68c7
DS
71static int graph_verify(int argc, const char **argv)
72{
73 struct commit_graph *graph = NULL;
0bd52e27 74 struct object_directory *odb = NULL;
283e68c7 75 char *graph_name;
61df89c8
ÆAB
76 int open_ok;
77 int fd;
78 struct stat st;
3da4b609 79 int flags = 0;
283e68c7
DS
80
81 static struct option builtin_commit_graph_verify_options[] = {
82 OPT_STRING(0, "object-dir", &opts.obj_dir,
83 N_("dir"),
e73fe3dd 84 N_("the object directory to store the graph")),
3da4b609
DS
85 OPT_BOOL(0, "shallow", &opts.shallow,
86 N_("if the commit-graph is split, only verify the tip file")),
73716122 87 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
283e68c7
DS
88 OPT_END(),
89 };
90
0bd7f578
GS
91 trace2_cmd_mode("verify");
92
73716122 93 opts.progress = isatty(2);
283e68c7
DS
94 argc = parse_options(argc, argv, NULL,
95 builtin_commit_graph_verify_options,
96 builtin_commit_graph_verify_usage, 0);
97
98 if (!opts.obj_dir)
99 opts.obj_dir = get_object_directory();
3da4b609
DS
100 if (opts.shallow)
101 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
73716122
GS
102 if (opts.progress)
103 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
283e68c7 104
0bd52e27 105 odb = find_odb(the_repository, opts.obj_dir);
ad2dd5bb 106 graph_name = get_commit_graph_filename(odb);
61df89c8 107 open_ok = open_commit_graph(graph_name, &fd, &st);
3da4b609 108 if (!open_ok && errno != ENOENT)
7b8ce9c6 109 die_errno(_("Could not open commit-graph '%s'"), graph_name);
3da4b609 110
283e68c7
DS
111 FREE_AND_NULL(graph_name);
112
3da4b609 113 if (open_ok)
ab14d067 114 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
0bd52e27 115 else
13c24992 116 graph = read_commit_graph_one(the_repository, odb);
3da4b609
DS
117
118 /* Return failure if open_ok predicted success */
283e68c7 119 if (!graph)
3da4b609 120 return !!open_ok;
283e68c7 121
0bfb48e6 122 UNLEAK(graph);
3da4b609 123 return verify_commit_graph(the_repository, graph, flags);
283e68c7
DS
124}
125
d6538246 126extern int read_replace_refs;
98bb7961 127static struct commit_graph_opts write_opts;
d6538246 128
4f027355
TB
129static int write_option_parse_split(const struct option *opt, const char *arg,
130 int unset)
131{
fdbde82f
TB
132 enum commit_graph_split_flags *flags = opt->value;
133
8d2aa8df
JK
134 BUG_ON_OPT_NEG(unset);
135
4f027355
TB
136 opts.split = 1;
137 if (!arg)
138 return 0;
139
fdbde82f
TB
140 if (!strcmp(arg, "no-merge"))
141 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
8a6ac287
TB
142 else if (!strcmp(arg, "replace"))
143 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
fdbde82f
TB
144 else
145 die(_("unrecognized --split argument, %s"), arg);
4f027355
TB
146
147 return 0;
148}
149
5b6653e5
TB
150static int read_one_commit(struct oidset *commits, struct progress *progress,
151 const char *hash)
fa8953cb 152{
2f00c355 153 struct object *result;
fa8953cb
TB
154 struct object_id oid;
155 const char *end;
156
157 if (parse_oid_hex(hash, &oid, &end))
158 return error(_("unexpected non-hex object ID: %s"), hash);
159
2f00c355
TB
160 result = deref_tag(the_repository, parse_object(the_repository, &oid),
161 NULL, 0);
162 if (!result)
163 return error(_("invalid object: %s"), hash);
6da43d93 164 else if (object_as_type(result, OBJ_COMMIT, 1))
2f00c355 165 oidset_insert(commits, &result->oid);
5b6653e5
TB
166
167 display_progress(progress, oidset_size(commits));
168
fa8953cb
TB
169 return 0;
170}
171
809e0327
TB
172static int write_option_max_new_filters(const struct option *opt,
173 const char *arg,
174 int unset)
175{
176 int *to = opt->value;
177 if (unset)
178 *to = -1;
179 else {
180 const char *s;
181 *to = strtol(arg, (char **)&s, 10);
182 if (*s)
183 return error(_("%s expects a numerical value"),
184 optname(opt, opt->flags));
185 }
186 return 0;
187}
188
d356d5de
TB
189static int git_commit_graph_write_config(const char *var, const char *value,
190 void *cb)
191{
192 if (!strcmp(var, "commitgraph.maxnewfilters"))
193 write_opts.max_new_filters = git_config_int(var, value);
194 /*
195 * No need to fall-back to 'git_default_config', since this was already
196 * called in 'cmd_commit_graph()'.
197 */
198 return 0;
199}
200
f237c8b6
DS
201static int graph_write(int argc, const char **argv)
202{
fa8953cb
TB
203 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
204 struct strbuf buf = STRBUF_INIT;
6830c360 205 struct oidset commits = OIDSET_INIT;
0bd52e27 206 struct object_directory *odb = NULL;
e103f727 207 int result = 0;
73716122 208 enum commit_graph_write_flags flags = 0;
5b6653e5 209 struct progress *progress = NULL;
049d51a2 210
f237c8b6
DS
211 static struct option builtin_commit_graph_write_options[] = {
212 OPT_STRING(0, "object-dir", &opts.obj_dir,
213 N_("dir"),
e73fe3dd 214 N_("the object directory to store the graph")),
59fb8770
DS
215 OPT_BOOL(0, "reachable", &opts.reachable,
216 N_("start walk at all refs")),
049d51a2
DS
217 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
218 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
219 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
220 N_("start walk at commits listed by stdin")),
7547b95b
DS
221 OPT_BOOL(0, "append", &opts.append,
222 N_("include all commits already in the commit-graph file")),
d38e07b8
GS
223 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
224 N_("enable computation for changed paths")),
73716122 225 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
98bb7961 226 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
4f027355
TB
227 N_("allow writing an incremental commit-graph file"),
228 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
229 write_option_parse_split),
98bb7961 230 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
c2bc6e6a 231 N_("maximum number of commits in a non-base split commit-graph")),
98bb7961 232 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
c2bc6e6a 233 N_("maximum ratio between two levels of a split commit-graph")),
98bb7961 234 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
b09b785c 235 N_("only expire files older than a given date-time")),
809e0327
TB
236 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
237 NULL, N_("maximum number of changed-path Bloom filters to compute"),
238 0, write_option_max_new_filters),
f237c8b6
DS
239 OPT_END(),
240 };
241
73716122 242 opts.progress = isatty(2);
0087a87b 243 opts.enable_changed_paths = -1;
98bb7961
TB
244 write_opts.size_multiple = 2;
245 write_opts.max_commits = 0;
246 write_opts.expire_time = 0;
809e0327 247 write_opts.max_new_filters = -1;
c2bc6e6a 248
0bd7f578
GS
249 trace2_cmd_mode("write");
250
d356d5de
TB
251 git_config(git_commit_graph_write_config, &opts);
252
f237c8b6
DS
253 argc = parse_options(argc, argv, NULL,
254 builtin_commit_graph_write_options,
255 builtin_commit_graph_write_usage, 0);
256
59fb8770
DS
257 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
258 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
f237c8b6
DS
259 if (!opts.obj_dir)
260 opts.obj_dir = get_object_directory();
5af80394 261 if (opts.append)
39d88318 262 flags |= COMMIT_GRAPH_WRITE_APPEND;
135a7123 263 if (opts.split)
39d88318 264 flags |= COMMIT_GRAPH_WRITE_SPLIT;
73716122
GS
265 if (opts.progress)
266 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
0087a87b
DS
267 if (!opts.enable_changed_paths)
268 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
269 if (opts.enable_changed_paths == 1 ||
d5b873c8 270 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
d38e07b8 271 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
f237c8b6 272
d6538246 273 read_replace_refs = 0;
0bd52e27 274 odb = find_odb(the_repository, opts.obj_dir);
d6538246 275
c2bc6e6a 276 if (opts.reachable) {
98bb7961 277 if (write_commit_graph_reachable(odb, flags, &write_opts))
c2bc6e6a
DS
278 return 1;
279 return 0;
280 }
59fb8770 281
fa8953cb 282 if (opts.stdin_packs) {
d88b14b3 283 while (strbuf_getline(&buf, stdin) != EOF)
fa8953cb
TB
284 string_list_append(&pack_indexes,
285 strbuf_detach(&buf, NULL));
286 } else if (opts.stdin_commits) {
287 oidset_init(&commits, 0);
5b6653e5
TB
288 if (opts.progress)
289 progress = start_delayed_progress(
290 _("Collecting commits from input"), 0);
fa8953cb
TB
291
292 while (strbuf_getline(&buf, stdin) != EOF) {
5b6653e5 293 if (read_one_commit(&commits, progress, buf.buf)) {
fa8953cb
TB
294 result = 1;
295 goto cleanup;
6830c360 296 }
7c5c9b9c 297 }
5b6653e5 298
862aead2 299 stop_progress(&progress);
049d51a2
DS
300 }
301
0bd52e27 302 if (write_commit_graph(odb,
fa8953cb 303 opts.stdin_packs ? &pack_indexes : NULL,
6830c360 304 opts.stdin_commits ? &commits : NULL,
c2bc6e6a 305 flags,
98bb7961 306 &write_opts))
e103f727 307 result = 1;
049d51a2 308
fa8953cb
TB
309cleanup:
310 string_list_clear(&pack_indexes, 0);
311 strbuf_release(&buf);
e103f727 312 return result;
f237c8b6 313}
4ce58ee3
DS
314
315int cmd_commit_graph(int argc, const char **argv, const char *prefix)
316{
317 static struct option builtin_commit_graph_options[] = {
318 OPT_STRING(0, "object-dir", &opts.obj_dir,
319 N_("dir"),
e73fe3dd 320 N_("the object directory to store the graph")),
4ce58ee3
DS
321 OPT_END(),
322 };
323
4ce58ee3
DS
324 git_config(git_default_config, NULL);
325 argc = parse_options(argc, argv, prefix,
326 builtin_commit_graph_options,
327 builtin_commit_graph_usage,
328 PARSE_OPT_STOP_AT_NON_OPTION);
329
dd2e50a8
JK
330 save_commit_buffer = 0;
331
f237c8b6 332 if (argc > 0) {
283e68c7
DS
333 if (!strcmp(argv[0], "verify"))
334 return graph_verify(argc, argv);
f237c8b6
DS
335 if (!strcmp(argv[0], "write"))
336 return graph_write(argc, argv);
337 }
338
4ce58ee3
DS
339 usage_with_options(builtin_commit_graph_usage,
340 builtin_commit_graph_options);
341}