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