4 #include "environment.h"
8 #include "parse-options.h"
9 #include "repository.h"
10 #include "commit-graph.h"
11 #include "object-store.h"
13 #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 * builtin_commit_graph_verify_usage
[] = {
26 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
30 static const char * 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
)
67 struct commit_graph
*graph
= NULL
;
68 struct object_directory
*odb
= NULL
;
76 static struct option builtin_commit_graph_verify_options
[] = {
77 OPT_BOOL(0, "shallow", &opts
.shallow
,
78 N_("if the commit-graph is split, only verify the tip file")),
79 OPT_BOOL(0, "progress", &opts
.progress
,
80 N_("force progress reporting")),
83 struct option
*options
= add_common_options(builtin_commit_graph_verify_options
);
85 trace2_cmd_mode("verify");
87 opts
.progress
= isatty(2);
88 argc
= parse_options(argc
, argv
, prefix
,
90 builtin_commit_graph_verify_usage
, 0);
92 usage_with_options(builtin_commit_graph_verify_usage
, options
);
95 opts
.obj_dir
= get_object_directory();
97 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
99 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
101 odb
= find_odb(the_repository
, opts
.obj_dir
);
102 graph_name
= get_commit_graph_filename(odb
);
103 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
104 if (!open_ok
&& errno
!= ENOENT
)
105 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
107 FREE_AND_NULL(graph_name
);
108 FREE_AND_NULL(options
);
111 graph
= load_commit_graph_one_fd_st(the_repository
, fd
, &st
, odb
);
113 graph
= read_commit_graph_one(the_repository
, odb
);
115 /* Return failure if open_ok predicted success */
119 ret
= verify_commit_graph(the_repository
, graph
, flags
);
120 free_commit_graph(graph
);
124 extern int read_replace_refs
;
125 static struct commit_graph_opts write_opts
;
127 static int write_option_parse_split(const struct option
*opt
, const char *arg
,
130 enum commit_graph_split_flags
*flags
= opt
->value
;
132 BUG_ON_OPT_NEG(unset
);
138 if (!strcmp(arg
, "no-merge"))
139 *flags
= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
;
140 else if (!strcmp(arg
, "replace"))
141 *flags
= COMMIT_GRAPH_SPLIT_REPLACE
;
143 die(_("unrecognized --split argument, %s"), arg
);
148 static int read_one_commit(struct oidset
*commits
, struct progress
*progress
,
151 struct object
*result
;
152 struct object_id oid
;
155 if (parse_oid_hex(hash
, &oid
, &end
))
156 return error(_("unexpected non-hex object ID: %s"), hash
);
158 result
= deref_tag(the_repository
, parse_object(the_repository
, &oid
),
161 return error(_("invalid object: %s"), hash
);
162 else if (object_as_type(result
, OBJ_COMMIT
, 1))
163 oidset_insert(commits
, &result
->oid
);
165 display_progress(progress
, oidset_size(commits
));
170 static int write_option_max_new_filters(const struct option
*opt
,
174 int *to
= opt
->value
;
179 *to
= strtol(arg
, (char **)&s
, 10);
181 return error(_("option `%s' expects a numerical value"),
187 static int git_commit_graph_write_config(const char *var
, const char *value
,
190 if (!strcmp(var
, "commitgraph.maxnewfilters"))
191 write_opts
.max_new_filters
= git_config_int(var
, value
);
193 * No need to fall-back to 'git_default_config', since this was already
194 * called in 'cmd_commit_graph()'.
199 static int graph_write(int argc
, const char **argv
, const char *prefix
)
201 struct string_list pack_indexes
= STRING_LIST_INIT_DUP
;
202 struct strbuf buf
= STRBUF_INIT
;
203 struct oidset commits
= OIDSET_INIT
;
204 struct object_directory
*odb
= NULL
;
206 enum commit_graph_write_flags flags
= 0;
207 struct progress
*progress
= NULL
;
209 static struct option builtin_commit_graph_write_options
[] = {
210 OPT_BOOL(0, "reachable", &opts
.reachable
,
211 N_("start walk at all refs")),
212 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
213 N_("scan pack-indexes listed by stdin for commits")),
214 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
215 N_("start walk at commits listed by stdin")),
216 OPT_BOOL(0, "append", &opts
.append
,
217 N_("include all commits already in the commit-graph file")),
218 OPT_BOOL(0, "changed-paths", &opts
.enable_changed_paths
,
219 N_("enable computation for changed paths")),
220 OPT_CALLBACK_F(0, "split", &write_opts
.split_flags
, NULL
,
221 N_("allow writing an incremental commit-graph file"),
222 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
223 write_option_parse_split
),
224 OPT_INTEGER(0, "max-commits", &write_opts
.max_commits
,
225 N_("maximum number of commits in a non-base split commit-graph")),
226 OPT_INTEGER(0, "size-multiple", &write_opts
.size_multiple
,
227 N_("maximum ratio between two levels of a split commit-graph")),
228 OPT_EXPIRY_DATE(0, "expire-time", &write_opts
.expire_time
,
229 N_("only expire files older than a given date-time")),
230 OPT_CALLBACK_F(0, "max-new-filters", &write_opts
.max_new_filters
,
231 NULL
, N_("maximum number of changed-path Bloom filters to compute"),
232 0, write_option_max_new_filters
),
233 OPT_BOOL(0, "progress", &opts
.progress
,
234 N_("force progress reporting")),
237 struct option
*options
= add_common_options(builtin_commit_graph_write_options
);
239 opts
.progress
= isatty(2);
240 opts
.enable_changed_paths
= -1;
241 write_opts
.size_multiple
= 2;
242 write_opts
.max_commits
= 0;
243 write_opts
.expire_time
= 0;
244 write_opts
.max_new_filters
= -1;
246 trace2_cmd_mode("write");
248 git_config(git_commit_graph_write_config
, &opts
);
250 argc
= parse_options(argc
, argv
, prefix
,
252 builtin_commit_graph_write_usage
, 0);
254 usage_with_options(builtin_commit_graph_write_usage
, options
);
256 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
257 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
259 opts
.obj_dir
= get_object_directory();
261 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
263 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
265 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
266 if (!opts
.enable_changed_paths
)
267 flags
|= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
;
268 if (opts
.enable_changed_paths
== 1 ||
269 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
270 flags
|= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
272 odb
= find_odb(the_repository
, opts
.obj_dir
);
274 if (opts
.reachable
) {
275 if (write_commit_graph_reachable(odb
, flags
, &write_opts
))
280 if (opts
.stdin_packs
) {
281 while (strbuf_getline(&buf
, stdin
) != EOF
)
282 string_list_append_nodup(&pack_indexes
,
283 strbuf_detach(&buf
, NULL
));
284 } else if (opts
.stdin_commits
) {
285 oidset_init(&commits
, 0);
287 progress
= start_delayed_progress(
288 _("Collecting commits from input"), 0);
290 while (strbuf_getline(&buf
, stdin
) != EOF
) {
291 if (read_one_commit(&commits
, progress
, buf
.buf
)) {
297 stop_progress(&progress
);
300 if (write_commit_graph(odb
,
301 opts
.stdin_packs
? &pack_indexes
: NULL
,
302 opts
.stdin_commits
? &commits
: NULL
,
308 FREE_AND_NULL(options
);
309 string_list_clear(&pack_indexes
, 0);
310 strbuf_release(&buf
);
314 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
316 parse_opt_subcommand_fn
*fn
= NULL
;
317 struct option builtin_commit_graph_options
[] = {
318 OPT_SUBCOMMAND("verify", &fn
, graph_verify
),
319 OPT_SUBCOMMAND("write", &fn
, graph_write
),
322 struct option
*options
= parse_options_concat(builtin_commit_graph_options
, common_opts
);
324 git_config(git_default_config
, NULL
);
326 read_replace_refs
= 0;
327 save_commit_buffer
= 0;
329 argc
= parse_options(argc
, argv
, prefix
, options
,
330 builtin_commit_graph_usage
, 0);
331 FREE_AND_NULL(options
);
333 return fn(argc
, argv
, prefix
);