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