Merge branch 'sh/mergetool-hideresolved'
[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 static char const * const builtin_commit_graph_usage[] = {
13         N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
14         N_("git commit-graph write [--object-dir <objdir>] [--append] "
15            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
16            "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
17            "<split options>"),
18         NULL
19 };
20
21 static const char * const builtin_commit_graph_verify_usage[] = {
22         N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
23         NULL
24 };
25
26 static const char * const builtin_commit_graph_write_usage[] = {
27         N_("git commit-graph write [--object-dir <objdir>] [--append] "
28            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
29            "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
30            "<split options>"),
31         NULL
32 };
33
34 static struct opts_commit_graph {
35         const char *obj_dir;
36         int reachable;
37         int stdin_packs;
38         int stdin_commits;
39         int append;
40         int split;
41         int shallow;
42         int progress;
43         int enable_changed_paths;
44 } opts;
45
46 static 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);
51         struct strbuf odb_path_real = STRBUF_INIT;
52
53         prepare_alt_odb(r);
54         for (odb = r->objects->odb; odb; odb = odb->next) {
55                 strbuf_realpath(&odb_path_real, odb->path, 1);
56                 if (!strcmp(obj_dir_real, odb_path_real.buf))
57                         break;
58         }
59
60         free(obj_dir_real);
61         strbuf_release(&odb_path_real);
62
63         if (!odb)
64                 die(_("could not find object directory matching %s"), obj_dir);
65         return odb;
66 }
67
68 static int graph_verify(int argc, const char **argv)
69 {
70         struct commit_graph *graph = NULL;
71         struct object_directory *odb = NULL;
72         char *graph_name;
73         int open_ok;
74         int fd;
75         struct stat st;
76         int flags = 0;
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")),
82                 OPT_BOOL(0, "shallow", &opts.shallow,
83                          N_("if the commit-graph is split, only verify the tip file")),
84                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
85                 OPT_END(),
86         };
87
88         trace2_cmd_mode("verify");
89
90         opts.progress = isatty(2);
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();
97         if (opts.shallow)
98                 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
99         if (opts.progress)
100                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
101
102         odb = find_odb(the_repository, opts.obj_dir);
103         graph_name = get_commit_graph_filename(odb);
104         open_ok = open_commit_graph(graph_name, &fd, &st);
105         if (!open_ok && errno != ENOENT)
106                 die_errno(_("Could not open commit-graph '%s'"), graph_name);
107
108         FREE_AND_NULL(graph_name);
109
110         if (open_ok)
111                 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
112         else
113                 graph = read_commit_graph_one(the_repository, odb);
114
115         /* Return failure if open_ok predicted success */
116         if (!graph)
117                 return !!open_ok;
118
119         UNLEAK(graph);
120         return verify_commit_graph(the_repository, graph, flags);
121 }
122
123 extern int read_replace_refs;
124 static struct commit_graph_opts write_opts;
125
126 static int write_option_parse_split(const struct option *opt, const char *arg,
127                                     int unset)
128 {
129         enum commit_graph_split_flags *flags = opt->value;
130
131         BUG_ON_OPT_NEG(unset);
132
133         opts.split = 1;
134         if (!arg)
135                 return 0;
136
137         if (!strcmp(arg, "no-merge"))
138                 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
139         else if (!strcmp(arg, "replace"))
140                 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
141         else
142                 die(_("unrecognized --split argument, %s"), arg);
143
144         return 0;
145 }
146
147 static int read_one_commit(struct oidset *commits, struct progress *progress,
148                            const char *hash)
149 {
150         struct object *result;
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
157         result = deref_tag(the_repository, parse_object(the_repository, &oid),
158                            NULL, 0);
159         if (!result)
160                 return error(_("invalid object: %s"), hash);
161         else if (object_as_type(result, OBJ_COMMIT, 1))
162                 oidset_insert(commits, &result->oid);
163
164         display_progress(progress, oidset_size(commits));
165
166         return 0;
167 }
168
169 static 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
186 static 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
198 static int graph_write(int argc, const char **argv)
199 {
200         struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
201         struct strbuf buf = STRBUF_INIT;
202         struct oidset commits = OIDSET_INIT;
203         struct object_directory *odb = NULL;
204         int result = 0;
205         enum commit_graph_write_flags flags = 0;
206         struct progress *progress = NULL;
207
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")),
212                 OPT_BOOL(0, "reachable", &opts.reachable,
213                         N_("start walk at all refs")),
214                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
215                         N_("scan pack-indexes listed by stdin for commits")),
216                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
217                         N_("start walk at commits listed by stdin")),
218                 OPT_BOOL(0, "append", &opts.append,
219                         N_("include all commits already in the commit-graph file")),
220                 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
221                         N_("enable computation for changed paths")),
222                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
223                 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
224                         N_("allow writing an incremental commit-graph file"),
225                         PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
226                         write_option_parse_split),
227                 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
228                         N_("maximum number of commits in a non-base split commit-graph")),
229                 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
230                         N_("maximum ratio between two levels of a split commit-graph")),
231                 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
232                         N_("only expire files older than a given date-time")),
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),
236                 OPT_END(),
237         };
238
239         opts.progress = isatty(2);
240         opts.enable_changed_paths = -1;
241         write_opts.size_multiple = 2;
242         write_opts.max_commits = 0;
243         write_opts.expire_time = 0;
244         write_opts.max_new_filters = -1;
245
246         trace2_cmd_mode("write");
247
248         git_config(git_commit_graph_write_config, &opts);
249
250         argc = parse_options(argc, argv, NULL,
251                              builtin_commit_graph_write_options,
252                              builtin_commit_graph_write_usage, 0);
253
254         if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
255                 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
256         if (!opts.obj_dir)
257                 opts.obj_dir = get_object_directory();
258         if (opts.append)
259                 flags |= COMMIT_GRAPH_WRITE_APPEND;
260         if (opts.split)
261                 flags |= COMMIT_GRAPH_WRITE_SPLIT;
262         if (opts.progress)
263                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
264         if (!opts.enable_changed_paths)
265                 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
266         if (opts.enable_changed_paths == 1 ||
267             git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
268                 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
269
270         read_replace_refs = 0;
271         odb = find_odb(the_repository, opts.obj_dir);
272
273         if (opts.reachable) {
274                 if (write_commit_graph_reachable(odb, flags, &write_opts))
275                         return 1;
276                 return 0;
277         }
278
279         if (opts.stdin_packs) {
280                 while (strbuf_getline(&buf, stdin) != EOF)
281                         string_list_append(&pack_indexes,
282                                            strbuf_detach(&buf, NULL));
283         } else if (opts.stdin_commits) {
284                 oidset_init(&commits, 0);
285                 if (opts.progress)
286                         progress = start_delayed_progress(
287                                 _("Collecting commits from input"), 0);
288
289                 while (strbuf_getline(&buf, stdin) != EOF) {
290                         if (read_one_commit(&commits, progress, buf.buf)) {
291                                 result = 1;
292                                 goto cleanup;
293                         }
294                 }
295
296                 stop_progress(&progress);
297         }
298
299         if (write_commit_graph(odb,
300                                opts.stdin_packs ? &pack_indexes : NULL,
301                                opts.stdin_commits ? &commits : NULL,
302                                flags,
303                                &write_opts))
304                 result = 1;
305
306 cleanup:
307         string_list_clear(&pack_indexes, 0);
308         strbuf_release(&buf);
309         return result;
310 }
311
312 int 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
331         save_commit_buffer = 0;
332
333         if (argc > 0) {
334                 if (!strcmp(argv[0], "verify"))
335                         return graph_verify(argc, argv);
336                 if (!strcmp(argv[0], "write"))
337                         return graph_write(argc, argv);
338         }
339
340         usage_with_options(builtin_commit_graph_usage,
341                            builtin_commit_graph_options);
342 }