5 #include "parse-options.h"
6 #include "commit-graph.h"
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]"),
15 static const char * const builtin_commit_graph_read_usage[] = {
16 N_("git commit-graph read [--object-dir <objdir>] [--file=<hash>]"),
20 static const char * const builtin_commit_graph_write_usage[] = {
21 N_("git commit-graph write [--object-dir <objdir>] [--set-latest]"),
25 static struct opts_commit_graph {
27 const char *graph_file;
31 static int graph_read(int argc, const char **argv)
33 struct commit_graph *graph = 0;
34 struct strbuf full_path = STRBUF_INIT;
36 static struct option builtin_commit_graph_read_options[] = {
37 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
39 N_("The object directory to store the graph") },
40 { OPTION_STRING, 'H', "file", &opts.graph_file,
42 N_("The filename for a specific commit graph file in the object directory."),
43 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
47 argc = parse_options(argc, argv, NULL,
48 builtin_commit_graph_read_options,
49 builtin_commit_graph_read_usage, 0);
52 opts.obj_dir = get_object_directory();
55 die("no graph hash specified");
57 strbuf_addf(&full_path, "%s/info/%s", opts.obj_dir, opts.graph_file);
58 graph = load_commit_graph_one(full_path.buf);
61 die("graph file %s does not exist", full_path.buf);
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);
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");
85 static void set_latest_file(const char *obj_dir, const char *graph_file)
88 struct lock_file lk = LOCK_INIT;
89 char *latest_fname = get_graph_latest_filename(obj_dir);
91 fd = hold_lock_file_for_update(&lk, latest_fname, LOCK_DIE_ON_ERROR);
92 FREE_AND_NULL(latest_fname);
95 die_errno("unable to open graph-head");
97 write_in_full(fd, graph_file, strlen(graph_file));
98 commit_lock_file(&lk);
101 static int graph_write(int argc, const char **argv)
105 static struct option builtin_commit_graph_write_options[] = {
106 { OPTION_STRING, 'o', "object-dir", &opts.obj_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")),
114 argc = parse_options(argc, argv, NULL,
115 builtin_commit_graph_write_options,
116 builtin_commit_graph_write_usage, 0);
119 opts.obj_dir = get_object_directory();
121 graph_name = write_commit_graph(opts.obj_dir);
125 set_latest_file(opts.obj_dir, graph_name);
127 printf("%s\n", graph_name);
128 FREE_AND_NULL(graph_name);
134 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
136 static struct option builtin_commit_graph_options[] = {
137 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
139 N_("The object directory to store the graph") },
143 if (argc == 2 && !strcmp(argv[1], "-h"))
144 usage_with_options(builtin_commit_graph_usage,
145 builtin_commit_graph_options);
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);
154 if (!strcmp(argv[0], "read"))
155 return graph_read(argc, argv);
156 if (!strcmp(argv[0], "write"))
157 return graph_write(argc, argv);
160 usage_with_options(builtin_commit_graph_usage,
161 builtin_commit_graph_options);