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
51TRASHDIR=$(pwd)
52test_expect_success 'correct GIT_DIR while using -d' '
53 mkdir drepo &&
54 ( cd drepo &&
55 git init &&
56 test_commit drepo &&
57 git filter-branch -d "$TRASHDIR/dfoo" \
58 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
59 ) &&
60 grep drepo "$TRASHDIR/backup-refs"
61'
62
63test_expect_success 'Fail if commit filter fails' '
64 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
65'
66
67test_expect_success 'rewrite, renaming a specific file' '
68 git filter-branch -f --tree-filter "mv d doh || :" HEAD
69'
70
71test_expect_success 'test that the file was renamed' '
72 test d = "$(git show HEAD:doh --)" &&
73 ! test -f d &&
74 test -f doh &&
75 test d = "$(cat doh)"
76'
77
78test_expect_success 'rewrite, renaming a specific directory' '
79 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
80'
81
82test_expect_success 'test that the directory was renamed' '
83 test dir/d = "$(git show HEAD:diroh/d --)" &&
84 ! test -d dir &&
85 test -d diroh &&
86 ! test -d diroh/dir &&
87 test -f diroh/d &&
88 test dir/d = "$(cat diroh/d)"
89'
90
91git tag oldD HEAD~4
92test_expect_success 'rewrite one branch, keeping a side branch' '
93 git branch modD oldD &&
94 git filter-branch -f --tree-filter "mv b boh || :" D..modD
95'
96
97test_expect_success 'common ancestor is still common (unchanged)' '
98 test "$(git merge-base modD D)" = "$(git rev-parse B)"
99'
100
101test_expect_success 'filter subdirectory only' '
102 mkdir subdir &&
103 touch subdir/new &&
104 git add subdir/new &&
105 test_tick &&
106 git commit -m "subdir" &&
107 echo H > a &&
108 test_tick &&
109 git commit -m "not subdir" a &&
110 echo A > subdir/new &&
111 test_tick &&
112 git commit -m "again subdir" subdir/new &&
113 git rm a &&
114 test_tick &&
115 git commit -m "again not subdir" &&
116 git branch sub &&
117 git branch sub-earlier HEAD~2 &&
118 git filter-branch -f --subdirectory-filter subdir \
119 refs/heads/sub refs/heads/sub-earlier
120'
121
122test_expect_success 'subdirectory filter result looks okay' '
123 test 2 = $(git rev-list sub | wc -l) &&
124 git show sub:new &&
125 test_must_fail git show sub:subdir &&
126 git show sub-earlier:new &&
127 test_must_fail git show sub-earlier:subdir
128'
129
130test_expect_success 'more setup' '
131 git checkout master &&
132 mkdir subdir &&
133 echo A > subdir/new &&
134 git add subdir/new &&
135 test_tick &&
136 git commit -m "subdir on master" subdir/new &&
137 git rm a &&
138 test_tick &&
139 git commit -m "again subdir on master" &&
140 git merge branch
141'
142
143test_expect_success 'use index-filter to move into a subdirectory' '
144 git branch directorymoved &&
145 git filter-branch -f --index-filter \
146 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
147 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
148 git update-index --index-info &&
149 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
150 git diff --exit-code HEAD directorymoved:newsubdir
151'
152
153test_expect_success 'stops when msg filter fails' '
154 old=$(git rev-parse HEAD) &&
155 test_must_fail git filter-branch -f --msg-filter false HEAD &&
156 test $old = $(git rev-parse HEAD) &&
157 rm -rf .git-rewrite
158'
159
160test_expect_success 'author information is preserved' '
161 : > i &&
162 git add i &&
163 test_tick &&
164 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
165 git branch preserved-author &&
166 git filter-branch -f --msg-filter "cat; \
167 test \$GIT_COMMIT != $(git rev-parse master) || \
168 echo Hallo" \
169 preserved-author &&
170 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
171'
172
173test_expect_success "remove a certain author's commits" '
174 echo i > i &&
175 test_tick &&
176 git commit -m i i &&
177 git branch removed-author &&
178 git filter-branch -f --commit-filter "\
179 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
180 then\
181 skip_commit \"\$@\";
182 else\
183 git commit-tree \"\$@\";\
184 fi" removed-author &&
185 cnt1=$(git rev-list master | wc -l) &&
186 cnt2=$(git rev-list removed-author | wc -l) &&
187 test $cnt1 -eq $(($cnt2 + 1)) &&
188 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
189'
190
191test_expect_success 'barf on invalid name' '
192 test_must_fail git filter-branch -f master xy-problem &&
193 test_must_fail git filter-branch -f HEAD^
194'
195
196test_expect_success '"map" works in commit filter' '
197 git filter-branch -f --commit-filter "\
198 parent=\$(git rev-parse \$GIT_COMMIT^) &&
199 mapped=\$(map \$parent) &&
200 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
201 test \$mapped = \$actual &&
202 git commit-tree \"\$@\";" master~2..master &&
203 git rev-parse --verify master
204'
205
206test_expect_success 'Name needing quotes' '
207
208 git checkout -b rerere A &&
209 mkdir foo &&
210 name="れれれ" &&
211 >foo/$name &&
212 git add foo &&
213 git commit -m "Adding a file" &&
214 git filter-branch --tree-filter "rm -fr foo" &&
215 test_must_fail git ls-files --error-unmatch "foo/$name" &&
216 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
217
218'
219
220test_expect_success 'Subdirectory filter with disappearing trees' '
221 git reset --hard &&
222 git checkout master &&
223
224 mkdir foo &&
225 touch foo/bar &&
226 git add foo &&
227 test_tick &&
228 git commit -m "Adding foo" &&
229
230 git rm -r foo &&
231 test_tick &&
232 git commit -m "Removing foo" &&
233
234 mkdir foo &&
235 touch foo/bar &&
236 git add foo &&
237 test_tick &&
238 git commit -m "Re-adding foo" &&
239
240 git filter-branch -f --subdirectory-filter foo &&
241 test $(git rev-list master | wc -l) = 3
242'
243
244test_expect_success 'Tag name filtering retains tag message' '
245 git tag -m atag T &&
246 git cat-file tag T > expect &&
247 git filter-branch -f --tag-name-filter cat &&
248 git cat-file tag T > actual &&
249 test_cmp expect actual
250'
251
252faux_gpg_tag='object XXXXXX
253type commit
254tag S
255tagger T A Gger <tagger@example.com> 1206026339 -0500
256
257This is a faux gpg signed tag.
258-----BEGIN PGP SIGNATURE-----
259Version: FauxGPG v0.0.0 (FAUX/Linux)
260
261gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
262acmwXaWET20H0GeAGP+7vow=
263=agpO
264-----END PGP SIGNATURE-----
265'
266test_expect_success 'Tag name filtering strips gpg signature' '
267 sha1=$(git rev-parse HEAD) &&
268 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
269 git update-ref "refs/tags/S" "$sha1t" &&
270 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
271 git filter-branch -f --tag-name-filter cat &&
272 git cat-file tag S > actual &&
273 test_cmp expect actual
274'
275
276test_expect_success 'Tag name filtering allows slashes in tag names' '
277 git tag -m tag-with-slash X/1 &&
278 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
279 git filter-branch -f --tag-name-filter "echo X/2" &&
280 git cat-file tag X/2 > actual &&
281 test_cmp expect actual
282'
283
284test_expect_success 'Prune empty commits' '
285 git rev-list HEAD > expect &&
286 make_commit to_remove &&
287 git filter-branch -f --index-filter "git update-index --remove to_remove" --prune-empty HEAD &&
288 git rev-list HEAD > actual &&
289 test_cmp expect actual
290'
291
292test_expect_success '--remap-to-ancestor with filename filters' '
293 git checkout master &&
294 git reset --hard A &&
295 test_commit add-foo foo 1 &&
296 git branch moved-foo &&
297 test_commit add-bar bar a &&
298 git branch invariant &&
299 orig_invariant=$(git rev-parse invariant) &&
300 git branch moved-bar &&
301 test_commit change-foo foo 2 &&
302 git filter-branch -f --remap-to-ancestor \
303 moved-foo moved-bar A..master \
304 -- -- foo &&
305 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
306 test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
307 test $orig_invariant = $(git rev-parse invariant)
308'
309
310test_expect_success 'setup submodule' '
311 rm -fr ?* .git &&
312 git init &&
313 test_commit file &&
314 mkdir submod &&
315 submodurl="$PWD/submod" &&
316 ( cd submod &&
317 git init &&
318 test_commit file-in-submod ) &&
319 git submodule add "$submodurl" &&
320 git commit -m "added submodule" &&
321 test_commit add-file &&
322 ( cd submod && test_commit add-in-submodule ) &&
323 git add submod &&
324 git commit -m "changed submodule" &&
325 git branch original HEAD
326'
327
328orig_head=`git show-ref --hash --head HEAD`
329
330test_expect_success 'rewrite submodule with another content' '
331 git filter-branch --tree-filter "test -d submod && {
332 rm -rf submod &&
333 git rm -rf --quiet submod &&
334 mkdir submod &&
335 : > submod/file
336 } || :" HEAD &&
337 test $orig_head != `git show-ref --hash --head HEAD`
338'
339
340test_expect_success 'replace submodule revision' '
341 git reset --hard original &&
342 git filter-branch -f --tree-filter \
343 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
344 then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
345 fi" HEAD &&
346 test $orig_head != `git show-ref --hash --head HEAD`
347'
348
349test_done