5 #include "parse-options.h"
6 #include "repository.h"
7 #include "commit-graph.h"
8 #include "object-store.h"
10 static char const * const builtin_commit_graph_usage
[] = {
11 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
12 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
16 static const char * const builtin_commit_graph_verify_usage
[] = {
17 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
21 static const char * const builtin_commit_graph_write_usage
[] = {
22 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
26 static struct opts_commit_graph
{
37 static int graph_verify(int argc
, const char **argv
)
39 struct commit_graph
*graph
= NULL
;
46 static struct option builtin_commit_graph_verify_options
[] = {
47 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
49 N_("The object directory to store the graph")),
50 OPT_BOOL(0, "shallow", &opts
.shallow
,
51 N_("if the commit-graph is split, only verify the tip file")),
52 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
56 trace2_cmd_mode("verify");
58 opts
.progress
= isatty(2);
59 argc
= parse_options(argc
, argv
, NULL
,
60 builtin_commit_graph_verify_options
,
61 builtin_commit_graph_verify_usage
, 0);
64 opts
.obj_dir
= get_object_directory();
66 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
68 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
70 graph_name
= get_commit_graph_filename(opts
.obj_dir
);
71 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
72 if (!open_ok
&& errno
!= ENOENT
)
73 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
75 FREE_AND_NULL(graph_name
);
78 graph
= load_commit_graph_one_fd_st(fd
, &st
);
80 graph
= read_commit_graph_one(the_repository
, opts
.obj_dir
);
82 /* Return failure if open_ok predicted success */
87 return verify_commit_graph(the_repository
, graph
, flags
);
90 extern int read_replace_refs
;
91 static struct split_commit_graph_opts split_opts
;
93 static int graph_write(int argc
, const char **argv
)
95 struct string_list
*pack_indexes
= NULL
;
96 struct string_list
*commit_hex
= NULL
;
97 struct string_list lines
;
99 enum commit_graph_write_flags flags
= 0;
101 static struct option builtin_commit_graph_write_options
[] = {
102 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
104 N_("The object directory to store the graph")),
105 OPT_BOOL(0, "reachable", &opts
.reachable
,
106 N_("start walk at all refs")),
107 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
108 N_("scan pack-indexes listed by stdin for commits")),
109 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
110 N_("start walk at commits listed by stdin")),
111 OPT_BOOL(0, "append", &opts
.append
,
112 N_("include all commits already in the commit-graph file")),
113 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
114 OPT_BOOL(0, "split", &opts
.split
,
115 N_("allow writing an incremental commit-graph file")),
116 OPT_INTEGER(0, "max-commits", &split_opts
.max_commits
,
117 N_("maximum number of commits in a non-base split commit-graph")),
118 OPT_INTEGER(0, "size-multiple", &split_opts
.size_multiple
,
119 N_("maximum ratio between two levels of a split commit-graph")),
120 OPT_EXPIRY_DATE(0, "expire-time", &split_opts
.expire_time
,
121 N_("maximum number of commits in a non-base split commit-graph")),
125 opts
.progress
= isatty(2);
126 split_opts
.size_multiple
= 2;
127 split_opts
.max_commits
= 0;
128 split_opts
.expire_time
= 0;
130 trace2_cmd_mode("write");
132 argc
= parse_options(argc
, argv
, NULL
,
133 builtin_commit_graph_write_options
,
134 builtin_commit_graph_write_usage
, 0);
136 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
137 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
139 opts
.obj_dir
= get_object_directory();
141 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
143 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
145 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
147 read_replace_refs
= 0;
149 if (opts
.reachable
) {
150 if (write_commit_graph_reachable(opts
.obj_dir
, flags
, &split_opts
))
155 string_list_init(&lines
, 0);
156 if (opts
.stdin_packs
|| opts
.stdin_commits
) {
157 struct strbuf buf
= STRBUF_INIT
;
159 while (strbuf_getline(&buf
, stdin
) != EOF
)
160 string_list_append(&lines
, strbuf_detach(&buf
, NULL
));
162 if (opts
.stdin_packs
)
163 pack_indexes
= &lines
;
164 if (opts
.stdin_commits
) {
166 flags
|= COMMIT_GRAPH_WRITE_CHECK_OIDS
;
172 if (write_commit_graph(opts
.obj_dir
,
183 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
185 static struct option builtin_commit_graph_options
[] = {
186 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
188 N_("The object directory to store the graph")),
192 if (argc
== 2 && !strcmp(argv
[1], "-h"))
193 usage_with_options(builtin_commit_graph_usage
,
194 builtin_commit_graph_options
);
196 git_config(git_default_config
, NULL
);
197 argc
= parse_options(argc
, argv
, prefix
,
198 builtin_commit_graph_options
,
199 builtin_commit_graph_usage
,
200 PARSE_OPT_STOP_AT_NON_OPTION
);
202 save_commit_buffer
= 0;
205 if (!strcmp(argv
[0], "verify"))
206 return graph_verify(argc
, argv
);
207 if (!strcmp(argv
[0], "write"))
208 return graph_write(argc
, argv
);
211 usage_with_options(builtin_commit_graph_usage
,
212 builtin_commit_graph_options
);