759cf12e16bcddc3dab9d6b3ad8abd5590245a94
[git/git.git] / t / t1450-fsck.sh
1 #!/bin/sh
2
3 test_description='git fsck random collection of tests'
4
5 . ./test-lib.sh
6
7 test_expect_success setup '
8 git config i18n.commitencoding ISO-8859-1 &&
9 test_commit A fileA one &&
10 git config --unset i18n.commitencoding &&
11 git checkout HEAD^0 &&
12 test_commit B fileB two &&
13 git tag -d A B &&
14 git reflog expire --expire=now --all
15 '
16
17 test_expect_success 'HEAD is part of refs' '
18 test 0 = $(git fsck | wc -l)
19 '
20
21 test_expect_success 'loose objects borrowed from alternate are not missing' '
22 mkdir another &&
23 (
24 cd another &&
25 git init &&
26 echo ../../../.git/objects >.git/objects/info/alternates &&
27 test_commit C fileC one &&
28 git fsck >out &&
29 ! grep "missing blob" out
30 )
31 '
32
33 test_expect_success 'valid objects appear valid' '
34 { git fsck 2>out; true; } &&
35 ! grep error out &&
36 ! grep fatal out
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
43 test_expect_success 'object with bad sha1' '
44 sha=$(echo blob | git hash-object -w --stdin) &&
45 echo $sha &&
46 old=$(echo $sha | sed "s+^..+&/+") &&
47 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
48 sha="$(dirname $new)$(basename $new)"
49 mv .git/objects/$old .git/objects/$new &&
50 git update-index --add --cacheinfo 100644 $sha foo &&
51 tree=$(git write-tree) &&
52 cmt=$(echo bogus | git commit-tree $tree) &&
53 git update-ref refs/heads/bogus $cmt &&
54 (git fsck 2>out; true) &&
55 grep "$sha.*corrupt" out &&
56 rm -f .git/objects/$new &&
57 git update-ref -d refs/heads/bogus &&
58 git read-tree -u --reset HEAD
59 '
60
61 test_expect_success 'branch pointing to non-commit' '
62 git rev-parse HEAD^{tree} > .git/refs/heads/invalid &&
63 git fsck 2>out &&
64 grep "not a commit" out &&
65 git update-ref -d refs/heads/invalid
66 '
67
68 new=nothing
69 test_expect_success 'email without @ is okay' '
70 git cat-file commit HEAD >basis &&
71 sed "s/@/AT/" basis >okay &&
72 new=$(git hash-object -t commit -w --stdin <okay) &&
73 echo "$new" &&
74 git update-ref refs/heads/bogus "$new" &&
75 git fsck 2>out &&
76 cat out &&
77 ! grep "error in commit $new" out
78 '
79 git update-ref -d refs/heads/bogus
80 rm -f ".git/objects/$new"
81
82 new=nothing
83 test_expect_success 'email with embedded > is not okay' '
84 git cat-file commit HEAD >basis &&
85 sed "s/@[a-z]/&>/" basis >bad-email &&
86 new=$(git hash-object -t commit -w --stdin <bad-email) &&
87 echo "$new" &&
88 git update-ref refs/heads/bogus "$new" &&
89 git fsck 2>out &&
90 cat out &&
91 grep "error in commit $new" out
92 '
93 git update-ref -d refs/heads/bogus
94 rm -f ".git/objects/$new"
95
96 cat > invalid-tag <<EOF
97 object ffffffffffffffffffffffffffffffffffffffff
98 type commit
99 tag invalid
100 tagger T A Gger <tagger@example.com> 1234567890 -0000
101
102 This is an invalid tag.
103 EOF
104
105 test_expect_success 'tag pointing to nonexistent' '
106 tag=$(git hash-object -t tag -w --stdin < invalid-tag) &&
107 echo $tag > .git/refs/tags/invalid &&
108 test_must_fail git fsck --tags >out &&
109 cat out &&
110 grep "broken link" out &&
111 rm .git/refs/tags/invalid
112 '
113
114 cat > wrong-tag <<EOF
115 object $(echo blob | git hash-object -w --stdin)
116 type commit
117 tag wrong
118 tagger T A Gger <tagger@example.com> 1234567890 -0000
119
120 This is an invalid tag.
121 EOF
122
123 test_expect_success 'tag pointing to something else than its type' '
124 tag=$(git hash-object -t tag -w --stdin < wrong-tag) &&
125 echo $tag > .git/refs/tags/wrong &&
126 test_must_fail git fsck --tags 2>out &&
127 cat out &&
128 grep "error in tag.*broken links" out &&
129 rm .git/refs/tags/wrong
130 '
131
132
133
134 test_done