builtin/commit-graph.c: introduce split strategy 'no-merge'
[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
10 static char const * const builtin_commit_graph_usage[] = {
11         N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
12         N_("git commit-graph write [--object-dir <objdir>] [--append] "
13            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
14            "[--[no-]progress] <split options>"),
15         NULL
16 };
17
18 static const char * const builtin_commit_graph_verify_usage[] = {
19         N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
20         NULL
21 };
22
23 static const char * const builtin_commit_graph_write_usage[] = {
24         N_("git commit-graph write [--object-dir <objdir>] [--append] "
25            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
26            "[--[no-]progress] <split options>"),
27         NULL
28 };
29
30 static struct opts_commit_graph {
31         const char *obj_dir;
32         int reachable;
33         int stdin_packs;
34         int stdin_commits;
35         int append;
36         int split;
37         int shallow;
38         int progress;
39 } opts;
40
41 static struct object_directory *find_odb(struct repository *r,
42                                          const char *obj_dir)
43 {
44         struct object_directory *odb;
45         char *obj_dir_real = real_pathdup(obj_dir, 1);
46
47         prepare_alt_odb(r);
48         for (odb = r->objects->odb; odb; odb = odb->next) {
49                 if (!strcmp(obj_dir_real, real_path(odb->path)))
50                         break;
51         }
52
53         free(obj_dir_real);
54
55         if (!odb)
56                 die(_("could not find object directory matching %s"), obj_dir);
57         return odb;
58 }
59
60 static int graph_verify(int argc, const char **argv)
61 {
62         struct commit_graph *graph = NULL;
63         struct object_directory *odb = NULL;
64         char *graph_name;
65         int open_ok;
66         int fd;
67         struct stat st;
68         int flags = 0;
69
70         static struct option builtin_commit_graph_verify_options[] = {
71                 OPT_STRING(0, "object-dir", &opts.obj_dir,
72                            N_("dir"),
73                            N_("The object directory to store the graph")),
74                 OPT_BOOL(0, "shallow", &opts.shallow,
75                          N_("if the commit-graph is split, only verify the tip file")),
76                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
77                 OPT_END(),
78         };
79
80         trace2_cmd_mode("verify");
81
82         opts.progress = isatty(2);
83         argc = parse_options(argc, argv, NULL,
84                              builtin_commit_graph_verify_options,
85                              builtin_commit_graph_verify_usage, 0);
86
87         if (!opts.obj_dir)
88                 opts.obj_dir = get_object_directory();
89         if (opts.shallow)
90                 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
91         if (opts.progress)
92                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
93
94         odb = find_odb(the_repository, opts.obj_dir);
95         graph_name = get_commit_graph_filename(odb);
96         open_ok = open_commit_graph(graph_name, &fd, &st);
97         if (!open_ok && errno != ENOENT)
98                 die_errno(_("Could not open commit-graph '%s'"), graph_name);
99
100         FREE_AND_NULL(graph_name);
101
102         if (open_ok)
103                 graph = load_commit_graph_one_fd_st(fd, &st, odb);
104         else
105                 graph = read_commit_graph_one(the_repository, odb);
106
107         /* Return failure if open_ok predicted success */
108         if (!graph)
109                 return !!open_ok;
110
111         UNLEAK(graph);
112         return verify_commit_graph(the_repository, graph, flags);
113 }
114
115 extern int read_replace_refs;
116 static struct split_commit_graph_opts split_opts;
117
118 static int write_option_parse_split(const struct option *opt, const char *arg,
119                                     int unset)
120 {
121         enum commit_graph_split_flags *flags = opt->value;
122
123         opts.split = 1;
124         if (!arg)
125                 return 0;
126
127         if (!strcmp(arg, "no-merge"))
128                 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
129         else
130                 die(_("unrecognized --split argument, %s"), arg);
131
132         return 0;
133 }
134
135 static int graph_write(int argc, const char **argv)
136 {
137         struct string_list *pack_indexes = NULL;
138         struct string_list *commit_hex = NULL;
139         struct object_directory *odb = NULL;
140         struct string_list lines;
141         int result = 0;
142         enum commit_graph_write_flags flags = 0;
143
144         static struct option builtin_commit_graph_write_options[] = {
145                 OPT_STRING(0, "object-dir", &opts.obj_dir,
146                         N_("dir"),
147                         N_("The object directory to store the graph")),
148                 OPT_BOOL(0, "reachable", &opts.reachable,
149                         N_("start walk at all refs")),
150                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
151                         N_("scan pack-indexes listed by stdin for commits")),
152                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
153                         N_("start walk at commits listed by stdin")),
154                 OPT_BOOL(0, "append", &opts.append,
155                         N_("include all commits already in the commit-graph file")),
156                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
157                 OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
158                         N_("allow writing an incremental commit-graph file"),
159                         PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
160                         write_option_parse_split),
161                 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
162                         N_("maximum number of commits in a non-base split commit-graph")),
163                 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
164                         N_("maximum ratio between two levels of a split commit-graph")),
165                 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
166                         N_("maximum number of commits in a non-base split commit-graph")),
167                 OPT_END(),
168         };
169
170         opts.progress = isatty(2);
171         split_opts.size_multiple = 2;
172         split_opts.max_commits = 0;
173         split_opts.expire_time = 0;
174
175         trace2_cmd_mode("write");
176
177         argc = parse_options(argc, argv, NULL,
178                              builtin_commit_graph_write_options,
179                              builtin_commit_graph_write_usage, 0);
180
181         if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
182                 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
183         if (!opts.obj_dir)
184                 opts.obj_dir = get_object_directory();
185         if (opts.append)
186                 flags |= COMMIT_GRAPH_WRITE_APPEND;
187         if (opts.split)
188                 flags |= COMMIT_GRAPH_WRITE_SPLIT;
189         if (opts.progress)
190                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
191
192         read_replace_refs = 0;
193         odb = find_odb(the_repository, opts.obj_dir);
194
195         if (opts.reachable) {
196                 if (write_commit_graph_reachable(odb, flags, &split_opts))
197                         return 1;
198                 return 0;
199         }
200
201         string_list_init(&lines, 0);
202         if (opts.stdin_packs || opts.stdin_commits) {
203                 struct strbuf buf = STRBUF_INIT;
204
205                 while (strbuf_getline(&buf, stdin) != EOF)
206                         string_list_append(&lines, strbuf_detach(&buf, NULL));
207
208                 if (opts.stdin_packs)
209                         pack_indexes = &lines;
210                 if (opts.stdin_commits) {
211                         commit_hex = &lines;
212                         flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
213                 }
214
215                 UNLEAK(buf);
216         }
217
218         if (write_commit_graph(odb,
219                                pack_indexes,
220                                commit_hex,
221                                flags,
222                                &split_opts))
223                 result = 1;
224
225         UNLEAK(lines);
226         return result;
227 }
228
229 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
230 {
231         static struct option builtin_commit_graph_options[] = {
232                 OPT_STRING(0, "object-dir", &opts.obj_dir,
233                         N_("dir"),
234                         N_("The object directory to store the graph")),
235                 OPT_END(),
236         };
237
238         if (argc == 2 && !strcmp(argv[1], "-h"))
239                 usage_with_options(builtin_commit_graph_usage,
240                                    builtin_commit_graph_options);
241
242         git_config(git_default_config, NULL);
243         argc = parse_options(argc, argv, prefix,
244                              builtin_commit_graph_options,
245                              builtin_commit_graph_usage,
246                              PARSE_OPT_STOP_AT_NON_OPTION);
247
248         save_commit_buffer = 0;
249
250         if (argc > 0) {
251                 if (!strcmp(argv[0], "verify"))
252                         return graph_verify(argc, argv);
253                 if (!strcmp(argv[0], "write"))
254                         return graph_write(argc, argv);
255         }
256
257         usage_with_options(builtin_commit_graph_usage,
258                            builtin_commit_graph_options);
259 }