]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-graph.c
commit-graph: don't early exit(1) on e.g. "git status"
[thirdparty/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>]"),
59fb8770 13 N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--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[] = {
59fb8770 28 N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
4ce58ee3
DS
29 NULL
30};
31
32static struct opts_commit_graph {
33 const char *obj_dir;
59fb8770 34 int reachable;
049d51a2 35 int stdin_packs;
3d5df01b 36 int stdin_commits;
7547b95b 37 int append;
4ce58ee3
DS
38} opts;
39
283e68c7
DS
40
41static int graph_verify(int argc, const char **argv)
42{
43 struct commit_graph *graph = NULL;
44 char *graph_name;
61df89c8
ÆAB
45 int open_ok;
46 int fd;
47 struct stat st;
283e68c7
DS
48
49 static struct option builtin_commit_graph_verify_options[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
51 N_("dir"),
52 N_("The object directory to store the graph")),
53 OPT_END(),
54 };
55
56 argc = parse_options(argc, argv, NULL,
57 builtin_commit_graph_verify_options,
58 builtin_commit_graph_verify_usage, 0);
59
60 if (!opts.obj_dir)
61 opts.obj_dir = get_object_directory();
62
63 graph_name = get_commit_graph_filename(opts.obj_dir);
61df89c8
ÆAB
64 open_ok = open_commit_graph(graph_name, &fd, &st);
65 if (!open_ok)
66 return 0;
67 graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
283e68c7
DS
68 FREE_AND_NULL(graph_name);
69
70 if (!graph)
61df89c8 71 return 1;
283e68c7 72
0bfb48e6 73 UNLEAK(graph);
283e68c7
DS
74 return verify_commit_graph(the_repository, graph);
75}
76
2a2e32bd
DS
77static int graph_read(int argc, const char **argv)
78{
79 struct commit_graph *graph = NULL;
80 char *graph_name;
61df89c8
ÆAB
81 int open_ok;
82 int fd;
83 struct stat st;
2a2e32bd
DS
84
85 static struct option builtin_commit_graph_read_options[] = {
86 OPT_STRING(0, "object-dir", &opts.obj_dir,
87 N_("dir"),
88 N_("The object directory to store the graph")),
89 OPT_END(),
90 };
91
92 argc = parse_options(argc, argv, NULL,
93 builtin_commit_graph_read_options,
94 builtin_commit_graph_read_usage, 0);
95
96 if (!opts.obj_dir)
97 opts.obj_dir = get_object_directory();
98
99 graph_name = get_commit_graph_filename(opts.obj_dir);
2a2e32bd 100
61df89c8
ÆAB
101 open_ok = open_commit_graph(graph_name, &fd, &st);
102 if (!open_ok)
103 die_errno(_("Could not open commit-graph '%s'"), graph_name);
104
105 graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
0bfb48e6 106 if (!graph)
61df89c8 107 return 1;
883e5c7f 108
2a2e32bd
DS
109 FREE_AND_NULL(graph_name);
110
111 printf("header: %08x %d %d %d %d\n",
112 ntohl(*(uint32_t*)graph->data),
113 *(unsigned char*)(graph->data + 4),
114 *(unsigned char*)(graph->data + 5),
115 *(unsigned char*)(graph->data + 6),
116 *(unsigned char*)(graph->data + 7));
117 printf("num_commits: %u\n", graph->num_commits);
118 printf("chunks:");
119
120 if (graph->chunk_oid_fanout)
121 printf(" oid_fanout");
122 if (graph->chunk_oid_lookup)
123 printf(" oid_lookup");
124 if (graph->chunk_commit_data)
125 printf(" commit_metadata");
5af7417b
SG
126 if (graph->chunk_extra_edges)
127 printf(" extra_edges");
2a2e32bd
DS
128 printf("\n");
129
0bfb48e6 130 UNLEAK(graph);
c3756d5b 131
2a2e32bd
DS
132 return 0;
133}
134
d6538246
DS
135extern int read_replace_refs;
136
f237c8b6
DS
137static int graph_write(int argc, const char **argv)
138{
d88b14b3
DS
139 struct string_list *pack_indexes = NULL;
140 struct string_list *commit_hex = NULL;
141 struct string_list lines;
049d51a2 142
f237c8b6
DS
143 static struct option builtin_commit_graph_write_options[] = {
144 OPT_STRING(0, "object-dir", &opts.obj_dir,
145 N_("dir"),
146 N_("The object directory to store the graph")),
59fb8770
DS
147 OPT_BOOL(0, "reachable", &opts.reachable,
148 N_("start walk at all refs")),
049d51a2
DS
149 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
150 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
151 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
152 N_("start walk at commits listed by stdin")),
7547b95b
DS
153 OPT_BOOL(0, "append", &opts.append,
154 N_("include all commits already in the commit-graph file")),
f237c8b6
DS
155 OPT_END(),
156 };
157
158 argc = parse_options(argc, argv, NULL,
159 builtin_commit_graph_write_options,
160 builtin_commit_graph_write_usage, 0);
161
59fb8770
DS
162 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
163 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
f237c8b6
DS
164 if (!opts.obj_dir)
165 opts.obj_dir = get_object_directory();
166
d6538246
DS
167 read_replace_refs = 0;
168
59fb8770 169 if (opts.reachable) {
7b0f2292 170 write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
59fb8770
DS
171 return 0;
172 }
173
d88b14b3 174 string_list_init(&lines, 0);
3d5df01b 175 if (opts.stdin_packs || opts.stdin_commits) {
049d51a2 176 struct strbuf buf = STRBUF_INIT;
d88b14b3
DS
177
178 while (strbuf_getline(&buf, stdin) != EOF)
179 string_list_append(&lines, strbuf_detach(&buf, NULL));
180
181 if (opts.stdin_packs)
182 pack_indexes = &lines;
183 if (opts.stdin_commits)
184 commit_hex = &lines;
0bfb48e6
185
186 UNLEAK(buf);
049d51a2
DS
187 }
188
189 write_commit_graph(opts.obj_dir,
190 pack_indexes,
3d5df01b 191 commit_hex,
7b0f2292
ÆAB
192 opts.append,
193 1);
049d51a2 194
0bfb48e6 195 UNLEAK(lines);
f237c8b6
DS
196 return 0;
197}
4ce58ee3
DS
198
199int cmd_commit_graph(int argc, const char **argv, const char *prefix)
200{
201 static struct option builtin_commit_graph_options[] = {
202 OPT_STRING(0, "object-dir", &opts.obj_dir,
203 N_("dir"),
204 N_("The object directory to store the graph")),
205 OPT_END(),
206 };
207
208 if (argc == 2 && !strcmp(argv[1], "-h"))
209 usage_with_options(builtin_commit_graph_usage,
210 builtin_commit_graph_options);
211
212 git_config(git_default_config, NULL);
213 argc = parse_options(argc, argv, prefix,
214 builtin_commit_graph_options,
215 builtin_commit_graph_usage,
216 PARSE_OPT_STOP_AT_NON_OPTION);
217
f237c8b6 218 if (argc > 0) {
2a2e32bd
DS
219 if (!strcmp(argv[0], "read"))
220 return graph_read(argc, argv);
283e68c7
DS
221 if (!strcmp(argv[0], "verify"))
222 return graph_verify(argc, argv);
f237c8b6
DS
223 if (!strcmp(argv[0], "write"))
224 return graph_write(argc, argv);
225 }
226
4ce58ee3
DS
227 usage_with_options(builtin_commit_graph_usage,
228 builtin_commit_graph_options);
229}