Skip to content

Commit 2fcb03b

Browse files
ttaylorrgitster
authored andcommitted
builtin/repack.c: don't move existing packs out of the way
When 'git repack' creates a pack with the same name as any existing pack, it moves the existing one to 'old-pack-xxx.{pack,idx,...}' and then renames the new one into place. Eventually, it would be nice to have 'git repack' allow for writing a multi-pack index at the critical time (after the new packs have been written / moved into place, but before the old ones have been deleted). Guessing that this option might be called '--write-midx', this makes the following situation (where repacks are issued back-to-back without any new objects) impossible: $ git repack -adb $ git repack -adb --write-midx In the second repack, the existing packs are overwritten verbatim with the same rename-to-old sequence. At that point, the current MIDX is invalidated, since it refers to now-missing packs. So that code wants to be run after the MIDX is re-written. But (prior to this patch) the new MIDX can't be written until the new packs are moved into place. So, we have a circular dependency. This is all hypothetical, since no code currently exists to write a MIDX safely during a 'git repack' (the 'GIT_TEST_MULTI_PACK_INDEX' does so unsafely). Putting hypothetical aside, though: why do we need to rename existing packs to be prefixed with 'old-' anyway? This behavior dates all the way back to 2ad47d6 (git-repack: Be careful when updating the same pack as an existing one., 2006-06-25). 2ad47d6 is mainly concerned about a case where a newly written pack would have a different structure than its index. This used to be possible when the pack name was a hash of the set of objects. Under this naming scheme, two packs that store the same set of objects could differ in delta selection, object positioning, or both. If this happened, then any such packs would be unreadable in the instant between copying the new pack and new index (i.e., either the index or pack will be stale depending on the order that they were copied). But since 1190a1a (pack-objects: name pack files after trailer hash, 2013-12-05), this is no longer possible, since pack files are named not after their logical contents (i.e., the set of objects), but by the actual checksum of their contents. So, this old- behavior can safely go, which allows us to avoid our circular dependency above. In addition to avoiding the circular dependency, this patch also makes 'git repack' a lot simpler, since we don't have to deal with failures encountered when renaming existing packs to be prefixed with 'old-'. This patch is mostly limited to removing code paths that deal with the 'old' prefixing, with the exception of files that include the pack's name in their own filename, like .idx, .bitmap, and related files. The exception is that we want to continue to trust what pack-objects wrote. That is, it is not the case that we pretend as if pack-objects didn't write files identical to ones that already exist, but rather that we respect what pack-objects wrote as the source of truth. That cuts two ways: - If pack-objects produced an identical pack to one that already exists with a bitmap, but did not produce a bitmap, we remove the bitmap that already exists. (This behavior is codified in t7700.14). - If pack-objects produced an identical pack to one that already exists, we trust the just-written version of the coresponding .idx, .promisor, and other files over the ones that already exist. This ensures that we use the most up-to-date versions of this files, which is safe even in the face of format changes in, say, the .idx file (which would not be reflected in the .idx file's name). Helped-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 704c4a5 commit 2fcb03b

File tree

1 file changed

+14
-89
lines changed

1 file changed

+14
-89
lines changed

builtin/repack.c

Lines changed: 14 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
306306
struct string_list rollback = STRING_LIST_INIT_NODUP;
307307
struct string_list existing_packs = STRING_LIST_INIT_DUP;
308308
struct strbuf line = STRBUF_INIT;
309-
int i, ext, ret, failed;
309+
int i, ext, ret;
310310
FILE *out;
311311

312312
/* variables to be filled by option parsing */
@@ -463,109 +463,34 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
463463

464464
/*
465465
* Ok we have prepared all new packfiles.
466-
* First see if there are packs of the same name and if so
467-
* if we can move them out of the way (this can happen if we
468-
* repacked immediately after packing fully.
469466
*/
470-
failed = 0;
471467
for_each_string_list_item(item, &names) {
472468
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
473469
char *fname, *fname_old;
474470

475-
fname = mkpathdup("%s/pack-%s%s", packdir,
476-
item->string, exts[ext].name);
477-
if (!file_exists(fname)) {
478-
free(fname);
479-
continue;
480-
}
481-
482-
fname_old = mkpathdup("%s/old-%s%s", packdir,
483-
item->string, exts[ext].name);
484-
if (file_exists(fname_old))
485-
if (unlink(fname_old))
486-
failed = 1;
487-
488-
if (!failed && rename(fname, fname_old)) {
489-
free(fname);
490-
free(fname_old);
491-
failed = 1;
492-
break;
493-
} else {
494-
string_list_append(&rollback, fname);
495-
free(fname_old);
496-
}
497-
}
498-
if (failed)
499-
break;
500-
}
501-
if (failed) {
502-
struct string_list rollback_failure = STRING_LIST_INIT_DUP;
503-
for_each_string_list_item(item, &rollback) {
504-
char *fname, *fname_old;
505-
fname = mkpathdup("%s/%s", packdir, item->string);
506-
fname_old = mkpathdup("%s/old-%s", packdir, item->string);
507-
if (rename(fname_old, fname))
508-
string_list_append(&rollback_failure, fname);
509-
free(fname);
510-
free(fname_old);
511-
}
512-
513-
if (rollback_failure.nr) {
514-
int i;
515-
fprintf(stderr,
516-
_("WARNING: Some packs in use have been renamed by\n"
517-
"WARNING: prefixing old- to their name, in order to\n"
518-
"WARNING: replace them with the new version of the\n"
519-
"WARNING: file. But the operation failed, and the\n"
520-
"WARNING: attempt to rename them back to their\n"
521-
"WARNING: original names also failed.\n"
522-
"WARNING: Please rename them in %s manually:\n"), packdir);
523-
for (i = 0; i < rollback_failure.nr; i++)
524-
fprintf(stderr, "WARNING: old-%s -> %s\n",
525-
rollback_failure.items[i].string,
526-
rollback_failure.items[i].string);
527-
}
528-
exit(1);
529-
}
530-
531-
/* Now the ones with the same name are out of the way... */
532-
for_each_string_list_item(item, &names) {
533-
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
534-
char *fname, *fname_old;
535-
struct stat statbuffer;
536-
int exists = 0;
537471
fname = mkpathdup("%s/pack-%s%s",
538472
packdir, item->string, exts[ext].name);
539473
fname_old = mkpathdup("%s-%s%s",
540474
packtmp, item->string, exts[ext].name);
541-
if (!stat(fname_old, &statbuffer)) {
542-
statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
543-
chmod(fname_old, statbuffer.st_mode);
544-
exists = 1;
545-
}
546-
if (exists || !exts[ext].optional) {
475+
476+
if (((uintptr_t)item->util) & (1 << ext)) {
477+
struct stat statbuffer;
478+
if (!stat(fname_old, &statbuffer)) {
479+
statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
480+
chmod(fname_old, statbuffer.st_mode);
481+
}
482+
547483
if (rename(fname_old, fname))
548484
die_errno(_("renaming '%s' failed"), fname_old);
549-
}
550-
free(fname);
551-
free(fname_old);
552-
}
553-
}
485+
} else if (!exts[ext].optional)
486+
die(_("missing required file: %s"), fname_old);
487+
else if (unlink(fname) < 0 && errno != ENOENT)
488+
die_errno(_("could not unlink: %s"), fname);
554489

555-
/* Remove the "old-" files */
556-
for_each_string_list_item(item, &names) {
557-
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
558-
char *fname;
559-
fname = mkpathdup("%s/old-%s%s",
560-
packdir,
561-
item->string,
562-
exts[ext].name);
563-
if (remove_path(fname))
564-
warning(_("failed to remove '%s'"), fname);
565490
free(fname);
491+
free(fname_old);
566492
}
567493
}
568-
569494
/* End of pack replacement. */
570495

571496
reprepare_packed_git(the_repository);

0 commit comments

Comments
 (0)