blob: 78fa08f43af5aaccc05965131ba0b4026c7f7bab [file] [log] [blame]
Derrick Stolee4ce58ee2018-04-02 16:34:18 -04001#include "builtin.h"
2#include "config.h"
Derrick Stoleef237c8b2018-04-02 16:34:20 -04003#include "dir.h"
4#include "lockfile.h"
Derrick Stolee4ce58ee2018-04-02 16:34:18 -04005#include "parse-options.h"
Derrick Stolee283e68c2018-06-27 09:24:32 -04006#include "repository.h"
Derrick Stoleef237c8b2018-04-02 16:34:20 -04007#include "commit-graph.h"
Derrick Stolee3da4b602019-06-18 11:14:32 -07008#include "object-store.h"
Taylor Blau5b6653e2020-05-13 15:59:44 -06009#include "progress.h"
Taylor Blau2f00c352020-05-13 15:59:55 -060010#include "tag.h"
Derrick Stolee4ce58ee2018-04-02 16:34:18 -040011
12static char const * const builtin_commit_graph_usage[] = {
Garima Singh73716122019-08-26 09:29:58 -070013 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
Taylor Blau4f027352020-04-13 22:04:08 -060014 N_("git commit-graph write [--object-dir <objdir>] [--append] "
15 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
Taylor Blau809e0322020-09-18 09:27:27 -040016 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
17 "<split options>"),
Derrick Stoleef237c8b2018-04-02 16:34:20 -040018 NULL
19};
20
Derrick Stolee283e68c2018-06-27 09:24:32 -040021static const char * const builtin_commit_graph_verify_usage[] = {
Garima Singh73716122019-08-26 09:29:58 -070022 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
Derrick Stolee283e68c2018-06-27 09:24:32 -040023 NULL
24};
25
Derrick Stoleef237c8b2018-04-02 16:34:20 -040026static const char * const builtin_commit_graph_write_usage[] = {
Taylor Blau4f027352020-04-13 22:04:08 -060027 N_("git commit-graph write [--object-dir <objdir>] [--append] "
28 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
Taylor Blau809e0322020-09-18 09:27:27 -040029 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
30 "<split options>"),
Derrick Stolee4ce58ee2018-04-02 16:34:18 -040031 NULL
32};
33
34static struct opts_commit_graph {
35 const char *obj_dir;
Derrick Stolee59fb8772018-06-27 09:24:45 -040036 int reachable;
Derrick Stolee049d51a2018-04-10 08:56:06 -040037 int stdin_packs;
Derrick Stolee3d5df012018-04-10 08:56:07 -040038 int stdin_commits;
Derrick Stolee7547b952018-04-10 08:56:08 -040039 int append;
Derrick Stolee135a7122019-06-18 11:14:28 -070040 int split;
Derrick Stolee3da4b602019-06-18 11:14:32 -070041 int shallow;
Garima Singh73716122019-08-26 09:29:58 -070042 int progress;
Garima Singhd38e07b2020-04-06 16:59:51 +000043 int enable_changed_paths;
Derrick Stolee4ce58ee2018-04-02 16:34:18 -040044} opts;
45
Taylor Blau0bd52e22020-02-03 21:51:50 -080046static struct object_directory *find_odb(struct repository *r,
47 const char *obj_dir)
48{
49 struct object_directory *odb;
50 char *obj_dir_real = real_pathdup(obj_dir, 1);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000051 struct strbuf odb_path_real = STRBUF_INIT;
Taylor Blau0bd52e22020-02-03 21:51:50 -080052
53 prepare_alt_odb(r);
54 for (odb = r->objects->odb; odb; odb = odb->next) {
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000055 strbuf_realpath(&odb_path_real, odb->path, 1);
56 if (!strcmp(obj_dir_real, odb_path_real.buf))
Taylor Blau0bd52e22020-02-03 21:51:50 -080057 break;
58 }
59
60 free(obj_dir_real);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000061 strbuf_release(&odb_path_real);
Taylor Blau0bd52e22020-02-03 21:51:50 -080062
63 if (!odb)
64 die(_("could not find object directory matching %s"), obj_dir);
65 return odb;
66}
67
Derrick Stolee283e68c2018-06-27 09:24:32 -040068static int graph_verify(int argc, const char **argv)
69{
70 struct commit_graph *graph = NULL;
Taylor Blau0bd52e22020-02-03 21:51:50 -080071 struct object_directory *odb = NULL;
Derrick Stolee283e68c2018-06-27 09:24:32 -040072 char *graph_name;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +010073 int open_ok;
74 int fd;
75 struct stat st;
Derrick Stolee3da4b602019-06-18 11:14:32 -070076 int flags = 0;
Derrick Stolee283e68c2018-06-27 09:24:32 -040077
78 static struct option builtin_commit_graph_verify_options[] = {
79 OPT_STRING(0, "object-dir", &opts.obj_dir,
80 N_("dir"),
81 N_("The object directory to store the graph")),
Derrick Stolee3da4b602019-06-18 11:14:32 -070082 OPT_BOOL(0, "shallow", &opts.shallow,
83 N_("if the commit-graph is split, only verify the tip file")),
Garima Singh73716122019-08-26 09:29:58 -070084 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
Derrick Stolee283e68c2018-06-27 09:24:32 -040085 OPT_END(),
86 };
87
Garima Singh0bd7f572019-08-27 09:56:34 -070088 trace2_cmd_mode("verify");
89
Garima Singh73716122019-08-26 09:29:58 -070090 opts.progress = isatty(2);
Derrick Stolee283e68c2018-06-27 09:24:32 -040091 argc = parse_options(argc, argv, NULL,
92 builtin_commit_graph_verify_options,
93 builtin_commit_graph_verify_usage, 0);
94
95 if (!opts.obj_dir)
96 opts.obj_dir = get_object_directory();
Derrick Stolee3da4b602019-06-18 11:14:32 -070097 if (opts.shallow)
98 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
Garima Singh73716122019-08-26 09:29:58 -070099 if (opts.progress)
100 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee283e68c2018-06-27 09:24:32 -0400101
Taylor Blau0bd52e22020-02-03 21:51:50 -0800102 odb = find_odb(the_repository, opts.obj_dir);
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800103 graph_name = get_commit_graph_filename(odb);
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100104 open_ok = open_commit_graph(graph_name, &fd, &st);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700105 if (!open_ok && errno != ENOENT)
Ævar Arnfjörð Bjarmason7b8ce9c2019-03-25 13:08:32 +0100106 die_errno(_("Could not open commit-graph '%s'"), graph_name);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700107
Derrick Stolee283e68c2018-06-27 09:24:32 -0400108 FREE_AND_NULL(graph_name);
109
Derrick Stolee3da4b602019-06-18 11:14:32 -0700110 if (open_ok)
Taylor Blauab14d062020-09-09 11:22:56 -0400111 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
Taylor Blau0bd52e22020-02-03 21:51:50 -0800112 else
Taylor Blau13c24992020-02-03 13:18:00 -0800113 graph = read_commit_graph_one(the_repository, odb);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700114
115 /* Return failure if open_ok predicted success */
Derrick Stolee283e68c2018-06-27 09:24:32 -0400116 if (!graph)
Derrick Stolee3da4b602019-06-18 11:14:32 -0700117 return !!open_ok;
Derrick Stolee283e68c2018-06-27 09:24:32 -0400118
Martin Ågren0bfb48e2018-10-03 10:12:17 -0700119 UNLEAK(graph);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700120 return verify_commit_graph(the_repository, graph, flags);
Derrick Stolee283e68c2018-06-27 09:24:32 -0400121}
122
Derrick Stoleed6538242018-08-20 18:24:27 +0000123extern int read_replace_refs;
Taylor Blau98bb7962020-09-17 22:59:49 -0400124static struct commit_graph_opts write_opts;
Derrick Stoleed6538242018-08-20 18:24:27 +0000125
Taylor Blau4f027352020-04-13 22:04:08 -0600126static int write_option_parse_split(const struct option *opt, const char *arg,
127 int unset)
128{
Taylor Blaufdbde822020-04-13 22:04:12 -0600129 enum commit_graph_split_flags *flags = opt->value;
130
Jeff King8d2aa8d2020-09-30 08:29:02 -0400131 BUG_ON_OPT_NEG(unset);
132
Taylor Blau4f027352020-04-13 22:04:08 -0600133 opts.split = 1;
134 if (!arg)
135 return 0;
136
Taylor Blaufdbde822020-04-13 22:04:12 -0600137 if (!strcmp(arg, "no-merge"))
138 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
Taylor Blau8a6ac282020-04-13 22:04:17 -0600139 else if (!strcmp(arg, "replace"))
140 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
Taylor Blaufdbde822020-04-13 22:04:12 -0600141 else
142 die(_("unrecognized --split argument, %s"), arg);
Taylor Blau4f027352020-04-13 22:04:08 -0600143
144 return 0;
145}
146
Taylor Blau5b6653e2020-05-13 15:59:44 -0600147static int read_one_commit(struct oidset *commits, struct progress *progress,
148 const char *hash)
Taylor Blaufa8953c2020-05-18 13:27:09 -0600149{
Taylor Blau2f00c352020-05-13 15:59:55 -0600150 struct object *result;
Taylor Blaufa8953c2020-05-18 13:27:09 -0600151 struct object_id oid;
152 const char *end;
153
154 if (parse_oid_hex(hash, &oid, &end))
155 return error(_("unexpected non-hex object ID: %s"), hash);
156
Taylor Blau2f00c352020-05-13 15:59:55 -0600157 result = deref_tag(the_repository, parse_object(the_repository, &oid),
158 NULL, 0);
159 if (!result)
160 return error(_("invalid object: %s"), hash);
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530161 else if (object_as_type(result, OBJ_COMMIT, 1))
Taylor Blau2f00c352020-05-13 15:59:55 -0600162 oidset_insert(commits, &result->oid);
Taylor Blau5b6653e2020-05-13 15:59:44 -0600163
164 display_progress(progress, oidset_size(commits));
165
Taylor Blaufa8953c2020-05-18 13:27:09 -0600166 return 0;
167}
168
Taylor Blau809e0322020-09-18 09:27:27 -0400169static int write_option_max_new_filters(const struct option *opt,
170 const char *arg,
171 int unset)
172{
173 int *to = opt->value;
174 if (unset)
175 *to = -1;
176 else {
177 const char *s;
178 *to = strtol(arg, (char **)&s, 10);
179 if (*s)
180 return error(_("%s expects a numerical value"),
181 optname(opt, opt->flags));
182 }
183 return 0;
184}
185
Taylor Blaud356d5d2020-09-17 22:59:57 -0400186static int git_commit_graph_write_config(const char *var, const char *value,
187 void *cb)
188{
189 if (!strcmp(var, "commitgraph.maxnewfilters"))
190 write_opts.max_new_filters = git_config_int(var, value);
191 /*
192 * No need to fall-back to 'git_default_config', since this was already
193 * called in 'cmd_commit_graph()'.
194 */
195 return 0;
196}
197
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400198static int graph_write(int argc, const char **argv)
199{
Taylor Blaufa8953c2020-05-18 13:27:09 -0600200 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
201 struct strbuf buf = STRBUF_INIT;
Taylor Blau6830c362020-04-13 22:04:25 -0600202 struct oidset commits = OIDSET_INIT;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800203 struct object_directory *odb = NULL;
Derrick Stoleee103f722019-06-12 06:29:37 -0700204 int result = 0;
Garima Singh73716122019-08-26 09:29:58 -0700205 enum commit_graph_write_flags flags = 0;
Taylor Blau5b6653e2020-05-13 15:59:44 -0600206 struct progress *progress = NULL;
Derrick Stolee049d51a2018-04-10 08:56:06 -0400207
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400208 static struct option builtin_commit_graph_write_options[] = {
209 OPT_STRING(0, "object-dir", &opts.obj_dir,
210 N_("dir"),
211 N_("The object directory to store the graph")),
Derrick Stolee59fb8772018-06-27 09:24:45 -0400212 OPT_BOOL(0, "reachable", &opts.reachable,
213 N_("start walk at all refs")),
Derrick Stolee049d51a2018-04-10 08:56:06 -0400214 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
215 N_("scan pack-indexes listed by stdin for commits")),
Derrick Stolee3d5df012018-04-10 08:56:07 -0400216 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
217 N_("start walk at commits listed by stdin")),
Derrick Stolee7547b952018-04-10 08:56:08 -0400218 OPT_BOOL(0, "append", &opts.append,
219 N_("include all commits already in the commit-graph file")),
Garima Singhd38e07b2020-04-06 16:59:51 +0000220 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
221 N_("enable computation for changed paths")),
Garima Singh73716122019-08-26 09:29:58 -0700222 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400223 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
Taylor Blau4f027352020-04-13 22:04:08 -0600224 N_("allow writing an incremental commit-graph file"),
225 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
226 write_option_parse_split),
Taylor Blau98bb7962020-09-17 22:59:49 -0400227 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700228 N_("maximum number of commits in a non-base split commit-graph")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400229 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700230 N_("maximum ratio between two levels of a split commit-graph")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400231 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
Derrick Stoleeb09b7852020-04-01 21:00:44 +0000232 N_("only expire files older than a given date-time")),
Taylor Blau809e0322020-09-18 09:27:27 -0400233 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
234 NULL, N_("maximum number of changed-path Bloom filters to compute"),
235 0, write_option_max_new_filters),
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400236 OPT_END(),
237 };
238
Garima Singh73716122019-08-26 09:29:58 -0700239 opts.progress = isatty(2);
Derrick Stolee0087a872020-07-01 13:27:24 +0000240 opts.enable_changed_paths = -1;
Taylor Blau98bb7962020-09-17 22:59:49 -0400241 write_opts.size_multiple = 2;
242 write_opts.max_commits = 0;
243 write_opts.expire_time = 0;
Taylor Blau809e0322020-09-18 09:27:27 -0400244 write_opts.max_new_filters = -1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700245
Garima Singh0bd7f572019-08-27 09:56:34 -0700246 trace2_cmd_mode("write");
247
Taylor Blaud356d5d2020-09-17 22:59:57 -0400248 git_config(git_commit_graph_write_config, &opts);
249
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400250 argc = parse_options(argc, argv, NULL,
251 builtin_commit_graph_write_options,
252 builtin_commit_graph_write_usage, 0);
253
Derrick Stolee59fb8772018-06-27 09:24:45 -0400254 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
255 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400256 if (!opts.obj_dir)
257 opts.obj_dir = get_object_directory();
Derrick Stolee5af80392019-06-12 06:29:38 -0700258 if (opts.append)
SZEDER Gábor39d88312019-08-05 10:02:39 +0200259 flags |= COMMIT_GRAPH_WRITE_APPEND;
Derrick Stolee135a7122019-06-18 11:14:28 -0700260 if (opts.split)
SZEDER Gábor39d88312019-08-05 10:02:39 +0200261 flags |= COMMIT_GRAPH_WRITE_SPLIT;
Garima Singh73716122019-08-26 09:29:58 -0700262 if (opts.progress)
263 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee0087a872020-07-01 13:27:24 +0000264 if (!opts.enable_changed_paths)
265 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
266 if (opts.enable_changed_paths == 1 ||
Garima Singhd5b873c2020-04-06 16:59:55 +0000267 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
Garima Singhd38e07b2020-04-06 16:59:51 +0000268 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400269
Derrick Stoleed6538242018-08-20 18:24:27 +0000270 read_replace_refs = 0;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800271 odb = find_odb(the_repository, opts.obj_dir);
Derrick Stoleed6538242018-08-20 18:24:27 +0000272
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700273 if (opts.reachable) {
Taylor Blau98bb7962020-09-17 22:59:49 -0400274 if (write_commit_graph_reachable(odb, flags, &write_opts))
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700275 return 1;
276 return 0;
277 }
Derrick Stolee59fb8772018-06-27 09:24:45 -0400278
Taylor Blaufa8953c2020-05-18 13:27:09 -0600279 if (opts.stdin_packs) {
Derrick Stoleed88b14b2018-06-27 09:24:44 -0400280 while (strbuf_getline(&buf, stdin) != EOF)
Taylor Blaufa8953c2020-05-18 13:27:09 -0600281 string_list_append(&pack_indexes,
282 strbuf_detach(&buf, NULL));
283 } else if (opts.stdin_commits) {
284 oidset_init(&commits, 0);
Taylor Blau5b6653e2020-05-13 15:59:44 -0600285 if (opts.progress)
286 progress = start_delayed_progress(
287 _("Collecting commits from input"), 0);
Derrick Stolee049d51a2018-04-10 08:56:06 -0400288
Taylor Blaufa8953c2020-05-18 13:27:09 -0600289 while (strbuf_getline(&buf, stdin) != EOF) {
Taylor Blau5b6653e2020-05-13 15:59:44 -0600290 if (read_one_commit(&commits, progress, buf.buf)) {
Taylor Blaufa8953c2020-05-18 13:27:09 -0600291 result = 1;
292 goto cleanup;
Taylor Blau6830c362020-04-13 22:04:25 -0600293 }
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200294 }
Taylor Blau5b6653e2020-05-13 15:59:44 -0600295
SZEDER Gábor862aead2020-07-10 21:02:38 +0200296 stop_progress(&progress);
Derrick Stolee049d51a2018-04-10 08:56:06 -0400297 }
298
Taylor Blau0bd52e22020-02-03 21:51:50 -0800299 if (write_commit_graph(odb,
Taylor Blaufa8953c2020-05-18 13:27:09 -0600300 opts.stdin_packs ? &pack_indexes : NULL,
Taylor Blau6830c362020-04-13 22:04:25 -0600301 opts.stdin_commits ? &commits : NULL,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700302 flags,
Taylor Blau98bb7962020-09-17 22:59:49 -0400303 &write_opts))
Derrick Stoleee103f722019-06-12 06:29:37 -0700304 result = 1;
Derrick Stolee049d51a2018-04-10 08:56:06 -0400305
Taylor Blaufa8953c2020-05-18 13:27:09 -0600306cleanup:
307 string_list_clear(&pack_indexes, 0);
308 strbuf_release(&buf);
Derrick Stoleee103f722019-06-12 06:29:37 -0700309 return result;
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400310}
Derrick Stolee4ce58ee2018-04-02 16:34:18 -0400311
312int cmd_commit_graph(int argc, const char **argv, const char *prefix)
313{
314 static struct option builtin_commit_graph_options[] = {
315 OPT_STRING(0, "object-dir", &opts.obj_dir,
316 N_("dir"),
317 N_("The object directory to store the graph")),
318 OPT_END(),
319 };
320
321 if (argc == 2 && !strcmp(argv[1], "-h"))
322 usage_with_options(builtin_commit_graph_usage,
323 builtin_commit_graph_options);
324
325 git_config(git_default_config, NULL);
326 argc = parse_options(argc, argv, prefix,
327 builtin_commit_graph_options,
328 builtin_commit_graph_usage,
329 PARSE_OPT_STOP_AT_NON_OPTION);
330
Jeff Kingdd2e50a2019-09-07 01:04:40 -0400331 save_commit_buffer = 0;
332
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400333 if (argc > 0) {
Derrick Stolee283e68c2018-06-27 09:24:32 -0400334 if (!strcmp(argv[0], "verify"))
335 return graph_verify(argc, argv);
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400336 if (!strcmp(argv[0], "write"))
337 return graph_write(argc, argv);
338 }
339
Derrick Stolee4ce58ee2018-04-02 16:34:18 -0400340 usage_with_options(builtin_commit_graph_usage,
341 builtin_commit_graph_options);
342}