]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/repack.c
repack: simplify handling of --write-bitmap-index
[thirdparty/git.git] / builtin / repack.c
CommitLineData
a1bbc6c0
SB
1#include "builtin.h"
2#include "cache.h"
3#include "dir.h"
4#include "parse-options.h"
5#include "run-command.h"
6#include "sigchain.h"
7#include "strbuf.h"
8#include "string-list.h"
9#include "argv-array.h"
10
11static int delta_base_offset = 1;
ee34a2be 12static int pack_kept_objects = -1;
2bed2d47 13static int write_bitmaps;
a1bbc6c0
SB
14static char *packdir, *packtmp;
15
16static const char *const git_repack_usage[] = {
17 N_("git repack [options]"),
18 NULL
19};
20
21static int repack_config(const char *var, const char *value, void *cb)
22{
23 if (!strcmp(var, "repack.usedeltabaseoffset")) {
24 delta_base_offset = git_config_bool(var, value);
25 return 0;
26 }
ee34a2be
JK
27 if (!strcmp(var, "repack.packkeptobjects")) {
28 pack_kept_objects = git_config_bool(var, value);
29 return 0;
30 }
3198b89f 31 if (!strcmp(var, "pack.writebitmaps")) {
d078d85b 32 write_bitmaps = git_config_bool(var, value);
3198b89f
JK
33 return 0;
34 }
a1bbc6c0
SB
35 return git_default_config(var, value, cb);
36}
37
38/*
39 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
40 */
41static void remove_temporary_files(void)
42{
43 struct strbuf buf = STRBUF_INIT;
44 size_t dirlen, prefixlen;
45 DIR *dir;
46 struct dirent *e;
47
48 dir = opendir(packdir);
49 if (!dir)
50 return;
51
52 /* Point at the slash at the end of ".../objects/pack/" */
53 dirlen = strlen(packdir) + 1;
54 strbuf_addstr(&buf, packtmp);
55 /* Hold the length of ".tmp-%d-pack-" */
56 prefixlen = buf.len - dirlen;
57
58 while ((e = readdir(dir))) {
59 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
60 continue;
61 strbuf_setlen(&buf, dirlen);
62 strbuf_addstr(&buf, e->d_name);
63 unlink(buf.buf);
64 }
65 closedir(dir);
66 strbuf_release(&buf);
67}
68
69static void remove_pack_on_signal(int signo)
70{
71 remove_temporary_files();
72 sigchain_pop(signo);
73 raise(signo);
74}
75
76/*
77 * Adds all packs hex strings to the fname list, which do not
78 * have a corresponding .keep file.
79 */
80static void get_non_kept_pack_filenames(struct string_list *fname_list)
81{
82 DIR *dir;
83 struct dirent *e;
84 char *fname;
85 size_t len;
86
87 if (!(dir = opendir(packdir)))
88 return;
89
90 while ((e = readdir(dir)) != NULL) {
91 if (suffixcmp(e->d_name, ".pack"))
92 continue;
93
94 len = strlen(e->d_name) - strlen(".pack");
95 fname = xmemdupz(e->d_name, len);
96
97 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
98 string_list_append_nodup(fname_list, fname);
99 else
100 free(fname);
101 }
102 closedir(dir);
103}
104
105static void remove_redundant_pack(const char *dir_name, const char *base_name)
106{
5cf2741c 107 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
a1bbc6c0
SB
108 int i;
109 struct strbuf buf = STRBUF_INIT;
110 size_t plen;
111
112 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
113 plen = buf.len;
114
115 for (i = 0; i < ARRAY_SIZE(exts); i++) {
116 strbuf_setlen(&buf, plen);
117 strbuf_addstr(&buf, exts[i]);
118 unlink(buf.buf);
119 }
120 strbuf_release(&buf);
121}
122
123#define ALL_INTO_ONE 1
124#define LOOSEN_UNREACHABLE 2
125
126int cmd_repack(int argc, const char **argv, const char *prefix)
127{
42a02d85
JK
128 struct {
129 const char *name;
b77fcd1e 130 unsigned optional:1;
42a02d85
JK
131 } exts[] = {
132 {".pack"},
133 {".idx"},
5cf2741c 134 {".bitmap", 1},
42a02d85 135 };
a1bbc6c0
SB
136 struct child_process cmd;
137 struct string_list_item *item;
138 struct argv_array cmd_args = ARGV_ARRAY_INIT;
139 struct string_list names = STRING_LIST_INIT_DUP;
140 struct string_list rollback = STRING_LIST_INIT_NODUP;
141 struct string_list existing_packs = STRING_LIST_INIT_DUP;
142 struct strbuf line = STRBUF_INIT;
143 int nr_packs, ext, ret, failed;
144 FILE *out;
145
146 /* variables to be filled by option parsing */
147 int pack_everything = 0;
148 int delete_redundant = 0;
149 char *unpack_unreachable = NULL;
150 int window = 0, window_memory = 0;
151 int depth = 0;
152 int max_pack_size = 0;
153 int no_reuse_delta = 0, no_reuse_object = 0;
154 int no_update_server_info = 0;
155 int quiet = 0;
156 int local = 0;
157
158 struct option builtin_repack_options[] = {
159 OPT_BIT('a', NULL, &pack_everything,
160 N_("pack everything in a single pack"), ALL_INTO_ONE),
161 OPT_BIT('A', NULL, &pack_everything,
162 N_("same as -a, and turn unreachable objects loose"),
163 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
164 OPT_BOOL('d', NULL, &delete_redundant,
165 N_("remove redundant packs, and run git-prune-packed")),
166 OPT_BOOL('f', NULL, &no_reuse_delta,
167 N_("pass --no-reuse-delta to git-pack-objects")),
168 OPT_BOOL('F', NULL, &no_reuse_object,
169 N_("pass --no-reuse-object to git-pack-objects")),
170 OPT_BOOL('n', NULL, &no_update_server_info,
171 N_("do not run git-update-server-info")),
172 OPT__QUIET(&quiet, N_("be quiet")),
173 OPT_BOOL('l', "local", &local,
174 N_("pass --local to git-pack-objects")),
d078d85b 175 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
5cf2741c 176 N_("write bitmap index")),
a1bbc6c0
SB
177 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
178 N_("with -A, do not loosen objects older than this")),
179 OPT_INTEGER(0, "window", &window,
180 N_("size of the window used for delta compression")),
181 OPT_INTEGER(0, "window-memory", &window_memory,
182 N_("same as the above, but limit memory size instead of entries count")),
183 OPT_INTEGER(0, "depth", &depth,
184 N_("limits the maximum delta depth")),
185 OPT_INTEGER(0, "max-pack-size", &max_pack_size,
186 N_("maximum size of each packfile")),
ee34a2be
JK
187 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
188 N_("repack objects in packs marked with .keep")),
a1bbc6c0
SB
189 OPT_END()
190 };
191
192 git_config(repack_config, NULL);
193
194 argc = parse_options(argc, argv, prefix, builtin_repack_options,
195 git_repack_usage, 0);
196
ee34a2be 197 if (pack_kept_objects < 0)
2bed2d47 198 pack_kept_objects = write_bitmaps;
ee34a2be 199
a1bbc6c0
SB
200 packdir = mkpathdup("%s/pack", get_object_directory());
201 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
202
203 sigchain_push_common(remove_pack_on_signal);
204
205 argv_array_push(&cmd_args, "pack-objects");
206 argv_array_push(&cmd_args, "--keep-true-parents");
ee34a2be
JK
207 if (!pack_kept_objects)
208 argv_array_push(&cmd_args, "--honor-pack-keep");
a1bbc6c0
SB
209 argv_array_push(&cmd_args, "--non-empty");
210 argv_array_push(&cmd_args, "--all");
211 argv_array_push(&cmd_args, "--reflog");
212 if (window)
213 argv_array_pushf(&cmd_args, "--window=%u", window);
214 if (window_memory)
215 argv_array_pushf(&cmd_args, "--window-memory=%u", window_memory);
216 if (depth)
217 argv_array_pushf(&cmd_args, "--depth=%u", depth);
218 if (max_pack_size)
219 argv_array_pushf(&cmd_args, "--max_pack_size=%u", max_pack_size);
220 if (no_reuse_delta)
221 argv_array_pushf(&cmd_args, "--no-reuse-delta");
222 if (no_reuse_object)
223 argv_array_pushf(&cmd_args, "--no-reuse-object");
2bed2d47
JK
224 if (write_bitmaps)
225 argv_array_push(&cmd_args, "--write-bitmap-index");
a1bbc6c0
SB
226
227 if (pack_everything & ALL_INTO_ONE) {
228 get_non_kept_pack_filenames(&existing_packs);
229
230 if (existing_packs.nr && delete_redundant) {
231 if (unpack_unreachable)
232 argv_array_pushf(&cmd_args,
233 "--unpack-unreachable=%s",
234 unpack_unreachable);
235 else if (pack_everything & LOOSEN_UNREACHABLE)
236 argv_array_push(&cmd_args,
237 "--unpack-unreachable");
238 }
239 } else {
240 argv_array_push(&cmd_args, "--unpacked");
241 argv_array_push(&cmd_args, "--incremental");
242 }
243
244 if (local)
245 argv_array_push(&cmd_args, "--local");
246 if (quiet)
247 argv_array_push(&cmd_args, "--quiet");
248 if (delta_base_offset)
249 argv_array_push(&cmd_args, "--delta-base-offset");
250
251 argv_array_push(&cmd_args, packtmp);
252
253 memset(&cmd, 0, sizeof(cmd));
254 cmd.argv = cmd_args.argv;
255 cmd.git_cmd = 1;
256 cmd.out = -1;
257 cmd.no_stdin = 1;
258
259 ret = start_command(&cmd);
260 if (ret)
ffc9329f 261 return ret;
a1bbc6c0
SB
262
263 nr_packs = 0;
264 out = xfdopen(cmd.out, "r");
265 while (strbuf_getline(&line, out, '\n') != EOF) {
266 if (line.len != 40)
267 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
268 string_list_append(&names, line.buf);
269 nr_packs++;
270 }
271 fclose(out);
272 ret = finish_command(&cmd);
273 if (ret)
ffc9329f 274 return ret;
a1bbc6c0
SB
275 argv_array_clear(&cmd_args);
276
277 if (!nr_packs && !quiet)
278 printf("Nothing new to pack.\n");
279
280 /*
281 * Ok we have prepared all new packfiles.
282 * First see if there are packs of the same name and if so
283 * if we can move them out of the way (this can happen if we
284 * repacked immediately after packing fully.
285 */
286 failed = 0;
287 for_each_string_list_item(item, &names) {
b328c216 288 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
a1bbc6c0
SB
289 char *fname, *fname_old;
290 fname = mkpathdup("%s/%s%s", packdir,
42a02d85 291 item->string, exts[ext].name);
a1bbc6c0
SB
292 if (!file_exists(fname)) {
293 free(fname);
294 continue;
295 }
296
297 fname_old = mkpath("%s/old-%s%s", packdir,
42a02d85 298 item->string, exts[ext].name);
a1bbc6c0
SB
299 if (file_exists(fname_old))
300 if (unlink(fname_old))
301 failed = 1;
302
303 if (!failed && rename(fname, fname_old)) {
304 free(fname);
305 failed = 1;
306 break;
307 } else {
308 string_list_append(&rollback, fname);
309 }
310 }
311 if (failed)
312 break;
313 }
314 if (failed) {
315 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
316 for_each_string_list_item(item, &rollback) {
317 char *fname, *fname_old;
318 fname = mkpathdup("%s/%s", packdir, item->string);
319 fname_old = mkpath("%s/old-%s", packdir, item->string);
320 if (rename(fname_old, fname))
321 string_list_append(&rollback_failure, fname);
322 free(fname);
323 }
324
325 if (rollback_failure.nr) {
326 int i;
327 fprintf(stderr,
328 "WARNING: Some packs in use have been renamed by\n"
329 "WARNING: prefixing old- to their name, in order to\n"
330 "WARNING: replace them with the new version of the\n"
331 "WARNING: file. But the operation failed, and the\n"
332 "WARNING: attempt to rename them back to their\n"
333 "WARNING: original names also failed.\n"
334 "WARNING: Please rename them in %s manually:\n", packdir);
335 for (i = 0; i < rollback_failure.nr; i++)
336 fprintf(stderr, "WARNING: old-%s -> %s\n",
337 rollback_failure.items[i].string,
338 rollback_failure.items[i].string);
339 }
340 exit(1);
341 }
342
343 /* Now the ones with the same name are out of the way... */
344 for_each_string_list_item(item, &names) {
b328c216 345 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
a1bbc6c0
SB
346 char *fname, *fname_old;
347 struct stat statbuffer;
b77fcd1e 348 int exists = 0;
a1bbc6c0 349 fname = mkpathdup("%s/pack-%s%s",
42a02d85 350 packdir, item->string, exts[ext].name);
a1bbc6c0 351 fname_old = mkpathdup("%s-%s%s",
42a02d85 352 packtmp, item->string, exts[ext].name);
a1bbc6c0
SB
353 if (!stat(fname_old, &statbuffer)) {
354 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
355 chmod(fname_old, statbuffer.st_mode);
b77fcd1e
JK
356 exists = 1;
357 }
358 if (exists || !exts[ext].optional) {
359 if (rename(fname_old, fname))
360 die_errno(_("renaming '%s' failed"), fname_old);
a1bbc6c0 361 }
a1bbc6c0
SB
362 free(fname);
363 free(fname_old);
364 }
365 }
366
367 /* Remove the "old-" files */
368 for_each_string_list_item(item, &names) {
b328c216 369 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
a1bbc6c0
SB
370 char *fname;
371 fname = mkpath("%s/old-pack-%s%s",
372 packdir,
373 item->string,
42a02d85 374 exts[ext].name);
0b63c6a5
SB
375 if (remove_path(fname))
376 warning(_("removing '%s' failed"), fname);
a1bbc6c0
SB
377 }
378 }
379
380 /* End of pack replacement. */
381
382 if (delete_redundant) {
383 sort_string_list(&names);
384 for_each_string_list_item(item, &existing_packs) {
385 char *sha1;
386 size_t len = strlen(item->string);
387 if (len < 40)
388 continue;
389 sha1 = item->string + len - 40;
390 if (!string_list_has_string(&names, sha1))
391 remove_redundant_pack(packdir, item->string);
392 }
393 argv_array_push(&cmd_args, "prune-packed");
394 if (quiet)
395 argv_array_push(&cmd_args, "--quiet");
396
397 memset(&cmd, 0, sizeof(cmd));
398 cmd.argv = cmd_args.argv;
399 cmd.git_cmd = 1;
400 run_command(&cmd);
401 argv_array_clear(&cmd_args);
402 }
403
404 if (!no_update_server_info) {
405 argv_array_push(&cmd_args, "update-server-info");
406 memset(&cmd, 0, sizeof(cmd));
407 cmd.argv = cmd_args.argv;
408 cmd.git_cmd = 1;
409 run_command(&cmd);
410 argv_array_clear(&cmd_args);
411 }
412 remove_temporary_files();
413 string_list_clear(&names, 0);
414 string_list_clear(&rollback, 0);
415 string_list_clear(&existing_packs, 0);
416 strbuf_release(&line);
417
418 return 0;
419}