3 test_description
='git filter-branch'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 .
"$TEST_DIRECTORY/lib-gpg.sh"
10 test_expect_success
'setup' '
12 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
13 test_commit --notick B &&
14 git checkout -b branch B &&
21 git checkout branch &&
29 # * Merge commit 'C' into branch
42 test_expect_success
'rewrite identically' '
43 git filter-branch branch
45 test_expect_success
'result is really identical' '
46 test $H = $(git rev-parse HEAD)
49 test_expect_success
'rewrite bare repository identically' '
50 (git config core.bare true && cd .git &&
51 git filter-branch branch > filter-output 2>&1 &&
52 ! grep fatal filter-output)
54 git config core.bare false
55 test_expect_success
'result is really identical' '
56 test $H = $(git rev-parse HEAD)
60 test_expect_success
'correct GIT_DIR while using -d' '
65 git filter-branch -d "$TRASHDIR/dfoo" \
66 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
68 grep drepo "$TRASHDIR/backup-refs"
71 test_expect_success
'tree-filter works with -d' '
72 git init drepo-tree &&
76 git filter-branch -d "$TRASHDIR/dfoo" \
77 --tree-filter "echo changed >one.t" &&
78 echo changed >expect &&
79 git cat-file blob HEAD:one.t >actual &&
80 test_cmp expect actual &&
85 test_expect_success
'Fail if commit filter fails' '
86 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
89 test_expect_success
'rewrite, renaming a specific file' '
90 git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
93 test_expect_success
'test that the file was renamed' '
94 test D = "$(git show HEAD:doh --)" &&
100 test_expect_success
'rewrite, renaming a specific directory' '
101 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
104 test_expect_success
'test that the directory was renamed' '
105 test dir/D = "$(git show HEAD:diroh/D.t --)" &&
108 ! test -d diroh/dir &&
110 test dir/D = "$(cat diroh/D.t)"
113 V
=$
(git rev-parse HEAD
)
115 test_expect_success
'populate --state-branch' '
116 git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
119 W
=$
(git rev-parse HEAD
)
121 test_expect_success
'using --state-branch to skip already rewritten commits' '
122 test_when_finished git reset --hard $V &&
123 git reset --hard $V &&
124 git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
129 test_expect_success
'rewrite one branch, keeping a side branch' '
130 git branch modD oldD &&
131 git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
134 test_expect_success
'common ancestor is still common (unchanged)' '
135 test "$(git merge-base modD D)" = "$(git rev-parse B)"
138 test_expect_success
'filter subdirectory only' '
141 git add subdir/new &&
143 git commit -m "subdir" &&
146 git commit -m "not subdir" A.t &&
147 echo A > subdir/new &&
149 git commit -m "again subdir" subdir/new &&
152 git commit -m "again not subdir" &&
154 git branch sub-earlier HEAD~2 &&
155 git filter-branch -f --subdirectory-filter subdir \
156 refs/heads/sub refs/heads/sub-earlier
159 test_expect_success
'subdirectory filter result looks okay' '
160 test 2 = $(git rev-list sub | wc -l) &&
162 test_must_fail git show sub:subdir &&
163 git show sub-earlier:new &&
164 test_must_fail git show sub-earlier:subdir
167 test_expect_success
'more setup' '
170 echo A > subdir/new &&
171 git add subdir/new &&
173 git commit -m "subdir on main" subdir/new &&
176 git commit -m "again subdir on main" &&
180 test_expect_success
'use index-filter to move into a subdirectory' '
181 git branch directorymoved &&
182 git filter-branch -f --index-filter \
183 "git ls-files -s | sed \"s- -&newsubdir/-\" |
184 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
185 git update-index --index-info &&
186 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
187 git diff --exit-code HEAD directorymoved:newsubdir
190 test_expect_success
'stops when msg filter fails' '
191 old=$(git rev-parse HEAD) &&
192 test_must_fail git filter-branch -f --msg-filter false HEAD &&
193 test $old = $(git rev-parse HEAD) &&
197 test_expect_success
'author information is preserved' '
201 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
202 git branch preserved-author &&
203 (sane_unset GIT_AUTHOR_NAME &&
204 git filter-branch -f --msg-filter "cat; \
205 test \$GIT_COMMIT != $(git rev-parse main) || \
208 git rev-list --author="B V Uips" preserved-author >actual &&
209 test_line_count = 1 actual
212 test_expect_success
"remove a certain author's commits" '
216 git branch removed-author &&
217 git filter-branch -f --commit-filter "\
218 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
222 git commit-tree \"\$@\";\
223 fi" removed-author &&
224 cnt1=$(git rev-list main | wc -l) &&
225 cnt2=$(git rev-list removed-author | wc -l) &&
226 test $cnt1 -eq $(($cnt2 + 1)) &&
227 git rev-list --author="B V Uips" removed-author >actual &&
228 test_line_count = 0 actual
231 test_expect_success
'barf on invalid name' '
232 test_must_fail git filter-branch -f main xy-problem &&
233 test_must_fail git filter-branch -f HEAD^
236 test_expect_success
'"map" works in commit filter' '
237 git filter-branch -f --commit-filter "\
238 parent=\$(git rev-parse \$GIT_COMMIT^) &&
239 mapped=\$(map \$parent) &&
240 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
241 test \$mapped = \$actual &&
242 git commit-tree \"\$@\";" main~2..main &&
243 git rev-parse --verify main
246 test_expect_success
'Name needing quotes' '
248 git checkout -b rerere A &&
253 git commit -m "Adding a file" &&
254 git filter-branch --tree-filter "rm -fr foo" &&
255 test_must_fail git ls-files --error-unmatch "foo/$name" &&
256 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
260 test_expect_success
'Subdirectory filter with disappearing trees' '
268 git commit -m "Adding foo" &&
272 git commit -m "Removing foo" &&
278 git commit -m "Re-adding foo" &&
280 git filter-branch -f --subdirectory-filter foo &&
281 git rev-list main >actual &&
282 test_line_count = 3 actual
285 test_expect_success
'Tag name filtering retains tag message' '
287 git cat-file tag T > expect &&
288 git filter-branch -f --tag-name-filter cat &&
289 git cat-file tag T > actual &&
290 test_cmp expect actual
293 faux_gpg_tag
='object XXXXXX
296 tagger T A Gger <tagger@example.com> 1206026339 -0500
298 This is a faux gpg signed tag.
299 -----BEGIN PGP SIGNATURE-----
300 Version: FauxGPG v0.0.0 (FAUX/Linux)
302 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
303 acmwXaWET20H0GeAGP+7vow=
305 -----END PGP SIGNATURE-----
307 test_expect_success
'Tag name filtering strips gpg signature' '
308 sha1=$(git rev-parse HEAD) &&
309 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
310 git update-ref "refs/tags/S" "$sha1t" &&
311 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
312 git filter-branch -f --tag-name-filter cat &&
313 git cat-file tag S > actual &&
314 test_cmp expect actual
317 test_expect_success GPG
'Filtering retains message of gpg signed commit' '
322 git commit -S -m "Adding gpg" &&
324 git log -1 --format="%s" > expect &&
325 git filter-branch -f --msg-filter "cat" &&
326 git log -1 --format="%s" > actual &&
327 test_cmp expect actual
330 test_expect_success
'Tag name filtering allows slashes in tag names' '
331 git tag -m tag-with-slash X/1 &&
332 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
333 git filter-branch -f --tag-name-filter "echo X/2" &&
334 git cat-file tag X/2 > actual &&
335 test_cmp expect actual
337 test_expect_success
'setup --prune-empty comparisons' '
338 git checkout --orphan main-no-a &&
342 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
343 test_commit --notick B B.t B Bx &&
344 git checkout -b branch-no-a Bx &&
345 test_commit D D.t D Dx &&
347 test_commit dir/D dir/D.t dir/D dir/Dx &&
348 test_commit E E.t E Ex &&
349 git checkout main-no-a &&
350 test_commit C C.t C Cx &&
351 git checkout branch-no-a &&
352 git merge Cx -m "Merge tag '\''C'\'' into branch" &&
354 test_commit G G.t G Gx &&
355 test_commit H H.t H Hx &&
359 test_expect_success
'Prune empty commits' '
360 git rev-list HEAD > expect &&
361 test_commit to_remove &&
362 git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
363 git rev-list HEAD > actual &&
364 test_cmp expect actual
367 test_expect_success
'prune empty collapsed merges' '
368 test_config merge.ff false &&
369 git rev-list HEAD >expect &&
370 test_commit to_remove_2 &&
371 git reset --hard HEAD^ &&
372 test_merge non-ff to_remove_2 &&
373 git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
374 git rev-list HEAD >actual &&
375 test_cmp expect actual
378 test_expect_success
'prune empty works even without index/tree filters' '
379 git rev-list HEAD >expect &&
380 git commit --allow-empty -m empty &&
381 git filter-branch -f --prune-empty HEAD &&
382 git rev-list HEAD >actual &&
383 test_cmp expect actual
386 test_expect_success
'--prune-empty is able to prune root commit' '
387 git rev-list branch-no-a >expect &&
388 git branch testing H &&
389 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
390 git rev-list testing >actual &&
391 git branch -D testing &&
392 test_cmp expect actual
395 test_expect_success
'--prune-empty is able to prune entire branch' '
396 git branch prune-entire B &&
397 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
398 test_must_fail git rev-parse refs/heads/prune-entire &&
399 test_must_fail git reflog exists refs/heads/prune-entire
402 test_expect_success
'--remap-to-ancestor with filename filters' '
404 git reset --hard A &&
405 test_commit add-foo foo 1 &&
406 git branch moved-foo &&
407 test_commit add-bar bar a &&
408 git branch invariant &&
409 orig_invariant=$(git rev-parse invariant) &&
410 git branch moved-bar &&
411 test_commit change-foo foo 2 &&
412 git filter-branch -f --remap-to-ancestor \
413 moved-foo moved-bar A..main \
415 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
416 test $(git rev-parse moved-foo) = $(git rev-parse main^) &&
417 test $orig_invariant = $(git rev-parse invariant)
420 test_expect_success
'automatic remapping to ancestor with filename filters' '
422 git reset --hard A &&
423 test_commit add-foo2 foo 1 &&
424 git branch moved-foo2 &&
425 test_commit add-bar2 bar a &&
426 git branch invariant2 &&
427 orig_invariant=$(git rev-parse invariant2) &&
428 git branch moved-bar2 &&
429 test_commit change-foo2 foo 2 &&
430 git filter-branch -f \
431 moved-foo2 moved-bar2 A..main \
433 test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
434 test $(git rev-parse moved-foo2) = $(git rev-parse main^) &&
435 test $orig_invariant = $(git rev-parse invariant2)
438 test_expect_success
'setup submodule' '
443 submodurl="$PWD/submod" &&
446 test_commit file-in-submod ) &&
447 git submodule add "$submodurl" &&
448 git commit -m "added submodule" &&
449 test_commit add-file &&
450 ( cd submod && test_commit add-in-submodule ) &&
452 git commit -m "changed submodule" &&
453 git branch original HEAD
456 orig_head
=$
(git show-ref
--hash --head HEAD
)
458 test_expect_success
'rewrite submodule with another content' '
459 git filter-branch --tree-filter "test -d submod && {
461 git rm -rf --quiet submod &&
465 test $orig_head != $(git show-ref --hash --head HEAD)
468 test_expect_success
'replace submodule revision' '
469 invalid=$(test_oid numeric) &&
470 git reset --hard original &&
471 git filter-branch -f --tree-filter \
472 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
473 then git update-index --cacheinfo 160000 $invalid submod
475 test $orig_head != $(git show-ref --hash --head HEAD)
478 test_expect_success
'filter commit message without trailing newline' '
479 git reset --hard original &&
480 commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
481 git update-ref refs/heads/no-newline $commit &&
482 git filter-branch -f refs/heads/no-newline &&
483 echo $commit >expect &&
484 git rev-parse refs/heads/no-newline >actual &&
485 test_cmp expect actual
488 test_expect_success
'tree-filter deals with object name vs pathname ambiguity' '
489 test_when_finished "git reset --hard original" &&
490 ambiguous=$(git rev-list -1 HEAD) &&
491 git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
492 git show HEAD:$ambiguous
495 test_expect_success
'rewrite repository including refs that point at non-commit object' '
496 test_when_finished "git reset --hard original" &&
497 tree=$(git rev-parse HEAD^{tree}) &&
498 test_when_finished "git replace -d $tree" &&
501 new_tree=$(git write-tree) &&
502 git replace $tree $new_tree &&
503 git tag -a -m "tag to a tree" treetag $new_tree &&
504 git reset --hard HEAD &&
505 git filter-branch -f -- --all >filter-output 2>&1 &&
506 ! grep fatal filter-output
509 test_expect_success
'filter-branch handles ref deletion' '
510 git switch --orphan empty-commit &&
511 git commit --allow-empty -m "empty commit" &&
513 git branch to-delete &&
514 git filter-branch -f --prune-empty to-delete >out 2>&1 &&
515 grep "to-delete.*was deleted" out &&
516 test_must_fail git rev-parse --verify to-delete
519 test_expect_success
'filter-branch handles ref rewrite' '
520 git checkout empty &&
521 test_commit to-drop &&
522 git branch rewrite &&
523 git filter-branch -f \
524 --index-filter "git rm --ignore-unmatch --cached to-drop.t" \
526 grep "rewrite.*was rewritten" out &&
527 ! grep -i warning out &&
528 git diff-tree empty rewrite
531 test_expect_success
'filter-branch handles ancestor rewrite' '
532 test_commit to-exclude &&
533 git branch ancestor &&
534 git filter-branch -f ancestor -- :^to-exclude.t >out 2>&1 &&
535 grep "ancestor.*was rewritten" out &&
536 ! grep -i warning out &&
537 git diff-tree HEAD^ ancestor