Merge branch 'jk/maint-send-email-compose'
[git] / t / t7003-filter-branch.sh
1 #!/bin/sh
2
3 test_description='git-filter-branch'
4 . ./test-lib.sh
5
6 make_commit () {
7         lower=$(echo $1 | tr '[A-Z]' '[a-z]')
8         echo $lower > $lower
9         git add $lower
10         test_tick
11         git commit -m $1
12         git tag $1
13 }
14
15 test_expect_success 'setup' '
16         make_commit A
17         make_commit B
18         git checkout -b branch B
19         make_commit D
20         mkdir dir
21         make_commit dir/D
22         make_commit E
23         git checkout master
24         make_commit C
25         git checkout branch
26         git merge C
27         git tag F
28         make_commit G
29         make_commit H
30 '
31
32 H=$(git rev-parse H)
33
34 test_expect_success 'rewrite identically' '
35         git-filter-branch branch
36 '
37 test_expect_success 'result is really identical' '
38         test $H = $(git rev-parse HEAD)
39 '
40
41 test_expect_success 'rewrite, renaming a specific file' '
42         git-filter-branch -f --tree-filter "mv d doh || :" HEAD
43 '
44
45 test_expect_success 'test that the file was renamed' '
46         test d = "$(git show HEAD:doh --)" &&
47         ! test -f d &&
48         test -f doh &&
49         test d = "$(cat doh)"
50 '
51
52 test_expect_success 'rewrite, renaming a specific directory' '
53         git-filter-branch -f --tree-filter "mv dir diroh || :" HEAD
54 '
55
56 test_expect_success 'test that the directory was renamed' '
57         test dir/d = "$(git show HEAD:diroh/d --)" &&
58         ! test -d dir &&
59         test -d diroh &&
60         ! test -d diroh/dir &&
61         test -f diroh/d &&
62         test dir/d = "$(cat diroh/d)"
63 '
64
65 git tag oldD HEAD~4
66 test_expect_success 'rewrite one branch, keeping a side branch' '
67         git branch modD oldD &&
68         git-filter-branch -f --tree-filter "mv b boh || :" D..modD
69 '
70
71 test_expect_success 'common ancestor is still common (unchanged)' '
72         test "$(git merge-base modD D)" = "$(git rev-parse B)"
73 '
74
75 test_expect_success 'filter subdirectory only' '
76         mkdir subdir &&
77         touch subdir/new &&
78         git add subdir/new &&
79         test_tick &&
80         git commit -m "subdir" &&
81         echo H > a &&
82         test_tick &&
83         git commit -m "not subdir" a &&
84         echo A > subdir/new &&
85         test_tick &&
86         git commit -m "again subdir" subdir/new &&
87         git rm a &&
88         test_tick &&
89         git commit -m "again not subdir" &&
90         git branch sub &&
91         git-filter-branch -f --subdirectory-filter subdir refs/heads/sub
92 '
93
94 test_expect_success 'subdirectory filter result looks okay' '
95         test 2 = $(git rev-list sub | wc -l) &&
96         git show sub:new &&
97         test_must_fail git show sub:subdir
98 '
99
100 test_expect_success 'setup and filter history that requires --full-history' '
101         git checkout master &&
102         mkdir subdir &&
103         echo A > subdir/new &&
104         git add subdir/new &&
105         test_tick &&
106         git commit -m "subdir on master" subdir/new &&
107         git rm a &&
108         test_tick &&
109         git commit -m "again subdir on master" &&
110         git merge branch &&
111         git branch sub-master &&
112         git-filter-branch -f --subdirectory-filter subdir sub-master
113 '
114
115 test_expect_success 'subdirectory filter result looks okay' '
116         test 3 = $(git rev-list -1 --parents sub-master | wc -w) &&
117         git show sub-master^:new &&
118         git show sub-master^2:new &&
119         test_must_fail git show sub:subdir
120 '
121
122 test_expect_success 'use index-filter to move into a subdirectory' '
123         git branch directorymoved &&
124         git-filter-branch -f --index-filter \
125                  "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
126                   GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
127                         git update-index --index-info &&
128                   mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
129         test -z "$(git diff HEAD directorymoved:newsubdir)"'
130
131 test_expect_success 'stops when msg filter fails' '
132         old=$(git rev-parse HEAD) &&
133         test_must_fail git-filter-branch -f --msg-filter false HEAD &&
134         test $old = $(git rev-parse HEAD) &&
135         rm -rf .git-rewrite
136 '
137
138 test_expect_success 'author information is preserved' '
139         : > i &&
140         git add i &&
141         test_tick &&
142         GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
143         git branch preserved-author &&
144         git-filter-branch -f --msg-filter "cat; \
145                         test \$GIT_COMMIT != $(git rev-parse master) || \
146                         echo Hallo" \
147                 preserved-author &&
148         test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
149 '
150
151 test_expect_success "remove a certain author's commits" '
152         echo i > i &&
153         test_tick &&
154         git commit -m i i &&
155         git branch removed-author &&
156         git-filter-branch -f --commit-filter "\
157                 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
158                 then\
159                         skip_commit \"\$@\";
160                 else\
161                         git commit-tree \"\$@\";\
162                 fi" removed-author &&
163         cnt1=$(git rev-list master | wc -l) &&
164         cnt2=$(git rev-list removed-author | wc -l) &&
165         test $cnt1 -eq $(($cnt2 + 1)) &&
166         test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
167 '
168
169 test_expect_success 'barf on invalid name' '
170         test_must_fail git filter-branch -f master xy-problem &&
171         test_must_fail git filter-branch -f HEAD^
172 '
173
174 test_expect_success '"map" works in commit filter' '
175         git filter-branch -f --commit-filter "\
176                 parent=\$(git rev-parse \$GIT_COMMIT^) &&
177                 mapped=\$(map \$parent) &&
178                 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
179                 test \$mapped = \$actual &&
180                 git commit-tree \"\$@\";" master~2..master &&
181         git rev-parse --verify master
182 '
183
184 test_expect_success 'Name needing quotes' '
185
186         git checkout -b rerere A &&
187         mkdir foo &&
188         name="れれれ" &&
189         >foo/$name &&
190         git add foo &&
191         git commit -m "Adding a file" &&
192         git filter-branch --tree-filter "rm -fr foo" &&
193         test_must_fail git ls-files --error-unmatch "foo/$name" &&
194         test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
195
196 '
197
198 test_expect_success 'Subdirectory filter with disappearing trees' '
199         git reset --hard &&
200         git checkout master &&
201
202         mkdir foo &&
203         touch foo/bar &&
204         git add foo &&
205         test_tick &&
206         git commit -m "Adding foo" &&
207
208         git rm -r foo &&
209         test_tick &&
210         git commit -m "Removing foo" &&
211
212         mkdir foo &&
213         touch foo/bar &&
214         git add foo &&
215         test_tick &&
216         git commit -m "Re-adding foo" &&
217
218         git filter-branch -f --subdirectory-filter foo &&
219         test $(git rev-list master | wc -l) = 3
220 '
221
222 test_expect_success 'Tag name filtering retains tag message' '
223         git tag -m atag T &&
224         git cat-file tag T > expect &&
225         git filter-branch -f --tag-name-filter cat &&
226         git cat-file tag T > actual &&
227         git diff expect actual
228 '
229
230 faux_gpg_tag='object XXXXXX
231 type commit
232 tag S
233 tagger T A Gger <tagger@example.com> 1206026339 -0500
234
235 This is a faux gpg signed tag.
236 -----BEGIN PGP SIGNATURE-----
237 Version: FauxGPG v0.0.0 (FAUX/Linux)
238
239 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
240 acmwXaWET20H0GeAGP+7vow=
241 =agpO
242 -----END PGP SIGNATURE-----
243 '
244 test_expect_success 'Tag name filtering strips gpg signature' '
245         sha1=$(git rev-parse HEAD) &&
246         sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
247         git update-ref "refs/tags/S" "$sha1t" &&
248         echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
249         git filter-branch -f --tag-name-filter cat &&
250         git cat-file tag S > actual &&
251         git diff expect actual
252 '
253
254 test_done