commit-graph: implement --set-latest
[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]"),
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]"),
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 } opts;
30
31 static int graph_read(int argc, const char **argv)
32 {
33         struct commit_graph *graph = 0;
34         struct strbuf full_path = STRBUF_INIT;
35
36         static struct option builtin_commit_graph_read_options[] = {
37                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
38                         N_("dir"),
39                         N_("The object directory to store the graph") },
40                 { OPTION_STRING, 'H', "file", &opts.graph_file,
41                         N_("file"),
42                         N_("The filename for a specific commit graph file in the object directory."),
43                         PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
44                 OPT_END(),
45         };
46
47         argc = parse_options(argc, argv, NULL,
48                              builtin_commit_graph_read_options,
49                              builtin_commit_graph_read_usage, 0);
50
51         if (!opts.obj_dir)
52                 opts.obj_dir = get_object_directory();
53
54         if (!opts.graph_file)
55                 die("no graph hash specified");
56
57         strbuf_addf(&full_path, "%s/info/%s", opts.obj_dir, opts.graph_file);
58         graph = load_commit_graph_one(full_path.buf);
59
60         if (!graph)
61                 die("graph file %s does not exist", full_path.buf);
62
63         printf("header: %08x %d %d %d %d\n",
64                 ntohl(*(uint32_t*)graph->data),
65                 *(unsigned char*)(graph->data + 4),
66                 *(unsigned char*)(graph->data + 5),
67                 *(unsigned char*)(graph->data + 6),
68                 *(unsigned char*)(graph->data + 7));
69         printf("num_commits: %u\n", graph->num_commits);
70         printf("chunks:");
71
72         if (graph->chunk_oid_fanout)
73                 printf(" oid_fanout");
74         if (graph->chunk_oid_lookup)
75                 printf(" oid_lookup");
76         if (graph->chunk_commit_data)
77                 printf(" commit_metadata");
78         if (graph->chunk_large_edges)
79                 printf(" large_edges");
80         printf("\n");
81
82         return 0;
83 }
84
85 static void set_latest_file(const char *obj_dir, const char *graph_file)
86 {
87         int fd;
88         struct lock_file lk = LOCK_INIT;
89         char *latest_fname = get_graph_latest_filename(obj_dir);
90
91         fd = hold_lock_file_for_update(&lk, latest_fname, LOCK_DIE_ON_ERROR);
92         FREE_AND_NULL(latest_fname);
93
94         if (fd < 0)
95                 die_errno("unable to open graph-head");
96
97         write_in_full(fd, graph_file, strlen(graph_file));
98         commit_lock_file(&lk);
99 }
100
101 static int graph_write(int argc, const char **argv)
102 {
103         char *graph_name;
104
105         static struct option builtin_commit_graph_write_options[] = {
106                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
107                         N_("dir"),
108                         N_("The object directory to store the graph") },
109                 OPT_BOOL('u', "set-latest", &opts.set_latest,
110                         N_("update graph-head to written graph file")),
111                 OPT_END(),
112         };
113
114         argc = parse_options(argc, argv, NULL,
115                              builtin_commit_graph_write_options,
116                              builtin_commit_graph_write_usage, 0);
117
118         if (!opts.obj_dir)
119                 opts.obj_dir = get_object_directory();
120
121         graph_name = write_commit_graph(opts.obj_dir);
122
123         if (graph_name) {
124                 if (opts.set_latest)
125                         set_latest_file(opts.obj_dir, graph_name);
126
127                 printf("%s\n", graph_name);
128                 FREE_AND_NULL(graph_name);
129         }
130
131         return 0;
132 }
133
134 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
135 {
136         static struct option builtin_commit_graph_options[] = {
137                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
138                         N_("dir"),
139                         N_("The object directory to store the graph") },
140                 OPT_END(),
141         };
142
143         if (argc == 2 && !strcmp(argv[1], "-h"))
144                 usage_with_options(builtin_commit_graph_usage,
145                                    builtin_commit_graph_options);
146
147         git_config(git_default_config, NULL);
148         argc = parse_options(argc, argv, prefix,
149                              builtin_commit_graph_options,
150                              builtin_commit_graph_usage,
151                              PARSE_OPT_STOP_AT_NON_OPTION);
152
153         if (argc > 0) {
154                 if (!strcmp(argv[0], "read"))
155                         return graph_read(argc, argv);
156                 if (!strcmp(argv[0], "write"))
157                         return graph_write(argc, argv);
158         }
159
160         usage_with_options(builtin_commit_graph_usage,
161                            builtin_commit_graph_options);
162 }