commit-graph: read only from specific pack-indexes
[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"
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>]"),
049d51a2 11 N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
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[] = {
049d51a2 21 N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
4ce58ee3
DS
22 NULL
23};
24
25static struct opts_commit_graph {
26 const char *obj_dir;
049d51a2 27 int stdin_packs;
4ce58ee3
DS
28} opts;
29
2a2e32bd
DS
30static int graph_read(int argc, const char **argv)
31{
32 struct commit_graph *graph = NULL;
33 char *graph_name;
34
35 static struct option builtin_commit_graph_read_options[] = {
36 OPT_STRING(0, "object-dir", &opts.obj_dir,
37 N_("dir"),
38 N_("The object directory to store the graph")),
39 OPT_END(),
40 };
41
42 argc = parse_options(argc, argv, NULL,
43 builtin_commit_graph_read_options,
44 builtin_commit_graph_read_usage, 0);
45
46 if (!opts.obj_dir)
47 opts.obj_dir = get_object_directory();
48
49 graph_name = get_commit_graph_filename(opts.obj_dir);
50 graph = load_commit_graph_one(graph_name);
51
52 if (!graph)
53 die("graph file %s does not exist", graph_name);
54 FREE_AND_NULL(graph_name);
55
56 printf("header: %08x %d %d %d %d\n",
57 ntohl(*(uint32_t*)graph->data),
58 *(unsigned char*)(graph->data + 4),
59 *(unsigned char*)(graph->data + 5),
60 *(unsigned char*)(graph->data + 6),
61 *(unsigned char*)(graph->data + 7));
62 printf("num_commits: %u\n", graph->num_commits);
63 printf("chunks:");
64
65 if (graph->chunk_oid_fanout)
66 printf(" oid_fanout");
67 if (graph->chunk_oid_lookup)
68 printf(" oid_lookup");
69 if (graph->chunk_commit_data)
70 printf(" commit_metadata");
71 if (graph->chunk_large_edges)
72 printf(" large_edges");
73 printf("\n");
74
75 return 0;
76}
77
f237c8b6
DS
78static int graph_write(int argc, const char **argv)
79{
049d51a2
DS
80 const char **pack_indexes = NULL;
81 int packs_nr = 0;
82 const char **lines = NULL;
83 int lines_nr = 0;
84 int lines_alloc = 0;
85
f237c8b6
DS
86 static struct option builtin_commit_graph_write_options[] = {
87 OPT_STRING(0, "object-dir", &opts.obj_dir,
88 N_("dir"),
89 N_("The object directory to store the graph")),
049d51a2
DS
90 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
91 N_("scan pack-indexes listed by stdin for commits")),
f237c8b6
DS
92 OPT_END(),
93 };
94
95 argc = parse_options(argc, argv, NULL,
96 builtin_commit_graph_write_options,
97 builtin_commit_graph_write_usage, 0);
98
99 if (!opts.obj_dir)
100 opts.obj_dir = get_object_directory();
101
049d51a2
DS
102 if (opts.stdin_packs) {
103 struct strbuf buf = STRBUF_INIT;
104 lines_nr = 0;
105 lines_alloc = 128;
106 ALLOC_ARRAY(lines, lines_alloc);
107
108 while (strbuf_getline(&buf, stdin) != EOF) {
109 ALLOC_GROW(lines, lines_nr + 1, lines_alloc);
110 lines[lines_nr++] = strbuf_detach(&buf, NULL);
111 }
112
113 pack_indexes = lines;
114 packs_nr = lines_nr;
115 }
116
117 write_commit_graph(opts.obj_dir,
118 pack_indexes,
119 packs_nr);
120
f237c8b6
DS
121 return 0;
122}
4ce58ee3
DS
123
124int cmd_commit_graph(int argc, const char **argv, const char *prefix)
125{
126 static struct option builtin_commit_graph_options[] = {
127 OPT_STRING(0, "object-dir", &opts.obj_dir,
128 N_("dir"),
129 N_("The object directory to store the graph")),
130 OPT_END(),
131 };
132
133 if (argc == 2 && !strcmp(argv[1], "-h"))
134 usage_with_options(builtin_commit_graph_usage,
135 builtin_commit_graph_options);
136
137 git_config(git_default_config, NULL);
138 argc = parse_options(argc, argv, prefix,
139 builtin_commit_graph_options,
140 builtin_commit_graph_usage,
141 PARSE_OPT_STOP_AT_NON_OPTION);
142
f237c8b6 143 if (argc > 0) {
2a2e32bd
DS
144 if (!strcmp(argv[0], "read"))
145 return graph_read(argc, argv);
f237c8b6
DS
146 if (!strcmp(argv[0], "write"))
147 return graph_write(argc, argv);
148 }
149
4ce58ee3
DS
150 usage_with_options(builtin_commit_graph_usage,
151 builtin_commit_graph_options);
152}