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