blob: f3243bd9825bfe0effa65e3ea0e26269b4be727a [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] "
Junio C Hamano9b6606f2020-05-01 13:39:53 -070016 "[--changed-paths] [--[no-]progress] <split options>"),
Derrick Stoleef237c8b2018-04-02 16:34:20 -040017 NULL
18};
19
Derrick Stolee283e68c2018-06-27 09:24:32 -040020static const char * const builtin_commit_graph_verify_usage[] = {
Garima Singh73716122019-08-26 09:29:58 -070021 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
Derrick Stolee283e68c2018-06-27 09:24:32 -040022 NULL
23};
24
Derrick Stoleef237c8b2018-04-02 16:34:20 -040025static const char * const builtin_commit_graph_write_usage[] = {
Taylor Blau4f027352020-04-13 22:04:08 -060026 N_("git commit-graph write [--object-dir <objdir>] [--append] "
27 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
Junio C Hamano9b6606f2020-05-01 13:39:53 -070028 "[--changed-paths] [--[no-]progress] <split options>"),
Derrick Stolee4ce58ee2018-04-02 16:34:18 -040029 NULL
30};
31
32static struct opts_commit_graph {
33 const char *obj_dir;
Derrick Stolee59fb8772018-06-27 09:24:45 -040034 int reachable;
Derrick Stolee049d51a2018-04-10 08:56:06 -040035 int stdin_packs;
Derrick Stolee3d5df012018-04-10 08:56:07 -040036 int stdin_commits;
Derrick Stolee7547b952018-04-10 08:56:08 -040037 int append;
Derrick Stolee135a7122019-06-18 11:14:28 -070038 int split;
Derrick Stolee3da4b602019-06-18 11:14:32 -070039 int shallow;
Garima Singh73716122019-08-26 09:29:58 -070040 int progress;
Garima Singhd38e07b2020-04-06 16:59:51 +000041 int enable_changed_paths;
Derrick Stolee4ce58ee2018-04-02 16:34:18 -040042} opts;
43
Taylor Blau0bd52e22020-02-03 21:51:50 -080044static struct object_directory *find_odb(struct repository *r,
45 const char *obj_dir)
46{
47 struct object_directory *odb;
48 char *obj_dir_real = real_pathdup(obj_dir, 1);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000049 struct strbuf odb_path_real = STRBUF_INIT;
Taylor Blau0bd52e22020-02-03 21:51:50 -080050
51 prepare_alt_odb(r);
52 for (odb = r->objects->odb; odb; odb = odb->next) {
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000053 strbuf_realpath(&odb_path_real, odb->path, 1);
54 if (!strcmp(obj_dir_real, odb_path_real.buf))
Taylor Blau0bd52e22020-02-03 21:51:50 -080055 break;
56 }
57
58 free(obj_dir_real);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000059 strbuf_release(&odb_path_real);
Taylor Blau0bd52e22020-02-03 21:51:50 -080060
61 if (!odb)
62 die(_("could not find object directory matching %s"), obj_dir);
63 return odb;
64}
65
Derrick Stolee283e68c2018-06-27 09:24:32 -040066static int graph_verify(int argc, const char **argv)
67{
68 struct commit_graph *graph = NULL;
Taylor Blau0bd52e22020-02-03 21:51:50 -080069 struct object_directory *odb = NULL;
Derrick Stolee283e68c2018-06-27 09:24:32 -040070 char *graph_name;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +010071 int open_ok;
72 int fd;
73 struct stat st;
Derrick Stolee3da4b602019-06-18 11:14:32 -070074 int flags = 0;
Derrick Stolee283e68c2018-06-27 09:24:32 -040075
76 static struct option builtin_commit_graph_verify_options[] = {
77 OPT_STRING(0, "object-dir", &opts.obj_dir,
78 N_("dir"),
79 N_("The object directory to store the graph")),
Derrick Stolee3da4b602019-06-18 11:14:32 -070080 OPT_BOOL(0, "shallow", &opts.shallow,
81 N_("if the commit-graph is split, only verify the tip file")),
Garima Singh73716122019-08-26 09:29:58 -070082 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
Derrick Stolee283e68c2018-06-27 09:24:32 -040083 OPT_END(),
84 };
85
Garima Singh0bd7f572019-08-27 09:56:34 -070086 trace2_cmd_mode("verify");
87
Garima Singh73716122019-08-26 09:29:58 -070088 opts.progress = isatty(2);
Derrick Stolee283e68c2018-06-27 09:24:32 -040089 argc = parse_options(argc, argv, NULL,
90 builtin_commit_graph_verify_options,
91 builtin_commit_graph_verify_usage, 0);
92
93 if (!opts.obj_dir)
94 opts.obj_dir = get_object_directory();
Derrick Stolee3da4b602019-06-18 11:14:32 -070095 if (opts.shallow)
96 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
Garima Singh73716122019-08-26 09:29:58 -070097 if (opts.progress)
98 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee283e68c2018-06-27 09:24:32 -040099
Taylor Blau0bd52e22020-02-03 21:51:50 -0800100 odb = find_odb(the_repository, opts.obj_dir);
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800101 graph_name = get_commit_graph_filename(odb);
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100102 open_ok = open_commit_graph(graph_name, &fd, &st);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700103 if (!open_ok && errno != ENOENT)
Ævar Arnfjörð Bjarmason7b8ce9c2019-03-25 13:08:32 +0100104 die_errno(_("Could not open commit-graph '%s'"), graph_name);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700105
Derrick Stolee283e68c2018-06-27 09:24:32 -0400106 FREE_AND_NULL(graph_name);
107
Derrick Stolee3da4b602019-06-18 11:14:32 -0700108 if (open_ok)
Taylor Blauab14d062020-09-09 11:22:56 -0400109 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
Taylor Blau0bd52e22020-02-03 21:51:50 -0800110 else
Taylor Blau13c24992020-02-03 13:18:00 -0800111 graph = read_commit_graph_one(the_repository, odb);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700112
113 /* Return failure if open_ok predicted success */
Derrick Stolee283e68c2018-06-27 09:24:32 -0400114 if (!graph)
Derrick Stolee3da4b602019-06-18 11:14:32 -0700115 return !!open_ok;
Derrick Stolee283e68c2018-06-27 09:24:32 -0400116
Martin Ågren0bfb48e2018-10-03 10:12:17 -0700117 UNLEAK(graph);
Derrick Stolee3da4b602019-06-18 11:14:32 -0700118 return verify_commit_graph(the_repository, graph, flags);
Derrick Stolee283e68c2018-06-27 09:24:32 -0400119}
120
Derrick Stoleed6538242018-08-20 18:24:27 +0000121extern int read_replace_refs;
Taylor Blau98bb7962020-09-17 22:59:49 -0400122static struct commit_graph_opts write_opts;
Derrick Stoleed6538242018-08-20 18:24:27 +0000123
Taylor Blau4f027352020-04-13 22:04:08 -0600124static int write_option_parse_split(const struct option *opt, const char *arg,
125 int unset)
126{
Taylor Blaufdbde822020-04-13 22:04:12 -0600127 enum commit_graph_split_flags *flags = opt->value;
128
Taylor Blau4f027352020-04-13 22:04:08 -0600129 opts.split = 1;
130 if (!arg)
131 return 0;
132
Taylor Blaufdbde822020-04-13 22:04:12 -0600133 if (!strcmp(arg, "no-merge"))
134 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
Taylor Blau8a6ac282020-04-13 22:04:17 -0600135 else if (!strcmp(arg, "replace"))
136 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
Taylor Blaufdbde822020-04-13 22:04:12 -0600137 else
138 die(_("unrecognized --split argument, %s"), arg);
Taylor Blau4f027352020-04-13 22:04:08 -0600139
140 return 0;
141}
142
Taylor Blau5b6653e2020-05-13 15:59:44 -0600143static int read_one_commit(struct oidset *commits, struct progress *progress,
144 const char *hash)
Taylor Blaufa8953c2020-05-18 13:27:09 -0600145{
Taylor Blau2f00c352020-05-13 15:59:55 -0600146 struct object *result;
Taylor Blaufa8953c2020-05-18 13:27:09 -0600147 struct object_id oid;
148 const char *end;
149
150 if (parse_oid_hex(hash, &oid, &end))
151 return error(_("unexpected non-hex object ID: %s"), hash);
152
Taylor Blau2f00c352020-05-13 15:59:55 -0600153 result = deref_tag(the_repository, parse_object(the_repository, &oid),
154 NULL, 0);
155 if (!result)
156 return error(_("invalid object: %s"), hash);
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530157 else if (object_as_type(result, OBJ_COMMIT, 1))
Taylor Blau2f00c352020-05-13 15:59:55 -0600158 oidset_insert(commits, &result->oid);
Taylor Blau5b6653e2020-05-13 15:59:44 -0600159
160 display_progress(progress, oidset_size(commits));
161
Taylor Blaufa8953c2020-05-18 13:27:09 -0600162 return 0;
163}
164
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400165static int graph_write(int argc, const char **argv)
166{
Taylor Blaufa8953c2020-05-18 13:27:09 -0600167 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
168 struct strbuf buf = STRBUF_INIT;
Taylor Blau6830c362020-04-13 22:04:25 -0600169 struct oidset commits = OIDSET_INIT;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800170 struct object_directory *odb = NULL;
Derrick Stoleee103f722019-06-12 06:29:37 -0700171 int result = 0;
Garima Singh73716122019-08-26 09:29:58 -0700172 enum commit_graph_write_flags flags = 0;
Taylor Blau5b6653e2020-05-13 15:59:44 -0600173 struct progress *progress = NULL;
Derrick Stolee049d51a2018-04-10 08:56:06 -0400174
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400175 static struct option builtin_commit_graph_write_options[] = {
176 OPT_STRING(0, "object-dir", &opts.obj_dir,
177 N_("dir"),
178 N_("The object directory to store the graph")),
Derrick Stolee59fb8772018-06-27 09:24:45 -0400179 OPT_BOOL(0, "reachable", &opts.reachable,
180 N_("start walk at all refs")),
Derrick Stolee049d51a2018-04-10 08:56:06 -0400181 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
182 N_("scan pack-indexes listed by stdin for commits")),
Derrick Stolee3d5df012018-04-10 08:56:07 -0400183 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
184 N_("start walk at commits listed by stdin")),
Derrick Stolee7547b952018-04-10 08:56:08 -0400185 OPT_BOOL(0, "append", &opts.append,
186 N_("include all commits already in the commit-graph file")),
Garima Singhd38e07b2020-04-06 16:59:51 +0000187 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
188 N_("enable computation for changed paths")),
Garima Singh73716122019-08-26 09:29:58 -0700189 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400190 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
Taylor Blau4f027352020-04-13 22:04:08 -0600191 N_("allow writing an incremental commit-graph file"),
192 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
193 write_option_parse_split),
Taylor Blau98bb7962020-09-17 22:59:49 -0400194 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700195 N_("maximum number of commits in a non-base split commit-graph")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400196 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700197 N_("maximum ratio between two levels of a split commit-graph")),
Taylor Blau98bb7962020-09-17 22:59:49 -0400198 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
Derrick Stoleeb09b7852020-04-01 21:00:44 +0000199 N_("only expire files older than a given date-time")),
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400200 OPT_END(),
201 };
202
Garima Singh73716122019-08-26 09:29:58 -0700203 opts.progress = isatty(2);
Derrick Stolee0087a872020-07-01 13:27:24 +0000204 opts.enable_changed_paths = -1;
Taylor Blau98bb7962020-09-17 22:59:49 -0400205 write_opts.size_multiple = 2;
206 write_opts.max_commits = 0;
207 write_opts.expire_time = 0;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700208
Garima Singh0bd7f572019-08-27 09:56:34 -0700209 trace2_cmd_mode("write");
210
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400211 argc = parse_options(argc, argv, NULL,
212 builtin_commit_graph_write_options,
213 builtin_commit_graph_write_usage, 0);
214
Derrick Stolee59fb8772018-06-27 09:24:45 -0400215 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
216 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400217 if (!opts.obj_dir)
218 opts.obj_dir = get_object_directory();
Derrick Stolee5af80392019-06-12 06:29:38 -0700219 if (opts.append)
SZEDER Gábor39d88312019-08-05 10:02:39 +0200220 flags |= COMMIT_GRAPH_WRITE_APPEND;
Derrick Stolee135a7122019-06-18 11:14:28 -0700221 if (opts.split)
SZEDER Gábor39d88312019-08-05 10:02:39 +0200222 flags |= COMMIT_GRAPH_WRITE_SPLIT;
Garima Singh73716122019-08-26 09:29:58 -0700223 if (opts.progress)
224 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee0087a872020-07-01 13:27:24 +0000225 if (!opts.enable_changed_paths)
226 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
227 if (opts.enable_changed_paths == 1 ||
Garima Singhd5b873c2020-04-06 16:59:55 +0000228 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
Garima Singhd38e07b2020-04-06 16:59:51 +0000229 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400230
Derrick Stoleed6538242018-08-20 18:24:27 +0000231 read_replace_refs = 0;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800232 odb = find_odb(the_repository, opts.obj_dir);
Derrick Stoleed6538242018-08-20 18:24:27 +0000233
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700234 if (opts.reachable) {
Taylor Blau98bb7962020-09-17 22:59:49 -0400235 if (write_commit_graph_reachable(odb, flags, &write_opts))
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700236 return 1;
237 return 0;
238 }
Derrick Stolee59fb8772018-06-27 09:24:45 -0400239
Taylor Blaufa8953c2020-05-18 13:27:09 -0600240 if (opts.stdin_packs) {
Derrick Stoleed88b14b2018-06-27 09:24:44 -0400241 while (strbuf_getline(&buf, stdin) != EOF)
Taylor Blaufa8953c2020-05-18 13:27:09 -0600242 string_list_append(&pack_indexes,
243 strbuf_detach(&buf, NULL));
244 } else if (opts.stdin_commits) {
245 oidset_init(&commits, 0);
Taylor Blau5b6653e2020-05-13 15:59:44 -0600246 if (opts.progress)
247 progress = start_delayed_progress(
248 _("Collecting commits from input"), 0);
Derrick Stolee049d51a2018-04-10 08:56:06 -0400249
Taylor Blaufa8953c2020-05-18 13:27:09 -0600250 while (strbuf_getline(&buf, stdin) != EOF) {
Taylor Blau5b6653e2020-05-13 15:59:44 -0600251 if (read_one_commit(&commits, progress, buf.buf)) {
Taylor Blaufa8953c2020-05-18 13:27:09 -0600252 result = 1;
253 goto cleanup;
Taylor Blau6830c362020-04-13 22:04:25 -0600254 }
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200255 }
Taylor Blau5b6653e2020-05-13 15:59:44 -0600256
SZEDER Gábor862aead2020-07-10 21:02:38 +0200257 stop_progress(&progress);
Derrick Stolee049d51a2018-04-10 08:56:06 -0400258 }
259
Taylor Blau0bd52e22020-02-03 21:51:50 -0800260 if (write_commit_graph(odb,
Taylor Blaufa8953c2020-05-18 13:27:09 -0600261 opts.stdin_packs ? &pack_indexes : NULL,
Taylor Blau6830c362020-04-13 22:04:25 -0600262 opts.stdin_commits ? &commits : NULL,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700263 flags,
Taylor Blau98bb7962020-09-17 22:59:49 -0400264 &write_opts))
Derrick Stoleee103f722019-06-12 06:29:37 -0700265 result = 1;
Derrick Stolee049d51a2018-04-10 08:56:06 -0400266
Taylor Blaufa8953c2020-05-18 13:27:09 -0600267cleanup:
268 string_list_clear(&pack_indexes, 0);
269 strbuf_release(&buf);
Derrick Stoleee103f722019-06-12 06:29:37 -0700270 return result;
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400271}
Derrick Stolee4ce58ee2018-04-02 16:34:18 -0400272
273int cmd_commit_graph(int argc, const char **argv, const char *prefix)
274{
275 static struct option builtin_commit_graph_options[] = {
276 OPT_STRING(0, "object-dir", &opts.obj_dir,
277 N_("dir"),
278 N_("The object directory to store the graph")),
279 OPT_END(),
280 };
281
282 if (argc == 2 && !strcmp(argv[1], "-h"))
283 usage_with_options(builtin_commit_graph_usage,
284 builtin_commit_graph_options);
285
286 git_config(git_default_config, NULL);
287 argc = parse_options(argc, argv, prefix,
288 builtin_commit_graph_options,
289 builtin_commit_graph_usage,
290 PARSE_OPT_STOP_AT_NON_OPTION);
291
Jeff Kingdd2e50a2019-09-07 01:04:40 -0400292 save_commit_buffer = 0;
293
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400294 if (argc > 0) {
Derrick Stolee283e68c2018-06-27 09:24:32 -0400295 if (!strcmp(argv[0], "verify"))
296 return graph_verify(argc, argv);
Derrick Stoleef237c8b2018-04-02 16:34:20 -0400297 if (!strcmp(argv[0], "write"))
298 return graph_write(argc, argv);
299 }
300
Derrick Stolee4ce58ee2018-04-02 16:34:18 -0400301 usage_with_options(builtin_commit_graph_usage,
302 builtin_commit_graph_options);
303}