Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "config.h" |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 3 | #include "dir.h" |
| 4 | #include "lockfile.h" |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 5 | #include "parse-options.h" |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 6 | #include "repository.h" |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 7 | #include "commit-graph.h" |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 8 | #include "object-store.h" |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 9 | #include "progress.h" |
Taylor Blau | 2f00c35 | 2020-05-13 15:59:55 -0600 | [diff] [blame] | 10 | #include "tag.h" |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 11 | |
| 12 | static char const * const builtin_commit_graph_usage[] = { |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 13 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 14 | N_("git commit-graph write [--object-dir <objdir>] [--append] " |
| 15 | "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 16 | "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] " |
| 17 | "<split options>"), |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 18 | NULL |
| 19 | }; |
| 20 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 21 | static const char * const builtin_commit_graph_verify_usage[] = { |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 22 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 23 | NULL |
| 24 | }; |
| 25 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 26 | static const char * const builtin_commit_graph_write_usage[] = { |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 27 | N_("git commit-graph write [--object-dir <objdir>] [--append] " |
| 28 | "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 29 | "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] " |
| 30 | "<split options>"), |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 31 | NULL |
| 32 | }; |
| 33 | |
| 34 | static struct opts_commit_graph { |
| 35 | const char *obj_dir; |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 36 | int reachable; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 37 | int stdin_packs; |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 38 | int stdin_commits; |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 39 | int append; |
Derrick Stolee | 135a712 | 2019-06-18 11:14:28 -0700 | [diff] [blame] | 40 | int split; |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 41 | int shallow; |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 42 | int progress; |
Garima Singh | d38e07b | 2020-04-06 16:59:51 +0000 | [diff] [blame] | 43 | int enable_changed_paths; |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 44 | } opts; |
| 45 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 46 | static 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 Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 51 | struct strbuf odb_path_real = STRBUF_INIT; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 52 | |
| 53 | prepare_alt_odb(r); |
| 54 | for (odb = r->objects->odb; odb; odb = odb->next) { |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 55 | strbuf_realpath(&odb_path_real, odb->path, 1); |
| 56 | if (!strcmp(obj_dir_real, odb_path_real.buf)) |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | |
| 60 | free(obj_dir_real); |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 61 | strbuf_release(&odb_path_real); |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 62 | |
| 63 | if (!odb) |
| 64 | die(_("could not find object directory matching %s"), obj_dir); |
| 65 | return odb; |
| 66 | } |
| 67 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 68 | static int graph_verify(int argc, const char **argv) |
| 69 | { |
| 70 | struct commit_graph *graph = NULL; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 71 | struct object_directory *odb = NULL; |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 72 | char *graph_name; |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 73 | int open_ok; |
| 74 | int fd; |
| 75 | struct stat st; |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 76 | int flags = 0; |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 77 | |
| 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 Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 82 | OPT_BOOL(0, "shallow", &opts.shallow, |
| 83 | N_("if the commit-graph is split, only verify the tip file")), |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 84 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 85 | OPT_END(), |
| 86 | }; |
| 87 | |
Garima Singh | 0bd7f57 | 2019-08-27 09:56:34 -0700 | [diff] [blame] | 88 | trace2_cmd_mode("verify"); |
| 89 | |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 90 | opts.progress = isatty(2); |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 91 | 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 Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 97 | if (opts.shallow) |
| 98 | flags |= COMMIT_GRAPH_VERIFY_SHALLOW; |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 99 | if (opts.progress) |
| 100 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 101 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 102 | odb = find_odb(the_repository, opts.obj_dir); |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 103 | graph_name = get_commit_graph_filename(odb); |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 104 | open_ok = open_commit_graph(graph_name, &fd, &st); |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 105 | if (!open_ok && errno != ENOENT) |
Ævar Arnfjörð Bjarmason | 7b8ce9c | 2019-03-25 13:08:32 +0100 | [diff] [blame] | 106 | die_errno(_("Could not open commit-graph '%s'"), graph_name); |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 107 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 108 | FREE_AND_NULL(graph_name); |
| 109 | |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 110 | if (open_ok) |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 111 | graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb); |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 112 | else |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 113 | graph = read_commit_graph_one(the_repository, odb); |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 114 | |
| 115 | /* Return failure if open_ok predicted success */ |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 116 | if (!graph) |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 117 | return !!open_ok; |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 118 | |
Martin Ågren | 0bfb48e | 2018-10-03 10:12:17 -0700 | [diff] [blame] | 119 | UNLEAK(graph); |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 120 | return verify_commit_graph(the_repository, graph, flags); |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 123 | extern int read_replace_refs; |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 124 | static struct commit_graph_opts write_opts; |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 125 | |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 126 | static int write_option_parse_split(const struct option *opt, const char *arg, |
| 127 | int unset) |
| 128 | { |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 129 | enum commit_graph_split_flags *flags = opt->value; |
| 130 | |
Jeff King | 8d2aa8d | 2020-09-30 08:29:02 -0400 | [diff] [blame] | 131 | BUG_ON_OPT_NEG(unset); |
| 132 | |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 133 | opts.split = 1; |
| 134 | if (!arg) |
| 135 | return 0; |
| 136 | |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 137 | if (!strcmp(arg, "no-merge")) |
| 138 | *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 139 | else if (!strcmp(arg, "replace")) |
| 140 | *flags = COMMIT_GRAPH_SPLIT_REPLACE; |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 141 | else |
| 142 | die(_("unrecognized --split argument, %s"), arg); |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 147 | static int read_one_commit(struct oidset *commits, struct progress *progress, |
| 148 | const char *hash) |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 149 | { |
Taylor Blau | 2f00c35 | 2020-05-13 15:59:55 -0600 | [diff] [blame] | 150 | struct object *result; |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 151 | 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 Blau | 2f00c35 | 2020-05-13 15:59:55 -0600 | [diff] [blame] | 157 | result = deref_tag(the_repository, parse_object(the_repository, &oid), |
| 158 | NULL, 0); |
| 159 | if (!result) |
| 160 | return error(_("invalid object: %s"), hash); |
Abhishek Kumar | 6da43d9 | 2020-06-17 14:44:08 +0530 | [diff] [blame] | 161 | else if (object_as_type(result, OBJ_COMMIT, 1)) |
Taylor Blau | 2f00c35 | 2020-05-13 15:59:55 -0600 | [diff] [blame] | 162 | oidset_insert(commits, &result->oid); |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 163 | |
| 164 | display_progress(progress, oidset_size(commits)); |
| 165 | |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 169 | static 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 Blau | d356d5d | 2020-09-17 22:59:57 -0400 | [diff] [blame] | 186 | static 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 Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 198 | static int graph_write(int argc, const char **argv) |
| 199 | { |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 200 | struct string_list pack_indexes = STRING_LIST_INIT_NODUP; |
| 201 | struct strbuf buf = STRBUF_INIT; |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 202 | struct oidset commits = OIDSET_INIT; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 203 | struct object_directory *odb = NULL; |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 204 | int result = 0; |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 205 | enum commit_graph_write_flags flags = 0; |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 206 | struct progress *progress = NULL; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 207 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 208 | 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 Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 212 | OPT_BOOL(0, "reachable", &opts.reachable, |
| 213 | N_("start walk at all refs")), |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 214 | OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, |
| 215 | N_("scan pack-indexes listed by stdin for commits")), |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 216 | OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, |
| 217 | N_("start walk at commits listed by stdin")), |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 218 | OPT_BOOL(0, "append", &opts.append, |
| 219 | N_("include all commits already in the commit-graph file")), |
Garima Singh | d38e07b | 2020-04-06 16:59:51 +0000 | [diff] [blame] | 220 | OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths, |
| 221 | N_("enable computation for changed paths")), |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 222 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 223 | OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL, |
Taylor Blau | 4f02735 | 2020-04-13 22:04:08 -0600 | [diff] [blame] | 224 | N_("allow writing an incremental commit-graph file"), |
| 225 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
| 226 | write_option_parse_split), |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 227 | OPT_INTEGER(0, "max-commits", &write_opts.max_commits, |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 228 | N_("maximum number of commits in a non-base split commit-graph")), |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 229 | OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple, |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 230 | N_("maximum ratio between two levels of a split commit-graph")), |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 231 | OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time, |
Derrick Stolee | b09b785 | 2020-04-01 21:00:44 +0000 | [diff] [blame] | 232 | N_("only expire files older than a given date-time")), |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 233 | 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 Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 236 | OPT_END(), |
| 237 | }; |
| 238 | |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 239 | opts.progress = isatty(2); |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 240 | opts.enable_changed_paths = -1; |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 241 | write_opts.size_multiple = 2; |
| 242 | write_opts.max_commits = 0; |
| 243 | write_opts.expire_time = 0; |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 244 | write_opts.max_new_filters = -1; |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 245 | |
Garima Singh | 0bd7f57 | 2019-08-27 09:56:34 -0700 | [diff] [blame] | 246 | trace2_cmd_mode("write"); |
| 247 | |
Taylor Blau | d356d5d | 2020-09-17 22:59:57 -0400 | [diff] [blame] | 248 | git_config(git_commit_graph_write_config, &opts); |
| 249 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 250 | argc = parse_options(argc, argv, NULL, |
| 251 | builtin_commit_graph_write_options, |
| 252 | builtin_commit_graph_write_usage, 0); |
| 253 | |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 254 | if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1) |
| 255 | die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs")); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 256 | if (!opts.obj_dir) |
| 257 | opts.obj_dir = get_object_directory(); |
Derrick Stolee | 5af8039 | 2019-06-12 06:29:38 -0700 | [diff] [blame] | 258 | if (opts.append) |
SZEDER Gábor | 39d8831 | 2019-08-05 10:02:39 +0200 | [diff] [blame] | 259 | flags |= COMMIT_GRAPH_WRITE_APPEND; |
Derrick Stolee | 135a712 | 2019-06-18 11:14:28 -0700 | [diff] [blame] | 260 | if (opts.split) |
SZEDER Gábor | 39d8831 | 2019-08-05 10:02:39 +0200 | [diff] [blame] | 261 | flags |= COMMIT_GRAPH_WRITE_SPLIT; |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 262 | if (opts.progress) |
| 263 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 264 | if (!opts.enable_changed_paths) |
| 265 | flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS; |
| 266 | if (opts.enable_changed_paths == 1 || |
Garima Singh | d5b873c | 2020-04-06 16:59:55 +0000 | [diff] [blame] | 267 | git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) |
Garima Singh | d38e07b | 2020-04-06 16:59:51 +0000 | [diff] [blame] | 268 | flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS; |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 269 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 270 | read_replace_refs = 0; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 271 | odb = find_odb(the_repository, opts.obj_dir); |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 272 | |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 273 | if (opts.reachable) { |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 274 | if (write_commit_graph_reachable(odb, flags, &write_opts)) |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 275 | return 1; |
| 276 | return 0; |
| 277 | } |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 278 | |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 279 | if (opts.stdin_packs) { |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 280 | while (strbuf_getline(&buf, stdin) != EOF) |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 281 | string_list_append(&pack_indexes, |
| 282 | strbuf_detach(&buf, NULL)); |
| 283 | } else if (opts.stdin_commits) { |
| 284 | oidset_init(&commits, 0); |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 285 | if (opts.progress) |
| 286 | progress = start_delayed_progress( |
| 287 | _("Collecting commits from input"), 0); |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 288 | |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 289 | while (strbuf_getline(&buf, stdin) != EOF) { |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 290 | if (read_one_commit(&commits, progress, buf.buf)) { |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 291 | result = 1; |
| 292 | goto cleanup; |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 293 | } |
SZEDER Gábor | 7c5c9b9 | 2019-08-05 10:02:40 +0200 | [diff] [blame] | 294 | } |
Taylor Blau | 5b6653e | 2020-05-13 15:59:44 -0600 | [diff] [blame] | 295 | |
SZEDER Gábor | 862aead | 2020-07-10 21:02:38 +0200 | [diff] [blame] | 296 | stop_progress(&progress); |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 299 | if (write_commit_graph(odb, |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 300 | opts.stdin_packs ? &pack_indexes : NULL, |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 301 | opts.stdin_commits ? &commits : NULL, |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 302 | flags, |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 303 | &write_opts)) |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 304 | result = 1; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 305 | |
Taylor Blau | fa8953c | 2020-05-18 13:27:09 -0600 | [diff] [blame] | 306 | cleanup: |
| 307 | string_list_clear(&pack_indexes, 0); |
| 308 | strbuf_release(&buf); |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 309 | return result; |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 310 | } |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 311 | |
| 312 | int 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 King | dd2e50a | 2019-09-07 01:04:40 -0400 | [diff] [blame] | 331 | save_commit_buffer = 0; |
| 332 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 333 | if (argc > 0) { |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 334 | if (!strcmp(argv[0], "verify")) |
| 335 | return graph_verify(argc, argv); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 336 | if (!strcmp(argv[0], "write")) |
| 337 | return graph_write(argc, argv); |
| 338 | } |
| 339 | |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 340 | usage_with_options(builtin_commit_graph_usage, |
| 341 | builtin_commit_graph_options); |
| 342 | } |