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