3 test_description='git filter-branch'
6 test_expect_success 'setup' '
9 git checkout -b branch B &&
14 git checkout master &&
16 git checkout branch &&
24 # * Merge commit 'C' into branch
37 test_expect_success 'rewrite identically' '
38 git filter-branch branch
40 test_expect_success 'result is really identical' '
41 test $H = $(git rev-parse HEAD)
44 test_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)
49 git config core.bare false
50 test_expect_success 'result is really identical' '
51 test $H = $(git rev-parse HEAD)
55 test_expect_success 'correct GIT_DIR while using -d' '
60 git filter-branch -d "$TRASHDIR/dfoo" \
61 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
63 grep drepo "$TRASHDIR/backup-refs"
66 test_expect_success 'Fail if commit filter fails' '
67 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
70 test_expect_success 'rewrite, renaming a specific file' '
71 git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
74 test_expect_success 'test that the file was renamed' '
75 test D = "$(git show HEAD:doh --)" &&
81 test_expect_success 'rewrite, renaming a specific directory' '
82 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
85 test_expect_success 'test that the directory was renamed' '
86 test dir/D = "$(git show HEAD:diroh/D.t --)" &&
89 ! test -d diroh/dir &&
91 test dir/D = "$(cat diroh/D.t)"
95 test_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
100 test_expect_success 'common ancestor is still common (unchanged)' '
101 test "$(git merge-base modD D)" = "$(git rev-parse B)"
104 test_expect_success 'filter subdirectory only' '
107 git add subdir/new &&
109 git commit -m "subdir" &&
112 git commit -m "not subdir" A.t &&
113 echo A > subdir/new &&
115 git commit -m "again subdir" subdir/new &&
118 git commit -m "again not subdir" &&
120 git branch sub-earlier HEAD~2 &&
121 git filter-branch -f --subdirectory-filter subdir \
122 refs/heads/sub refs/heads/sub-earlier
125 test_expect_success 'subdirectory filter result looks okay' '
126 test 2 = $(git rev-list sub | wc -l) &&
128 test_must_fail git show sub:subdir &&
129 git show sub-earlier:new &&
130 test_must_fail git show sub-earlier:subdir
133 test_expect_success 'more setup' '
134 git checkout master &&
136 echo A > subdir/new &&
137 git add subdir/new &&
139 git commit -m "subdir on master" subdir/new &&
142 git commit -m "again subdir on master" &&
146 test_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
156 test_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) &&
163 test_expect_success 'author information is preserved' '
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) || \
173 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
176 test_expect_success "remove a certain author's commits" '
180 git branch removed-author &&
181 git filter-branch -f --commit-filter "\
182 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
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)
194 test_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^
199 test_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
209 test_expect_success 'Name needing quotes' '
211 git checkout -b rerere A &&
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)
223 test_expect_success 'Subdirectory filter with disappearing trees' '
225 git checkout master &&
231 git commit -m "Adding foo" &&
235 git commit -m "Removing foo" &&
241 git commit -m "Re-adding foo" &&
243 git filter-branch -f --subdirectory-filter foo &&
244 test $(git rev-list master | wc -l) = 3
247 test_expect_success 'Tag name filtering retains tag message' '
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
255 faux_gpg_tag='object XXXXXX
258 tagger T A Gger <tagger@example.com> 1206026339 -0500
260 This is a faux gpg signed tag.
261 -----BEGIN PGP SIGNATURE-----
262 Version: FauxGPG v0.0.0 (FAUX/Linux)
264 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
265 acmwXaWET20H0GeAGP+7vow=
267 -----END PGP SIGNATURE-----
269 test_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
279 test_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
287 test_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
295 test_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 \
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)
313 test_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 \
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)
331 test_expect_success 'setup submodule' '
336 submodurl="$PWD/submod" &&
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 ) &&
345 git commit -m "changed submodule" &&
346 git branch original HEAD
349 orig_head=`git show-ref --hash --head HEAD`
351 test_expect_success 'rewrite submodule with another content' '
352 git filter-branch --tree-filter "test -d submod && {
354 git rm -rf --quiet submod &&
358 test $orig_head != `git show-ref --hash --head HEAD`
361 test_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
367 test $orig_head != `git show-ref --hash --head HEAD`