3 test_description
='git filter-branch'
5 .
"$TEST_DIRECTORY/lib-gpg.sh"
7 test_expect_success
'setup' '
9 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
10 test_commit --notick B &&
11 git checkout -b branch B &&
16 git checkout master &&
18 git checkout branch &&
26 # * Merge commit 'C' into branch
39 test_expect_success
'rewrite identically' '
40 git filter-branch branch
42 test_expect_success
'result is really identical' '
43 test $H = $(git rev-parse HEAD)
46 test_expect_success
'rewrite bare repository identically' '
47 (git config core.bare true && cd .git &&
48 git filter-branch branch > filter-output 2>&1 &&
49 ! fgrep fatal filter-output)
51 git config core.bare false
52 test_expect_success
'result is really identical' '
53 test $H = $(git rev-parse HEAD)
57 test_expect_success
'correct GIT_DIR while using -d' '
62 git filter-branch -d "$TRASHDIR/dfoo" \
63 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
65 grep drepo "$TRASHDIR/backup-refs"
68 test_expect_success
'tree-filter works with -d' '
69 git init drepo-tree &&
73 git filter-branch -d "$TRASHDIR/dfoo" \
74 --tree-filter "echo changed >one.t" &&
75 echo changed >expect &&
76 git cat-file blob HEAD:one.t >actual &&
77 test_cmp expect actual &&
82 test_expect_success
'Fail if commit filter fails' '
83 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
86 test_expect_success
'rewrite, renaming a specific file' '
87 git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
90 test_expect_success
'test that the file was renamed' '
91 test D = "$(git show HEAD:doh --)" &&
97 test_expect_success
'rewrite, renaming a specific directory' '
98 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
101 test_expect_success
'test that the directory was renamed' '
102 test dir/D = "$(git show HEAD:diroh/D.t --)" &&
105 ! test -d diroh/dir &&
107 test dir/D = "$(cat diroh/D.t)"
111 test_expect_success
'rewrite one branch, keeping a side branch' '
112 git branch modD oldD &&
113 git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
116 test_expect_success
'common ancestor is still common (unchanged)' '
117 test "$(git merge-base modD D)" = "$(git rev-parse B)"
120 test_expect_success
'filter subdirectory only' '
123 git add subdir/new &&
125 git commit -m "subdir" &&
128 git commit -m "not subdir" A.t &&
129 echo A > subdir/new &&
131 git commit -m "again subdir" subdir/new &&
134 git commit -m "again not subdir" &&
136 git branch sub-earlier HEAD~2 &&
137 git filter-branch -f --subdirectory-filter subdir \
138 refs/heads/sub refs/heads/sub-earlier
141 test_expect_success
'subdirectory filter result looks okay' '
142 test 2 = $(git rev-list sub | wc -l) &&
144 test_must_fail git show sub:subdir &&
145 git show sub-earlier:new &&
146 test_must_fail git show sub-earlier:subdir
149 test_expect_success
'more setup' '
150 git checkout master &&
152 echo A > subdir/new &&
153 git add subdir/new &&
155 git commit -m "subdir on master" subdir/new &&
158 git commit -m "again subdir on master" &&
162 test_expect_success
'use index-filter to move into a subdirectory' '
163 git branch directorymoved &&
164 git filter-branch -f --index-filter \
165 "git ls-files -s | sed \"s- -&newsubdir/-\" |
166 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
167 git update-index --index-info &&
168 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
169 git diff --exit-code HEAD directorymoved:newsubdir
172 test_expect_success
'stops when msg filter fails' '
173 old=$(git rev-parse HEAD) &&
174 test_must_fail git filter-branch -f --msg-filter false HEAD &&
175 test $old = $(git rev-parse HEAD) &&
179 test_expect_success
'author information is preserved' '
183 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
184 git branch preserved-author &&
185 (sane_unset GIT_AUTHOR_NAME &&
186 git filter-branch -f --msg-filter "cat; \
187 test \$GIT_COMMIT != $(git rev-parse master) || \
190 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
193 test_expect_success
"remove a certain author's commits" '
197 git branch removed-author &&
198 git filter-branch -f --commit-filter "\
199 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
203 git commit-tree \"\$@\";\
204 fi" removed-author &&
205 cnt1=$(git rev-list master | wc -l) &&
206 cnt2=$(git rev-list removed-author | wc -l) &&
207 test $cnt1 -eq $(($cnt2 + 1)) &&
208 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
211 test_expect_success
'barf on invalid name' '
212 test_must_fail git filter-branch -f master xy-problem &&
213 test_must_fail git filter-branch -f HEAD^
216 test_expect_success
'"map" works in commit filter' '
217 git filter-branch -f --commit-filter "\
218 parent=\$(git rev-parse \$GIT_COMMIT^) &&
219 mapped=\$(map \$parent) &&
220 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
221 test \$mapped = \$actual &&
222 git commit-tree \"\$@\";" master~2..master &&
223 git rev-parse --verify master
226 test_expect_success
'Name needing quotes' '
228 git checkout -b rerere A &&
233 git commit -m "Adding a file" &&
234 git filter-branch --tree-filter "rm -fr foo" &&
235 test_must_fail git ls-files --error-unmatch "foo/$name" &&
236 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
240 test_expect_success
'Subdirectory filter with disappearing trees' '
242 git checkout master &&
248 git commit -m "Adding foo" &&
252 git commit -m "Removing foo" &&
258 git commit -m "Re-adding foo" &&
260 git filter-branch -f --subdirectory-filter foo &&
261 test $(git rev-list master | wc -l) = 3
264 test_expect_success
'Tag name filtering retains tag message' '
266 git cat-file tag T > expect &&
267 git filter-branch -f --tag-name-filter cat &&
268 git cat-file tag T > actual &&
269 test_cmp expect actual
272 faux_gpg_tag
='object XXXXXX
275 tagger T A Gger <tagger@example.com> 1206026339 -0500
277 This is a faux gpg signed tag.
278 -----BEGIN PGP SIGNATURE-----
279 Version: FauxGPG v0.0.0 (FAUX/Linux)
281 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
282 acmwXaWET20H0GeAGP+7vow=
284 -----END PGP SIGNATURE-----
286 test_expect_success
'Tag name filtering strips gpg signature' '
287 sha1=$(git rev-parse HEAD) &&
288 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
289 git update-ref "refs/tags/S" "$sha1t" &&
290 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
291 git filter-branch -f --tag-name-filter cat &&
292 git cat-file tag S > actual &&
293 test_cmp expect actual
296 test_expect_success GPG
'Filtering retains message of gpg signed commit' '
301 git commit -S -m "Adding gpg" &&
303 git log -1 --format="%s" > expect &&
304 git filter-branch -f --msg-filter "cat" &&
305 git log -1 --format="%s" > actual &&
306 test_cmp expect actual
309 test_expect_success
'Tag name filtering allows slashes in tag names' '
310 git tag -m tag-with-slash X/1 &&
311 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
312 git filter-branch -f --tag-name-filter "echo X/2" &&
313 git cat-file tag X/2 > actual &&
314 test_cmp expect actual
316 test_expect_success
'setup --prune-empty comparisons' '
317 git checkout --orphan master-no-a &&
321 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
322 test_commit --notick B B.t B Bx &&
323 git checkout -b branch-no-a Bx &&
324 test_commit D D.t D Dx &&
326 test_commit dir/D dir/D.t dir/D dir/Dx &&
327 test_commit E E.t E Ex &&
328 git checkout master-no-a &&
329 test_commit C C.t C Cx &&
330 git checkout branch-no-a &&
331 git merge Cx -m "Merge tag '\''C'\'' into branch" &&
333 test_commit G G.t G Gx &&
334 test_commit H H.t H Hx &&
338 test_expect_success
'Prune empty commits' '
339 git rev-list HEAD > expect &&
340 test_commit to_remove &&
341 git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
342 git rev-list HEAD > actual &&
343 test_cmp expect actual
346 test_expect_success
'prune empty collapsed merges' '
347 test_config merge.ff false &&
348 git rev-list HEAD >expect &&
349 test_commit to_remove_2 &&
350 git reset --hard HEAD^ &&
351 test_merge non-ff to_remove_2 &&
352 git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
353 git rev-list HEAD >actual &&
354 test_cmp expect actual
357 test_expect_success
'prune empty works even without index/tree filters' '
358 git rev-list HEAD >expect &&
359 git commit --allow-empty -m empty &&
360 git filter-branch -f --prune-empty HEAD &&
361 git rev-list HEAD >actual &&
362 test_cmp expect actual
365 test_expect_success
'--prune-empty is able to prune root commit' '
366 git rev-list branch-no-a >expect &&
367 git branch testing H &&
368 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
369 git rev-list testing >actual &&
370 git branch -D testing &&
371 test_cmp expect actual
374 test_expect_success
'--prune-empty is able to prune entire branch' '
375 git branch prune-entire B &&
376 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
377 test_path_is_missing .git/refs/heads/prune-entire &&
378 test_must_fail git reflog exists refs/heads/prune-entire
381 test_expect_success
'--remap-to-ancestor with filename filters' '
382 git checkout master &&
383 git reset --hard A &&
384 test_commit add-foo foo 1 &&
385 git branch moved-foo &&
386 test_commit add-bar bar a &&
387 git branch invariant &&
388 orig_invariant=$(git rev-parse invariant) &&
389 git branch moved-bar &&
390 test_commit change-foo foo 2 &&
391 git filter-branch -f --remap-to-ancestor \
392 moved-foo moved-bar A..master \
394 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
395 test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
396 test $orig_invariant = $(git rev-parse invariant)
399 test_expect_success
'automatic remapping to ancestor with filename filters' '
400 git checkout master &&
401 git reset --hard A &&
402 test_commit add-foo2 foo 1 &&
403 git branch moved-foo2 &&
404 test_commit add-bar2 bar a &&
405 git branch invariant2 &&
406 orig_invariant=$(git rev-parse invariant2) &&
407 git branch moved-bar2 &&
408 test_commit change-foo2 foo 2 &&
409 git filter-branch -f \
410 moved-foo2 moved-bar2 A..master \
412 test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
413 test $(git rev-parse moved-foo2) = $(git rev-parse master^) &&
414 test $orig_invariant = $(git rev-parse invariant2)
417 test_expect_success
'setup submodule' '
422 submodurl="$PWD/submod" &&
425 test_commit file-in-submod ) &&
426 git submodule add "$submodurl" &&
427 git commit -m "added submodule" &&
428 test_commit add-file &&
429 ( cd submod && test_commit add-in-submodule ) &&
431 git commit -m "changed submodule" &&
432 git branch original HEAD
435 orig_head
=$
(git show-ref
--hash --head HEAD
)
437 test_expect_success
'rewrite submodule with another content' '
438 git filter-branch --tree-filter "test -d submod && {
440 git rm -rf --quiet submod &&
444 test $orig_head != $(git show-ref --hash --head HEAD)
447 test_expect_success
'replace submodule revision' '
448 git reset --hard original &&
449 git filter-branch -f --tree-filter \
450 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
451 then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
453 test $orig_head != $(git show-ref --hash --head HEAD)
456 test_expect_success
'filter commit message without trailing newline' '
457 git reset --hard original &&
458 commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
459 git update-ref refs/heads/no-newline $commit &&
460 git filter-branch -f refs/heads/no-newline &&
461 echo $commit >expect &&
462 git rev-parse refs/heads/no-newline >actual &&
463 test_cmp expect actual
466 test_expect_success
'tree-filter deals with object name vs pathname ambiguity' '
467 test_when_finished "git reset --hard original" &&
468 ambiguous=$(git rev-list -1 HEAD) &&
469 git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
470 git show HEAD:$ambiguous