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