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