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