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