commit-graph: persist existence of changed-paths
[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|--split] [--reachable|--stdin-packs|--stdin-commits] [--changed-paths] [--[no-]progress] <split options>"),
13         NULL
14 };
15
16 static const char * const builtin_commit_graph_verify_usage[] = {
17         N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
18         NULL
19 };
20
21 static const char * const builtin_commit_graph_write_usage[] = {
22         N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--changed-paths] [--[no-]progress] <split options>"),
23         NULL
24 };
25
26 static struct opts_commit_graph {
27         const char *obj_dir;
28         int reachable;
29         int stdin_packs;
30         int stdin_commits;
31         int append;
32         int split;
33         int shallow;
34         int progress;
35         int enable_changed_paths;
36 } opts;
37
38 static struct object_directory *find_odb(struct repository *r,
39                                          const char *obj_dir)
40 {
41         struct object_directory *odb;
42         char *obj_dir_real = real_pathdup(obj_dir, 1);
43         struct strbuf odb_path_real = STRBUF_INIT;
44
45         prepare_alt_odb(r);
46         for (odb = r->objects->odb; odb; odb = odb->next) {
47                 strbuf_realpath(&odb_path_real, odb->path, 1);
48                 if (!strcmp(obj_dir_real, odb_path_real.buf))
49                         break;
50         }
51
52         free(obj_dir_real);
53         strbuf_release(&odb_path_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 graph_write(int argc, const char **argv)
119 {
120         struct string_list *pack_indexes = NULL;
121         struct string_list *commit_hex = NULL;
122         struct object_directory *odb = NULL;
123         struct string_list lines;
124         int result = 0;
125         enum commit_graph_write_flags flags = 0;
126
127         static struct option builtin_commit_graph_write_options[] = {
128                 OPT_STRING(0, "object-dir", &opts.obj_dir,
129                         N_("dir"),
130                         N_("The object directory to store the graph")),
131                 OPT_BOOL(0, "reachable", &opts.reachable,
132                         N_("start walk at all refs")),
133                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
134                         N_("scan pack-indexes listed by stdin for commits")),
135                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
136                         N_("start walk at commits listed by stdin")),
137                 OPT_BOOL(0, "append", &opts.append,
138                         N_("include all commits already in the commit-graph file")),
139                 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
140                         N_("enable computation for changed paths")),
141                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
142                 OPT_BOOL(0, "split", &opts.split,
143                         N_("allow writing an incremental commit-graph file")),
144                 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
145                         N_("maximum number of commits in a non-base split commit-graph")),
146                 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
147                         N_("maximum ratio between two levels of a split commit-graph")),
148                 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
149                         N_("maximum number of commits in a non-base split commit-graph")),
150                 OPT_END(),
151         };
152
153         opts.progress = isatty(2);
154         opts.enable_changed_paths = -1;
155         split_opts.size_multiple = 2;
156         split_opts.max_commits = 0;
157         split_opts.expire_time = 0;
158
159         trace2_cmd_mode("write");
160
161         argc = parse_options(argc, argv, NULL,
162                              builtin_commit_graph_write_options,
163                              builtin_commit_graph_write_usage, 0);
164
165         if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
166                 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
167         if (!opts.obj_dir)
168                 opts.obj_dir = get_object_directory();
169         if (opts.append)
170                 flags |= COMMIT_GRAPH_WRITE_APPEND;
171         if (opts.split)
172                 flags |= COMMIT_GRAPH_WRITE_SPLIT;
173         if (opts.progress)
174                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
175         if (!opts.enable_changed_paths)
176                 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
177         if (opts.enable_changed_paths == 1 ||
178             git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
179                 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
180
181         read_replace_refs = 0;
182         odb = find_odb(the_repository, opts.obj_dir);
183
184         if (opts.reachable) {
185                 if (write_commit_graph_reachable(odb, flags, &split_opts))
186                         return 1;
187                 return 0;
188         }
189
190         string_list_init(&lines, 0);
191         if (opts.stdin_packs || opts.stdin_commits) {
192                 struct strbuf buf = STRBUF_INIT;
193
194                 while (strbuf_getline(&buf, stdin) != EOF)
195                         string_list_append(&lines, strbuf_detach(&buf, NULL));
196
197                 if (opts.stdin_packs)
198                         pack_indexes = &lines;
199                 if (opts.stdin_commits) {
200                         commit_hex = &lines;
201                         flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
202                 }
203
204                 UNLEAK(buf);
205         }
206
207         if (write_commit_graph(odb,
208                                pack_indexes,
209                                commit_hex,
210                                flags,
211                                &split_opts))
212                 result = 1;
213
214         UNLEAK(lines);
215         return result;
216 }
217
218 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
219 {
220         static struct option builtin_commit_graph_options[] = {
221                 OPT_STRING(0, "object-dir", &opts.obj_dir,
222                         N_("dir"),
223                         N_("The object directory to store the graph")),
224                 OPT_END(),
225         };
226
227         if (argc == 2 && !strcmp(argv[1], "-h"))
228                 usage_with_options(builtin_commit_graph_usage,
229                                    builtin_commit_graph_options);
230
231         git_config(git_default_config, NULL);
232         argc = parse_options(argc, argv, prefix,
233                              builtin_commit_graph_options,
234                              builtin_commit_graph_usage,
235                              PARSE_OPT_STOP_AT_NON_OPTION);
236
237         save_commit_buffer = 0;
238
239         if (argc > 0) {
240                 if (!strcmp(argv[0], "verify"))
241                         return graph_verify(argc, argv);
242                 if (!strcmp(argv[0], "write"))
243                         return graph_write(argc, argv);
244         }
245
246         usage_with_options(builtin_commit_graph_usage,
247                            builtin_commit_graph_options);
248 }