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