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