t / t1450-fsck.shon commit t1700-split-index: date back files to avoid racy situations (c6e5607)
   1#!/bin/sh
   2
   3test_description='git fsck random collection of tests
   4
   5* (HEAD) B
   6* (master) A
   7'
   8
   9. ./test-lib.sh
  10
  11test_expect_success setup '
  12        git config gc.auto 0 &&
  13        git config i18n.commitencoding ISO-8859-1 &&
  14        test_commit A fileA one &&
  15        git config --unset i18n.commitencoding &&
  16        git checkout HEAD^0 &&
  17        test_commit B fileB two &&
  18        git tag -d A B &&
  19        git reflog expire --expire=now --all
  20'
  21
  22test_expect_success 'loose objects borrowed from alternate are not missing' '
  23        mkdir another &&
  24        (
  25                cd another &&
  26                git init &&
  27                echo ../../../.git/objects >.git/objects/info/alternates &&
  28                test_commit C fileC one &&
  29                git fsck --no-dangling >../actual 2>&1
  30        ) &&
  31        test_must_be_empty actual
  32'
  33
  34test_expect_success 'HEAD is part of refs, valid objects appear valid' '
  35        git fsck >actual 2>&1 &&
  36        test_must_be_empty actual
  37'
  38
  39# Corruption tests follow.  Make sure to remove all traces of the
  40# specific corruption you test afterwards, lest a later test trip over
  41# it.
  42
  43test_expect_success 'setup: helpers for corruption tests' '
  44        sha1_file() {
  45                remainder=${1#??} &&
  46                firsttwo=${1%$remainder} &&
  47                echo ".git/objects/$firsttwo/$remainder"
  48        } &&
  49
  50        remove_object() {
  51                rm "$(sha1_file "$1")"
  52        }
  53'
  54
  55test_expect_success 'object with bad sha1' '
  56        sha=$(echo blob | git hash-object -w --stdin) &&
  57        old=$(echo $sha | sed "s+^..+&/+") &&
  58        new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
  59        sha="$(dirname $new)$(basename $new)" &&
  60        mv .git/objects/$old .git/objects/$new &&
  61        test_when_finished "remove_object $sha" &&
  62        git update-index --add --cacheinfo 100644 $sha foo &&
  63        test_when_finished "git read-tree -u --reset HEAD" &&
  64        tree=$(git write-tree) &&
  65        test_when_finished "remove_object $tree" &&
  66        cmt=$(echo bogus | git commit-tree $tree) &&
  67        test_when_finished "remove_object $cmt" &&
  68        git update-ref refs/heads/bogus $cmt &&
  69        test_when_finished "git update-ref -d refs/heads/bogus" &&
  70
  71        test_must_fail git fsck 2>out &&
  72        cat out &&
  73        grep "$sha.*corrupt" out
  74'
  75
  76test_expect_success 'branch pointing to non-commit' '
  77        git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
  78        test_when_finished "git update-ref -d refs/heads/invalid" &&
  79        test_must_fail git fsck 2>out &&
  80        cat out &&
  81        grep "not a commit" out
  82'
  83
  84test_expect_success 'HEAD link pointing at a funny object' '
  85        test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
  86        mv .git/HEAD .git/SAVED_HEAD &&
  87        echo 0000000000000000000000000000000000000000 >.git/HEAD &&
  88        # avoid corrupt/broken HEAD from interfering with repo discovery
  89        test_must_fail env GIT_DIR=.git git fsck 2>out &&
  90        cat out &&
  91        grep "detached HEAD points" out
  92'
  93
  94test_expect_success 'HEAD link pointing at a funny place' '
  95        test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
  96        mv .git/HEAD .git/SAVED_HEAD &&
  97        echo "ref: refs/funny/place" >.git/HEAD &&
  98        # avoid corrupt/broken HEAD from interfering with repo discovery
  99        test_must_fail env GIT_DIR=.git git fsck 2>out &&
 100        cat out &&
 101        grep "HEAD points to something strange" out
 102'
 103
 104test_expect_success 'email without @ is okay' '
 105        git cat-file commit HEAD >basis &&
 106        sed "s/@/AT/" basis >okay &&
 107        new=$(git hash-object -t commit -w --stdin <okay) &&
 108        test_when_finished "remove_object $new" &&
 109        git update-ref refs/heads/bogus "$new" &&
 110        test_when_finished "git update-ref -d refs/heads/bogus" &&
 111        git fsck 2>out &&
 112        cat out &&
 113        ! grep "commit $new" out
 114'
 115
 116test_expect_success 'email with embedded > is not okay' '
 117        git cat-file commit HEAD >basis &&
 118        sed "s/@[a-z]/&>/" basis >bad-email &&
 119        new=$(git hash-object -t commit -w --stdin <bad-email) &&
 120        test_when_finished "remove_object $new" &&
 121        git update-ref refs/heads/bogus "$new" &&
 122        test_when_finished "git update-ref -d refs/heads/bogus" &&
 123        test_must_fail git fsck 2>out &&
 124        cat out &&
 125        grep "error in commit $new" out
 126'
 127
 128test_expect_success 'missing < email delimiter is reported nicely' '
 129        git cat-file commit HEAD >basis &&
 130        sed "s/<//" basis >bad-email-2 &&
 131        new=$(git hash-object -t commit -w --stdin <bad-email-2) &&
 132        test_when_finished "remove_object $new" &&
 133        git update-ref refs/heads/bogus "$new" &&
 134        test_when_finished "git update-ref -d refs/heads/bogus" &&
 135        test_must_fail git fsck 2>out &&
 136        cat out &&
 137        grep "error in commit $new.* - bad name" out
 138'
 139
 140test_expect_success 'missing email is reported nicely' '
 141        git cat-file commit HEAD >basis &&
 142        sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
 143        new=$(git hash-object -t commit -w --stdin <bad-email-3) &&
 144        test_when_finished "remove_object $new" &&
 145        git update-ref refs/heads/bogus "$new" &&
 146        test_when_finished "git update-ref -d refs/heads/bogus" &&
 147        test_must_fail git fsck 2>out &&
 148        cat out &&
 149        grep "error in commit $new.* - missing email" out
 150'
 151
 152test_expect_success '> in name is reported' '
 153        git cat-file commit HEAD >basis &&
 154        sed "s/ </> </" basis >bad-email-4 &&
 155        new=$(git hash-object -t commit -w --stdin <bad-email-4) &&
 156        test_when_finished "remove_object $new" &&
 157        git update-ref refs/heads/bogus "$new" &&
 158        test_when_finished "git update-ref -d refs/heads/bogus" &&
 159        test_must_fail git fsck 2>out &&
 160        cat out &&
 161        grep "error in commit $new" out
 162'
 163
 164# date is 2^64 + 1
 165test_expect_success 'integer overflow in timestamps is reported' '
 166        git cat-file commit HEAD >basis &&
 167        sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
 168                <basis >bad-timestamp &&
 169        new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
 170        test_when_finished "remove_object $new" &&
 171        git update-ref refs/heads/bogus "$new" &&
 172        test_when_finished "git update-ref -d refs/heads/bogus" &&
 173        test_must_fail git fsck 2>out &&
 174        cat out &&
 175        grep "error in commit $new.*integer overflow" out
 176'
 177
 178test_expect_success 'commit with NUL in header' '
 179        git cat-file commit HEAD >basis &&
 180        sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
 181        new=$(git hash-object -t commit -w --stdin <commit-NUL-header) &&
 182        test_when_finished "remove_object $new" &&
 183        git update-ref refs/heads/bogus "$new" &&
 184        test_when_finished "git update-ref -d refs/heads/bogus" &&
 185        test_must_fail git fsck 2>out &&
 186        cat out &&
 187        grep "error in commit $new.*unterminated header: NUL at offset" out
 188'
 189
 190test_expect_success 'tree object with duplicate entries' '
 191        test_when_finished "for i in \$T; do remove_object \$i; done" &&
 192        T=$(
 193                GIT_INDEX_FILE=test-index &&
 194                export GIT_INDEX_FILE &&
 195                rm -f test-index &&
 196                >x &&
 197                git add x &&
 198                git rev-parse :x &&
 199                T=$(git write-tree) &&
 200                echo $T &&
 201                (
 202                        git cat-file tree $T &&
 203                        git cat-file tree $T
 204                ) |
 205                git hash-object -w -t tree --stdin
 206        ) &&
 207        test_must_fail git fsck 2>out &&
 208        grep "error in tree .*contains duplicate file entries" out
 209'
 210
 211test_expect_success 'unparseable tree object' '
 212        test_when_finished "git update-ref -d refs/heads/wrong" &&
 213        test_when_finished "remove_object \$tree_sha1" &&
 214        test_when_finished "remove_object \$commit_sha1" &&
 215        tree_sha1=$(printf "100644 \0twenty-bytes-of-junk" | git hash-object -t tree --stdin -w --literally) &&
 216        commit_sha1=$(git commit-tree $tree_sha1) &&
 217        git update-ref refs/heads/wrong $commit_sha1 &&
 218        test_must_fail git fsck 2>out &&
 219        test_i18ngrep "error: empty filename in tree entry" out &&
 220        test_i18ngrep "$tree_sha1" out &&
 221        test_i18ngrep ! "fatal: empty filename in tree entry" out
 222'
 223
 224hex2oct() {
 225        perl -ne 'printf "\\%03o", hex for /../g'
 226}
 227
 228test_expect_success 'tree entry with type mismatch' '
 229        test_when_finished "remove_object \$blob" &&
 230        test_when_finished "remove_object \$tree" &&
 231        test_when_finished "remove_object \$commit" &&
 232        test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
 233        blob=$(echo blob | git hash-object -w --stdin) &&
 234        blob_bin=$(echo $blob | hex2oct) &&
 235        tree=$(
 236                printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
 237                git hash-object -t tree --stdin -w --literally
 238        ) &&
 239        commit=$(git commit-tree $tree) &&
 240        git update-ref refs/heads/type_mismatch $commit &&
 241        test_must_fail git fsck >out 2>&1 &&
 242        test_i18ngrep "is a blob, not a tree" out &&
 243        test_i18ngrep ! "dangling blob" out
 244'
 245
 246test_expect_success 'tag pointing to nonexistent' '
 247        cat >invalid-tag <<-\EOF &&
 248        object ffffffffffffffffffffffffffffffffffffffff
 249        type commit
 250        tag invalid
 251        tagger T A Gger <tagger@example.com> 1234567890 -0000
 252
 253        This is an invalid tag.
 254        EOF
 255
 256        tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
 257        test_when_finished "remove_object $tag" &&
 258        echo $tag >.git/refs/tags/invalid &&
 259        test_when_finished "git update-ref -d refs/tags/invalid" &&
 260        test_must_fail git fsck --tags >out &&
 261        cat out &&
 262        grep "broken link" out
 263'
 264
 265test_expect_success 'tag pointing to something else than its type' '
 266        sha=$(echo blob | git hash-object -w --stdin) &&
 267        test_when_finished "remove_object $sha" &&
 268        cat >wrong-tag <<-EOF &&
 269        object $sha
 270        type commit
 271        tag wrong
 272        tagger T A Gger <tagger@example.com> 1234567890 -0000
 273
 274        This is an invalid tag.
 275        EOF
 276
 277        tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
 278        test_when_finished "remove_object $tag" &&
 279        echo $tag >.git/refs/tags/wrong &&
 280        test_when_finished "git update-ref -d refs/tags/wrong" &&
 281        test_must_fail git fsck --tags
 282'
 283
 284test_expect_success 'tag with incorrect tag name & missing tagger' '
 285        sha=$(git rev-parse HEAD) &&
 286        cat >wrong-tag <<-EOF &&
 287        object $sha
 288        type commit
 289        tag wrong name format
 290
 291        This is an invalid tag.
 292        EOF
 293
 294        tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
 295        test_when_finished "remove_object $tag" &&
 296        echo $tag >.git/refs/tags/wrong &&
 297        test_when_finished "git update-ref -d refs/tags/wrong" &&
 298        git fsck --tags 2>out &&
 299
 300        cat >expect <<-EOF &&
 301        warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
 302        warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
 303        EOF
 304        test_cmp expect out
 305'
 306
 307test_expect_success 'tag with bad tagger' '
 308        sha=$(git rev-parse HEAD) &&
 309        cat >wrong-tag <<-EOF &&
 310        object $sha
 311        type commit
 312        tag not-quite-wrong
 313        tagger Bad Tagger Name
 314
 315        This is an invalid tag.
 316        EOF
 317
 318        tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
 319        test_when_finished "remove_object $tag" &&
 320        echo $tag >.git/refs/tags/wrong &&
 321        test_when_finished "git update-ref -d refs/tags/wrong" &&
 322        test_must_fail git fsck --tags 2>out &&
 323        grep "error in tag .*: invalid author/committer" out
 324'
 325
 326test_expect_success 'tag with NUL in header' '
 327        sha=$(git rev-parse HEAD) &&
 328        q_to_nul >tag-NUL-header <<-EOF &&
 329        object $sha
 330        type commit
 331        tag contains-Q-in-header
 332        tagger T A Gger <tagger@example.com> 1234567890 -0000
 333
 334        This is an invalid tag.
 335        EOF
 336
 337        tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
 338        test_when_finished "remove_object $tag" &&
 339        echo $tag >.git/refs/tags/wrong &&
 340        test_when_finished "git update-ref -d refs/tags/wrong" &&
 341        test_must_fail git fsck --tags 2>out &&
 342        cat out &&
 343        grep "error in tag $tag.*unterminated header: NUL at offset" out
 344'
 345
 346test_expect_success 'cleaned up' '
 347        git fsck >actual 2>&1 &&
 348        test_must_be_empty actual
 349'
 350
 351test_expect_success 'rev-list --verify-objects' '
 352        git rev-list --verify-objects --all >/dev/null 2>out &&
 353        test_must_be_empty out
 354'
 355
 356test_expect_success 'rev-list --verify-objects with bad sha1' '
 357        sha=$(echo blob | git hash-object -w --stdin) &&
 358        old=$(echo $sha | sed "s+^..+&/+") &&
 359        new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
 360        sha="$(dirname $new)$(basename $new)" &&
 361        mv .git/objects/$old .git/objects/$new &&
 362        test_when_finished "remove_object $sha" &&
 363        git update-index --add --cacheinfo 100644 $sha foo &&
 364        test_when_finished "git read-tree -u --reset HEAD" &&
 365        tree=$(git write-tree) &&
 366        test_when_finished "remove_object $tree" &&
 367        cmt=$(echo bogus | git commit-tree $tree) &&
 368        test_when_finished "remove_object $cmt" &&
 369        git update-ref refs/heads/bogus $cmt &&
 370        test_when_finished "git update-ref -d refs/heads/bogus" &&
 371
 372        test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
 373        cat out &&
 374        test_i18ngrep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out
 375'
 376
 377test_expect_success 'force fsck to ignore double author' '
 378        git cat-file commit HEAD >basis &&
 379        sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
 380        new=$(git hash-object -t commit -w --stdin <multiple-authors) &&
 381        test_when_finished "remove_object $new" &&
 382        git update-ref refs/heads/bogus "$new" &&
 383        test_when_finished "git update-ref -d refs/heads/bogus" &&
 384        test_must_fail git fsck &&
 385        git -c fsck.multipleAuthors=ignore fsck
 386'
 387
 388_bz='\0'
 389_bz5="$_bz$_bz$_bz$_bz$_bz"
 390_bz20="$_bz5$_bz5$_bz5$_bz5"
 391
 392test_expect_success 'fsck notices blob entry pointing to null sha1' '
 393        (git init null-blob &&
 394         cd null-blob &&
 395         sha=$(printf "100644 file$_bz$_bz20" |
 396               git hash-object -w --stdin -t tree) &&
 397          git fsck 2>out &&
 398          cat out &&
 399          grep "warning.*null sha1" out
 400        )
 401'
 402
 403test_expect_success 'fsck notices submodule entry pointing to null sha1' '
 404        (git init null-commit &&
 405         cd null-commit &&
 406         sha=$(printf "160000 submodule$_bz$_bz20" |
 407               git hash-object -w --stdin -t tree) &&
 408          git fsck 2>out &&
 409          cat out &&
 410          grep "warning.*null sha1" out
 411        )
 412'
 413
 414while read name path pretty; do
 415        while read mode type; do
 416                : ${pretty:=$path}
 417                test_expect_success "fsck notices $pretty as $type" '
 418                (
 419                        git init $name-$type &&
 420                        cd $name-$type &&
 421                        echo content >file &&
 422                        git add file &&
 423                        git commit -m base &&
 424                        blob=$(git rev-parse :file) &&
 425                        tree=$(git rev-parse HEAD^{tree}) &&
 426                        value=$(eval "echo \$$type") &&
 427                        printf "$mode $type %s\t%s" "$value" "$path" >bad &&
 428                        bad_tree=$(git mktree <bad) &&
 429                        git fsck 2>out &&
 430                        cat out &&
 431                        grep "warning.*tree $bad_tree" out
 432                )'
 433        done <<-\EOF
 434        100644 blob
 435        040000 tree
 436        EOF
 437done <<-EOF
 438dot .
 439dotdot ..
 440dotgit .git
 441dotgit-case .GIT
 442dotgit-unicode .gI${u200c}T .gI{u200c}T
 443dotgit-case2 .Git
 444git-tilde1 git~1
 445dotgitdot .git.
 446dot-backslash-case .\\\\.GIT\\\\foobar
 447dotgit-case-backslash .git\\\\foobar
 448EOF
 449
 450test_expect_success 'fsck allows .Ňit' '
 451        (
 452                git init not-dotgit &&
 453                cd not-dotgit &&
 454                echo content >file &&
 455                git add file &&
 456                git commit -m base &&
 457                blob=$(git rev-parse :file) &&
 458                printf "100644 blob $blob\t.\\305\\207it" >tree &&
 459                tree=$(git mktree <tree) &&
 460                git fsck 2>err &&
 461                test_line_count = 0 err
 462        )
 463'
 464
 465test_expect_success 'NUL in commit' '
 466        rm -fr nul-in-commit &&
 467        git init nul-in-commit &&
 468        (
 469                cd nul-in-commit &&
 470                git commit --allow-empty -m "initial commitQNUL after message" &&
 471                git cat-file commit HEAD >original &&
 472                q_to_nul <original >munged &&
 473                git hash-object -w -t commit --stdin <munged >name &&
 474                git branch bad $(cat name) &&
 475
 476                test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
 477                grep nulInCommit warn.1 &&
 478                git fsck 2>warn.2 &&
 479                grep nulInCommit warn.2
 480        )
 481'
 482
 483# create a static test repo which is broken by omitting
 484# one particular object ($1, which is looked up via rev-parse
 485# in the new repository).
 486create_repo_missing () {
 487        rm -rf missing &&
 488        git init missing &&
 489        (
 490                cd missing &&
 491                git commit -m one --allow-empty &&
 492                mkdir subdir &&
 493                echo content >subdir/file &&
 494                git add subdir/file &&
 495                git commit -m two &&
 496                unrelated=$(echo unrelated | git hash-object --stdin -w) &&
 497                git tag -m foo tag $unrelated &&
 498                sha1=$(git rev-parse --verify "$1") &&
 499                path=$(echo $sha1 | sed 's|..|&/|') &&
 500                rm .git/objects/$path
 501        )
 502}
 503
 504test_expect_success 'fsck notices missing blob' '
 505        create_repo_missing HEAD:subdir/file &&
 506        test_must_fail git -C missing fsck
 507'
 508
 509test_expect_success 'fsck notices missing subtree' '
 510        create_repo_missing HEAD:subdir &&
 511        test_must_fail git -C missing fsck
 512'
 513
 514test_expect_success 'fsck notices missing root tree' '
 515        create_repo_missing HEAD^{tree} &&
 516        test_must_fail git -C missing fsck
 517'
 518
 519test_expect_success 'fsck notices missing parent' '
 520        create_repo_missing HEAD^ &&
 521        test_must_fail git -C missing fsck
 522'
 523
 524test_expect_success 'fsck notices missing tagged object' '
 525        create_repo_missing tag^{blob} &&
 526        test_must_fail git -C missing fsck
 527'
 528
 529test_expect_success 'fsck notices ref pointing to missing commit' '
 530        create_repo_missing HEAD &&
 531        test_must_fail git -C missing fsck
 532'
 533
 534test_expect_success 'fsck notices ref pointing to missing tag' '
 535        create_repo_missing tag &&
 536        test_must_fail git -C missing fsck
 537'
 538
 539test_expect_success 'fsck --connectivity-only' '
 540        rm -rf connectivity-only &&
 541        git init connectivity-only &&
 542        (
 543                cd connectivity-only &&
 544                touch empty &&
 545                git add empty &&
 546                test_commit empty &&
 547
 548                # Drop the index now; we want to be sure that we
 549                # recursively notice the broken objects
 550                # because they are reachable from refs, not because
 551                # they are in the index.
 552                rm -f .git/index &&
 553
 554                # corrupt the blob, but in a way that we can still identify
 555                # its type. That lets us see that --connectivity-only is
 556                # not actually looking at the contents, but leaves it
 557                # free to examine the type if it chooses.
 558                empty=.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 &&
 559                blob=$(echo unrelated | git hash-object -w --stdin) &&
 560                mv -f $(sha1_file $blob) $empty &&
 561
 562                test_must_fail git fsck --strict &&
 563                git fsck --strict --connectivity-only &&
 564                tree=$(git rev-parse HEAD:) &&
 565                suffix=${tree#??} &&
 566                tree=.git/objects/${tree%$suffix}/$suffix &&
 567                rm -f $tree &&
 568                echo invalid >$tree &&
 569                test_must_fail git fsck --strict --connectivity-only
 570        )
 571'
 572
 573test_expect_success 'fsck --connectivity-only with explicit head' '
 574        rm -rf connectivity-only &&
 575        git init connectivity-only &&
 576        (
 577                cd connectivity-only &&
 578                test_commit foo &&
 579                rm -f .git/index &&
 580                tree=$(git rev-parse HEAD^{tree}) &&
 581                remove_object $(git rev-parse HEAD:foo.t) &&
 582                test_must_fail git fsck --connectivity-only $tree
 583        )
 584'
 585
 586test_expect_success 'fsck --name-objects' '
 587        rm -rf name-objects &&
 588        git init name-objects &&
 589        (
 590                cd name-objects &&
 591                test_commit julius caesar.t &&
 592                test_commit augustus &&
 593                test_commit caesar &&
 594                remove_object $(git rev-parse julius:caesar.t) &&
 595                test_must_fail git fsck --name-objects >out &&
 596                tree=$(git rev-parse --verify julius:) &&
 597                egrep "$tree \((refs/heads/master|HEAD)@\{[0-9]*\}:" out
 598        )
 599'
 600
 601test_expect_success 'alternate objects are correctly blamed' '
 602        test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
 603        git init --bare alt.git &&
 604        echo "../../alt.git/objects" >.git/objects/info/alternates &&
 605        mkdir alt.git/objects/12 &&
 606        >alt.git/objects/12/34567890123456789012345678901234567890 &&
 607        test_must_fail git fsck >out 2>&1 &&
 608        grep alt.git out
 609'
 610
 611test_expect_success 'fsck errors in packed objects' '
 612        git cat-file commit HEAD >basis &&
 613        sed "s/</one/" basis >one &&
 614        sed "s/</foo/" basis >two &&
 615        one=$(git hash-object -t commit -w one) &&
 616        two=$(git hash-object -t commit -w two) &&
 617        pack=$(
 618                {
 619                        echo $one &&
 620                        echo $two
 621                } | git pack-objects .git/objects/pack/pack
 622        ) &&
 623        test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
 624        remove_object $one &&
 625        remove_object $two &&
 626        test_must_fail git fsck 2>out &&
 627        grep "error in commit $one.* - bad name" out &&
 628        grep "error in commit $two.* - bad name" out &&
 629        ! grep corrupt out
 630'
 631
 632test_expect_success 'fsck fails on corrupt packfile' '
 633        hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
 634        pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
 635
 636        # Corrupt the first byte of the first object. (It contains 3 type bits,
 637        # at least one of which is not zero, so setting the first byte to 0 is
 638        # sufficient.)
 639        chmod a+w .git/objects/pack/pack-$pack.pack &&
 640        printf '\0' | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
 641
 642        test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
 643        remove_object $hsh &&
 644        test_must_fail git fsck 2>out &&
 645        test_i18ngrep "checksum mismatch" out
 646'
 647
 648test_expect_success 'fsck finds problems in duplicate loose objects' '
 649        rm -rf broken-duplicate &&
 650        git init broken-duplicate &&
 651        (
 652                cd broken-duplicate &&
 653                test_commit duplicate &&
 654                # no "-d" here, so we end up with duplicates
 655                git repack &&
 656                # now corrupt the loose copy
 657                file=$(sha1_file "$(git rev-parse HEAD)") &&
 658                rm "$file" &&
 659                echo broken >"$file" &&
 660                test_must_fail git fsck
 661        )
 662'
 663
 664test_expect_success 'fsck detects trailing loose garbage (commit)' '
 665        git cat-file commit HEAD >basis &&
 666        echo bump-commit-sha1 >>basis &&
 667        commit=$(git hash-object -w -t commit basis) &&
 668        file=$(sha1_file $commit) &&
 669        test_when_finished "remove_object $commit" &&
 670        chmod +w "$file" &&
 671        echo garbage >>"$file" &&
 672        test_must_fail git fsck 2>out &&
 673        test_i18ngrep "garbage.*$commit" out
 674'
 675
 676test_expect_success 'fsck detects trailing loose garbage (blob)' '
 677        blob=$(echo trailing | git hash-object -w --stdin) &&
 678        file=$(sha1_file $blob) &&
 679        test_when_finished "remove_object $blob" &&
 680        chmod +w "$file" &&
 681        echo garbage >>"$file" &&
 682        test_must_fail git fsck 2>out &&
 683        test_i18ngrep "garbage.*$blob" out
 684'
 685
 686# for each of type, we have one version which is referenced by another object
 687# (and so while unreachable, not dangling), and another variant which really is
 688# dangling.
 689test_expect_success 'fsck notices dangling objects' '
 690        git init dangling &&
 691        (
 692                cd dangling &&
 693                blob=$(echo not-dangling | git hash-object -w --stdin) &&
 694                dblob=$(echo dangling | git hash-object -w --stdin) &&
 695                tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
 696                dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
 697                commit=$(git commit-tree $tree) &&
 698                dcommit=$(git commit-tree -p $commit $tree) &&
 699
 700                cat >expect <<-EOF &&
 701                dangling blob $dblob
 702                dangling commit $dcommit
 703                dangling tree $dtree
 704                EOF
 705
 706                git fsck >actual &&
 707                # the output order is non-deterministic, as it comes from a hash
 708                sort <actual >actual.sorted &&
 709                test_cmp expect actual.sorted
 710        )
 711'
 712
 713test_expect_success 'fsck $name notices bogus $name' '
 714        test_must_fail git fsck bogus &&
 715        test_must_fail git fsck $ZERO_OID
 716'
 717
 718test_expect_success 'bogus head does not fallback to all heads' '
 719        # set up a case that will cause a reachability complaint
 720        echo to-be-deleted >foo &&
 721        git add foo &&
 722        blob=$(git rev-parse :foo) &&
 723        test_when_finished "git rm --cached foo" &&
 724        remove_object $blob &&
 725        test_must_fail git fsck $ZERO_OID >out 2>&1 &&
 726        ! grep $blob out
 727'
 728
 729# Corrupt the checksum on the index.
 730# Add 1 to the last byte in the SHA.
 731corrupt_index_checksum () {
 732    perl -w -e '
 733        use Fcntl ":seek";
 734        open my $fh, "+<", ".git/index" or die "open: $!";
 735        binmode $fh;
 736        seek $fh, -1, SEEK_END or die "seek: $!";
 737        read $fh, my $in_byte, 1 or die "read: $!";
 738
 739        $in_value = unpack("C", $in_byte);
 740        $out_value = ($in_value + 1) & 255;
 741
 742        $out_byte = pack("C", $out_value);
 743
 744        seek $fh, -1, SEEK_END or die "seek: $!";
 745        print $fh $out_byte;
 746        close $fh or die "close: $!";
 747    '
 748}
 749
 750# Corrupt the checksum on the index and then
 751# verify that only fsck notices.
 752test_expect_success 'detect corrupt index file in fsck' '
 753        cp .git/index .git/index.backup &&
 754        test_when_finished "mv .git/index.backup .git/index" &&
 755        corrupt_index_checksum &&
 756        test_must_fail git fsck --cache 2>errors &&
 757        grep "bad index file" errors
 758'
 759
 760test_done