t1450 (fsck): remove dangling objects
[git/git.git] / t / t1450-fsck.sh
CommitLineData
469e2ebf
JH
1#!/bin/sh
2
dbedf8bf
JN
3test_description='git fsck random collection of tests
4
5* (HEAD) B
6* (master) A
7'
469e2ebf
JH
8
9. ./test-lib.sh
10
11test_expect_success setup '
dbedf8bf 12 git config gc.auto 0 &&
0adc6a3d 13 git config i18n.commitencoding ISO-8859-1 &&
469e2ebf 14 test_commit A fileA one &&
0adc6a3d 15 git config --unset i18n.commitencoding &&
469e2ebf
JH
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 git tag -d A B &&
dbedf8bf
JN
19 git reflog expire --expire=now --all &&
20 >empty
469e2ebf
JH
21'
22
e15ef669
JH
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 &&
dbedf8bf
JN
30 git fsck >../out 2>&1
31 ) &&
32 {
33 grep -v dangling out >actual ||
34 :
35 } &&
36 test_cmp empty actual
e15ef669
JH
37'
38
dbedf8bf
JN
39test_expect_success 'HEAD is part of refs, valid objects appear valid' '
40 git fsck >actual 2>&1 &&
41 test_cmp empty actual
0adc6a3d
JN
42'
43
02a6552c
TR
44# Corruption tests follow. Make sure to remove all traces of the
45# specific corruption you test afterwards, lest a later test trip over
46# it.
47
dbedf8bf
JN
48test_expect_success 'setup: helpers for corruption tests' '
49 sha1_file() {
50 echo "$*" | sed "s#..#.git/objects/&/#"
51 } &&
52
53 remove_object() {
54 file=$(sha1_file "$*") &&
55 test -e "$file" &&
56 rm -f "$file"
57 }
58'
59
02a6552c
TR
60test_expect_success 'object with bad sha1' '
61 sha=$(echo blob | git hash-object -w --stdin) &&
02a6552c
TR
62 old=$(echo $sha | sed "s+^..+&/+") &&
63 new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
64 sha="$(dirname $new)$(basename $new)"
65 mv .git/objects/$old .git/objects/$new &&
dbedf8bf 66 test_when_finished "remove_object $sha" &&
02a6552c 67 git update-index --add --cacheinfo 100644 $sha foo &&
dbedf8bf 68 test_when_finished "git read-tree -u --reset HEAD" &&
02a6552c 69 tree=$(git write-tree) &&
dbedf8bf 70 test_when_finished "remove_object $tree" &&
02a6552c 71 cmt=$(echo bogus | git commit-tree $tree) &&
dbedf8bf 72 test_when_finished "remove_object $cmt" &&
02a6552c 73 git update-ref refs/heads/bogus $cmt &&
dbedf8bf
JN
74 test_when_finished "git update-ref -d refs/heads/bogus" &&
75
76 test_might_fail git fsck 2>out &&
77 cat out &&
78 grep "$sha.*corrupt" out
02a6552c
TR
79'
80
81test_expect_success 'branch pointing to non-commit' '
dbedf8bf
JN
82 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
83 test_when_finished "git update-ref -d refs/heads/invalid" &&
02a6552c 84 git fsck 2>out &&
dbedf8bf
JN
85 cat out &&
86 grep "not a commit" out
02a6552c
TR
87'
88
daae1922
JN
89test_expect_success 'email without @ is okay' '
90 git cat-file commit HEAD >basis &&
91 sed "s/@/AT/" basis >okay &&
92 new=$(git hash-object -t commit -w --stdin <okay) &&
dbedf8bf 93 test_when_finished "remove_object $new" &&
daae1922 94 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 95 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922
JN
96 git fsck 2>out &&
97 cat out &&
dbedf8bf 98 ! grep "commit $new" out
daae1922 99'
daae1922 100
daae1922
JN
101test_expect_success 'email with embedded > is not okay' '
102 git cat-file commit HEAD >basis &&
103 sed "s/@[a-z]/&>/" basis >bad-email &&
104 new=$(git hash-object -t commit -w --stdin <bad-email) &&
dbedf8bf 105 test_when_finished "remove_object $new" &&
daae1922 106 git update-ref refs/heads/bogus "$new" &&
dbedf8bf 107 test_when_finished "git update-ref -d refs/heads/bogus" &&
daae1922
JN
108 git fsck 2>out &&
109 cat out &&
110 grep "error in commit $new" out
111'
02a6552c 112
4551d035 113test_expect_success 'tag pointing to nonexistent' '
dbedf8bf
JN
114 cat >invalid-tag <<-\EOF
115 object ffffffffffffffffffffffffffffffffffffffff
116 type commit
117 tag invalid
118 tagger T A Gger <tagger@example.com> 1234567890 -0000
119
120 This is an invalid tag.
121 EOF
122
123 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
124 test_when_finished "remove_object $tag" &&
125 echo $tag >.git/refs/tags/invalid &&
126 test_when_finished "git update-ref -d refs/tags/invalid" &&
4551d035 127 test_must_fail git fsck --tags >out &&
02a6552c 128 cat out &&
dbedf8bf 129 grep "broken link" out
02a6552c
TR
130'
131
4551d035 132test_expect_success 'tag pointing to something else than its type' '
dbedf8bf
JN
133 sha=$(echo blob | git hash-object -w --stdin) &&
134 test_when_finished "remove_object $sha" &&
135 cat >wrong-tag <<-EOF &&
136 object $sha
137 type commit
138 tag wrong
139 tagger T A Gger <tagger@example.com> 1234567890 -0000
140
141 This is an invalid tag.
142 EOF
143
144 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
145 test_when_finished "remove_object $tag" &&
146 echo $tag >.git/refs/tags/wrong &&
147 test_when_finished "git update-ref -d refs/tags/wrong" &&
4551d035 148 test_must_fail git fsck --tags 2>out &&
02a6552c 149 cat out &&
dbedf8bf 150 grep "error in tag.*broken links" out
02a6552c
TR
151'
152
dbedf8bf
JN
153test_expect_success 'cleaned up' '
154 git fsck >actual 2>&1 &&
155 test_cmp empty actual
156'
02a6552c 157
469e2ebf 158test_done