]>
Commit | Line | Data |
---|---|---|
a1bbc6c0 SB |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
b2141fc1 | 3 | #include "config.h" |
a1bbc6c0 SB |
4 | #include "dir.h" |
5 | #include "parse-options.h" | |
6 | #include "run-command.h" | |
7 | #include "sigchain.h" | |
8 | #include "strbuf.h" | |
9 | #include "string-list.h" | |
10 | #include "argv-array.h" | |
525e18c0 | 11 | #include "midx.h" |
5d19e813 | 12 | #include "packfile.h" |
9460fd48 | 13 | #include "prune-packed.h" |
36f0f344 | 14 | #include "object-store.h" |
b14ed5ad | 15 | #include "promisor-remote.h" |
a1bbc6c0 SB |
16 | |
17 | static int delta_base_offset = 1; | |
ee34a2be | 18 | static int pack_kept_objects = -1; |
36eba032 | 19 | static int write_bitmaps = -1; |
16d75fa4 | 20 | static int use_delta_islands; |
a1bbc6c0 SB |
21 | static char *packdir, *packtmp; |
22 | ||
23 | static const char *const git_repack_usage[] = { | |
9c9b4f2f | 24 | N_("git repack [<options>]"), |
a1bbc6c0 SB |
25 | NULL |
26 | }; | |
27 | ||
1c409a70 DT |
28 | static const char incremental_bitmap_conflict_error[] = N_( |
29 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
30 | "--no-write-bitmap-index or disable the pack.writebitmaps configuration." | |
31 | ); | |
32 | ||
33 | ||
a1bbc6c0 SB |
34 | static int repack_config(const char *var, const char *value, void *cb) |
35 | { | |
36 | if (!strcmp(var, "repack.usedeltabaseoffset")) { | |
37 | delta_base_offset = git_config_bool(var, value); | |
38 | return 0; | |
39 | } | |
ee34a2be JK |
40 | if (!strcmp(var, "repack.packkeptobjects")) { |
41 | pack_kept_objects = git_config_bool(var, value); | |
42 | return 0; | |
43 | } | |
71d76cb4 JK |
44 | if (!strcmp(var, "repack.writebitmaps") || |
45 | !strcmp(var, "pack.writebitmaps")) { | |
d078d85b | 46 | write_bitmaps = git_config_bool(var, value); |
3198b89f JK |
47 | return 0; |
48 | } | |
16d75fa4 JK |
49 | if (!strcmp(var, "repack.usedeltaislands")) { |
50 | use_delta_islands = git_config_bool(var, value); | |
51 | return 0; | |
52 | } | |
a1bbc6c0 SB |
53 | return git_default_config(var, value, cb); |
54 | } | |
55 | ||
56 | /* | |
57 | * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files. | |
58 | */ | |
59 | static void remove_temporary_files(void) | |
60 | { | |
61 | struct strbuf buf = STRBUF_INIT; | |
62 | size_t dirlen, prefixlen; | |
63 | DIR *dir; | |
64 | struct dirent *e; | |
65 | ||
66 | dir = opendir(packdir); | |
67 | if (!dir) | |
68 | return; | |
69 | ||
70 | /* Point at the slash at the end of ".../objects/pack/" */ | |
71 | dirlen = strlen(packdir) + 1; | |
72 | strbuf_addstr(&buf, packtmp); | |
73 | /* Hold the length of ".tmp-%d-pack-" */ | |
74 | prefixlen = buf.len - dirlen; | |
75 | ||
76 | while ((e = readdir(dir))) { | |
77 | if (strncmp(e->d_name, buf.buf + dirlen, prefixlen)) | |
78 | continue; | |
79 | strbuf_setlen(&buf, dirlen); | |
80 | strbuf_addstr(&buf, e->d_name); | |
81 | unlink(buf.buf); | |
82 | } | |
83 | closedir(dir); | |
84 | strbuf_release(&buf); | |
85 | } | |
86 | ||
87 | static void remove_pack_on_signal(int signo) | |
88 | { | |
89 | remove_temporary_files(); | |
90 | sigchain_pop(signo); | |
91 | raise(signo); | |
92 | } | |
93 | ||
94 | /* | |
95 | * Adds all packs hex strings to the fname list, which do not | |
5d19e813 | 96 | * have a corresponding .keep file. These packs are not to |
0c16cd49 | 97 | * be kept if we are going to pack everything into one file. |
a1bbc6c0 | 98 | */ |
ed7e5fc3 NTND |
99 | static void get_non_kept_pack_filenames(struct string_list *fname_list, |
100 | const struct string_list *extra_keep) | |
a1bbc6c0 SB |
101 | { |
102 | DIR *dir; | |
103 | struct dirent *e; | |
104 | char *fname; | |
a1bbc6c0 SB |
105 | |
106 | if (!(dir = opendir(packdir))) | |
107 | return; | |
108 | ||
109 | while ((e = readdir(dir)) != NULL) { | |
26936bfd | 110 | size_t len; |
ed7e5fc3 NTND |
111 | int i; |
112 | ||
113 | for (i = 0; i < extra_keep->nr; i++) | |
114 | if (!fspathcmp(e->d_name, extra_keep->items[i].string)) | |
115 | break; | |
116 | if (extra_keep->nr > 0 && i < extra_keep->nr) | |
117 | continue; | |
118 | ||
26936bfd | 119 | if (!strip_suffix(e->d_name, ".pack", &len)) |
a1bbc6c0 SB |
120 | continue; |
121 | ||
a1bbc6c0 SB |
122 | fname = xmemdupz(e->d_name, len); |
123 | ||
5d19e813 | 124 | if (!file_exists(mkpath("%s/%s.keep", packdir, fname))) |
a1bbc6c0 SB |
125 | string_list_append_nodup(fname_list, fname); |
126 | else | |
127 | free(fname); | |
128 | } | |
129 | closedir(dir); | |
130 | } | |
131 | ||
132 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
133 | { | |
a1bbc6c0 | 134 | struct strbuf buf = STRBUF_INIT; |
8434e85d DS |
135 | strbuf_addf(&buf, "%s/%s.pack", dir_name, base_name); |
136 | unlink_pack_path(buf.buf, 1); | |
a1bbc6c0 SB |
137 | strbuf_release(&buf); |
138 | } | |
139 | ||
2b958e79 JT |
140 | struct pack_objects_args { |
141 | const char *window; | |
142 | const char *window_memory; | |
143 | const char *depth; | |
144 | const char *threads; | |
145 | const char *max_pack_size; | |
146 | int no_reuse_delta; | |
147 | int no_reuse_object; | |
148 | int quiet; | |
149 | int local; | |
150 | }; | |
151 | ||
152 | static void prepare_pack_objects(struct child_process *cmd, | |
153 | const struct pack_objects_args *args) | |
154 | { | |
155 | argv_array_push(&cmd->args, "pack-objects"); | |
156 | if (args->window) | |
157 | argv_array_pushf(&cmd->args, "--window=%s", args->window); | |
158 | if (args->window_memory) | |
159 | argv_array_pushf(&cmd->args, "--window-memory=%s", args->window_memory); | |
160 | if (args->depth) | |
161 | argv_array_pushf(&cmd->args, "--depth=%s", args->depth); | |
162 | if (args->threads) | |
163 | argv_array_pushf(&cmd->args, "--threads=%s", args->threads); | |
164 | if (args->max_pack_size) | |
165 | argv_array_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size); | |
166 | if (args->no_reuse_delta) | |
167 | argv_array_pushf(&cmd->args, "--no-reuse-delta"); | |
168 | if (args->no_reuse_object) | |
169 | argv_array_pushf(&cmd->args, "--no-reuse-object"); | |
170 | if (args->local) | |
171 | argv_array_push(&cmd->args, "--local"); | |
172 | if (args->quiet) | |
173 | argv_array_push(&cmd->args, "--quiet"); | |
174 | if (delta_base_offset) | |
175 | argv_array_push(&cmd->args, "--delta-base-offset"); | |
176 | argv_array_push(&cmd->args, packtmp); | |
177 | cmd->git_cmd = 1; | |
178 | cmd->out = -1; | |
179 | } | |
180 | ||
5d19e813 JT |
181 | /* |
182 | * Write oid to the given struct child_process's stdin, starting it first if | |
183 | * necessary. | |
184 | */ | |
185 | static int write_oid(const struct object_id *oid, struct packed_git *pack, | |
186 | uint32_t pos, void *data) | |
187 | { | |
188 | struct child_process *cmd = data; | |
189 | ||
190 | if (cmd->in == -1) { | |
191 | if (start_command(cmd)) | |
c83d950e | 192 | die(_("could not start pack-objects to repack promisor objects")); |
5d19e813 JT |
193 | } |
194 | ||
dd336a55 | 195 | xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz); |
5d19e813 JT |
196 | xwrite(cmd->in, "\n", 1); |
197 | return 0; | |
198 | } | |
199 | ||
200 | static void repack_promisor_objects(const struct pack_objects_args *args, | |
201 | struct string_list *names) | |
202 | { | |
203 | struct child_process cmd = CHILD_PROCESS_INIT; | |
204 | FILE *out; | |
205 | struct strbuf line = STRBUF_INIT; | |
206 | ||
207 | prepare_pack_objects(&cmd, args); | |
208 | cmd.in = -1; | |
209 | ||
210 | /* | |
211 | * NEEDSWORK: Giving pack-objects only the OIDs without any ordering | |
212 | * hints may result in suboptimal deltas in the resulting pack. See if | |
213 | * the OIDs can be sent with fake paths such that pack-objects can use a | |
214 | * {type -> existing pack order} ordering when computing deltas instead | |
215 | * of a {type -> size} ordering, which may produce better deltas. | |
216 | */ | |
217 | for_each_packed_object(write_oid, &cmd, | |
218 | FOR_EACH_OBJECT_PROMISOR_ONLY); | |
219 | ||
220 | if (cmd.in == -1) | |
221 | /* No packed objects; cmd was never started */ | |
222 | return; | |
223 | ||
224 | close(cmd.in); | |
225 | ||
226 | out = xfdopen(cmd.out, "r"); | |
227 | while (strbuf_getline_lf(&line, out) != EOF) { | |
228 | char *promisor_name; | |
229 | int fd; | |
2f0c9e9a | 230 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 231 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
5d19e813 JT |
232 | string_list_append(names, line.buf); |
233 | ||
234 | /* | |
235 | * pack-objects creates the .pack and .idx files, but not the | |
236 | * .promisor file. Create the .promisor file, which is empty. | |
5374a290 JT |
237 | * |
238 | * NEEDSWORK: fetch-pack sometimes generates non-empty | |
239 | * .promisor files containing the ref names and associated | |
240 | * hashes at the point of generation of the corresponding | |
241 | * packfile, but this would not preserve their contents. Maybe | |
242 | * concatenate the contents of all .promisor files instead of | |
243 | * just creating a new empty file. | |
5d19e813 JT |
244 | */ |
245 | promisor_name = mkpathdup("%s-%s.promisor", packtmp, | |
246 | line.buf); | |
247 | fd = open(promisor_name, O_CREAT|O_EXCL|O_WRONLY, 0600); | |
248 | if (fd < 0) | |
c83d950e | 249 | die_errno(_("unable to create '%s'"), promisor_name); |
5d19e813 JT |
250 | close(fd); |
251 | free(promisor_name); | |
252 | } | |
253 | fclose(out); | |
254 | if (finish_command(&cmd)) | |
c83d950e | 255 | die(_("could not finish pack-objects to repack promisor objects")); |
5d19e813 JT |
256 | } |
257 | ||
a1bbc6c0 SB |
258 | #define ALL_INTO_ONE 1 |
259 | #define LOOSEN_UNREACHABLE 2 | |
260 | ||
261 | int cmd_repack(int argc, const char **argv, const char *prefix) | |
262 | { | |
42a02d85 JK |
263 | struct { |
264 | const char *name; | |
b77fcd1e | 265 | unsigned optional:1; |
42a02d85 JK |
266 | } exts[] = { |
267 | {".pack"}, | |
268 | {".idx"}, | |
5cf2741c | 269 | {".bitmap", 1}, |
5d19e813 | 270 | {".promisor", 1}, |
42a02d85 | 271 | }; |
d3180279 | 272 | struct child_process cmd = CHILD_PROCESS_INIT; |
a1bbc6c0 | 273 | struct string_list_item *item; |
a1bbc6c0 SB |
274 | struct string_list names = STRING_LIST_INIT_DUP; |
275 | struct string_list rollback = STRING_LIST_INIT_NODUP; | |
276 | struct string_list existing_packs = STRING_LIST_INIT_DUP; | |
277 | struct strbuf line = STRBUF_INIT; | |
ed7e5fc3 | 278 | int i, ext, ret, failed; |
a1bbc6c0 SB |
279 | FILE *out; |
280 | ||
281 | /* variables to be filled by option parsing */ | |
282 | int pack_everything = 0; | |
283 | int delete_redundant = 0; | |
aa8bd519 | 284 | const char *unpack_unreachable = NULL; |
905f27b8 | 285 | int keep_unreachable = 0; |
ed7e5fc3 | 286 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
a1bbc6c0 | 287 | int no_update_server_info = 0; |
525e18c0 | 288 | int midx_cleared = 0; |
2b958e79 | 289 | struct pack_objects_args po_args = {NULL}; |
a1bbc6c0 SB |
290 | |
291 | struct option builtin_repack_options[] = { | |
292 | OPT_BIT('a', NULL, &pack_everything, | |
293 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
294 | OPT_BIT('A', NULL, &pack_everything, | |
295 | N_("same as -a, and turn unreachable objects loose"), | |
296 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
297 | OPT_BOOL('d', NULL, &delete_redundant, | |
298 | N_("remove redundant packs, and run git-prune-packed")), | |
2b958e79 | 299 | OPT_BOOL('f', NULL, &po_args.no_reuse_delta, |
a1bbc6c0 | 300 | N_("pass --no-reuse-delta to git-pack-objects")), |
2b958e79 | 301 | OPT_BOOL('F', NULL, &po_args.no_reuse_object, |
a1bbc6c0 SB |
302 | N_("pass --no-reuse-object to git-pack-objects")), |
303 | OPT_BOOL('n', NULL, &no_update_server_info, | |
304 | N_("do not run git-update-server-info")), | |
2b958e79 JT |
305 | OPT__QUIET(&po_args.quiet, N_("be quiet")), |
306 | OPT_BOOL('l', "local", &po_args.local, | |
a1bbc6c0 | 307 | N_("pass --local to git-pack-objects")), |
d078d85b | 308 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, |
5cf2741c | 309 | N_("write bitmap index")), |
16d75fa4 JK |
310 | OPT_BOOL('i', "delta-islands", &use_delta_islands, |
311 | N_("pass --delta-islands to git-pack-objects")), | |
a1bbc6c0 SB |
312 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), |
313 | N_("with -A, do not loosen objects older than this")), | |
905f27b8 JK |
314 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, |
315 | N_("with -a, repack unreachable objects")), | |
2b958e79 | 316 | OPT_STRING(0, "window", &po_args.window, N_("n"), |
a1bbc6c0 | 317 | N_("size of the window used for delta compression")), |
2b958e79 | 318 | OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"), |
a1bbc6c0 | 319 | N_("same as the above, but limit memory size instead of entries count")), |
2b958e79 | 320 | OPT_STRING(0, "depth", &po_args.depth, N_("n"), |
a1bbc6c0 | 321 | N_("limits the maximum delta depth")), |
2b958e79 | 322 | OPT_STRING(0, "threads", &po_args.threads, N_("n"), |
40bcf318 | 323 | N_("limits the maximum number of threads")), |
2b958e79 | 324 | OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"), |
a1bbc6c0 | 325 | N_("maximum size of each packfile")), |
ee34a2be JK |
326 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, |
327 | N_("repack objects in packs marked with .keep")), | |
ed7e5fc3 NTND |
328 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
329 | N_("do not repack this pack")), | |
a1bbc6c0 SB |
330 | OPT_END() |
331 | }; | |
332 | ||
333 | git_config(repack_config, NULL); | |
334 | ||
335 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
336 | git_repack_usage, 0); | |
337 | ||
067fbd41 JK |
338 | if (delete_redundant && repository_format_precious_objects) |
339 | die(_("cannot delete packs in a precious-objects repo")); | |
340 | ||
905f27b8 JK |
341 | if (keep_unreachable && |
342 | (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))) | |
343 | die(_("--keep-unreachable and -A are incompatible")); | |
344 | ||
73284822 | 345 | if (write_bitmaps < 0) { |
25575015 | 346 | if (!(pack_everything & ALL_INTO_ONE) || |
7ff024e7 | 347 | !is_bare_repository()) |
25575015 | 348 | write_bitmaps = 0; |
73284822 | 349 | } |
ee34a2be | 350 | if (pack_kept_objects < 0) |
7ff024e7 | 351 | pack_kept_objects = write_bitmaps > 0; |
ee34a2be | 352 | |
1c409a70 DT |
353 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE)) |
354 | die(_(incremental_bitmap_conflict_error)); | |
355 | ||
a1bbc6c0 SB |
356 | packdir = mkpathdup("%s/pack", get_object_directory()); |
357 | packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid()); | |
358 | ||
359 | sigchain_push_common(remove_pack_on_signal); | |
360 | ||
2b958e79 JT |
361 | prepare_pack_objects(&cmd, &po_args); |
362 | ||
a2bae2dc | 363 | argv_array_push(&cmd.args, "--keep-true-parents"); |
ee34a2be | 364 | if (!pack_kept_objects) |
a2bae2dc | 365 | argv_array_push(&cmd.args, "--honor-pack-keep"); |
ed7e5fc3 NTND |
366 | for (i = 0; i < keep_pack_list.nr; i++) |
367 | argv_array_pushf(&cmd.args, "--keep-pack=%s", | |
368 | keep_pack_list.items[i].string); | |
a2bae2dc RS |
369 | argv_array_push(&cmd.args, "--non-empty"); |
370 | argv_array_push(&cmd.args, "--all"); | |
371 | argv_array_push(&cmd.args, "--reflog"); | |
372 | argv_array_push(&cmd.args, "--indexed-objects"); | |
b14ed5ad | 373 | if (has_promisor_remote()) |
0c16cd49 | 374 | argv_array_push(&cmd.args, "--exclude-promisor-objects"); |
25575015 | 375 | if (write_bitmaps > 0) |
a2bae2dc | 376 | argv_array_push(&cmd.args, "--write-bitmap-index"); |
25575015 JK |
377 | else if (write_bitmaps < 0) |
378 | argv_array_push(&cmd.args, "--write-bitmap-index-quiet"); | |
16d75fa4 JK |
379 | if (use_delta_islands) |
380 | argv_array_push(&cmd.args, "--delta-islands"); | |
a1bbc6c0 SB |
381 | |
382 | if (pack_everything & ALL_INTO_ONE) { | |
ed7e5fc3 | 383 | get_non_kept_pack_filenames(&existing_packs, &keep_pack_list); |
a1bbc6c0 | 384 | |
5d19e813 JT |
385 | repack_promisor_objects(&po_args, &names); |
386 | ||
a1bbc6c0 | 387 | if (existing_packs.nr && delete_redundant) { |
8d422993 | 388 | if (unpack_unreachable) { |
a2bae2dc | 389 | argv_array_pushf(&cmd.args, |
a1bbc6c0 SB |
390 | "--unpack-unreachable=%s", |
391 | unpack_unreachable); | |
8d422993 JK |
392 | argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); |
393 | } else if (pack_everything & LOOSEN_UNREACHABLE) { | |
a2bae2dc | 394 | argv_array_push(&cmd.args, |
a1bbc6c0 | 395 | "--unpack-unreachable"); |
905f27b8 JK |
396 | } else if (keep_unreachable) { |
397 | argv_array_push(&cmd.args, "--keep-unreachable"); | |
e26a8c47 | 398 | argv_array_push(&cmd.args, "--pack-loose-unreachable"); |
8d422993 JK |
399 | } else { |
400 | argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); | |
401 | } | |
a1bbc6c0 SB |
402 | } |
403 | } else { | |
a2bae2dc RS |
404 | argv_array_push(&cmd.args, "--unpacked"); |
405 | argv_array_push(&cmd.args, "--incremental"); | |
a1bbc6c0 SB |
406 | } |
407 | ||
a1bbc6c0 SB |
408 | cmd.no_stdin = 1; |
409 | ||
410 | ret = start_command(&cmd); | |
411 | if (ret) | |
ffc9329f | 412 | return ret; |
a1bbc6c0 | 413 | |
a1bbc6c0 | 414 | out = xfdopen(cmd.out, "r"); |
8f309aeb | 415 | while (strbuf_getline_lf(&line, out) != EOF) { |
2f0c9e9a | 416 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 417 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
a1bbc6c0 | 418 | string_list_append(&names, line.buf); |
a1bbc6c0 SB |
419 | } |
420 | fclose(out); | |
421 | ret = finish_command(&cmd); | |
422 | if (ret) | |
ffc9329f | 423 | return ret; |
a1bbc6c0 | 424 | |
2b958e79 | 425 | if (!names.nr && !po_args.quiet) |
c83d950e | 426 | printf_ln(_("Nothing new to pack.")); |
a1bbc6c0 | 427 | |
2d511cfc | 428 | close_object_store(the_repository->objects); |
5bdece0d | 429 | |
a1bbc6c0 SB |
430 | /* |
431 | * Ok we have prepared all new packfiles. | |
432 | * First see if there are packs of the same name and if so | |
433 | * if we can move them out of the way (this can happen if we | |
434 | * repacked immediately after packing fully. | |
435 | */ | |
436 | failed = 0; | |
437 | for_each_string_list_item(item, &names) { | |
b328c216 | 438 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
e3cf2303 | 439 | char *fname, *fname_old; |
525e18c0 DS |
440 | |
441 | if (!midx_cleared) { | |
1dcd9f20 | 442 | clear_midx_file(the_repository); |
525e18c0 DS |
443 | midx_cleared = 1; |
444 | } | |
445 | ||
9d7fbfd2 | 446 | fname = mkpathdup("%s/pack-%s%s", packdir, |
42a02d85 | 447 | item->string, exts[ext].name); |
a1bbc6c0 SB |
448 | if (!file_exists(fname)) { |
449 | free(fname); | |
450 | continue; | |
451 | } | |
452 | ||
e3cf2303 | 453 | fname_old = mkpathdup("%s/old-%s%s", packdir, |
42a02d85 | 454 | item->string, exts[ext].name); |
a1bbc6c0 SB |
455 | if (file_exists(fname_old)) |
456 | if (unlink(fname_old)) | |
457 | failed = 1; | |
458 | ||
459 | if (!failed && rename(fname, fname_old)) { | |
460 | free(fname); | |
e3cf2303 | 461 | free(fname_old); |
a1bbc6c0 SB |
462 | failed = 1; |
463 | break; | |
464 | } else { | |
465 | string_list_append(&rollback, fname); | |
e3cf2303 | 466 | free(fname_old); |
a1bbc6c0 SB |
467 | } |
468 | } | |
469 | if (failed) | |
470 | break; | |
471 | } | |
472 | if (failed) { | |
473 | struct string_list rollback_failure = STRING_LIST_INIT_DUP; | |
474 | for_each_string_list_item(item, &rollback) { | |
e3cf2303 | 475 | char *fname, *fname_old; |
a1bbc6c0 | 476 | fname = mkpathdup("%s/%s", packdir, item->string); |
e3cf2303 | 477 | fname_old = mkpathdup("%s/old-%s", packdir, item->string); |
a1bbc6c0 SB |
478 | if (rename(fname_old, fname)) |
479 | string_list_append(&rollback_failure, fname); | |
480 | free(fname); | |
e3cf2303 | 481 | free(fname_old); |
a1bbc6c0 SB |
482 | } |
483 | ||
484 | if (rollback_failure.nr) { | |
485 | int i; | |
486 | fprintf(stderr, | |
c83d950e NTND |
487 | _("WARNING: Some packs in use have been renamed by\n" |
488 | "WARNING: prefixing old- to their name, in order to\n" | |
489 | "WARNING: replace them with the new version of the\n" | |
490 | "WARNING: file. But the operation failed, and the\n" | |
491 | "WARNING: attempt to rename them back to their\n" | |
492 | "WARNING: original names also failed.\n" | |
493 | "WARNING: Please rename them in %s manually:\n"), packdir); | |
a1bbc6c0 SB |
494 | for (i = 0; i < rollback_failure.nr; i++) |
495 | fprintf(stderr, "WARNING: old-%s -> %s\n", | |
496 | rollback_failure.items[i].string, | |
497 | rollback_failure.items[i].string); | |
498 | } | |
499 | exit(1); | |
500 | } | |
501 | ||
502 | /* Now the ones with the same name are out of the way... */ | |
503 | for_each_string_list_item(item, &names) { | |
b328c216 | 504 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
a1bbc6c0 SB |
505 | char *fname, *fname_old; |
506 | struct stat statbuffer; | |
b77fcd1e | 507 | int exists = 0; |
a1bbc6c0 | 508 | fname = mkpathdup("%s/pack-%s%s", |
42a02d85 | 509 | packdir, item->string, exts[ext].name); |
a1bbc6c0 | 510 | fname_old = mkpathdup("%s-%s%s", |
42a02d85 | 511 | packtmp, item->string, exts[ext].name); |
a1bbc6c0 SB |
512 | if (!stat(fname_old, &statbuffer)) { |
513 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
514 | chmod(fname_old, statbuffer.st_mode); | |
b77fcd1e JK |
515 | exists = 1; |
516 | } | |
517 | if (exists || !exts[ext].optional) { | |
518 | if (rename(fname_old, fname)) | |
519 | die_errno(_("renaming '%s' failed"), fname_old); | |
a1bbc6c0 | 520 | } |
a1bbc6c0 SB |
521 | free(fname); |
522 | free(fname_old); | |
523 | } | |
524 | } | |
525 | ||
526 | /* Remove the "old-" files */ | |
527 | for_each_string_list_item(item, &names) { | |
b328c216 | 528 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
e3cf2303 JK |
529 | char *fname; |
530 | fname = mkpathdup("%s/old-%s%s", | |
531 | packdir, | |
532 | item->string, | |
533 | exts[ext].name); | |
0b63c6a5 | 534 | if (remove_path(fname)) |
e923a8ab | 535 | warning(_("failed to remove '%s'"), fname); |
e3cf2303 | 536 | free(fname); |
a1bbc6c0 SB |
537 | } |
538 | } | |
539 | ||
540 | /* End of pack replacement. */ | |
541 | ||
5d19e813 JT |
542 | reprepare_packed_git(the_repository); |
543 | ||
a1bbc6c0 | 544 | if (delete_redundant) { |
2f0c9e9a | 545 | const int hexsz = the_hash_algo->hexsz; |
4489a480 | 546 | int opts = 0; |
3383e199 | 547 | string_list_sort(&names); |
a1bbc6c0 SB |
548 | for_each_string_list_item(item, &existing_packs) { |
549 | char *sha1; | |
550 | size_t len = strlen(item->string); | |
2f0c9e9a | 551 | if (len < hexsz) |
a1bbc6c0 | 552 | continue; |
2f0c9e9a | 553 | sha1 = item->string + len - hexsz; |
a1bbc6c0 SB |
554 | if (!string_list_has_string(&names, sha1)) |
555 | remove_redundant_pack(packdir, item->string); | |
556 | } | |
2b958e79 | 557 | if (!po_args.quiet && isatty(2)) |
4489a480 RS |
558 | opts |= PRUNE_PACKED_VERBOSE; |
559 | prune_packed_objects(opts); | |
5dcfbf56 JS |
560 | |
561 | if (!keep_unreachable && | |
562 | (!(pack_everything & LOOSEN_UNREACHABLE) || | |
563 | unpack_unreachable) && | |
564 | is_repository_shallow(the_repository)) | |
565 | prune_shallow(PRUNE_QUICK); | |
a1bbc6c0 SB |
566 | } |
567 | ||
4489a480 RS |
568 | if (!no_update_server_info) |
569 | update_server_info(0); | |
a1bbc6c0 | 570 | remove_temporary_files(); |
0465a505 DS |
571 | |
572 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) | |
efbc3aee | 573 | write_midx_file(get_object_directory(), 0); |
0465a505 | 574 | |
a1bbc6c0 SB |
575 | string_list_clear(&names, 0); |
576 | string_list_clear(&rollback, 0); | |
577 | string_list_clear(&existing_packs, 0); | |
578 | strbuf_release(&line); | |
579 | ||
580 | return 0; | |
581 | } |