commit-graph: use string-list API for input
[git/git.git] / builtin / commit-graph.c
CommitLineData
4ce58ee3
DS
1#include "builtin.h"
2#include "config.h"
f237c8b6
DS
3#include "dir.h"
4#include "lockfile.h"
4ce58ee3 5#include "parse-options.h"
283e68c7 6#include "repository.h"
f237c8b6 7#include "commit-graph.h"
4ce58ee3
DS
8
9static char const * const builtin_commit_graph_usage[] = {
10 N_("git commit-graph [--object-dir <objdir>]"),
2a2e32bd 11 N_("git commit-graph read [--object-dir <objdir>]"),
283e68c7 12 N_("git commit-graph verify [--object-dir <objdir>]"),
7547b95b 13 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
f237c8b6
DS
14 NULL
15};
16
283e68c7
DS
17static const char * const builtin_commit_graph_verify_usage[] = {
18 N_("git commit-graph verify [--object-dir <objdir>]"),
19 NULL
20};
21
2a2e32bd
DS
22static const char * const builtin_commit_graph_read_usage[] = {
23 N_("git commit-graph read [--object-dir <objdir>]"),
24 NULL
25};
26
f237c8b6 27static const char * const builtin_commit_graph_write_usage[] = {
7547b95b 28 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
4ce58ee3
DS
29 NULL
30};
31
32static struct opts_commit_graph {
33 const char *obj_dir;
049d51a2 34 int stdin_packs;
3d5df01b 35 int stdin_commits;
7547b95b 36 int append;
4ce58ee3
DS
37} opts;
38
283e68c7
DS
39
40static int graph_verify(int argc, const char **argv)
41{
42 struct commit_graph *graph = NULL;
43 char *graph_name;
44
45 static struct option builtin_commit_graph_verify_options[] = {
46 OPT_STRING(0, "object-dir", &opts.obj_dir,
47 N_("dir"),
48 N_("The object directory to store the graph")),
49 OPT_END(),
50 };
51
52 argc = parse_options(argc, argv, NULL,
53 builtin_commit_graph_verify_options,
54 builtin_commit_graph_verify_usage, 0);
55
56 if (!opts.obj_dir)
57 opts.obj_dir = get_object_directory();
58
59 graph_name = get_commit_graph_filename(opts.obj_dir);
60 graph = load_commit_graph_one(graph_name);
61 FREE_AND_NULL(graph_name);
62
63 if (!graph)
64 return 0;
65
66 return verify_commit_graph(the_repository, graph);
67}
68
2a2e32bd
DS
69static int graph_read(int argc, const char **argv)
70{
71 struct commit_graph *graph = NULL;
72 char *graph_name;
73
74 static struct option builtin_commit_graph_read_options[] = {
75 OPT_STRING(0, "object-dir", &opts.obj_dir,
76 N_("dir"),
77 N_("The object directory to store the graph")),
78 OPT_END(),
79 };
80
81 argc = parse_options(argc, argv, NULL,
82 builtin_commit_graph_read_options,
83 builtin_commit_graph_read_usage, 0);
84
85 if (!opts.obj_dir)
86 opts.obj_dir = get_object_directory();
87
88 graph_name = get_commit_graph_filename(opts.obj_dir);
89 graph = load_commit_graph_one(graph_name);
90
883e5c7f
DS
91 if (!graph) {
92 UNLEAK(graph_name);
2a2e32bd 93 die("graph file %s does not exist", graph_name);
883e5c7f
DS
94 }
95
2a2e32bd
DS
96 FREE_AND_NULL(graph_name);
97
98 printf("header: %08x %d %d %d %d\n",
99 ntohl(*(uint32_t*)graph->data),
100 *(unsigned char*)(graph->data + 4),
101 *(unsigned char*)(graph->data + 5),
102 *(unsigned char*)(graph->data + 6),
103 *(unsigned char*)(graph->data + 7));
104 printf("num_commits: %u\n", graph->num_commits);
105 printf("chunks:");
106
107 if (graph->chunk_oid_fanout)
108 printf(" oid_fanout");
109 if (graph->chunk_oid_lookup)
110 printf(" oid_lookup");
111 if (graph->chunk_commit_data)
112 printf(" commit_metadata");
113 if (graph->chunk_large_edges)
114 printf(" large_edges");
115 printf("\n");
116
117 return 0;
118}
119
f237c8b6
DS
120static int graph_write(int argc, const char **argv)
121{
d88b14b3
DS
122 struct string_list *pack_indexes = NULL;
123 struct string_list *commit_hex = NULL;
124 struct string_list lines;
049d51a2 125
f237c8b6
DS
126 static struct option builtin_commit_graph_write_options[] = {
127 OPT_STRING(0, "object-dir", &opts.obj_dir,
128 N_("dir"),
129 N_("The object directory to store the graph")),
049d51a2
DS
130 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
131 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
132 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
133 N_("start walk at commits listed by stdin")),
7547b95b
DS
134 OPT_BOOL(0, "append", &opts.append,
135 N_("include all commits already in the commit-graph file")),
f237c8b6
DS
136 OPT_END(),
137 };
138
139 argc = parse_options(argc, argv, NULL,
140 builtin_commit_graph_write_options,
141 builtin_commit_graph_write_usage, 0);
142
3d5df01b
DS
143 if (opts.stdin_packs && opts.stdin_commits)
144 die(_("cannot use both --stdin-commits and --stdin-packs"));
f237c8b6
DS
145 if (!opts.obj_dir)
146 opts.obj_dir = get_object_directory();
147
d88b14b3 148 string_list_init(&lines, 0);
3d5df01b 149 if (opts.stdin_packs || opts.stdin_commits) {
049d51a2 150 struct strbuf buf = STRBUF_INIT;
d88b14b3
DS
151
152 while (strbuf_getline(&buf, stdin) != EOF)
153 string_list_append(&lines, strbuf_detach(&buf, NULL));
154
155 if (opts.stdin_packs)
156 pack_indexes = &lines;
157 if (opts.stdin_commits)
158 commit_hex = &lines;
049d51a2
DS
159 }
160
161 write_commit_graph(opts.obj_dir,
162 pack_indexes,
3d5df01b 163 commit_hex,
7547b95b 164 opts.append);
049d51a2 165
d88b14b3 166 string_list_clear(&lines, 0);
f237c8b6
DS
167 return 0;
168}
4ce58ee3
DS
169
170int cmd_commit_graph(int argc, const char **argv, const char *prefix)
171{
172 static struct option builtin_commit_graph_options[] = {
173 OPT_STRING(0, "object-dir", &opts.obj_dir,
174 N_("dir"),
175 N_("The object directory to store the graph")),
176 OPT_END(),
177 };
178
179 if (argc == 2 && !strcmp(argv[1], "-h"))
180 usage_with_options(builtin_commit_graph_usage,
181 builtin_commit_graph_options);
182
183 git_config(git_default_config, NULL);
184 argc = parse_options(argc, argv, prefix,
185 builtin_commit_graph_options,
186 builtin_commit_graph_usage,
187 PARSE_OPT_STOP_AT_NON_OPTION);
188
f237c8b6 189 if (argc > 0) {
2a2e32bd
DS
190 if (!strcmp(argv[0], "read"))
191 return graph_read(argc, argv);
283e68c7
DS
192 if (!strcmp(argv[0], "verify"))
193 return graph_verify(argc, argv);
f237c8b6
DS
194 if (!strcmp(argv[0], "write"))
195 return graph_write(argc, argv);
196 }
197
4ce58ee3
DS
198 usage_with_options(builtin_commit_graph_usage,
199 builtin_commit_graph_options);
200}