7 #include "parse-options.h"
8 #include "repository.h"
9 #include "commit-graph.h"
10 #include "object-store.h"
12 #include "replace-object.h"
15 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
16 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
18 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
19 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
20 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
21 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
24 static const char * builtin_commit_graph_verify_usage
[] = {
25 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
29 static const char * builtin_commit_graph_write_usage
[] = {
30 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
34 static char const * const builtin_commit_graph_usage
[] = {
35 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE
,
36 BUILTIN_COMMIT_GRAPH_WRITE_USAGE
,
40 static struct opts_commit_graph
{
49 int enable_changed_paths
;
52 static struct option common_opts
[] = {
53 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
55 N_("the object directory to store the graph")),
59 static struct option
*add_common_options(struct option
*to
)
61 return parse_options_concat(common_opts
, to
);
64 static int graph_verify(int argc
, const char **argv
, const char *prefix
)
66 struct commit_graph
*graph
= NULL
;
67 struct object_directory
*odb
= NULL
;
75 static struct option builtin_commit_graph_verify_options
[] = {
76 OPT_BOOL(0, "shallow", &opts
.shallow
,
77 N_("if the commit-graph is split, only verify the tip file")),
78 OPT_BOOL(0, "progress", &opts
.progress
,
79 N_("force progress reporting")),
82 struct option
*options
= add_common_options(builtin_commit_graph_verify_options
);
84 trace2_cmd_mode("verify");
86 opts
.progress
= isatty(2);
87 argc
= parse_options(argc
, argv
, prefix
,
89 builtin_commit_graph_verify_usage
, 0);
91 usage_with_options(builtin_commit_graph_verify_usage
, options
);
94 opts
.obj_dir
= get_object_directory();
96 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
98 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
100 odb
= find_odb(the_repository
, opts
.obj_dir
);
101 graph_name
= get_commit_graph_filename(odb
);
102 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
103 if (!open_ok
&& errno
!= ENOENT
)
104 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
106 FREE_AND_NULL(graph_name
);
107 FREE_AND_NULL(options
);
110 graph
= load_commit_graph_one_fd_st(the_repository
, fd
, &st
, odb
);
112 graph
= read_commit_graph_one(the_repository
, odb
);
114 /* Return failure if open_ok predicted success */
118 ret
= verify_commit_graph(the_repository
, graph
, flags
);
119 free_commit_graph(graph
);
123 extern int read_replace_refs
;
124 static struct commit_graph_opts write_opts
;
126 static int write_option_parse_split(const struct option
*opt
, const char *arg
,
129 enum commit_graph_split_flags
*flags
= opt
->value
;
131 BUG_ON_OPT_NEG(unset
);
137 if (!strcmp(arg
, "no-merge"))
138 *flags
= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
;
139 else if (!strcmp(arg
, "replace"))
140 *flags
= COMMIT_GRAPH_SPLIT_REPLACE
;
142 die(_("unrecognized --split argument, %s"), arg
);
147 static int read_one_commit(struct oidset
*commits
, struct progress
*progress
,
150 struct object
*result
;
151 struct object_id oid
;
154 if (parse_oid_hex(hash
, &oid
, &end
))
155 return error(_("unexpected non-hex object ID: %s"), hash
);
157 result
= deref_tag(the_repository
, parse_object(the_repository
, &oid
),
160 return error(_("invalid object: %s"), hash
);
161 else if (object_as_type(result
, OBJ_COMMIT
, 1))
162 oidset_insert(commits
, &result
->oid
);
164 display_progress(progress
, oidset_size(commits
));
169 static int write_option_max_new_filters(const struct option
*opt
,
173 int *to
= opt
->value
;
178 *to
= strtol(arg
, (char **)&s
, 10);
180 return error(_("option `%s' expects a numerical value"),
186 static int git_commit_graph_write_config(const char *var
, const char *value
,
189 if (!strcmp(var
, "commitgraph.maxnewfilters"))
190 write_opts
.max_new_filters
= git_config_int(var
, value
);
192 * No need to fall-back to 'git_default_config', since this was already
193 * called in 'cmd_commit_graph()'.
198 static int graph_write(int argc
, const char **argv
, const char *prefix
)
200 struct string_list pack_indexes
= STRING_LIST_INIT_DUP
;
201 struct strbuf buf
= STRBUF_INIT
;
202 struct oidset commits
= OIDSET_INIT
;
203 struct object_directory
*odb
= NULL
;
205 enum commit_graph_write_flags flags
= 0;
206 struct progress
*progress
= NULL
;
208 static struct option builtin_commit_graph_write_options
[] = {
209 OPT_BOOL(0, "reachable", &opts
.reachable
,
210 N_("start walk at all refs")),
211 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
212 N_("scan pack-indexes listed by stdin for commits")),
213 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
214 N_("start walk at commits listed by stdin")),
215 OPT_BOOL(0, "append", &opts
.append
,
216 N_("include all commits already in the commit-graph file")),
217 OPT_BOOL(0, "changed-paths", &opts
.enable_changed_paths
,
218 N_("enable computation for changed paths")),
219 OPT_CALLBACK_F(0, "split", &write_opts
.split_flags
, NULL
,
220 N_("allow writing an incremental commit-graph file"),
221 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
222 write_option_parse_split
),
223 OPT_INTEGER(0, "max-commits", &write_opts
.max_commits
,
224 N_("maximum number of commits in a non-base split commit-graph")),
225 OPT_INTEGER(0, "size-multiple", &write_opts
.size_multiple
,
226 N_("maximum ratio between two levels of a split commit-graph")),
227 OPT_EXPIRY_DATE(0, "expire-time", &write_opts
.expire_time
,
228 N_("only expire files older than a given date-time")),
229 OPT_CALLBACK_F(0, "max-new-filters", &write_opts
.max_new_filters
,
230 NULL
, N_("maximum number of changed-path Bloom filters to compute"),
231 0, write_option_max_new_filters
),
232 OPT_BOOL(0, "progress", &opts
.progress
,
233 N_("force progress reporting")),
236 struct option
*options
= add_common_options(builtin_commit_graph_write_options
);
238 opts
.progress
= isatty(2);
239 opts
.enable_changed_paths
= -1;
240 write_opts
.size_multiple
= 2;
241 write_opts
.max_commits
= 0;
242 write_opts
.expire_time
= 0;
243 write_opts
.max_new_filters
= -1;
245 trace2_cmd_mode("write");
247 git_config(git_commit_graph_write_config
, &opts
);
249 argc
= parse_options(argc
, argv
, prefix
,
251 builtin_commit_graph_write_usage
, 0);
253 usage_with_options(builtin_commit_graph_write_usage
, options
);
255 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
256 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
258 opts
.obj_dir
= get_object_directory();
260 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
262 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
264 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
265 if (!opts
.enable_changed_paths
)
266 flags
|= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
;
267 if (opts
.enable_changed_paths
== 1 ||
268 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
269 flags
|= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
271 odb
= find_odb(the_repository
, opts
.obj_dir
);
273 if (opts
.reachable
) {
274 if (write_commit_graph_reachable(odb
, flags
, &write_opts
))
279 if (opts
.stdin_packs
) {
280 while (strbuf_getline(&buf
, stdin
) != EOF
)
281 string_list_append_nodup(&pack_indexes
,
282 strbuf_detach(&buf
, NULL
));
283 } else if (opts
.stdin_commits
) {
284 oidset_init(&commits
, 0);
286 progress
= start_delayed_progress(
287 _("Collecting commits from input"), 0);
289 while (strbuf_getline(&buf
, stdin
) != EOF
) {
290 if (read_one_commit(&commits
, progress
, buf
.buf
)) {
296 stop_progress(&progress
);
299 if (write_commit_graph(odb
,
300 opts
.stdin_packs
? &pack_indexes
: NULL
,
301 opts
.stdin_commits
? &commits
: NULL
,
307 FREE_AND_NULL(options
);
308 string_list_clear(&pack_indexes
, 0);
309 strbuf_release(&buf
);
313 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
315 parse_opt_subcommand_fn
*fn
= NULL
;
316 struct option builtin_commit_graph_options
[] = {
317 OPT_SUBCOMMAND("verify", &fn
, graph_verify
),
318 OPT_SUBCOMMAND("write", &fn
, graph_write
),
321 struct option
*options
= parse_options_concat(builtin_commit_graph_options
, common_opts
);
323 git_config(git_default_config
, NULL
);
325 read_replace_refs
= 0;
326 save_commit_buffer
= 0;
328 argc
= parse_options(argc
, argv
, prefix
, options
,
329 builtin_commit_graph_usage
, 0);
330 FREE_AND_NULL(options
);
332 return fn(argc
, argv
, prefix
);