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