3 test_description
='git filter-branch'
6 test_expect_success
'setup' '
8 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
9 test_commit --notick B &&
10 git checkout -b branch B &&
15 git checkout master &&
17 git checkout branch &&
25 # * Merge commit 'C' into branch
38 test_expect_success
'rewrite identically' '
39 git filter-branch branch
41 test_expect_success
'result is really identical' '
42 test $H = $(git rev-parse HEAD)
45 test_expect_success
'rewrite bare repository identically' '
46 (git config core.bare true && cd .git &&
47 git filter-branch branch > filter-output 2>&1 &&
48 ! fgrep fatal filter-output)
50 git config core.bare false
51 test_expect_success
'result is really identical' '
52 test $H = $(git rev-parse HEAD)
56 test_expect_success
'correct GIT_DIR while using -d' '
61 git filter-branch -d "$TRASHDIR/dfoo" \
62 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
64 grep drepo "$TRASHDIR/backup-refs"
67 test_expect_success
'tree-filter works with -d' '
68 git init drepo-tree &&
72 git filter-branch -d "$TRASHDIR/dfoo" \
73 --tree-filter "echo changed >one.t" &&
74 echo changed >expect &&
75 git cat-file blob HEAD:one.t >actual &&
76 test_cmp expect actual &&
81 test_expect_success
'Fail if commit filter fails' '
82 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
85 test_expect_success
'rewrite, renaming a specific file' '
86 git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
89 test_expect_success
'test that the file was renamed' '
90 test D = "$(git show HEAD:doh --)" &&
96 test_expect_success
'rewrite, renaming a specific directory' '
97 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
100 test_expect_success
'test that the directory was renamed' '
101 test dir/D = "$(git show HEAD:diroh/D.t --)" &&
104 ! test -d diroh/dir &&
106 test dir/D = "$(cat diroh/D.t)"
110 test_expect_success
'rewrite one branch, keeping a side branch' '
111 git branch modD oldD &&
112 git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
115 test_expect_success
'common ancestor is still common (unchanged)' '
116 test "$(git merge-base modD D)" = "$(git rev-parse B)"
119 test_expect_success
'filter subdirectory only' '
122 git add subdir/new &&
124 git commit -m "subdir" &&
127 git commit -m "not subdir" A.t &&
128 echo A > subdir/new &&
130 git commit -m "again subdir" subdir/new &&
133 git commit -m "again not subdir" &&
135 git branch sub-earlier HEAD~2 &&
136 git filter-branch -f --subdirectory-filter subdir \
137 refs/heads/sub refs/heads/sub-earlier
140 test_expect_success
'subdirectory filter result looks okay' '
141 test 2 = $(git rev-list sub | wc -l) &&
143 test_must_fail git show sub:subdir &&
144 git show sub-earlier:new &&
145 test_must_fail git show sub-earlier:subdir
148 test_expect_success
'more setup' '
149 git checkout master &&
151 echo A > subdir/new &&
152 git add subdir/new &&
154 git commit -m "subdir on master" subdir/new &&
157 git commit -m "again subdir on master" &&
161 test_expect_success
'use index-filter to move into a subdirectory' '
162 git branch directorymoved &&
163 git filter-branch -f --index-filter \
164 "git ls-files -s | sed \"s- -&newsubdir/-\" |
165 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
166 git update-index --index-info &&
167 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
168 git diff --exit-code HEAD directorymoved:newsubdir
171 test_expect_success
'stops when msg filter fails' '
172 old=$(git rev-parse HEAD) &&
173 test_must_fail git filter-branch -f --msg-filter false HEAD &&
174 test $old = $(git rev-parse HEAD) &&
178 test_expect_success
'author information is preserved' '
182 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
183 git branch preserved-author &&
184 (sane_unset GIT_AUTHOR_NAME &&
185 git filter-branch -f --msg-filter "cat; \
186 test \$GIT_COMMIT != $(git rev-parse master) || \
189 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
192 test_expect_success
"remove a certain author's commits" '
196 git branch removed-author &&
197 git filter-branch -f --commit-filter "\
198 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
202 git commit-tree \"\$@\";\
203 fi" removed-author &&
204 cnt1=$(git rev-list master | wc -l) &&
205 cnt2=$(git rev-list removed-author | wc -l) &&
206 test $cnt1 -eq $(($cnt2 + 1)) &&
207 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
210 test_expect_success
'barf on invalid name' '
211 test_must_fail git filter-branch -f master xy-problem &&
212 test_must_fail git filter-branch -f HEAD^
215 test_expect_success
'"map" works in commit filter' '
216 git filter-branch -f --commit-filter "\
217 parent=\$(git rev-parse \$GIT_COMMIT^) &&
218 mapped=\$(map \$parent) &&
219 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
220 test \$mapped = \$actual &&
221 git commit-tree \"\$@\";" master~2..master &&
222 git rev-parse --verify master
225 test_expect_success
'Name needing quotes' '
227 git checkout -b rerere A &&
232 git commit -m "Adding a file" &&
233 git filter-branch --tree-filter "rm -fr foo" &&
234 test_must_fail git ls-files --error-unmatch "foo/$name" &&
235 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
239 test_expect_success
'Subdirectory filter with disappearing trees' '
241 git checkout master &&
247 git commit -m "Adding foo" &&
251 git commit -m "Removing foo" &&
257 git commit -m "Re-adding foo" &&
259 git filter-branch -f --subdirectory-filter foo &&
260 test $(git rev-list master | wc -l) = 3
263 test_expect_success
'Tag name filtering retains tag message' '
265 git cat-file tag T > expect &&
266 git filter-branch -f --tag-name-filter cat &&
267 git cat-file tag T > actual &&
268 test_cmp expect actual
271 faux_gpg_tag
='object XXXXXX
274 tagger T A Gger <tagger@example.com> 1206026339 -0500
276 This is a faux gpg signed tag.
277 -----BEGIN PGP SIGNATURE-----
278 Version: FauxGPG v0.0.0 (FAUX/Linux)
280 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
281 acmwXaWET20H0GeAGP+7vow=
283 -----END PGP SIGNATURE-----
285 test_expect_success
'Tag name filtering strips gpg signature' '
286 sha1=$(git rev-parse HEAD) &&
287 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
288 git update-ref "refs/tags/S" "$sha1t" &&
289 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
290 git filter-branch -f --tag-name-filter cat &&
291 git cat-file tag S > actual &&
292 test_cmp expect actual
295 test_expect_success
'Tag name filtering allows slashes in tag names' '
296 git tag -m tag-with-slash X/1 &&
297 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
298 git filter-branch -f --tag-name-filter "echo X/2" &&
299 git cat-file tag X/2 > actual &&
300 test_cmp expect actual
303 test_expect_success
'Prune empty commits' '
304 git rev-list HEAD > expect &&
305 test_commit to_remove &&
306 git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
307 git rev-list HEAD > actual &&
308 test_cmp expect actual
311 test_expect_success
'prune empty collapsed merges' '
312 test_config merge.ff false &&
313 git rev-list HEAD >expect &&
314 test_commit to_remove_2 &&
315 git reset --hard HEAD^ &&
316 test_merge non-ff to_remove_2 &&
317 git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
318 git rev-list HEAD >actual &&
319 test_cmp expect actual
322 test_expect_success
'--remap-to-ancestor with filename filters' '
323 git checkout master &&
324 git reset --hard A &&
325 test_commit add-foo foo 1 &&
326 git branch moved-foo &&
327 test_commit add-bar bar a &&
328 git branch invariant &&
329 orig_invariant=$(git rev-parse invariant) &&
330 git branch moved-bar &&
331 test_commit change-foo foo 2 &&
332 git filter-branch -f --remap-to-ancestor \
333 moved-foo moved-bar A..master \
335 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
336 test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
337 test $orig_invariant = $(git rev-parse invariant)
340 test_expect_success
'automatic remapping to ancestor with filename filters' '
341 git checkout master &&
342 git reset --hard A &&
343 test_commit add-foo2 foo 1 &&
344 git branch moved-foo2 &&
345 test_commit add-bar2 bar a &&
346 git branch invariant2 &&
347 orig_invariant=$(git rev-parse invariant2) &&
348 git branch moved-bar2 &&
349 test_commit change-foo2 foo 2 &&
350 git filter-branch -f \
351 moved-foo2 moved-bar2 A..master \
353 test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
354 test $(git rev-parse moved-foo2) = $(git rev-parse master^) &&
355 test $orig_invariant = $(git rev-parse invariant2)
358 test_expect_success
'setup submodule' '
363 submodurl="$PWD/submod" &&
366 test_commit file-in-submod ) &&
367 git submodule add "$submodurl" &&
368 git commit -m "added submodule" &&
369 test_commit add-file &&
370 ( cd submod && test_commit add-in-submodule ) &&
372 git commit -m "changed submodule" &&
373 git branch original HEAD
376 orig_head
=`git show-ref --hash --head HEAD`
378 test_expect_success
'rewrite submodule with another content' '
379 git filter-branch --tree-filter "test -d submod && {
381 git rm -rf --quiet submod &&
385 test $orig_head != `git show-ref --hash --head HEAD`
388 test_expect_success
'replace submodule revision' '
389 git reset --hard original &&
390 git filter-branch -f --tree-filter \
391 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
392 then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
394 test $orig_head != `git show-ref --hash --head HEAD`