3 test_description='git filter-branch'
7 lower=$(echo $1 | tr '[A-Z]' '[a-z]')
15 test_expect_success 'setup' '
18 git checkout -b branch B
34 test_expect_success 'rewrite identically' '
35 git filter-branch branch
37 test_expect_success 'result is really identical' '
38 test $H = $(git rev-parse HEAD)
41 test_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)
46 git config core.bare false
47 test_expect_success 'result is really identical' '
48 test $H = $(git rev-parse HEAD)
52 test_expect_success 'correct GIT_DIR while using -d' '
57 git filter-branch -d "$TRASHDIR/dfoo" \
58 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
60 grep drepo "$TRASHDIR/backup-refs"
63 test_expect_success 'Fail if commit filter fails' '
64 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
67 test_expect_success 'rewrite, renaming a specific file' '
68 git filter-branch -f --tree-filter "mv d doh || :" HEAD
71 test_expect_success 'test that the file was renamed' '
72 test d = "$(git show HEAD:doh --)" &&
78 test_expect_success 'rewrite, renaming a specific directory' '
79 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
82 test_expect_success 'test that the directory was renamed' '
83 test dir/d = "$(git show HEAD:diroh/d --)" &&
86 ! test -d diroh/dir &&
88 test dir/d = "$(cat diroh/d)"
92 test_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
97 test_expect_success 'common ancestor is still common (unchanged)' '
98 test "$(git merge-base modD D)" = "$(git rev-parse B)"
101 test_expect_success 'filter subdirectory only' '
104 git add subdir/new &&
106 git commit -m "subdir" &&
109 git commit -m "not subdir" a &&
110 echo A > subdir/new &&
112 git commit -m "again subdir" subdir/new &&
115 git commit -m "again not subdir" &&
117 git branch sub-earlier HEAD~2 &&
118 git filter-branch -f --subdirectory-filter subdir \
119 refs/heads/sub refs/heads/sub-earlier
122 test_expect_success 'subdirectory filter result looks okay' '
123 test 2 = $(git rev-list sub | wc -l) &&
125 test_must_fail git show sub:subdir &&
126 git show sub-earlier:new &&
127 test_must_fail git show sub-earlier:subdir
130 test_expect_success 'more setup' '
131 git checkout master &&
133 echo A > subdir/new &&
134 git add subdir/new &&
136 git commit -m "subdir on master" subdir/new &&
139 git commit -m "again subdir on master" &&
143 test_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 test -z "$(git diff HEAD directorymoved:newsubdir)"'
152 test_expect_success 'stops when msg filter fails' '
153 old=$(git rev-parse HEAD) &&
154 test_must_fail git filter-branch -f --msg-filter false HEAD &&
155 test $old = $(git rev-parse HEAD) &&
159 test_expect_success 'author information is preserved' '
163 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
164 git branch preserved-author &&
165 git filter-branch -f --msg-filter "cat; \
166 test \$GIT_COMMIT != $(git rev-parse master) || \
169 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
172 test_expect_success "remove a certain author's commits" '
176 git branch removed-author &&
177 git filter-branch -f --commit-filter "\
178 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
182 git commit-tree \"\$@\";\
183 fi" removed-author &&
184 cnt1=$(git rev-list master | wc -l) &&
185 cnt2=$(git rev-list removed-author | wc -l) &&
186 test $cnt1 -eq $(($cnt2 + 1)) &&
187 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
190 test_expect_success 'barf on invalid name' '
191 test_must_fail git filter-branch -f master xy-problem &&
192 test_must_fail git filter-branch -f HEAD^
195 test_expect_success '"map" works in commit filter' '
196 git filter-branch -f --commit-filter "\
197 parent=\$(git rev-parse \$GIT_COMMIT^) &&
198 mapped=\$(map \$parent) &&
199 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
200 test \$mapped = \$actual &&
201 git commit-tree \"\$@\";" master~2..master &&
202 git rev-parse --verify master
205 test_expect_success 'Name needing quotes' '
207 git checkout -b rerere A &&
212 git commit -m "Adding a file" &&
213 git filter-branch --tree-filter "rm -fr foo" &&
214 test_must_fail git ls-files --error-unmatch "foo/$name" &&
215 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
219 test_expect_success 'Subdirectory filter with disappearing trees' '
221 git checkout master &&
227 git commit -m "Adding foo" &&
231 git commit -m "Removing foo" &&
237 git commit -m "Re-adding foo" &&
239 git filter-branch -f --subdirectory-filter foo &&
240 test $(git rev-list master | wc -l) = 3
243 test_expect_success 'Tag name filtering retains tag message' '
245 git cat-file tag T > expect &&
246 git filter-branch -f --tag-name-filter cat &&
247 git cat-file tag T > actual &&
248 test_cmp expect actual
251 faux_gpg_tag='object XXXXXX
254 tagger T A Gger <tagger@example.com> 1206026339 -0500
256 This is a faux gpg signed tag.
257 -----BEGIN PGP SIGNATURE-----
258 Version: FauxGPG v0.0.0 (FAUX/Linux)
260 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
261 acmwXaWET20H0GeAGP+7vow=
263 -----END PGP SIGNATURE-----
265 test_expect_success 'Tag name filtering strips gpg signature' '
266 sha1=$(git rev-parse HEAD) &&
267 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
268 git update-ref "refs/tags/S" "$sha1t" &&
269 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
270 git filter-branch -f --tag-name-filter cat &&
271 git cat-file tag S > actual &&
272 test_cmp expect actual
275 test_expect_success 'Tag name filtering allows slashes in tag names' '
276 git tag -m tag-with-slash X/1 &&
277 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
278 git filter-branch -f --tag-name-filter "echo X/2" &&
279 git cat-file tag X/2 > actual &&
280 test_cmp expect actual
283 test_expect_success 'Prune empty commits' '
284 git rev-list HEAD > expect &&
285 make_commit to_remove &&
286 git filter-branch -f --index-filter "git update-index --remove to_remove" --prune-empty HEAD &&
287 git rev-list HEAD > actual &&
288 test_cmp expect actual