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>] [--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>] [--stdin-packs|--stdin-commits]"),
25 static struct opts_commit_graph
{
31 static int graph_read(int argc
, const char **argv
)
33 struct commit_graph
*graph
= NULL
;
36 static struct option builtin_commit_graph_read_options
[] = {
37 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
39 N_("The object directory to store the graph")),
43 argc
= parse_options(argc
, argv
, NULL
,
44 builtin_commit_graph_read_options
,
45 builtin_commit_graph_read_usage
, 0);
48 opts
.obj_dir
= get_object_directory();
50 graph_name
= get_commit_graph_filename(opts
.obj_dir
);
51 graph
= load_commit_graph_one(graph_name
);
54 die("graph file %s does not exist", graph_name
);
55 FREE_AND_NULL(graph_name
);
57 printf("header: %08x %d %d %d %d\n",
58 ntohl(*(uint32_t*)graph
->data
),
59 *(unsigned char*)(graph
->data
+ 4),
60 *(unsigned char*)(graph
->data
+ 5),
61 *(unsigned char*)(graph
->data
+ 6),
62 *(unsigned char*)(graph
->data
+ 7));
63 printf("num_commits: %u\n", graph
->num_commits
);
66 if (graph
->chunk_oid_fanout
)
67 printf(" oid_fanout");
68 if (graph
->chunk_oid_lookup
)
69 printf(" oid_lookup");
70 if (graph
->chunk_commit_data
)
71 printf(" commit_metadata");
72 if (graph
->chunk_large_edges
)
73 printf(" large_edges");
79 static int graph_write(int argc
, const char **argv
)
81 const char **pack_indexes
= NULL
;
83 const char **commit_hex
= NULL
;
85 const char **lines
= NULL
;
89 static struct option builtin_commit_graph_write_options
[] = {
90 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
92 N_("The object directory to store the graph")),
93 OPT_BOOL(0, "stdin-packs", &opts
.stdin_packs
,
94 N_("scan pack-indexes listed by stdin for commits")),
95 OPT_BOOL(0, "stdin-commits", &opts
.stdin_commits
,
96 N_("start walk at commits listed by stdin")),
100 argc
= parse_options(argc
, argv
, NULL
,
101 builtin_commit_graph_write_options
,
102 builtin_commit_graph_write_usage
, 0);
104 if (opts
.stdin_packs
&& opts
.stdin_commits
)
105 die(_("cannot use both --stdin-commits and --stdin-packs"));
107 opts
.obj_dir
= get_object_directory();
109 if (opts
.stdin_packs
|| opts
.stdin_commits
) {
110 struct strbuf buf
= STRBUF_INIT
;
113 ALLOC_ARRAY(lines
, lines_alloc
);
115 while (strbuf_getline(&buf
, stdin
) != EOF
) {
116 ALLOC_GROW(lines
, lines_nr
+ 1, lines_alloc
);
117 lines
[lines_nr
++] = strbuf_detach(&buf
, NULL
);
120 if (opts
.stdin_packs
) {
121 pack_indexes
= lines
;
124 if (opts
.stdin_commits
) {
126 commits_nr
= lines_nr
;
130 write_commit_graph(opts
.obj_dir
,
139 int cmd_commit_graph(int argc
, const char **argv
, const char *prefix
)
141 static struct option builtin_commit_graph_options
[] = {
142 OPT_STRING(0, "object-dir", &opts
.obj_dir
,
144 N_("The object directory to store the graph")),
148 if (argc
== 2 && !strcmp(argv
[1], "-h"))
149 usage_with_options(builtin_commit_graph_usage
,
150 builtin_commit_graph_options
);
152 git_config(git_default_config
, NULL
);
153 argc
= parse_options(argc
, argv
, prefix
,
154 builtin_commit_graph_options
,
155 builtin_commit_graph_usage
,
156 PARSE_OPT_STOP_AT_NON_OPTION
);
159 if (!strcmp(argv
[0], "read"))
160 return graph_read(argc
, argv
);
161 if (!strcmp(argv
[0], "write"))
162 return graph_write(argc
, argv
);
165 usage_with_options(builtin_commit_graph_usage
,
166 builtin_commit_graph_options
);