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