fsck --name-objects: be more careful parsing generation numbers

In 7b35efd734e (fsck_walk(): optionally name objects on the go, 2016-07-17), the `fsck` machinery learned to optionally name the objects, so that it is easier to see what part of the repository is in a bad shape, say, when objects are missing. To save on complexity, this machinery uses a parser to determine the name of a parent given a commit's name: any `~<n>` suffix is parsed and the parent's name is formed from the prefix together with `~<n+1>`. However, this parser has a bug: if it finds a suffix `<n>` that is _not_ `~<n>`, it will mistake the empty string for the prefix and `<n>` for the generation number. In other words, it will generate a name of the form `~<bogus-number>`. Let's fix this. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 10, 2021 at 18:01 UTC e89f89361cd7b706858eb22a6cf3d59d31a00acf
2 files changed +11 -4
fsck.c
+5
@@ -461,6 +461,11 @@ static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_optio
461 generation += power * (name[--len] - '0');
462 if (power > 1 && len && name[len - 1] == '~')
463 name_prefix_len = len - 1;
464 + else {
465 + /* Maybe a non-first parent, e.g. HEAD^2 */
466 + generation = 0;
467 + name_prefix_len = len;
468 + }
469 }
470 }
471
t/t1450-fsck.sh
+6 -4
@@ -658,13 +658,15 @@ test_expect_success 'fsck --name-objects' '
658 git init name-objects &&
659 (
660 cd name-objects &&
661 + git config core.logAllRefUpdates false &&
662 test_commit julius caesar.t &&
662 - test_commit augustus &&
663 - test_commit caesar &&
663 + test_commit augustus44 &&
664 + test_commit caesar &&
665 remove_object $(git rev-parse julius:caesar.t) &&
665 - test_must_fail git fsck --name-objects >out &&
666 tree=$(git rev-parse --verify julius:) &&
667 - test_i18ngrep "$tree (refs/tags/julius:" out
667 + git tag -d julius &&
668 + test_must_fail git fsck --name-objects >out &&
669 + test_i18ngrep "$tree (refs/tags/augustus44\\^:" out
670 )
671 '
672