]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/multi-pack-index.c
clone: allow "--bare" with "-o"
[thirdparty/git.git] / builtin / multi-pack-index.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "parse-options.h"
5 #include "midx.h"
6 #include "trace2.h"
7 #include "object-store.h"
8
9 #define BUILTIN_MIDX_WRITE_USAGE \
10 N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
11 "[--refs-snapshot=<path>]")
12
13 #define BUILTIN_MIDX_VERIFY_USAGE \
14 N_("git multi-pack-index [<options>] verify")
15
16 #define BUILTIN_MIDX_EXPIRE_USAGE \
17 N_("git multi-pack-index [<options>] expire")
18
19 #define BUILTIN_MIDX_REPACK_USAGE \
20 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
21
22 static char const * const builtin_multi_pack_index_write_usage[] = {
23 BUILTIN_MIDX_WRITE_USAGE,
24 NULL
25 };
26 static char const * const builtin_multi_pack_index_verify_usage[] = {
27 BUILTIN_MIDX_VERIFY_USAGE,
28 NULL
29 };
30 static char const * const builtin_multi_pack_index_expire_usage[] = {
31 BUILTIN_MIDX_EXPIRE_USAGE,
32 NULL
33 };
34 static char const * const builtin_multi_pack_index_repack_usage[] = {
35 BUILTIN_MIDX_REPACK_USAGE,
36 NULL
37 };
38 static char const * const builtin_multi_pack_index_usage[] = {
39 BUILTIN_MIDX_WRITE_USAGE,
40 BUILTIN_MIDX_VERIFY_USAGE,
41 BUILTIN_MIDX_EXPIRE_USAGE,
42 BUILTIN_MIDX_REPACK_USAGE,
43 NULL
44 };
45
46 static struct opts_multi_pack_index {
47 char *object_dir;
48 const char *preferred_pack;
49 const char *refs_snapshot;
50 unsigned long batch_size;
51 unsigned flags;
52 int stdin_packs;
53 } opts;
54
55
56 static int parse_object_dir(const struct option *opt, const char *arg,
57 int unset)
58 {
59 free(opts.object_dir);
60 if (unset)
61 opts.object_dir = xstrdup(get_object_directory());
62 else
63 opts.object_dir = real_pathdup(arg, 1);
64 return 0;
65 }
66
67 static struct option common_opts[] = {
68 OPT_CALLBACK(0, "object-dir", &opts.object_dir,
69 N_("directory"),
70 N_("object directory containing set of packfile and pack-index pairs"),
71 parse_object_dir),
72 OPT_END(),
73 };
74
75 static struct option *add_common_options(struct option *prev)
76 {
77 return parse_options_concat(common_opts, prev);
78 }
79
80 static int git_multi_pack_index_write_config(const char *var, const char *value,
81 void *cb)
82 {
83 if (!strcmp(var, "pack.writebitmaphashcache")) {
84 if (git_config_bool(var, value))
85 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
86 else
87 opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE;
88 }
89
90 /*
91 * We should never make a fall-back call to 'git_default_config', since
92 * this was already called in 'cmd_multi_pack_index()'.
93 */
94 return 0;
95 }
96
97 static void read_packs_from_stdin(struct string_list *to)
98 {
99 struct strbuf buf = STRBUF_INIT;
100 while (strbuf_getline(&buf, stdin) != EOF)
101 string_list_append(to, buf.buf);
102 string_list_sort(to);
103
104 strbuf_release(&buf);
105 }
106
107 static int cmd_multi_pack_index_write(int argc, const char **argv)
108 {
109 struct option *options;
110 static struct option builtin_multi_pack_index_write_options[] = {
111 OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
112 N_("preferred-pack"),
113 N_("pack for reuse when computing a multi-pack bitmap")),
114 OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
115 MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
116 OPT_BIT(0, "progress", &opts.flags,
117 N_("force progress reporting"), MIDX_PROGRESS),
118 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
119 N_("write multi-pack index containing only given indexes")),
120 OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
121 N_("refs snapshot for selecting bitmap commits")),
122 OPT_END(),
123 };
124
125 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
126
127 git_config(git_multi_pack_index_write_config, NULL);
128
129 options = add_common_options(builtin_multi_pack_index_write_options);
130
131 trace2_cmd_mode(argv[0]);
132
133 if (isatty(2))
134 opts.flags |= MIDX_PROGRESS;
135 argc = parse_options(argc, argv, NULL,
136 options, builtin_multi_pack_index_write_usage,
137 PARSE_OPT_KEEP_UNKNOWN);
138 if (argc)
139 usage_with_options(builtin_multi_pack_index_write_usage,
140 options);
141
142 FREE_AND_NULL(options);
143
144 if (opts.stdin_packs) {
145 struct string_list packs = STRING_LIST_INIT_DUP;
146 int ret;
147
148 read_packs_from_stdin(&packs);
149
150 ret = write_midx_file_only(opts.object_dir, &packs,
151 opts.preferred_pack,
152 opts.refs_snapshot, opts.flags);
153
154 string_list_clear(&packs, 0);
155
156 return ret;
157
158 }
159 return write_midx_file(opts.object_dir, opts.preferred_pack,
160 opts.refs_snapshot, opts.flags);
161 }
162
163 static int cmd_multi_pack_index_verify(int argc, const char **argv)
164 {
165 struct option *options;
166 static struct option builtin_multi_pack_index_verify_options[] = {
167 OPT_BIT(0, "progress", &opts.flags,
168 N_("force progress reporting"), MIDX_PROGRESS),
169 OPT_END(),
170 };
171 options = add_common_options(builtin_multi_pack_index_verify_options);
172
173 trace2_cmd_mode(argv[0]);
174
175 if (isatty(2))
176 opts.flags |= MIDX_PROGRESS;
177 argc = parse_options(argc, argv, NULL,
178 options, builtin_multi_pack_index_verify_usage,
179 PARSE_OPT_KEEP_UNKNOWN);
180 if (argc)
181 usage_with_options(builtin_multi_pack_index_verify_usage,
182 options);
183
184 FREE_AND_NULL(options);
185
186 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
187 }
188
189 static int cmd_multi_pack_index_expire(int argc, const char **argv)
190 {
191 struct option *options;
192 static struct option builtin_multi_pack_index_expire_options[] = {
193 OPT_BIT(0, "progress", &opts.flags,
194 N_("force progress reporting"), MIDX_PROGRESS),
195 OPT_END(),
196 };
197 options = add_common_options(builtin_multi_pack_index_expire_options);
198
199 trace2_cmd_mode(argv[0]);
200
201 if (isatty(2))
202 opts.flags |= MIDX_PROGRESS;
203 argc = parse_options(argc, argv, NULL,
204 options, builtin_multi_pack_index_expire_usage,
205 PARSE_OPT_KEEP_UNKNOWN);
206 if (argc)
207 usage_with_options(builtin_multi_pack_index_expire_usage,
208 options);
209
210 FREE_AND_NULL(options);
211
212 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
213 }
214
215 static int cmd_multi_pack_index_repack(int argc, const char **argv)
216 {
217 struct option *options;
218 static struct option builtin_multi_pack_index_repack_options[] = {
219 OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
220 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
221 OPT_BIT(0, "progress", &opts.flags,
222 N_("force progress reporting"), MIDX_PROGRESS),
223 OPT_END(),
224 };
225
226 options = add_common_options(builtin_multi_pack_index_repack_options);
227
228 trace2_cmd_mode(argv[0]);
229
230 if (isatty(2))
231 opts.flags |= MIDX_PROGRESS;
232 argc = parse_options(argc, argv, NULL,
233 options,
234 builtin_multi_pack_index_repack_usage,
235 PARSE_OPT_KEEP_UNKNOWN);
236 if (argc)
237 usage_with_options(builtin_multi_pack_index_repack_usage,
238 options);
239
240 FREE_AND_NULL(options);
241
242 return midx_repack(the_repository, opts.object_dir,
243 (size_t)opts.batch_size, opts.flags);
244 }
245
246 int cmd_multi_pack_index(int argc, const char **argv,
247 const char *prefix)
248 {
249 int res;
250 struct option *builtin_multi_pack_index_options = common_opts;
251
252 git_config(git_default_config, NULL);
253
254 if (the_repository &&
255 the_repository->objects &&
256 the_repository->objects->odb)
257 opts.object_dir = xstrdup(the_repository->objects->odb->path);
258
259 argc = parse_options(argc, argv, prefix,
260 builtin_multi_pack_index_options,
261 builtin_multi_pack_index_usage,
262 PARSE_OPT_STOP_AT_NON_OPTION);
263
264 if (!argc)
265 goto usage;
266
267 if (!strcmp(argv[0], "repack"))
268 res = cmd_multi_pack_index_repack(argc, argv);
269 else if (!strcmp(argv[0], "write"))
270 res = cmd_multi_pack_index_write(argc, argv);
271 else if (!strcmp(argv[0], "verify"))
272 res = cmd_multi_pack_index_verify(argc, argv);
273 else if (!strcmp(argv[0], "expire"))
274 res = cmd_multi_pack_index_expire(argc, argv);
275 else {
276 error(_("unrecognized subcommand: %s"), argv[0]);
277 goto usage;
278 }
279
280 free(opts.object_dir);
281 return res;
282
283 usage:
284 usage_with_options(builtin_multi_pack_index_usage,
285 builtin_multi_pack_index_options);
286 }