]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-graph.c
commit-graph write: use pack order when finding commits
[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;
45
46 static struct option builtin_commit_graph_verify_options[] = {
47 OPT_STRING(0, "object-dir", &opts.obj_dir,
48 N_("dir"),
49 N_("The object directory to store the graph")),
50 OPT_END(),
51 };
52
53 argc = parse_options(argc, argv, NULL,
54 builtin_commit_graph_verify_options,
55 builtin_commit_graph_verify_usage, 0);
56
57 if (!opts.obj_dir)
58 opts.obj_dir = get_object_directory();
59
60 graph_name = get_commit_graph_filename(opts.obj_dir);
61 graph = load_commit_graph_one(graph_name);
62 FREE_AND_NULL(graph_name);
63
64 if (!graph)
65 return 0;
66
0bfb48e6 67 UNLEAK(graph);
283e68c7
DS
68 return verify_commit_graph(the_repository, graph);
69}
70
2a2e32bd
DS
71static int graph_read(int argc, const char **argv)
72{
73 struct commit_graph *graph = NULL;
74 char *graph_name;
75
76 static struct option builtin_commit_graph_read_options[] = {
77 OPT_STRING(0, "object-dir", &opts.obj_dir,
78 N_("dir"),
79 N_("The object directory to store the graph")),
80 OPT_END(),
81 };
82
83 argc = parse_options(argc, argv, NULL,
84 builtin_commit_graph_read_options,
85 builtin_commit_graph_read_usage, 0);
86
87 if (!opts.obj_dir)
88 opts.obj_dir = get_object_directory();
89
90 graph_name = get_commit_graph_filename(opts.obj_dir);
91 graph = load_commit_graph_one(graph_name);
92
0bfb48e6 93 if (!graph)
2a2e32bd 94 die("graph file %s does not exist", graph_name);
883e5c7f 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
0bfb48e6 117 UNLEAK(graph);
c3756d5b 118
2a2e32bd
DS
119 return 0;
120}
121
d6538246
DS
122extern int read_replace_refs;
123
f237c8b6
DS
124static int graph_write(int argc, const char **argv)
125{
d88b14b3
DS
126 struct string_list *pack_indexes = NULL;
127 struct string_list *commit_hex = NULL;
128 struct string_list lines;
049d51a2 129
f237c8b6
DS
130 static struct option builtin_commit_graph_write_options[] = {
131 OPT_STRING(0, "object-dir", &opts.obj_dir,
132 N_("dir"),
133 N_("The object directory to store the graph")),
59fb8770
DS
134 OPT_BOOL(0, "reachable", &opts.reachable,
135 N_("start walk at all refs")),
049d51a2
DS
136 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
137 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
138 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
139 N_("start walk at commits listed by stdin")),
7547b95b
DS
140 OPT_BOOL(0, "append", &opts.append,
141 N_("include all commits already in the commit-graph file")),
f237c8b6
DS
142 OPT_END(),
143 };
144
145 argc = parse_options(argc, argv, NULL,
146 builtin_commit_graph_write_options,
147 builtin_commit_graph_write_usage, 0);
148
59fb8770
DS
149 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
150 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
f237c8b6
DS
151 if (!opts.obj_dir)
152 opts.obj_dir = get_object_directory();
153
d6538246
DS
154 read_replace_refs = 0;
155
59fb8770 156 if (opts.reachable) {
7b0f2292 157 write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
59fb8770
DS
158 return 0;
159 }
160
d88b14b3 161 string_list_init(&lines, 0);
3d5df01b 162 if (opts.stdin_packs || opts.stdin_commits) {
049d51a2 163 struct strbuf buf = STRBUF_INIT;
d88b14b3
DS
164
165 while (strbuf_getline(&buf, stdin) != EOF)
166 string_list_append(&lines, strbuf_detach(&buf, NULL));
167
168 if (opts.stdin_packs)
169 pack_indexes = &lines;
170 if (opts.stdin_commits)
171 commit_hex = &lines;
0bfb48e6
172
173 UNLEAK(buf);
049d51a2
DS
174 }
175
176 write_commit_graph(opts.obj_dir,
177 pack_indexes,
3d5df01b 178 commit_hex,
7b0f2292
ÆAB
179 opts.append,
180 1);
049d51a2 181
0bfb48e6 182 UNLEAK(lines);
f237c8b6
DS
183 return 0;
184}
4ce58ee3
DS
185
186int cmd_commit_graph(int argc, const char **argv, const char *prefix)
187{
188 static struct option builtin_commit_graph_options[] = {
189 OPT_STRING(0, "object-dir", &opts.obj_dir,
190 N_("dir"),
191 N_("The object directory to store the graph")),
192 OPT_END(),
193 };
194
195 if (argc == 2 && !strcmp(argv[1], "-h"))
196 usage_with_options(builtin_commit_graph_usage,
197 builtin_commit_graph_options);
198
199 git_config(git_default_config, NULL);
200 argc = parse_options(argc, argv, prefix,
201 builtin_commit_graph_options,
202 builtin_commit_graph_usage,
203 PARSE_OPT_STOP_AT_NON_OPTION);
204
f237c8b6 205 if (argc > 0) {
2a2e32bd
DS
206 if (!strcmp(argv[0], "read"))
207 return graph_read(argc, argv);
283e68c7
DS
208 if (!strcmp(argv[0], "verify"))
209 return graph_verify(argc, argv);
f237c8b6
DS
210 if (!strcmp(argv[0], "write"))
211 return graph_write(argc, argv);
212 }
213
4ce58ee3
DS
214 usage_with_options(builtin_commit_graph_usage,
215 builtin_commit_graph_options);
216}