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