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 struct object_directory
*find_odb(struct repository
*r
,
40 struct object_directory
*odb
;
41 char *obj_dir_real
= real_pathdup(obj_dir
, 1);
42 struct strbuf odb_path_real
= STRBUF_INIT
;
45 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
46 strbuf_realpath(&odb_path_real
, odb
->path
, 1);
47 if (!strcmp(obj_dir_real
, odb_path_real
.buf
))
52 strbuf_release(&odb_path_real
);
55 die(_("could not find object directory matching %s"), obj_dir
);
59 static int graph_verify(int argc
, const char **argv
)
61 struct commit_graph
*graph
= NULL
;
62 struct object_directory
*odb
= NULL
;
69 static struct option builtin_commit_graph_verify_options
[] = {
70 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
72 N_("The object directory to store the graph")),
73 OPT_BOOL(0, "shallow", &opts
.shallow
,
74 N_("if the commit-graph is split, only verify the tip file")),
75 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
79 trace2_cmd_mode("verify");
81 opts
.progress
= isatty(2);
82 argc
= parse_options(argc
, argv
, NULL
,
83 builtin_commit_graph_verify_options
,
84 builtin_commit_graph_verify_usage
, 0);
87 opts
.obj_dir
= get_object_directory();
89 flags
|= COMMIT_GRAPH_VERIFY_SHALLOW
;
91 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
93 odb
= find_odb(the_repository
, opts
.obj_dir
);
94 graph_name
= get_commit_graph_filename(odb
);
95 open_ok
= open_commit_graph(graph_name
, &fd
, &st
);
96 if (!open_ok
&& errno
!= ENOENT
)
97 die_errno(_("Could not open commit-graph '%s'"), graph_name
);
99 FREE_AND_NULL(graph_name
);
102 graph
= load_commit_graph_one_fd_st(fd
, &st
, odb
);
104 graph
= read_commit_graph_one(the_repository
, odb
);
106 /* Return failure if open_ok predicted success */
111 return verify_commit_graph(the_repository
, graph
, flags
);
114 extern int read_replace_refs
;
115 static struct split_commit_graph_opts split_opts
;
117 static int graph_write(int argc
, const char **argv
)
119 struct string_list
*pack_indexes
= NULL
;
120 struct string_list
*commit_hex
= NULL
;
121 struct object_directory
*odb
= NULL
;
122 struct string_list lines
;
124 enum commit_graph_write_flags flags
= 0;
126 static struct option builtin_commit_graph_write_options
[] = {
127 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
129 N_("The object directory to store the graph")),
130 OPT_BOOL(0, "reachable", &opts
.reachable
,
131 N_("start walk at all refs")),
132 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
133 N_("scan pack-indexes listed by stdin for commits")),
134 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
135 N_("start walk at commits listed by stdin")),
136 OPT_BOOL(0, "append", &opts
.append
,
137 N_("include all commits already in the commit-graph file")),
138 OPT_BOOL(0, "progress", &opts
.progress
, N_("force progress reporting")),
139 OPT_BOOL(0, "split", &opts
.split
,
140 N_("allow writing an incremental commit-graph file")),
141 OPT_INTEGER(0, "max-commits", &split_opts
.max_commits
,
142 N_("maximum number of commits in a non-base split commit-graph")),
143 OPT_INTEGER(0, "size-multiple", &split_opts
.size_multiple
,
144 N_("maximum ratio between two levels of a split commit-graph")),
145 OPT_EXPIRY_DATE(0, "expire-time", &split_opts
.expire_time
,
146 N_("maximum number of commits in a non-base split commit-graph")),
150 opts
.progress
= isatty(2);
151 split_opts
.size_multiple
= 2;
152 split_opts
.max_commits
= 0;
153 split_opts
.expire_time
= 0;
155 trace2_cmd_mode("write");
157 argc
= parse_options(argc
, argv
, NULL
,
158 builtin_commit_graph_write_options
,
159 builtin_commit_graph_write_usage
, 0);
161 if (opts
.reachable
+ opts
.stdin_packs
+ opts
.stdin_commits
> 1)
162 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
164 opts
.obj_dir
= get_object_directory();
166 flags
|= COMMIT_GRAPH_WRITE_APPEND
;
168 flags
|= COMMIT_GRAPH_WRITE_SPLIT
;
170 flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
172 read_replace_refs
= 0;
173 odb
= find_odb(the_repository
, opts
.obj_dir
);
175 if (opts
.reachable
) {
176 if (write_commit_graph_reachable(odb
, flags
, &split_opts
))
181 string_list_init(&lines
, 0);
182 if (opts
.stdin_packs
|| opts
.stdin_commits
) {
183 struct strbuf buf
= STRBUF_INIT
;
185 while (strbuf_getline(&buf
, stdin
) != EOF
)
186 string_list_append(&lines
, strbuf_detach(&buf
, NULL
));
188 if (opts
.stdin_packs
)
189 pack_indexes
= &lines
;
190 if (opts
.stdin_commits
) {
192 flags
|= COMMIT_GRAPH_WRITE_CHECK_OIDS
;
198 if (write_commit_graph(odb
,
209 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
211 static struct option builtin_commit_graph_options
[] = {
212 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
214 N_("The object directory to store the graph")),
218 if (argc
== 2 && !strcmp(argv
[1], "-h"))
219 usage_with_options(builtin_commit_graph_usage
,
220 builtin_commit_graph_options
);
222 git_config(git_default_config
, NULL
);
223 argc
= parse_options(argc
, argv
, prefix
,
224 builtin_commit_graph_options
,
225 builtin_commit_graph_usage
,
226 PARSE_OPT_STOP_AT_NON_OPTION
);
228 save_commit_buffer
= 0;
231 if (!strcmp(argv
[0], "verify"))
232 return graph_verify(argc
, argv
);
233 if (!strcmp(argv
[0], "write"))
234 return graph_write(argc
, argv
);
237 usage_with_options(builtin_commit_graph_usage
,
238 builtin_commit_graph_options
);