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