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