Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 3 | test_description='git fsck random collection of tests |
| 4 | |
| 5 | * (HEAD) B |
| 6 | * (master) A |
| 7 | ' |
Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 8 | |
| 9 | . ./test-lib.sh |
| 10 | |
| 11 | test_expect_success setup ' |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 12 | git config gc.auto 0 && |
Jonathan Nieder | 0adc6a3 | 2010-05-26 16:50:34 -0500 | [diff] [blame] | 13 | git config i18n.commitencoding ISO-8859-1 && |
Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 14 | test_commit A fileA one && |
Jonathan Nieder | 0adc6a3 | 2010-05-26 16:50:34 -0500 | [diff] [blame] | 15 | git config --unset i18n.commitencoding && |
Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 16 | git checkout HEAD^0 && |
| 17 | test_commit B fileB two && |
| 18 | git tag -d A B && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 19 | git reflog expire --expire=now --all && |
| 20 | >empty |
Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 21 | ' |
| 22 | |
Junio C Hamano | e15ef66 | 2009-01-30 00:50:54 -0800 | [diff] [blame] | 23 | test_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 && |
Junio C Hamano | c6a13b2 | 2012-02-28 14:55:39 -0800 | [diff] [blame] | 30 | git fsck --no-dangling >../actual 2>&1 |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 31 | ) && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 32 | test_cmp empty actual |
Junio C Hamano | e15ef66 | 2009-01-30 00:50:54 -0800 | [diff] [blame] | 33 | ' |
| 34 | |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 35 | test_expect_success 'HEAD is part of refs, valid objects appear valid' ' |
| 36 | git fsck >actual 2>&1 && |
| 37 | test_cmp empty actual |
Jonathan Nieder | 0adc6a3 | 2010-05-26 16:50:34 -0500 | [diff] [blame] | 38 | ' |
| 39 | |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 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 | |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 44 | test_expect_success 'setup: helpers for corruption tests' ' |
| 45 | sha1_file() { |
| 46 | echo "$*" | sed "s#..#.git/objects/&/#" |
| 47 | } && |
| 48 | |
| 49 | remove_object() { |
| 50 | file=$(sha1_file "$*") && |
| 51 | test -e "$file" && |
| 52 | rm -f "$file" |
| 53 | } |
| 54 | ' |
| 55 | |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 56 | test_expect_success 'object with bad sha1' ' |
| 57 | sha=$(echo blob | git hash-object -w --stdin) && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 58 | old=$(echo $sha | sed "s+^..+&/+") && |
| 59 | new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff && |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 60 | sha="$(dirname $new)$(basename $new)" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 61 | mv .git/objects/$old .git/objects/$new && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 62 | test_when_finished "remove_object $sha" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 63 | git update-index --add --cacheinfo 100644 $sha foo && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 64 | test_when_finished "git read-tree -u --reset HEAD" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 65 | tree=$(git write-tree) && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 66 | test_when_finished "remove_object $tree" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 67 | cmt=$(echo bogus | git commit-tree $tree) && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 68 | test_when_finished "remove_object $cmt" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 69 | git update-ref refs/heads/bogus $cmt && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 70 | test_when_finished "git update-ref -d refs/heads/bogus" && |
| 71 | |
| 72 | test_might_fail git fsck 2>out && |
| 73 | cat out && |
| 74 | grep "$sha.*corrupt" out |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 75 | ' |
| 76 | |
| 77 | test_expect_success 'branch pointing to non-commit' ' |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 78 | git rev-parse HEAD^{tree} >.git/refs/heads/invalid && |
| 79 | test_when_finished "git update-ref -d refs/heads/invalid" && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 80 | git fsck 2>out && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 81 | cat out && |
| 82 | grep "not a commit" out |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 83 | ' |
| 84 | |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 85 | test_expect_success 'email without @ is okay' ' |
| 86 | git cat-file commit HEAD >basis && |
| 87 | sed "s/@/AT/" basis >okay && |
| 88 | new=$(git hash-object -t commit -w --stdin <okay) && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 89 | test_when_finished "remove_object $new" && |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 90 | git update-ref refs/heads/bogus "$new" && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 91 | test_when_finished "git update-ref -d refs/heads/bogus" && |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 92 | git fsck 2>out && |
| 93 | cat out && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 94 | ! grep "commit $new" out |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 95 | ' |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 96 | |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 97 | test_expect_success 'email with embedded > is not okay' ' |
| 98 | git cat-file commit HEAD >basis && |
| 99 | sed "s/@[a-z]/&>/" basis >bad-email && |
| 100 | new=$(git hash-object -t commit -w --stdin <bad-email) && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 101 | test_when_finished "remove_object $new" && |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 102 | git update-ref refs/heads/bogus "$new" && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 103 | test_when_finished "git update-ref -d refs/heads/bogus" && |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 104 | git fsck 2>out && |
| 105 | cat out && |
| 106 | grep "error in commit $new" out |
| 107 | ' |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 108 | |
Dmitry Ivankov | 53f53cf | 2011-08-11 16:21:10 +0600 | [diff] [blame] | 109 | test_expect_success 'missing < email delimiter is reported nicely' ' |
Dmitry Ivankov | e3c9812 | 2011-08-11 16:21:09 +0600 | [diff] [blame] | 110 | git cat-file commit HEAD >basis && |
| 111 | sed "s/<//" basis >bad-email-2 && |
| 112 | new=$(git hash-object -t commit -w --stdin <bad-email-2) && |
| 113 | test_when_finished "remove_object $new" && |
| 114 | git update-ref refs/heads/bogus "$new" && |
| 115 | test_when_finished "git update-ref -d refs/heads/bogus" && |
| 116 | git fsck 2>out && |
| 117 | cat out && |
| 118 | grep "error in commit $new.* - bad name" out |
| 119 | ' |
| 120 | |
Dmitry Ivankov | 53f53cf | 2011-08-11 16:21:10 +0600 | [diff] [blame] | 121 | test_expect_success 'missing email is reported nicely' ' |
Dmitry Ivankov | e3c9812 | 2011-08-11 16:21:09 +0600 | [diff] [blame] | 122 | git cat-file commit HEAD >basis && |
| 123 | sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 && |
| 124 | new=$(git hash-object -t commit -w --stdin <bad-email-3) && |
| 125 | test_when_finished "remove_object $new" && |
| 126 | git update-ref refs/heads/bogus "$new" && |
| 127 | test_when_finished "git update-ref -d refs/heads/bogus" && |
| 128 | git fsck 2>out && |
| 129 | cat out && |
| 130 | grep "error in commit $new.* - missing email" out |
| 131 | ' |
| 132 | |
Dmitry Ivankov | 53f53cf | 2011-08-11 16:21:10 +0600 | [diff] [blame] | 133 | test_expect_success '> in name is reported' ' |
Dmitry Ivankov | e3c9812 | 2011-08-11 16:21:09 +0600 | [diff] [blame] | 134 | git cat-file commit HEAD >basis && |
| 135 | sed "s/ </> </" basis >bad-email-4 && |
| 136 | new=$(git hash-object -t commit -w --stdin <bad-email-4) && |
| 137 | test_when_finished "remove_object $new" && |
| 138 | git update-ref refs/heads/bogus "$new" && |
| 139 | test_when_finished "git update-ref -d refs/heads/bogus" && |
| 140 | git fsck 2>out && |
| 141 | cat out && |
| 142 | grep "error in commit $new" out |
| 143 | ' |
| 144 | |
Thomas Rast | 4551d03 | 2010-02-20 01:18:44 +0100 | [diff] [blame] | 145 | test_expect_success 'tag pointing to nonexistent' ' |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 146 | cat >invalid-tag <<-\EOF && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 147 | object ffffffffffffffffffffffffffffffffffffffff |
| 148 | type commit |
| 149 | tag invalid |
| 150 | tagger T A Gger <tagger@example.com> 1234567890 -0000 |
| 151 | |
| 152 | This is an invalid tag. |
| 153 | EOF |
| 154 | |
| 155 | tag=$(git hash-object -t tag -w --stdin <invalid-tag) && |
| 156 | test_when_finished "remove_object $tag" && |
| 157 | echo $tag >.git/refs/tags/invalid && |
| 158 | test_when_finished "git update-ref -d refs/tags/invalid" && |
Thomas Rast | 4551d03 | 2010-02-20 01:18:44 +0100 | [diff] [blame] | 159 | test_must_fail git fsck --tags >out && |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 160 | cat out && |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 161 | grep "broken link" out |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 162 | ' |
| 163 | |
Thomas Rast | 4551d03 | 2010-02-20 01:18:44 +0100 | [diff] [blame] | 164 | test_expect_success 'tag pointing to something else than its type' ' |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 165 | sha=$(echo blob | git hash-object -w --stdin) && |
| 166 | test_when_finished "remove_object $sha" && |
| 167 | cat >wrong-tag <<-EOF && |
| 168 | object $sha |
| 169 | type commit |
| 170 | tag wrong |
| 171 | tagger T A Gger <tagger@example.com> 1234567890 -0000 |
| 172 | |
| 173 | This is an invalid tag. |
| 174 | EOF |
| 175 | |
| 176 | tag=$(git hash-object -t tag -w --stdin <wrong-tag) && |
| 177 | test_when_finished "remove_object $tag" && |
| 178 | echo $tag >.git/refs/tags/wrong && |
| 179 | test_when_finished "git update-ref -d refs/tags/wrong" && |
Junio C Hamano | 9dad83b | 2012-10-02 15:08:16 -0700 | [diff] [blame] | 180 | test_must_fail git fsck --tags |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 181 | ' |
| 182 | |
Jonathan Nieder | dbedf8b | 2010-09-06 20:47:07 -0500 | [diff] [blame] | 183 | test_expect_success 'cleaned up' ' |
| 184 | git fsck >actual 2>&1 && |
| 185 | test_cmp empty actual |
| 186 | ' |
Thomas Rast | 02a6552 | 2009-02-19 12:13:39 +0100 | [diff] [blame] | 187 | |
Clemens Buchacher | cb8da70 | 2012-02-13 21:17:11 +0100 | [diff] [blame] | 188 | test_expect_success 'rev-list --verify-objects' ' |
| 189 | git rev-list --verify-objects --all >/dev/null 2>out && |
| 190 | test_cmp empty out |
| 191 | ' |
| 192 | |
| 193 | test_expect_success 'rev-list --verify-objects with bad sha1' ' |
| 194 | sha=$(echo blob | git hash-object -w --stdin) && |
| 195 | old=$(echo $sha | sed "s+^..+&/+") && |
| 196 | new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff && |
| 197 | sha="$(dirname $new)$(basename $new)" && |
| 198 | mv .git/objects/$old .git/objects/$new && |
| 199 | test_when_finished "remove_object $sha" && |
| 200 | git update-index --add --cacheinfo 100644 $sha foo && |
| 201 | test_when_finished "git read-tree -u --reset HEAD" && |
| 202 | tree=$(git write-tree) && |
| 203 | test_when_finished "remove_object $tree" && |
| 204 | cmt=$(echo bogus | git commit-tree $tree) && |
| 205 | test_when_finished "remove_object $cmt" && |
| 206 | git update-ref refs/heads/bogus $cmt && |
| 207 | test_when_finished "git update-ref -d refs/heads/bogus" && |
| 208 | |
| 209 | test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out && |
| 210 | cat out && |
| 211 | grep -q "error: sha1 mismatch 63ffffffffffffffffffffffffffffffffffffff" out |
| 212 | ' |
| 213 | |
Jeff King | c479d14 | 2012-07-28 11:06:29 -0400 | [diff] [blame] | 214 | _bz='\0' |
| 215 | _bz5="$_bz$_bz$_bz$_bz$_bz" |
| 216 | _bz20="$_bz5$_bz5$_bz5$_bz5" |
| 217 | |
| 218 | test_expect_success 'fsck notices blob entry pointing to null sha1' ' |
| 219 | (git init null-blob && |
| 220 | cd null-blob && |
| 221 | sha=$(printf "100644 file$_bz$_bz20" | |
| 222 | git hash-object -w --stdin -t tree) && |
| 223 | git fsck 2>out && |
| 224 | cat out && |
| 225 | grep "warning.*null sha1" out |
| 226 | ) |
| 227 | ' |
| 228 | |
| 229 | test_expect_success 'fsck notices submodule entry pointing to null sha1' ' |
| 230 | (git init null-commit && |
| 231 | cd null-commit && |
| 232 | sha=$(printf "160000 submodule$_bz$_bz20" | |
| 233 | git hash-object -w --stdin -t tree) && |
| 234 | git fsck 2>out && |
| 235 | cat out && |
| 236 | grep "warning.*null sha1" out |
| 237 | ) |
| 238 | ' |
| 239 | |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 240 | test_expect_success 'fsck notices "." and ".." in trees' ' |
| 241 | ( |
| 242 | git init dots && |
| 243 | cd dots && |
| 244 | blob=$(echo foo | git hash-object -w --stdin) && |
| 245 | tab=$(printf "\\t") && |
| 246 | git mktree <<-EOF && |
| 247 | 100644 blob $blob$tab. |
| 248 | 100644 blob $blob$tab.. |
| 249 | EOF |
| 250 | git fsck 2>out && |
| 251 | cat out && |
| 252 | grep "warning.*\\." out |
| 253 | ) |
| 254 | ' |
| 255 | |
Jeff King | 5c17f51 | 2012-11-28 16:35:29 -0500 | [diff] [blame] | 256 | test_expect_success 'fsck notices ".git" in trees' ' |
| 257 | ( |
| 258 | git init dotgit && |
| 259 | cd dotgit && |
| 260 | blob=$(echo foo | git hash-object -w --stdin) && |
| 261 | tab=$(printf "\\t") && |
| 262 | git mktree <<-EOF && |
| 263 | 100644 blob $blob$tab.git |
| 264 | EOF |
| 265 | git fsck 2>out && |
| 266 | cat out && |
| 267 | grep "warning.*\\.git" out |
| 268 | ) |
| 269 | ' |
| 270 | |
Junio C Hamano | 469e2eb | 2009-01-30 00:33:00 -0800 | [diff] [blame] | 271 | test_done |