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