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