Merge branch 'sg/subtree-signed-commits' into pu
[git] / builtin / commit-graph.c
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
8 static char const * const builtin_commit_graph_usage[] = {
9         N_("git commit-graph [--object-dir <objdir>]"),
10         N_("git commit-graph read [--object-dir <objdir>] [--file=<hash>]"),
11         N_("git commit-graph write [--object-dir <objdir>] [--set-latest] [--delete-expired] [--stdin-packs|--stdin-commits]"),
12         NULL
13 };
14
15 static const char * const builtin_commit_graph_read_usage[] = {
16         N_("git commit-graph read [--object-dir <objdir>] [--file=<hash>]"),
17         NULL
18 };
19
20 static const char * const builtin_commit_graph_write_usage[] = {
21         N_("git commit-graph write [--object-dir <objdir>] [--set-latest] [--delete-expired] [--stdin-packs|--stdin-commits]"),
22         NULL
23 };
24
25 static struct opts_commit_graph {
26         const char *obj_dir;
27         const char *graph_file;
28         int set_latest;
29         int delete_expired;
30         int stdin_packs;
31         int stdin_commits;
32 } opts;
33
34 static int graph_read(int argc, const char **argv)
35 {
36         struct commit_graph *graph = 0;
37         struct strbuf full_path = STRBUF_INIT;
38
39         static struct option builtin_commit_graph_read_options[] = {
40                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
41                         N_("dir"),
42                         N_("The object directory to store the graph") },
43                 { OPTION_STRING, 'H', "file", &opts.graph_file,
44                         N_("file"),
45                         N_("The filename for a specific commit graph file in the object directory."),
46                         PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
47                 OPT_END(),
48         };
49
50         argc = parse_options(argc, argv, NULL,
51                              builtin_commit_graph_read_options,
52                              builtin_commit_graph_read_usage, 0);
53
54         if (!opts.obj_dir)
55                 opts.obj_dir = get_object_directory();
56
57         if (!opts.graph_file)
58                 die("no graph hash specified");
59
60         strbuf_addf(&full_path, "%s/info/%s", opts.obj_dir, opts.graph_file);
61         graph = load_commit_graph_one(full_path.buf);
62
63         if (!graph)
64                 die("graph file %s does not exist", full_path.buf);
65
66         printf("header: %08x %d %d %d %d\n",
67                 ntohl(*(uint32_t*)graph->data),
68                 *(unsigned char*)(graph->data + 4),
69                 *(unsigned char*)(graph->data + 5),
70                 *(unsigned char*)(graph->data + 6),
71                 *(unsigned char*)(graph->data + 7));
72         printf("num_commits: %u\n", graph->num_commits);
73         printf("chunks:");
74
75         if (graph->chunk_oid_fanout)
76                 printf(" oid_fanout");
77         if (graph->chunk_oid_lookup)
78                 printf(" oid_lookup");
79         if (graph->chunk_commit_data)
80                 printf(" commit_metadata");
81         if (graph->chunk_large_edges)
82                 printf(" large_edges");
83         printf("\n");
84
85         return 0;
86 }
87
88 static void set_latest_file(const char *obj_dir, const char *graph_file)
89 {
90         int fd;
91         struct lock_file lk = LOCK_INIT;
92         char *latest_fname = get_graph_latest_filename(obj_dir);
93
94         fd = hold_lock_file_for_update(&lk, latest_fname, LOCK_DIE_ON_ERROR);
95         FREE_AND_NULL(latest_fname);
96
97         if (fd < 0)
98                 die_errno("unable to open graph-head");
99
100         write_in_full(fd, graph_file, strlen(graph_file));
101         commit_lock_file(&lk);
102 }
103
104 /*
105  * To avoid race conditions and deleting graph files that are being
106  * used by other processes, look inside a pack directory for all files
107  * of the form "graph-<hash>.graph" that do not match the old or new
108  * graph hashes and delete them.
109  */
110 static void do_delete_expired(const char *obj_dir,
111                               const char *old_graph_name,
112                               const char *new_graph_name)
113 {
114         DIR *dir;
115         struct dirent *de;
116         int dirnamelen;
117         struct strbuf path = STRBUF_INIT;
118
119         strbuf_addf(&path, "%s/info", obj_dir);
120         dir = opendir(path.buf);
121         if (!dir) {
122                 if (errno != ENOENT)
123                         error_errno("unable to open object pack directory: %s",
124                                     obj_dir);
125                 return;
126         }
127
128         strbuf_addch(&path, '/');
129         dirnamelen = path.len;
130         while ((de = readdir(dir)) != NULL) {
131                 size_t base_len;
132
133                 if (is_dot_or_dotdot(de->d_name))
134                         continue;
135
136                 strbuf_setlen(&path, dirnamelen);
137                 strbuf_addstr(&path, de->d_name);
138
139                 base_len = path.len;
140                 if (strip_suffix_mem(path.buf, &base_len, ".graph") &&
141                     strcmp(new_graph_name, de->d_name) &&
142                     (!old_graph_name || strcmp(old_graph_name, de->d_name)) &&
143                     remove_path(path.buf))
144                         die("failed to remove path %s", path.buf);
145         }
146
147         strbuf_release(&path);
148 }
149
150 static int graph_write(int argc, const char **argv)
151 {
152         char *graph_name;
153         char *old_graph_name;
154         const char **pack_indexes = NULL;
155         int nr_packs = 0;
156         const char **commit_hex = NULL;
157         int nr_commits = 0;
158         const char **lines = NULL;
159         int nr_lines = 0;
160         int alloc_lines = 0;
161
162         static struct option builtin_commit_graph_write_options[] = {
163                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
164                         N_("dir"),
165                         N_("The object directory to store the graph") },
166                 OPT_BOOL('u', "set-latest", &opts.set_latest,
167                         N_("update graph-head to written graph file")),
168                 OPT_BOOL('d', "delete-expired", &opts.delete_expired,
169                         N_("delete expired head graph file")),
170                 OPT_BOOL('s', "stdin-packs", &opts.stdin_packs,
171                         N_("only scan packfiles listed by stdin")),
172                 OPT_BOOL('C', "stdin-commits", &opts.stdin_commits,
173                         N_("start walk at commits listed by stdin")),
174                 OPT_END(),
175         };
176
177         argc = parse_options(argc, argv, NULL,
178                              builtin_commit_graph_write_options,
179                              builtin_commit_graph_write_usage, 0);
180
181         if (opts.stdin_packs && opts.stdin_commits)
182                 die(_("cannot use both --stdin-commits and --stdin-packs"));
183         if (!opts.obj_dir)
184                 opts.obj_dir = get_object_directory();
185
186         old_graph_name = get_graph_latest_contents(opts.obj_dir);
187
188         if (opts.stdin_packs || opts.stdin_commits) {
189                 struct strbuf buf = STRBUF_INIT;
190                 nr_lines = 0;
191                 alloc_lines = 128;
192                 ALLOC_ARRAY(lines, alloc_lines);
193
194                 while (strbuf_getline(&buf, stdin) != EOF) {
195                         ALLOC_GROW(lines, nr_lines + 1, alloc_lines);
196                         lines[nr_lines++] = buf.buf;
197                         strbuf_detach(&buf, NULL);
198                 }
199
200                 if (opts.stdin_packs) {
201                         pack_indexes = lines;
202                         nr_packs = nr_lines;
203                 }
204                 if (opts.stdin_commits) {
205                         commit_hex = lines;
206                         nr_commits = nr_lines;
207                 }
208         }
209
210         graph_name = write_commit_graph(opts.obj_dir,
211                                         pack_indexes,
212                                         nr_packs,
213                                         commit_hex,
214                                         nr_commits);
215
216         if (graph_name) {
217                 if (opts.set_latest)
218                         set_latest_file(opts.obj_dir, graph_name);
219
220                 if (opts.delete_expired)
221                         do_delete_expired(opts.obj_dir,
222                                           old_graph_name,
223                                           graph_name);
224
225                 printf("%s\n", graph_name);
226                 FREE_AND_NULL(graph_name);
227         }
228
229         return 0;
230 }
231
232 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
233 {
234         static struct option builtin_commit_graph_options[] = {
235                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
236                         N_("dir"),
237                         N_("The object directory to store the graph") },
238                 OPT_END(),
239         };
240
241         if (argc == 2 && !strcmp(argv[1], "-h"))
242                 usage_with_options(builtin_commit_graph_usage,
243                                    builtin_commit_graph_options);
244
245         git_config(git_default_config, NULL);
246         argc = parse_options(argc, argv, prefix,
247                              builtin_commit_graph_options,
248                              builtin_commit_graph_usage,
249                              PARSE_OPT_STOP_AT_NON_OPTION);
250
251         if (argc > 0) {
252                 if (!strcmp(argv[0], "read"))
253                         return graph_read(argc, argv);
254                 if (!strcmp(argv[0], "write"))
255                         return graph_write(argc, argv);
256         }
257
258         usage_with_options(builtin_commit_graph_usage,
259                            builtin_commit_graph_options);
260 }