5 #include "parse-options.h"
6 #include "commit-graph.h"
8 static char const * const builtin_commit_graph_usage
[] = {
9 N_("git commit-graph [--object-dir <objdir>]"),
10 N_("git commit-graph read [--object-dir <objdir>]"),
11 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
15 static const char * const builtin_commit_graph_read_usage
[] = {
16 N_("git commit-graph read [--object-dir <objdir>]"),
20 static const char * const builtin_commit_graph_write_usage
[] = {
21 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
25 static struct opts_commit_graph
{
32 static int graph_read(int argc
, const char **argv
)
34 struct commit_graph
*graph
= NULL
;
37 static struct option builtin_commit_graph_read_options
[] = {
38 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
40 N_("The object directory to store the graph")),
44 argc
= parse_options(argc
, argv
, NULL
,
45 builtin_commit_graph_read_options
,
46 builtin_commit_graph_read_usage
, 0);
49 opts
.obj_dir
= get_object_directory();
51 graph_name
= get_commit_graph_filename(opts
.obj_dir
);
52 graph
= load_commit_graph_one(graph_name
);
55 die("graph file %s does not exist", graph_name
);
56 FREE_AND_NULL(graph_name
);
58 printf("header: %08x %d %d %d %d\n",
59 ntohl(*(uint32_t*)graph
->data
),
60 *(unsigned char*)(graph
->data
+ 4),
61 *(unsigned char*)(graph
->data
+ 5),
62 *(unsigned char*)(graph
->data
+ 6),
63 *(unsigned char*)(graph
->data
+ 7));
64 printf("num_commits: %u\n", graph
->num_commits
);
67 if (graph
->chunk_oid_fanout
)
68 printf(" oid_fanout");
69 if (graph
->chunk_oid_lookup
)
70 printf(" oid_lookup");
71 if (graph
->chunk_commit_data
)
72 printf(" commit_metadata");
73 if (graph
->chunk_large_edges
)
74 printf(" large_edges");
80 static int graph_write(int argc
, const char **argv
)
82 const char **pack_indexes
= NULL
;
84 const char **commit_hex
= NULL
;
86 const char **lines
= NULL
;
90 static struct option builtin_commit_graph_write_options
[] = {
91 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
93 N_("The object directory to store the graph")),
94 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
95 N_("scan pack-indexes listed by stdin for commits")),
96 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
97 N_("start walk at commits listed by stdin")),
98 OPT_BOOL(0, "append", &opts
.append
,
99 N_("include all commits already in the commit-graph file")),
103 argc
= parse_options(argc
, argv
, NULL
,
104 builtin_commit_graph_write_options
,
105 builtin_commit_graph_write_usage
, 0);
107 if (opts
.stdin_packs
&& opts
.stdin_commits
)
108 die(_("cannot use both --stdin-commits and --stdin-packs"));
110 opts
.obj_dir
= get_object_directory();
112 if (opts
.stdin_packs
|| opts
.stdin_commits
) {
113 struct strbuf buf
= STRBUF_INIT
;
116 ALLOC_ARRAY(lines
, lines_alloc
);
118 while (strbuf_getline(&buf
, stdin
) != EOF
) {
119 ALLOC_GROW(lines
, lines_nr
+ 1, lines_alloc
);
120 lines
[lines_nr
++] = strbuf_detach(&buf
, NULL
);
123 if (opts
.stdin_packs
) {
124 pack_indexes
= lines
;
127 if (opts
.stdin_commits
) {
129 commits_nr
= lines_nr
;
133 write_commit_graph(opts
.obj_dir
,
143 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
145 static struct option builtin_commit_graph_options
[] = {
146 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
148 N_("The object directory to store the graph")),
152 if (argc
== 2 && !strcmp(argv
[1], "-h"))
153 usage_with_options(builtin_commit_graph_usage
,
154 builtin_commit_graph_options
);
156 git_config(git_default_config
, NULL
);
157 argc
= parse_options(argc
, argv
, prefix
,
158 builtin_commit_graph_options
,
159 builtin_commit_graph_usage
,
160 PARSE_OPT_STOP_AT_NON_OPTION
);
163 if (!strcmp(argv
[0], "read"))
164 return graph_read(argc
, argv
);
165 if (!strcmp(argv
[0], "write"))
166 return graph_write(argc
, argv
);
169 usage_with_options(builtin_commit_graph_usage
,
170 builtin_commit_graph_options
);