1 #define USE_THE_REPOSITORY_VARIABLE
7 #include "parse-options.h"
8 #include "commit-graph.h"
9 #include "object-store.h"
11 #include "replace-object.h"
16 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
17 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
19 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
20 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
21 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
22 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
25 static const char * const builtin_commit_graph_verify_usage
[] = {
26 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
30 static const char * const builtin_commit_graph_write_usage
[] = {
31 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
35 static char const * const builtin_commit_graph_usage
[] = {
36 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
37 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
41 static struct opts_commit_graph
{
50 int enable_changed_paths
;
53 static struct option common_opts
[] = {
54 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
56 N_("the object directory to store the graph")),
60 static struct option
*add_common_options(struct option
*to
)
62 return parse_options_concat(common_opts
, to
);
65 static int graph_verify(int argc
, const char **argv
, const char *prefix
,
66 struct repository
*repo UNUSED
)
68 struct commit_graph
*graph
= NULL
;
69 struct object_directory
*odb
= NULL
;
72 enum { OPENED_NONE
, OPENED_GRAPH
, OPENED_CHAIN
} opened
= OPENED_NONE
;
76 int incomplete_chain
= 0;
79 static struct option builtin_commit_graph_verify_options
[] = {
80 OPT_BOOL(0, "shallow", &opts
.shallow
,
81 N_("if the commit-graph is split, only verify the tip file")),
82 OPT_BOOL(0, "progress", &opts
.progress
,
83 N_("force progress reporting")),
86 struct option
*options
= add_common_options(builtin_commit_graph_verify_options
);
88 trace2_cmd_mode("verify");
90 opts
.progress
= isatty(2);
91 argc
= parse_options(argc
, argv
, prefix
,
93 builtin_commit_graph_verify_usage
, 0);
95 usage_with_options(builtin_commit_graph_verify_usage
, options
);
98 opts
.obj_dir
= repo_get_object_directory(the_repository
);
100 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
102 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
104 odb
= find_odb(the_repository
, opts
.obj_dir
);
105 graph_name
= get_commit_graph_filename(odb
);
106 chain_name
= get_commit_graph_chain_filename(odb
);
107 if (open_commit_graph(graph_name
, &fd
, &st
))
108 opened
= OPENED_GRAPH
;
109 else if (errno
!= ENOENT
)
110 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
111 else if (open_commit_graph_chain(chain_name
, &fd
, &st
))
112 opened
= OPENED_CHAIN
;
113 else if (errno
!= ENOENT
)
114 die_errno(_("could not open commit-graph chain '%s'"), chain_name
);
116 FREE_AND_NULL(graph_name
);
117 FREE_AND_NULL(chain_name
);
118 FREE_AND_NULL(options
);
120 if (opened
== OPENED_NONE
)
122 else if (opened
== OPENED_GRAPH
)
123 graph
= load_commit_graph_one_fd_st(the_repository
, fd
, &st
, odb
);
125 graph
= load_commit_graph_chain_fd_st(the_repository
, fd
, &st
,
131 ret
= verify_commit_graph(the_repository
, graph
, flags
);
132 free_commit_graph(graph
);
134 if (incomplete_chain
) {
135 error("one or more commit-graph chain files could not be loaded");
142 extern int read_replace_refs
;
143 static struct commit_graph_opts write_opts
;
145 static int write_option_parse_split(const struct option
*opt
, const char *arg
,
148 enum commit_graph_split_flags
*flags
= opt
->value
;
150 BUG_ON_OPT_NEG(unset
);
156 if (!strcmp(arg
, "no-merge"))
157 *flags
= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
;
158 else if (!strcmp(arg
, "replace"))
159 *flags
= COMMIT_GRAPH_SPLIT_REPLACE
;
161 die(_("unrecognized --split argument, %s"), arg
);
166 static int read_one_commit(struct oidset
*commits
, struct progress
*progress
,
169 struct object
*result
;
170 struct object_id oid
;
173 if (parse_oid_hex(hash
, &oid
, &end
))
174 return error(_("unexpected non-hex object ID: %s"), hash
);
176 result
= deref_tag(the_repository
, parse_object(the_repository
, &oid
),
179 return error(_("invalid object: %s"), hash
);
180 else if (object_as_type(result
, OBJ_COMMIT
, 1))
181 oidset_insert(commits
, &result
->oid
);
183 display_progress(progress
, oidset_size(commits
));
188 static int write_option_max_new_filters(const struct option
*opt
,
192 int *to
= opt
->value
;
197 *to
= strtol(arg
, (char **)&s
, 10);
199 return error(_("option `%s' expects a numerical value"),
205 static int git_commit_graph_write_config(const char *var
, const char *value
,
206 const struct config_context
*ctx
,
209 if (!strcmp(var
, "commitgraph.maxnewfilters"))
210 write_opts
.max_new_filters
= git_config_int(var
, value
, ctx
->kvi
);
212 * No need to fall-back to 'git_default_config', since this was already
213 * called in 'cmd_commit_graph()'.
218 static int graph_write(int argc
, const char **argv
, const char *prefix
,
219 struct repository
*repo UNUSED
)
221 struct string_list pack_indexes
= STRING_LIST_INIT_DUP
;
222 struct strbuf buf
= STRBUF_INIT
;
223 struct oidset commits
= OIDSET_INIT
;
224 struct object_directory
*odb
= NULL
;
226 enum commit_graph_write_flags flags
= 0;
227 struct progress
*progress
= NULL
;
229 static struct option builtin_commit_graph_write_options
[] = {
230 OPT_BOOL(0, "reachable", &opts
.reachable
,
231 N_("start walk at all refs")),
232 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
233 N_("scan pack-indexes listed by stdin for commits")),
234 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
235 N_("start walk at commits listed by stdin")),
236 OPT_BOOL(0, "append", &opts
.append
,
237 N_("include all commits already in the commit-graph file")),
238 OPT_BOOL(0, "changed-paths", &opts
.enable_changed_paths
,
239 N_("enable computation for changed paths")),
240 OPT_CALLBACK_F(0, "split", &write_opts
.split_flags
, NULL
,
241 N_("allow writing an incremental commit-graph file"),
242 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
243 write_option_parse_split
),
244 OPT_INTEGER(0, "max-commits", &write_opts
.max_commits
,
245 N_("maximum number of commits in a non-base split commit-graph")),
246 OPT_INTEGER(0, "size-multiple", &write_opts
.size_multiple
,
247 N_("maximum ratio between two levels of a split commit-graph")),
248 OPT_EXPIRY_DATE(0, "expire-time", &write_opts
.expire_time
,
249 N_("only expire files older than a given date-time")),
250 OPT_CALLBACK_F(0, "max-new-filters", &write_opts
.max_new_filters
,
251 NULL
, N_("maximum number of changed-path Bloom filters to compute"),
252 0, write_option_max_new_filters
),
253 OPT_BOOL(0, "progress", &opts
.progress
,
254 N_("force progress reporting")),
257 struct option
*options
= add_common_options(builtin_commit_graph_write_options
);
259 opts
.progress
= isatty(2);
260 opts
.enable_changed_paths
= -1;
261 write_opts
.size_multiple
= 2;
262 write_opts
.max_commits
= 0;
263 write_opts
.expire_time
= 0;
264 write_opts
.max_new_filters
= -1;
266 trace2_cmd_mode("write");
268 git_config(git_commit_graph_write_config
, &opts
);
270 argc
= parse_options(argc
, argv
, prefix
,
272 builtin_commit_graph_write_usage
, 0);
274 usage_with_options(builtin_commit_graph_write_usage
, options
);
276 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
277 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
279 opts
.obj_dir
= repo_get_object_directory(the_repository
);
281 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
283 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
285 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
286 if (!opts
.enable_changed_paths
)
287 flags
|= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
;
288 if (opts
.enable_changed_paths
== 1 ||
289 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
290 flags
|= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
292 odb
= find_odb(the_repository
, opts
.obj_dir
);
294 if (opts
.reachable
) {
295 if (write_commit_graph_reachable(odb
, flags
, &write_opts
))
300 if (opts
.stdin_packs
) {
301 while (strbuf_getline(&buf
, stdin
) != EOF
)
302 string_list_append_nodup(&pack_indexes
,
303 strbuf_detach(&buf
, NULL
));
304 } else if (opts
.stdin_commits
) {
305 oidset_init(&commits
, 0);
307 progress
= start_delayed_progress(
309 _("Collecting commits from input"), 0);
311 while (strbuf_getline(&buf
, stdin
) != EOF
) {
312 if (read_one_commit(&commits
, progress
, buf
.buf
)) {
314 stop_progress(&progress
);
319 stop_progress(&progress
);
322 if (write_commit_graph(odb
,
323 opts
.stdin_packs
? &pack_indexes
: NULL
,
324 opts
.stdin_commits
? &commits
: NULL
,
330 FREE_AND_NULL(options
);
331 string_list_clear(&pack_indexes
, 0);
332 strbuf_release(&buf
);
333 oidset_clear(&commits
);
337 int cmd_commit_graph(int argc
,
340 struct repository
*repo
)
342 parse_opt_subcommand_fn
*fn
= NULL
;
343 struct option builtin_commit_graph_options
[] = {
344 OPT_SUBCOMMAND("verify", &fn
, graph_verify
),
345 OPT_SUBCOMMAND("write", &fn
, graph_write
),
348 struct option
*options
= parse_options_concat(builtin_commit_graph_options
, common_opts
);
350 git_config(git_default_config
, NULL
);
352 disable_replace_refs();
353 save_commit_buffer
= 0;
355 argc
= parse_options(argc
, argv
, prefix
, options
,
356 builtin_commit_graph_usage
, 0);
357 FREE_AND_NULL(options
);
359 return fn(argc
, argv
, prefix
, repo
);