]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/repack.c: ensure that `names` is sorted
authorTaylor Blau <me@ttaylorr.com>
Fri, 20 May 2022 19:01:51 +0000 (15:01 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 May 2022 20:54:44 +0000 (13:54 -0700)
The previous patch demonstrates a scenario where the list of packs
written by `pack-objects` (and stored in the `names` string_list) is
out-of-order, and can thus cause us to delete packs we shouldn't.

This patch resolves that bug by ensuring that `names` is sorted in all
cases, not just when

    delete_redundant && pack_everything & ALL_INTO_ONE

is true.

Because we did sort `names` in that case (which, prior to `--geometric`
repacks, was the only time we would actually delete packs, this is only
a bug for `--geometric` repacks.

It would be sufficient to only sort `names` when `delete_redundant` is
set to a non-zero value. But sorting a small list of strings is cheap,
and it is defensive against future calls to `string_list_has_string()`
on this list.

Co-discovered-by: Victoria Dye <vdye@github.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repack.c
t/t7703-repack-geometric.sh

index ea56e92ad57a1423638421e105df30a111e7bd1c..0e4aae80c09913e732da4275f41f7d16b8f43587 100644 (file)
@@ -856,6 +856,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
        if (!names.nr && !po_args.quiet)
                printf_ln(_("Nothing new to pack."));
 
+       string_list_sort(&names);
+
        for_each_string_list_item(item, &names) {
                item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
        }
@@ -896,7 +898,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 
        if (delete_redundant && pack_everything & ALL_INTO_ONE) {
                const int hexsz = the_hash_algo->hexsz;
-               string_list_sort(&names);
                for_each_string_list_item(item, &existing_nonkept_packs) {
                        char *sha1;
                        size_t len = strlen(item->string);
index 2cd1de72958b94c03a59d3a4a115e562a7383867..da87f8b2d8822cd5f844cfb00bcdaf5c52117f79 100755 (executable)
@@ -231,7 +231,7 @@ test_expect_success '--geometric chooses largest MIDX preferred pack' '
        )
 '
 
-test_expect_failure '--geometric with pack.packSizeLimit' '
+test_expect_success '--geometric with pack.packSizeLimit' '
        git init pack-rewrite &&
        test_when_finished "rm -fr pack-rewrite" &&
        (