interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t7003-filter-branch.sh

563 lines · 16109 bytes Raw Blame History
1#!/bin/sh
2
3test_description='git filter-branch'
4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7. ./test-lib.sh
8. "$TEST_DIRECTORY/lib-gpg.sh"
9
10test_expect_success 'setup' '
11 test_commit A &&
12 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
13 test_commit --notick B &&
14 git checkout -b branch B &&
15 test_commit D &&
16 mkdir dir &&
17 test_commit dir/D &&
18 test_commit E &&
19 git checkout main &&
20 test_commit C &&
21 git checkout branch &&
22 git merge C &&
23 git tag F &&
24 test_commit G &&
25 test_commit H
26'
27# * (HEAD, branch) H
28# * G
29# * Merge commit 'C' into branch
30# |\
31# | * (main) C
32# * | E
33# * | dir/D
34# * | D
35# |/
36# * B
37# * A
38
39
40H=$(git rev-parse H)
41
42test_expect_success 'rewrite identically' '
43 git filter-branch branch
44'
45test_expect_success 'result is really identical' '
46 test $H = $(git rev-parse HEAD)
47'
48
49test_expect_success 'rewrite bare repository identically' '
50 (git config core.bare true && cd .git &&
51 git filter-branch branch > filter-output 2>&1 &&
52 test_grep ! fatal filter-output)
53'
54git config core.bare false
55test_expect_success 'result is really identical' '
56 test $H = $(git rev-parse HEAD)
57'
58
59TRASHDIR=$(pwd)
60test_expect_success 'correct GIT_DIR while using -d' '
61 mkdir drepo &&
62 ( cd drepo &&
63 git init &&
64 test_commit drepo &&
65 git filter-branch -d "$TRASHDIR/dfoo" \
66 --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
67 ) &&
68 test_grep drepo "$TRASHDIR/backup-refs"
69'
70
71test_expect_success 'tree-filter works with -d' '
72 git init drepo-tree &&
73 (
74 cd drepo-tree &&
75 test_commit one &&
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 &&
81 test_cmp one.t actual
82 )
83'
84
85test_expect_success 'Fail if commit filter fails' '
86 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
87'
88
89test_expect_success 'rewrite, renaming a specific file' '
90 git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
91'
92
93test_expect_success 'test that the file was renamed' '
94 test D = "$(git show HEAD:doh --)" &&
95 test_path_is_missing D.t &&
96 test_path_is_file doh &&
97 test D = "$(cat doh)"
98'
99
100test_expect_success 'rewrite, renaming a specific directory' '
101 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
102'
103
104test_expect_success 'test that the directory was renamed' '
105 test dir/D = "$(git show HEAD:diroh/D.t --)" &&
106 test_path_is_missing dir &&
107 test_path_is_dir diroh &&
108 test_path_is_missing diroh/dir &&
109 test_path_is_file diroh/D.t &&
110 test dir/D = "$(cat diroh/D.t)"
111'
112
113V=$(git rev-parse HEAD)
114
115test_expect_success 'populate --state-branch' '
116 git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
117'
118
119W=$(git rev-parse HEAD)
120
121test_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 "exit 1" HEAD &&
125 test_cmp_rev $W HEAD
126'
127
128test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '
129 test_when_finished "rm -fr incremental" &&
130 git init incremental &&
131 (
132 cd incremental &&
133 mkdir sub &&
134 test_commit first sub/file &&
135 test_commit outside root-file &&
136 git filter-branch --state-branch refs/state \
137 --prune-empty --subdirectory-filter sub -- HEAD &&
138 rewritten_first=$(git rev-parse HEAD) &&
139 git reset --hard outside &&
140 test_commit second sub/file &&
141 git filter-branch -f --state-branch refs/state \
142 --prune-empty --subdirectory-filter sub -- outside..HEAD &&
143 test_cmp_rev $rewritten_first HEAD^ &&
144 git show refs/state:filter.map >map &&
145 echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
146 grep "^$(git rev-parse second):" map >actual &&
147 test_cmp expect actual
148 )
149'
150
151git tag oldD HEAD~4
152test_expect_success 'rewrite one branch, keeping a side branch' '
153 git branch modD oldD &&
154 git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
155'
156
157test_expect_success 'common ancestor is still common (unchanged)' '
158 test "$(git merge-base modD D)" = "$(git rev-parse B)"
159'
160
161test_expect_success 'filter subdirectory only' '
162 mkdir subdir &&
163 touch subdir/new &&
164 git add subdir/new &&
165 test_tick &&
166 git commit -m "subdir" &&
167 echo H > A.t &&
168 test_tick &&
169 git commit -m "not subdir" A.t &&
170 echo A > subdir/new &&
171 test_tick &&
172 git commit -m "again subdir" subdir/new &&
173 git rm A.t &&
174 test_tick &&
175 git commit -m "again not subdir" &&
176 git branch sub &&
177 git branch sub-earlier HEAD~2 &&
178 git filter-branch -f --subdirectory-filter subdir \
179 refs/heads/sub refs/heads/sub-earlier
180'
181
182test_expect_success 'subdirectory filter result looks okay' '
183 test 2 = $(git rev-list sub | wc -l) &&
184 git show sub:new &&
185 test_must_fail git show sub:subdir &&
186 git show sub-earlier:new &&
187 test_must_fail git show sub-earlier:subdir
188'
189
190test_expect_success 'more setup' '
191 git checkout main &&
192 mkdir subdir &&
193 echo A > subdir/new &&
194 git add subdir/new &&
195 test_tick &&
196 git commit -m "subdir on main" subdir/new &&
197 git rm A.t &&
198 test_tick &&
199 git commit -m "again subdir on main" &&
200 git merge branch
201'
202
203test_expect_success 'use index-filter to move into a subdirectory' '
204 git branch directorymoved &&
205 git filter-branch -f --index-filter \
206 "git ls-files -s | sed \"s- -&newsubdir/-\" |
207 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
208 git update-index --index-info &&
209 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
210 git diff --exit-code HEAD directorymoved:newsubdir
211'
212
213test_expect_success 'stops when msg filter fails' '
214 old=$(git rev-parse HEAD) &&
215 test_must_fail git filter-branch -f --msg-filter false HEAD &&
216 test $old = $(git rev-parse HEAD) &&
217 rm -rf .git-rewrite
218'
219
220test_expect_success 'author information is preserved' '
221 : > i &&
222 git add i &&
223 test_tick &&
224 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
225 git branch preserved-author &&
226 (sane_unset GIT_AUTHOR_NAME &&
227 git filter-branch -f --msg-filter "cat; \
228 test \$GIT_COMMIT != $(git rev-parse main) || \
229 echo Hallo" \
230 preserved-author) &&
231 git rev-list --author="B V Uips" preserved-author >actual &&
232 test_line_count = 1 actual
233'
234
235test_expect_success "remove a certain author's commits" '
236 echo i > i &&
237 test_tick &&
238 git commit -m i i &&
239 git branch removed-author &&
240 git filter-branch -f --commit-filter "\
241 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
242 then\
243 skip_commit \"\$@\";
244 else\
245 git commit-tree \"\$@\";\
246 fi" removed-author &&
247 cnt1=$(git rev-list main | wc -l) &&
248 cnt2=$(git rev-list removed-author | wc -l) &&
249 test $cnt1 -eq $(($cnt2 + 1)) &&
250 git rev-list --author="B V Uips" removed-author >actual &&
251 test_line_count = 0 actual
252'
253
254test_expect_success 'barf on invalid name' '
255 test_must_fail git filter-branch -f main xy-problem &&
256 test_must_fail git filter-branch -f HEAD^
257'
258
259test_expect_success '"map" works in commit filter' '
260 git filter-branch -f --commit-filter "\
261 parent=\$(git rev-parse \$GIT_COMMIT^) &&
262 mapped=\$(map \$parent) &&
263 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
264 test \$mapped = \$actual &&
265 git commit-tree \"\$@\";" main~2..main &&
266 git rev-parse --verify main
267'
268
269test_expect_success 'Name needing quotes' '
270
271 git checkout -b rerere A &&
272 mkdir foo &&
273 name="れれれ" &&
274 >foo/$name &&
275 git add foo &&
276 git commit -m "Adding a file" &&
277 git filter-branch --tree-filter "rm -fr foo" &&
278 test_must_fail git ls-files --error-unmatch "foo/$name" &&
279 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
280
281'
282
283test_expect_success 'Subdirectory filter with disappearing trees' '
284 git reset --hard &&
285 git checkout main &&
286
287 mkdir foo &&
288 touch foo/bar &&
289 git add foo &&
290 test_tick &&
291 git commit -m "Adding foo" &&
292
293 git rm -r foo &&
294 test_tick &&
295 git commit -m "Removing foo" &&
296
297 mkdir foo &&
298 touch foo/bar &&
299 git add foo &&
300 test_tick &&
301 git commit -m "Re-adding foo" &&
302
303 git filter-branch -f --subdirectory-filter foo &&
304 git rev-list main >actual &&
305 test_line_count = 3 actual
306'
307
308test_expect_success 'Tag name filtering retains tag message' '
309 git tag -m atag T &&
310 git cat-file tag T > expect &&
311 git filter-branch -f --tag-name-filter cat &&
312 git cat-file tag T > actual &&
313 test_cmp expect actual
314'
315
316faux_gpg_tag='object XXXXXX
317type commit
318tag S
319tagger T A Gger <tagger@example.com> 1206026339 -0500
320
321This is a faux gpg signed tag.
322-----BEGIN PGP SIGNATURE-----
323Version: FauxGPG v0.0.0 (FAUX/Linux)
324
325gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
326acmwXaWET20H0GeAGP+7vow=
327=agpO
328-----END PGP SIGNATURE-----
329'
330test_expect_success 'Tag name filtering strips gpg signature' '
331 sha1=$(git rev-parse HEAD) &&
332 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
333 git update-ref "refs/tags/S" "$sha1t" &&
334 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
335 git filter-branch -f --tag-name-filter cat &&
336 git cat-file tag S > actual &&
337 test_cmp expect actual
338'
339
340test_expect_success GPG 'Filtering retains message of gpg signed commit' '
341 mkdir gpg &&
342 touch gpg/foo &&
343 git add gpg &&
344 test_tick &&
345 git commit -S -m "Adding gpg" &&
346
347 git log -1 --format="%s" > expect &&
348 git filter-branch -f --msg-filter "cat" &&
349 git log -1 --format="%s" > actual &&
350 test_cmp expect actual
351'
352
353test_expect_success 'Tag name filtering allows slashes in tag names' '
354 git tag -m tag-with-slash X/1 &&
355 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
356 git filter-branch -f --tag-name-filter "echo X/2" &&
357 git cat-file tag X/2 > actual &&
358 test_cmp expect actual
359'
360test_expect_success 'setup --prune-empty comparisons' '
361 git checkout --orphan main-no-a &&
362 git rm -rf . &&
363 unset test_tick &&
364 test_tick &&
365 GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
366 test_commit --notick B B.t B Bx &&
367 git checkout -b branch-no-a Bx &&
368 test_commit D D.t D Dx &&
369 mkdir dir &&
370 test_commit dir/D dir/D.t dir/D dir/Dx &&
371 test_commit E E.t E Ex &&
372 git checkout main-no-a &&
373 test_commit C C.t C Cx &&
374 git checkout branch-no-a &&
375 git merge Cx -m "Merge tag '\''C'\'' into branch" &&
376 git tag Fx &&
377 test_commit G G.t G Gx &&
378 test_commit H H.t H Hx &&
379 git checkout branch
380'
381
382test_expect_success 'Prune empty commits' '
383 git rev-list HEAD > expect &&
384 test_commit to_remove &&
385 git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
386 git rev-list HEAD > actual &&
387 test_cmp expect actual
388'
389
390test_expect_success 'prune empty collapsed merges' '
391 test_config merge.ff false &&
392 git rev-list HEAD >expect &&
393 test_commit to_remove_2 &&
394 git reset --hard HEAD^ &&
395 test_merge non-ff to_remove_2 &&
396 git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
397 git rev-list HEAD >actual &&
398 test_cmp expect actual
399'
400
401test_expect_success 'prune empty works even without index/tree filters' '
402 git rev-list HEAD >expect &&
403 git commit --allow-empty -m empty &&
404 git filter-branch -f --prune-empty HEAD &&
405 git rev-list HEAD >actual &&
406 test_cmp expect actual
407'
408
409test_expect_success '--prune-empty is able to prune root commit' '
410 git rev-list branch-no-a >expect &&
411 git branch testing H &&
412 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
413 git rev-list testing >actual &&
414 git branch -D testing &&
415 test_cmp expect actual
416'
417
418test_expect_success '--prune-empty is able to prune entire branch' '
419 git branch prune-entire B &&
420 git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
421 test_must_fail git rev-parse refs/heads/prune-entire &&
422 test_must_fail git reflog exists refs/heads/prune-entire
423'
424
425test_expect_success '--remap-to-ancestor with filename filters' '
426 git checkout main &&
427 git reset --hard A &&
428 test_commit add-foo foo 1 &&
429 git branch moved-foo &&
430 test_commit add-bar bar a &&
431 git branch invariant &&
432 orig_invariant=$(git rev-parse invariant) &&
433 git branch moved-bar &&
434 test_commit change-foo foo 2 &&
435 git filter-branch -f --remap-to-ancestor \
436 moved-foo moved-bar A..main \
437 -- -- foo &&
438 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
439 test $(git rev-parse moved-foo) = $(git rev-parse main^) &&
440 test $orig_invariant = $(git rev-parse invariant)
441'
442
443test_expect_success 'automatic remapping to ancestor with filename filters' '
444 git checkout main &&
445 git reset --hard A &&
446 test_commit add-foo2 foo 1 &&
447 git branch moved-foo2 &&
448 test_commit add-bar2 bar a &&
449 git branch invariant2 &&
450 orig_invariant=$(git rev-parse invariant2) &&
451 git branch moved-bar2 &&
452 test_commit change-foo2 foo 2 &&
453 git filter-branch -f \
454 moved-foo2 moved-bar2 A..main \
455 -- -- foo &&
456 test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
457 test $(git rev-parse moved-foo2) = $(git rev-parse main^) &&
458 test $orig_invariant = $(git rev-parse invariant2)
459'
460
461test_expect_success 'setup submodule' '
462 rm -fr ?* .git &&
463 git init &&
464 test_commit file &&
465 mkdir submod &&
466 submodurl="$PWD/submod" &&
467 ( cd submod &&
468 git init &&
469 test_commit file-in-submod ) &&
470 git submodule add "$submodurl" &&
471 git commit -m "added submodule" &&
472 test_commit add-file &&
473 ( cd submod && test_commit add-in-submodule ) &&
474 git add submod &&
475 git commit -m "changed submodule" &&
476 git branch original HEAD
477'
478
479orig_head=$(git show-ref --hash --head HEAD)
480
481test_expect_success 'rewrite submodule with another content' '
482 git filter-branch --tree-filter "test -d submod && {
483 rm -rf submod &&
484 git rm -rf --quiet submod &&
485 mkdir submod &&
486 : > submod/file
487 } || :" HEAD &&
488 test $orig_head != $(git show-ref --hash --head HEAD)
489'
490
491test_expect_success 'replace submodule revision' '
492 invalid=$(test_oid numeric) &&
493 git reset --hard original &&
494 git filter-branch -f --tree-filter \
495 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
496 then git update-index --cacheinfo 160000 $invalid submod
497 fi" HEAD &&
498 test $orig_head != $(git show-ref --hash --head HEAD)
499'
500
501test_expect_success 'filter commit message without trailing newline' '
502 git reset --hard original &&
503 commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
504 git update-ref refs/heads/no-newline $commit &&
505 git filter-branch -f refs/heads/no-newline &&
506 echo $commit >expect &&
507 git rev-parse refs/heads/no-newline >actual &&
508 test_cmp expect actual
509'
510
511test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
512 test_when_finished "git reset --hard original" &&
513 ambiguous=$(git rev-list -1 HEAD) &&
514 git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
515 git show HEAD:$ambiguous
516'
517
518test_expect_success 'rewrite repository including refs that point at non-commit object' '
519 test_when_finished "git reset --hard original" &&
520 tree=$(git rev-parse HEAD^{tree}) &&
521 test_when_finished "git replace -d $tree" &&
522 echo A >new &&
523 git add new &&
524 new_tree=$(git write-tree) &&
525 git replace $tree $new_tree &&
526 git tag -a -m "tag to a tree" treetag $new_tree &&
527 git reset --hard HEAD &&
528 git filter-branch -f -- --all >filter-output 2>&1 &&
529 test_grep ! fatal filter-output
530'
531
532test_expect_success 'filter-branch handles ref deletion' '
533 git switch --orphan empty-commit &&
534 git commit --allow-empty -m "empty commit" &&
535 git tag empty &&
536 git branch to-delete &&
537 git filter-branch -f --prune-empty to-delete >out 2>&1 &&
538 test_grep "to-delete.*was deleted" out &&
539 test_must_fail git rev-parse --verify to-delete
540'
541
542test_expect_success 'filter-branch handles ref rewrite' '
543 git checkout empty &&
544 test_commit to-drop &&
545 git branch rewrite &&
546 git filter-branch -f \
547 --index-filter "git rm --ignore-unmatch --cached to-drop.t" \
548 rewrite >out 2>&1 &&
549 test_grep "rewrite.*was rewritten" out &&
550 test_grep ! -i warning out &&
551 git diff-tree empty rewrite
552'
553
554test_expect_success 'filter-branch handles ancestor rewrite' '
555 test_commit to-exclude &&
556 git branch ancestor &&
557 git filter-branch -f ancestor -- :^to-exclude.t >out 2>&1 &&
558 test_grep "ancestor.*was rewritten" out &&
559 test_grep ! -i warning out &&
560 git diff-tree HEAD^ ancestor
561'
562
563test_done