]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/multi-pack-index.c
Merge branch 'jc/doc-submitting-patches-choice-of-base'
[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 const 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 static struct option common_opts[] = {
56 OPT_FILENAME(0, "object-dir", &opts.object_dir,
57 N_("object directory containing set of packfile and pack-index pairs")),
58 OPT_END(),
59 };
60
61 static struct option *add_common_options(struct option *prev)
62 {
63 return parse_options_concat(common_opts, prev);
64 }
65
66 static int git_multi_pack_index_write_config(const char *var, const char *value,
67 void *cb)
68 {
69 if (!strcmp(var, "pack.writebitmaphashcache")) {
70 if (git_config_bool(var, value))
71 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
72 else
73 opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE;
74 }
75
76 /*
77 * We should never make a fall-back call to 'git_default_config', since
78 * this was already called in 'cmd_multi_pack_index()'.
79 */
80 return 0;
81 }
82
83 static void read_packs_from_stdin(struct string_list *to)
84 {
85 struct strbuf buf = STRBUF_INIT;
86 while (strbuf_getline(&buf, stdin) != EOF)
87 string_list_append(to, buf.buf);
88 string_list_sort(to);
89
90 strbuf_release(&buf);
91 }
92
93 static int cmd_multi_pack_index_write(int argc, const char **argv)
94 {
95 struct option *options;
96 static struct option builtin_multi_pack_index_write_options[] = {
97 OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
98 N_("preferred-pack"),
99 N_("pack for reuse when computing a multi-pack bitmap")),
100 OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"),
101 MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX),
102 OPT_BIT(0, "progress", &opts.flags,
103 N_("force progress reporting"), MIDX_PROGRESS),
104 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
105 N_("write multi-pack index containing only given indexes")),
106 OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot,
107 N_("refs snapshot for selecting bitmap commits")),
108 OPT_END(),
109 };
110
111 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
112
113 git_config(git_multi_pack_index_write_config, NULL);
114
115 options = add_common_options(builtin_multi_pack_index_write_options);
116
117 trace2_cmd_mode(argv[0]);
118
119 if (isatty(2))
120 opts.flags |= MIDX_PROGRESS;
121 argc = parse_options(argc, argv, NULL,
122 options, builtin_multi_pack_index_write_usage,
123 PARSE_OPT_KEEP_UNKNOWN);
124 if (argc)
125 usage_with_options(builtin_multi_pack_index_write_usage,
126 options);
127
128 FREE_AND_NULL(options);
129
130 if (opts.stdin_packs) {
131 struct string_list packs = STRING_LIST_INIT_DUP;
132 int ret;
133
134 read_packs_from_stdin(&packs);
135
136 ret = write_midx_file_only(opts.object_dir, &packs,
137 opts.preferred_pack,
138 opts.refs_snapshot, opts.flags);
139
140 string_list_clear(&packs, 0);
141
142 return ret;
143
144 }
145 return write_midx_file(opts.object_dir, opts.preferred_pack,
146 opts.refs_snapshot, opts.flags);
147 }
148
149 static int cmd_multi_pack_index_verify(int argc, const char **argv)
150 {
151 struct option *options;
152 static struct option builtin_multi_pack_index_verify_options[] = {
153 OPT_BIT(0, "progress", &opts.flags,
154 N_("force progress reporting"), MIDX_PROGRESS),
155 OPT_END(),
156 };
157 options = add_common_options(builtin_multi_pack_index_verify_options);
158
159 trace2_cmd_mode(argv[0]);
160
161 if (isatty(2))
162 opts.flags |= MIDX_PROGRESS;
163 argc = parse_options(argc, argv, NULL,
164 options, builtin_multi_pack_index_verify_usage,
165 PARSE_OPT_KEEP_UNKNOWN);
166 if (argc)
167 usage_with_options(builtin_multi_pack_index_verify_usage,
168 options);
169
170 FREE_AND_NULL(options);
171
172 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
173 }
174
175 static int cmd_multi_pack_index_expire(int argc, const char **argv)
176 {
177 struct option *options;
178 static struct option builtin_multi_pack_index_expire_options[] = {
179 OPT_BIT(0, "progress", &opts.flags,
180 N_("force progress reporting"), MIDX_PROGRESS),
181 OPT_END(),
182 };
183 options = add_common_options(builtin_multi_pack_index_expire_options);
184
185 trace2_cmd_mode(argv[0]);
186
187 if (isatty(2))
188 opts.flags |= MIDX_PROGRESS;
189 argc = parse_options(argc, argv, NULL,
190 options, builtin_multi_pack_index_expire_usage,
191 PARSE_OPT_KEEP_UNKNOWN);
192 if (argc)
193 usage_with_options(builtin_multi_pack_index_expire_usage,
194 options);
195
196 FREE_AND_NULL(options);
197
198 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
199 }
200
201 static int cmd_multi_pack_index_repack(int argc, const char **argv)
202 {
203 struct option *options;
204 static struct option builtin_multi_pack_index_repack_options[] = {
205 OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
206 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
207 OPT_BIT(0, "progress", &opts.flags,
208 N_("force progress reporting"), MIDX_PROGRESS),
209 OPT_END(),
210 };
211
212 options = add_common_options(builtin_multi_pack_index_repack_options);
213
214 trace2_cmd_mode(argv[0]);
215
216 if (isatty(2))
217 opts.flags |= MIDX_PROGRESS;
218 argc = parse_options(argc, argv, NULL,
219 options,
220 builtin_multi_pack_index_repack_usage,
221 PARSE_OPT_KEEP_UNKNOWN);
222 if (argc)
223 usage_with_options(builtin_multi_pack_index_repack_usage,
224 options);
225
226 FREE_AND_NULL(options);
227
228 return midx_repack(the_repository, opts.object_dir,
229 (size_t)opts.batch_size, opts.flags);
230 }
231
232 int cmd_multi_pack_index(int argc, const char **argv,
233 const char *prefix)
234 {
235 struct option *builtin_multi_pack_index_options = common_opts;
236
237 git_config(git_default_config, NULL);
238
239 argc = parse_options(argc, argv, prefix,
240 builtin_multi_pack_index_options,
241 builtin_multi_pack_index_usage,
242 PARSE_OPT_STOP_AT_NON_OPTION);
243
244 if (!opts.object_dir)
245 opts.object_dir = get_object_directory();
246
247 if (!argc)
248 goto usage;
249
250 if (!strcmp(argv[0], "repack"))
251 return cmd_multi_pack_index_repack(argc, argv);
252 else if (!strcmp(argv[0], "write"))
253 return cmd_multi_pack_index_write(argc, argv);
254 else if (!strcmp(argv[0], "verify"))
255 return cmd_multi_pack_index_verify(argc, argv);
256 else if (!strcmp(argv[0], "expire"))
257 return cmd_multi_pack_index_expire(argc, argv);
258
259 error(_("unrecognized subcommand: %s"), argv[0]);
260 usage:
261 usage_with_options(builtin_multi_pack_index_usage,
262 builtin_multi_pack_index_options);
263 }