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