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] "
13 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
14 "[--[no-]progress] <split options>"),
18 static const char * const builtin_commit_graph_verify_usage
[] = {
19 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
23 static const char * const builtin_commit_graph_write_usage
[] = {
24 N_("git commit-graph write [--object-dir <objdir>] [--append] "
25 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
26 "[--[no-]progress] <split options>"),
30 static struct opts_commit_graph
{
41 static struct object_directory
*find_odb(struct repository
*r
,
44 struct object_directory
*odb
;
45 char *obj_dir_real
= real_pathdup(obj_dir
, 1);
48 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
49 if (!strcmp(obj_dir_real
, real_path(odb
->path
)))
56 die(_("could not find object directory matching %s"), obj_dir
);
60 static int graph_verify(int argc
, const char **argv
)
62 struct commit_graph
*graph
= NULL
;
63 struct object_directory
*odb
= NULL
;
70 static struct option builtin_commit_graph_verify_options
[] = {
71 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
73 N_("The object directory to store the graph")),
74 OPT_BOOL(0, "shallow", &opts
.shallow
,
75 N_("if the commit-graph is split, only verify the tip file")),
76 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
80 trace2_cmd_mode("verify");
82 opts
.progress
= isatty(2);
83 argc
= parse_options(argc
, argv
, NULL
,
84 builtin_commit_graph_verify_options
,
85 builtin_commit_graph_verify_usage
, 0);
88 opts
.obj_dir
= get_object_directory();
90 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
92 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
94 odb
= find_odb(the_repository
, opts
.obj_dir
);
95 graph_name
= get_commit_graph_filename(odb
);
96 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
97 if (!open_ok
&& errno
!= ENOENT
)
98 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
100 FREE_AND_NULL(graph_name
);
103 graph
= load_commit_graph_one_fd_st(fd
, &st
, odb
);
105 graph
= read_commit_graph_one(the_repository
, odb
);
107 /* Return failure if open_ok predicted success */
112 return verify_commit_graph(the_repository
, graph
, flags
);
115 extern int read_replace_refs
;
116 static struct split_commit_graph_opts split_opts
;
118 static int write_option_parse_split(const struct option
*opt
, const char *arg
,
121 enum commit_graph_split_flags
*flags
= opt
->value
;
127 if (!strcmp(arg
, "no-merge"))
128 *flags
= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
;
129 else if (!strcmp(arg
, "replace"))
130 *flags
= COMMIT_GRAPH_SPLIT_REPLACE
;
132 die(_("unrecognized --split argument, %s"), arg
);
137 static int graph_write(int argc
, const char **argv
)
139 struct string_list
*pack_indexes
= NULL
;
140 struct oidset commits
= OIDSET_INIT
;
141 struct object_directory
*odb
= NULL
;
142 struct string_list lines
;
144 enum commit_graph_write_flags flags
= 0;
146 static struct option builtin_commit_graph_write_options
[] = {
147 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
149 N_("The object directory to store the graph")),
150 OPT_BOOL(0, "reachable", &opts
.reachable
,
151 N_("start walk at all refs")),
152 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
153 N_("scan pack-indexes listed by stdin for commits")),
154 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
155 N_("start walk at commits listed by stdin")),
156 OPT_BOOL(0, "append", &opts
.append
,
157 N_("include all commits already in the commit-graph file")),
158 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
159 OPT_CALLBACK_F(0, "split", &split_opts
.flags
, NULL
,
160 N_("allow writing an incremental commit-graph file"),
161 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
162 write_option_parse_split
),
163 OPT_INTEGER(0, "max-commits", &split_opts
.max_commits
,
164 N_("maximum number of commits in a non-base split commit-graph")),
165 OPT_INTEGER(0, "size-multiple", &split_opts
.size_multiple
,
166 N_("maximum ratio between two levels of a split commit-graph")),
167 OPT_EXPIRY_DATE(0, "expire-time", &split_opts
.expire_time
,
168 N_("maximum number of commits in a non-base split commit-graph")),
172 opts
.progress
= isatty(2);
173 split_opts
.size_multiple
= 2;
174 split_opts
.max_commits
= 0;
175 split_opts
.expire_time
= 0;
177 trace2_cmd_mode("write");
179 argc
= parse_options(argc
, argv
, NULL
,
180 builtin_commit_graph_write_options
,
181 builtin_commit_graph_write_usage
, 0);
183 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
184 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
186 opts
.obj_dir
= get_object_directory();
188 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
190 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
192 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
194 read_replace_refs
= 0;
195 odb
= find_odb(the_repository
, opts
.obj_dir
);
197 if (opts
.reachable
) {
198 if (write_commit_graph_reachable(odb
, flags
, &split_opts
))
203 string_list_init(&lines
, 0);
204 if (opts
.stdin_packs
|| opts
.stdin_commits
) {
205 struct strbuf buf
= STRBUF_INIT
;
207 while (strbuf_getline(&buf
, stdin
) != EOF
)
208 string_list_append(&lines
, strbuf_detach(&buf
, NULL
));
210 if (opts
.stdin_packs
)
211 pack_indexes
= &lines
;
212 if (opts
.stdin_commits
) {
213 struct string_list_item
*item
;
214 oidset_init(&commits
, lines
.nr
);
215 for_each_string_list_item(item
, &lines
) {
216 struct object_id oid
;
219 if (parse_oid_hex(item
->string
, &oid
, &end
)) {
220 error(_("unexpected non-hex object ID: "
221 "%s"), item
->string
);
225 oidset_insert(&commits
, &oid
);
227 flags
|= COMMIT_GRAPH_WRITE_CHECK_OIDS
;
233 if (write_commit_graph(odb
,
235 opts
.stdin_commits
? &commits
: NULL
,
244 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
246 static struct option builtin_commit_graph_options
[] = {
247 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
249 N_("The object directory to store the graph")),
253 if (argc
== 2 && !strcmp(argv
[1], "-h"))
254 usage_with_options(builtin_commit_graph_usage
,
255 builtin_commit_graph_options
);
257 git_config(git_default_config
, NULL
);
258 argc
= parse_options(argc
, argv
, prefix
,
259 builtin_commit_graph_options
,
260 builtin_commit_graph_usage
,
261 PARSE_OPT_STOP_AT_NON_OPTION
);
263 save_commit_buffer
= 0;
266 if (!strcmp(argv
[0], "verify"))
267 return graph_verify(argc
, argv
);
268 if (!strcmp(argv
[0], "write"))
269 return graph_write(argc
, argv
);
272 usage_with_options(builtin_commit_graph_usage
,
273 builtin_commit_graph_options
);