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