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