]>
Commit | Line | Data |
---|---|---|
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" |
283e68c7 | 6 | #include "repository.h" |
f237c8b6 | 7 | #include "commit-graph.h" |
3da4b609 | 8 | #include "object-store.h" |
4ce58ee3 DS |
9 | |
10 | static char const * const builtin_commit_graph_usage[] = { | |
73716122 GS |
11 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
12 | N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"), | |
f237c8b6 DS |
13 | NULL |
14 | }; | |
15 | ||
283e68c7 | 16 | static const char * const builtin_commit_graph_verify_usage[] = { |
73716122 | 17 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
283e68c7 DS |
18 | NULL |
19 | }; | |
20 | ||
f237c8b6 | 21 | static const char * const builtin_commit_graph_write_usage[] = { |
73716122 | 22 | N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"), |
4ce58ee3 DS |
23 | NULL |
24 | }; | |
25 | ||
26 | static struct opts_commit_graph { | |
27 | const char *obj_dir; | |
59fb8770 | 28 | int reachable; |
049d51a2 | 29 | int stdin_packs; |
3d5df01b | 30 | int stdin_commits; |
7547b95b | 31 | int append; |
135a7123 | 32 | int split; |
3da4b609 | 33 | int shallow; |
73716122 | 34 | int progress; |
4ce58ee3 DS |
35 | } opts; |
36 | ||
283e68c7 DS |
37 | static int graph_verify(int argc, const char **argv) |
38 | { | |
39 | struct commit_graph *graph = NULL; | |
40 | char *graph_name; | |
61df89c8 ÆAB |
41 | int open_ok; |
42 | int fd; | |
43 | struct stat st; | |
3da4b609 | 44 | int flags = 0; |
283e68c7 DS |
45 | |
46 | static struct option builtin_commit_graph_verify_options[] = { | |
47 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
48 | N_("dir"), | |
49 | N_("The object directory to store the graph")), | |
3da4b609 DS |
50 | OPT_BOOL(0, "shallow", &opts.shallow, |
51 | N_("if the commit-graph is split, only verify the tip file")), | |
73716122 | 52 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
283e68c7 DS |
53 | OPT_END(), |
54 | }; | |
55 | ||
0bd7f578 GS |
56 | trace2_cmd_mode("verify"); |
57 | ||
73716122 | 58 | opts.progress = isatty(2); |
283e68c7 DS |
59 | argc = parse_options(argc, argv, NULL, |
60 | builtin_commit_graph_verify_options, | |
61 | builtin_commit_graph_verify_usage, 0); | |
62 | ||
63 | if (!opts.obj_dir) | |
64 | opts.obj_dir = get_object_directory(); | |
3da4b609 DS |
65 | if (opts.shallow) |
66 | flags |= COMMIT_GRAPH_VERIFY_SHALLOW; | |
73716122 GS |
67 | if (opts.progress) |
68 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; | |
283e68c7 DS |
69 | |
70 | graph_name = get_commit_graph_filename(opts.obj_dir); | |
61df89c8 | 71 | open_ok = open_commit_graph(graph_name, &fd, &st); |
3da4b609 | 72 | if (!open_ok && errno != ENOENT) |
7b8ce9c6 | 73 | die_errno(_("Could not open commit-graph '%s'"), graph_name); |
3da4b609 | 74 | |
283e68c7 DS |
75 | FREE_AND_NULL(graph_name); |
76 | ||
3da4b609 DS |
77 | if (open_ok) |
78 | graph = load_commit_graph_one_fd_st(fd, &st); | |
79 | else | |
80 | graph = read_commit_graph_one(the_repository, opts.obj_dir); | |
81 | ||
82 | /* Return failure if open_ok predicted success */ | |
283e68c7 | 83 | if (!graph) |
3da4b609 | 84 | return !!open_ok; |
283e68c7 | 85 | |
0bfb48e6 | 86 | UNLEAK(graph); |
3da4b609 | 87 | return verify_commit_graph(the_repository, graph, flags); |
283e68c7 DS |
88 | } |
89 | ||
d6538246 | 90 | extern int read_replace_refs; |
c2bc6e6a | 91 | static struct split_commit_graph_opts split_opts; |
d6538246 | 92 | |
f237c8b6 DS |
93 | static int graph_write(int argc, const char **argv) |
94 | { | |
d88b14b3 DS |
95 | struct string_list *pack_indexes = NULL; |
96 | struct string_list *commit_hex = NULL; | |
97 | struct string_list lines; | |
e103f727 | 98 | int result = 0; |
73716122 | 99 | enum commit_graph_write_flags flags = 0; |
049d51a2 | 100 | |
f237c8b6 DS |
101 | static struct option builtin_commit_graph_write_options[] = { |
102 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
103 | N_("dir"), | |
104 | N_("The object directory to store the graph")), | |
59fb8770 DS |
105 | OPT_BOOL(0, "reachable", &opts.reachable, |
106 | N_("start walk at all refs")), | |
049d51a2 DS |
107 | OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, |
108 | N_("scan pack-indexes listed by stdin for commits")), | |
3d5df01b DS |
109 | OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, |
110 | N_("start walk at commits listed by stdin")), | |
7547b95b DS |
111 | OPT_BOOL(0, "append", &opts.append, |
112 | N_("include all commits already in the commit-graph file")), | |
73716122 | 113 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
135a7123 DS |
114 | OPT_BOOL(0, "split", &opts.split, |
115 | N_("allow writing an incremental commit-graph file")), | |
c2bc6e6a DS |
116 | OPT_INTEGER(0, "max-commits", &split_opts.max_commits, |
117 | N_("maximum number of commits in a non-base split commit-graph")), | |
118 | OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple, | |
119 | N_("maximum ratio between two levels of a split commit-graph")), | |
120 | OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time, | |
121 | N_("maximum number of commits in a non-base split commit-graph")), | |
f237c8b6 DS |
122 | OPT_END(), |
123 | }; | |
124 | ||
73716122 | 125 | opts.progress = isatty(2); |
c2bc6e6a DS |
126 | split_opts.size_multiple = 2; |
127 | split_opts.max_commits = 0; | |
128 | split_opts.expire_time = 0; | |
129 | ||
0bd7f578 GS |
130 | trace2_cmd_mode("write"); |
131 | ||
f237c8b6 DS |
132 | argc = parse_options(argc, argv, NULL, |
133 | builtin_commit_graph_write_options, | |
134 | builtin_commit_graph_write_usage, 0); | |
135 | ||
59fb8770 DS |
136 | if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1) |
137 | die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs")); | |
f237c8b6 DS |
138 | if (!opts.obj_dir) |
139 | opts.obj_dir = get_object_directory(); | |
5af80394 | 140 | if (opts.append) |
39d88318 | 141 | flags |= COMMIT_GRAPH_WRITE_APPEND; |
135a7123 | 142 | if (opts.split) |
39d88318 | 143 | flags |= COMMIT_GRAPH_WRITE_SPLIT; |
73716122 GS |
144 | if (opts.progress) |
145 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; | |
f237c8b6 | 146 | |
d6538246 DS |
147 | read_replace_refs = 0; |
148 | ||
c2bc6e6a DS |
149 | if (opts.reachable) { |
150 | if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts)) | |
151 | return 1; | |
152 | return 0; | |
153 | } | |
59fb8770 | 154 | |
d88b14b3 | 155 | string_list_init(&lines, 0); |
3d5df01b | 156 | if (opts.stdin_packs || opts.stdin_commits) { |
049d51a2 | 157 | struct strbuf buf = STRBUF_INIT; |
d88b14b3 DS |
158 | |
159 | while (strbuf_getline(&buf, stdin) != EOF) | |
160 | string_list_append(&lines, strbuf_detach(&buf, NULL)); | |
161 | ||
162 | if (opts.stdin_packs) | |
163 | pack_indexes = &lines; | |
7c5c9b9c | 164 | if (opts.stdin_commits) { |
d88b14b3 | 165 | commit_hex = &lines; |
7c5c9b9c SG |
166 | flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS; |
167 | } | |
0bfb48e6 MÅ |
168 | |
169 | UNLEAK(buf); | |
049d51a2 DS |
170 | } |
171 | ||
e103f727 DS |
172 | if (write_commit_graph(opts.obj_dir, |
173 | pack_indexes, | |
174 | commit_hex, | |
c2bc6e6a DS |
175 | flags, |
176 | &split_opts)) | |
e103f727 | 177 | result = 1; |
049d51a2 | 178 | |
0bfb48e6 | 179 | UNLEAK(lines); |
e103f727 | 180 | return result; |
f237c8b6 | 181 | } |
4ce58ee3 DS |
182 | |
183 | int cmd_commit_graph(int argc, const char **argv, const char *prefix) | |
184 | { | |
185 | static struct option builtin_commit_graph_options[] = { | |
186 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
187 | N_("dir"), | |
188 | N_("The object directory to store the graph")), | |
189 | OPT_END(), | |
190 | }; | |
191 | ||
192 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
193 | usage_with_options(builtin_commit_graph_usage, | |
194 | builtin_commit_graph_options); | |
195 | ||
196 | git_config(git_default_config, NULL); | |
197 | argc = parse_options(argc, argv, prefix, | |
198 | builtin_commit_graph_options, | |
199 | builtin_commit_graph_usage, | |
200 | PARSE_OPT_STOP_AT_NON_OPTION); | |
201 | ||
dd2e50a8 JK |
202 | save_commit_buffer = 0; |
203 | ||
f237c8b6 | 204 | if (argc > 0) { |
283e68c7 DS |
205 | if (!strcmp(argv[0], "verify")) |
206 | return graph_verify(argc, argv); | |
f237c8b6 DS |
207 | if (!strcmp(argv[0], "write")) |
208 | return graph_write(argc, argv); | |
209 | } | |
210 | ||
4ce58ee3 DS |
211 | usage_with_options(builtin_commit_graph_usage, |
212 | builtin_commit_graph_options); | |
213 | } |