5 #include "environment.h"
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "commit-graph.h"
12 #include "object-store-ll.h"
14 #include "replace-object.h"
18 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
19 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
21 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
22 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
23 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
24 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
27 static const char * builtin_commit_graph_verify_usage
[] = {
28 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
32 static const char * builtin_commit_graph_write_usage
[] = {
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
37 static char const * const builtin_commit_graph_usage
[] = {
38 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
39 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
43 static struct opts_commit_graph
{
52 int enable_changed_paths
;
55 static struct option common_opts
[] = {
56 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
58 N_("the object directory to store the graph")),
62 static struct option
*add_common_options(struct option
*to
)
64 return parse_options_concat(common_opts
, to
);
67 static int graph_verify(int argc
, const char **argv
, const char *prefix
)
69 struct commit_graph
*graph
= NULL
;
70 struct object_directory
*odb
= NULL
;
78 static struct option builtin_commit_graph_verify_options
[] = {
79 OPT_BOOL(0, "shallow", &opts
.shallow
,
80 N_("if the commit-graph is split, only verify the tip file")),
81 OPT_BOOL(0, "progress", &opts
.progress
,
82 N_("force progress reporting")),
85 struct option
*options
= add_common_options(builtin_commit_graph_verify_options
);
87 trace2_cmd_mode("verify");
89 opts
.progress
= isatty(2);
90 argc
= parse_options(argc
, argv
, prefix
,
92 builtin_commit_graph_verify_usage
, 0);
94 usage_with_options(builtin_commit_graph_verify_usage
, options
);
97 opts
.obj_dir
= get_object_directory();
99 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
101 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
103 odb
= find_odb(the_repository
, opts
.obj_dir
);
104 graph_name
= get_commit_graph_filename(odb
);
105 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
106 if (!open_ok
&& errno
!= ENOENT
)
107 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
109 FREE_AND_NULL(graph_name
);
110 FREE_AND_NULL(options
);
113 graph
= load_commit_graph_one_fd_st(the_repository
, fd
, &st
, odb
);
115 graph
= read_commit_graph_one(the_repository
, odb
);
117 /* Return failure if open_ok predicted success */
121 ret
= verify_commit_graph(the_repository
, graph
, flags
);
122 free_commit_graph(graph
);
126 extern int read_replace_refs
;
127 static struct commit_graph_opts write_opts
;
129 static int write_option_parse_split(const struct option
*opt
, const char *arg
,
132 enum commit_graph_split_flags
*flags
= opt
->value
;
134 BUG_ON_OPT_NEG(unset
);
140 if (!strcmp(arg
, "no-merge"))
141 *flags
= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
;
142 else if (!strcmp(arg
, "replace"))
143 *flags
= COMMIT_GRAPH_SPLIT_REPLACE
;
145 die(_("unrecognized --split argument, %s"), arg
);
150 static int read_one_commit(struct oidset
*commits
, struct progress
*progress
,
153 struct object
*result
;
154 struct object_id oid
;
157 if (parse_oid_hex(hash
, &oid
, &end
))
158 return error(_("unexpected non-hex object ID: %s"), hash
);
160 result
= deref_tag(the_repository
, parse_object(the_repository
, &oid
),
163 return error(_("invalid object: %s"), hash
);
164 else if (object_as_type(result
, OBJ_COMMIT
, 1))
165 oidset_insert(commits
, &result
->oid
);
167 display_progress(progress
, oidset_size(commits
));
172 static int write_option_max_new_filters(const struct option
*opt
,
176 int *to
= opt
->value
;
181 *to
= strtol(arg
, (char **)&s
, 10);
183 return error(_("option `%s' expects a numerical value"),
189 static int git_commit_graph_write_config(const char *var
, const char *value
,
190 const struct config_context
*ctx
,
193 if (!strcmp(var
, "commitgraph.maxnewfilters"))
194 write_opts
.max_new_filters
= git_config_int(var
, value
, ctx
->kvi
);
196 * No need to fall-back to 'git_default_config', since this was already
197 * called in 'cmd_commit_graph()'.
202 static int graph_write(int argc
, const char **argv
, const char *prefix
)
204 struct string_list pack_indexes
= STRING_LIST_INIT_DUP
;
205 struct strbuf buf
= STRBUF_INIT
;
206 struct oidset commits
= OIDSET_INIT
;
207 struct object_directory
*odb
= NULL
;
209 enum commit_graph_write_flags flags
= 0;
210 struct progress
*progress
= NULL
;
212 static struct option builtin_commit_graph_write_options
[] = {
213 OPT_BOOL(0, "reachable", &opts
.reachable
,
214 N_("start walk at all refs")),
215 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
216 N_("scan pack-indexes listed by stdin for commits")),
217 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
218 N_("start walk at commits listed by stdin")),
219 OPT_BOOL(0, "append", &opts
.append
,
220 N_("include all commits already in the commit-graph file")),
221 OPT_BOOL(0, "changed-paths", &opts
.enable_changed_paths
,
222 N_("enable computation for changed paths")),
223 OPT_CALLBACK_F(0, "split", &write_opts
.split_flags
, NULL
,
224 N_("allow writing an incremental commit-graph file"),
225 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
226 write_option_parse_split
),
227 OPT_INTEGER(0, "max-commits", &write_opts
.max_commits
,
228 N_("maximum number of commits in a non-base split commit-graph")),
229 OPT_INTEGER(0, "size-multiple", &write_opts
.size_multiple
,
230 N_("maximum ratio between two levels of a split commit-graph")),
231 OPT_EXPIRY_DATE(0, "expire-time", &write_opts
.expire_time
,
232 N_("only expire files older than a given date-time")),
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
),
236 OPT_BOOL(0, "progress", &opts
.progress
,
237 N_("force progress reporting")),
240 struct option
*options
= add_common_options(builtin_commit_graph_write_options
);
242 opts
.progress
= isatty(2);
243 opts
.enable_changed_paths
= -1;
244 write_opts
.size_multiple
= 2;
245 write_opts
.max_commits
= 0;
246 write_opts
.expire_time
= 0;
247 write_opts
.max_new_filters
= -1;
249 trace2_cmd_mode("write");
251 git_config(git_commit_graph_write_config
, &opts
);
253 argc
= parse_options(argc
, argv
, prefix
,
255 builtin_commit_graph_write_usage
, 0);
257 usage_with_options(builtin_commit_graph_write_usage
, options
);
259 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
260 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
262 opts
.obj_dir
= get_object_directory();
264 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
266 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
268 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
269 if (!opts
.enable_changed_paths
)
270 flags
|= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
;
271 if (opts
.enable_changed_paths
== 1 ||
272 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
273 flags
|= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
275 odb
= find_odb(the_repository
, opts
.obj_dir
);
277 if (opts
.reachable
) {
278 if (write_commit_graph_reachable(odb
, flags
, &write_opts
))
283 if (opts
.stdin_packs
) {
284 while (strbuf_getline(&buf
, stdin
) != EOF
)
285 string_list_append_nodup(&pack_indexes
,
286 strbuf_detach(&buf
, NULL
));
287 } else if (opts
.stdin_commits
) {
288 oidset_init(&commits
, 0);
290 progress
= start_delayed_progress(
291 _("Collecting commits from input"), 0);
293 while (strbuf_getline(&buf
, stdin
) != EOF
) {
294 if (read_one_commit(&commits
, progress
, buf
.buf
)) {
300 stop_progress(&progress
);
303 if (write_commit_graph(odb
,
304 opts
.stdin_packs
? &pack_indexes
: NULL
,
305 opts
.stdin_commits
? &commits
: NULL
,
311 FREE_AND_NULL(options
);
312 string_list_clear(&pack_indexes
, 0);
313 strbuf_release(&buf
);
317 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
319 parse_opt_subcommand_fn
*fn
= NULL
;
320 struct option builtin_commit_graph_options
[] = {
321 OPT_SUBCOMMAND("verify", &fn
, graph_verify
),
322 OPT_SUBCOMMAND("write", &fn
, graph_write
),
325 struct option
*options
= parse_options_concat(builtin_commit_graph_options
, common_opts
);
327 git_config(git_default_config
, NULL
);
329 disable_replace_refs();
330 save_commit_buffer
= 0;
332 argc
= parse_options(argc
, argv
, prefix
, options
,
333 builtin_commit_graph_usage
, 0);
334 FREE_AND_NULL(options
);
336 return fn(argc
, argv
, prefix
);