]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7003-filter-branch.sh
t7003: ensure --prune-empty can prune root commit
[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
dfd05e38 110git tag oldD HEAD~4
98409060 111test_expect_success 'rewrite one branch, keeping a side branch' '
dfd05e38 112 git branch modD oldD &&
77f2e4f5 113 git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
98409060
JS
114'
115
116test_expect_success 'common ancestor is still common (unchanged)' '
5be60078 117 test "$(git merge-base modD D)" = "$(git rev-parse B)"
98409060
JS
118'
119
685ef546
JS
120test_expect_success 'filter subdirectory only' '
121 mkdir subdir &&
122 touch subdir/new &&
123 git add subdir/new &&
124 test_tick &&
125 git commit -m "subdir" &&
77f2e4f5 126 echo H > A.t &&
685ef546 127 test_tick &&
77f2e4f5 128 git commit -m "not subdir" A.t &&
685ef546
JS
129 echo A > subdir/new &&
130 test_tick &&
131 git commit -m "again subdir" subdir/new &&
77f2e4f5 132 git rm A.t &&
685ef546
JS
133 test_tick &&
134 git commit -m "again not subdir" &&
dfd05e38 135 git branch sub &&
6e84b712 136 git branch sub-earlier HEAD~2 &&
9b8ae93a 137 git filter-branch -f --subdirectory-filter subdir \
6e84b712 138 refs/heads/sub refs/heads/sub-earlier
685ef546
JS
139'
140
141test_expect_success 'subdirectory filter result looks okay' '
5be60078 142 test 2 = $(git rev-list sub | wc -l) &&
685ef546 143 git show sub:new &&
6e84b712
TR
144 test_must_fail git show sub:subdir &&
145 git show sub-earlier:new &&
146 test_must_fail git show sub-earlier:subdir
685ef546
JS
147'
148
a17171b4 149test_expect_success 'more setup' '
cfabd6ee
JS
150 git checkout master &&
151 mkdir subdir &&
152 echo A > subdir/new &&
153 git add subdir/new &&
154 test_tick &&
155 git commit -m "subdir on master" subdir/new &&
77f2e4f5 156 git rm A.t &&
cfabd6ee
JS
157 test_tick &&
158 git commit -m "again subdir on master" &&
a17171b4 159 git merge branch
cfabd6ee
JS
160'
161
55f22ff2 162test_expect_success 'use index-filter to move into a subdirectory' '
dfd05e38 163 git branch directorymoved &&
d592b315 164 git filter-branch -f --index-filter \
0d1d6e50 165 "git ls-files -s | sed \"s- -&newsubdir/-\" |
55f22ff2 166 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
5be60078 167 git update-index --index-info &&
f69e836f 168 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
3c8710ae
TR
169 git diff --exit-code HEAD directorymoved:newsubdir
170'
55f22ff2 171
8c1ce0f4 172test_expect_success 'stops when msg filter fails' '
dfd05e38 173 old=$(git rev-parse HEAD) &&
d592b315 174 test_must_fail git filter-branch -f --msg-filter false HEAD &&
dfd05e38
JS
175 test $old = $(git rev-parse HEAD) &&
176 rm -rf .git-rewrite
8c1ce0f4
JS
177'
178
f6b78c6e
JS
179test_expect_success 'author information is preserved' '
180 : > i &&
181 git add i &&
182 test_tick &&
183 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
dfd05e38 184 git branch preserved-author &&
3c730fab
JK
185 (sane_unset GIT_AUTHOR_NAME &&
186 git filter-branch -f --msg-filter "cat; \
8c1ce0f4 187 test \$GIT_COMMIT != $(git rev-parse master) || \
f6b78c6e 188 echo Hallo" \
3c730fab 189 preserved-author) &&
f6b78c6e
JS
190 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
191'
192
193test_expect_success "remove a certain author's commits" '
194 echo i > i &&
195 test_tick &&
196 git commit -m i i &&
dfd05e38 197 git branch removed-author &&
d592b315 198 git filter-branch -f --commit-filter "\
f6b78c6e
JS
199 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
200 then\
f95eef15 201 skip_commit \"\$@\";
f6b78c6e
JS
202 else\
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)
209'
210
dfd05e38 211test_expect_success 'barf on invalid name' '
c8a08692
BC
212 test_must_fail git filter-branch -f master xy-problem &&
213 test_must_fail git filter-branch -f HEAD^
dfd05e38
JS
214'
215
7e0f1704
JS
216test_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
224'
225
1fe32cb9
JH
226test_expect_success 'Name needing quotes' '
227
228 git checkout -b rerere A &&
229 mkdir foo &&
230 name="ă‚Śă‚Śă‚Ś" &&
231 >foo/$name &&
232 git add foo &&
233 git commit -m "Adding a file" &&
234 git filter-branch --tree-filter "rm -fr foo" &&
c8a08692 235 test_must_fail git ls-files --error-unmatch "foo/$name" &&
1fe32cb9
JH
236 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
237
238'
239
5b044ac3
JH
240test_expect_success 'Subdirectory filter with disappearing trees' '
241 git reset --hard &&
242 git checkout master &&
243
244 mkdir foo &&
245 touch foo/bar &&
246 git add foo &&
247 test_tick &&
248 git commit -m "Adding foo" &&
249
250 git rm -r foo &&
251 test_tick &&
252 git commit -m "Removing foo" &&
253
254 mkdir foo &&
255 touch foo/bar &&
256 git add foo &&
257 test_tick &&
258 git commit -m "Re-adding foo" &&
259
260 git filter-branch -f --subdirectory-filter foo &&
261 test $(git rev-list master | wc -l) = 3
262'
263
1bf6551e
BC
264test_expect_success 'Tag name filtering retains tag message' '
265 git tag -m atag T &&
266 git cat-file tag T > expect &&
267 git filter-branch -f --tag-name-filter cat &&
268 git cat-file tag T > actual &&
3af82863 269 test_cmp expect actual
1bf6551e
BC
270'
271
272faux_gpg_tag='object XXXXXX
273type commit
274tag S
275tagger T A Gger <tagger@example.com> 1206026339 -0500
276
277This is a faux gpg signed tag.
278-----BEGIN PGP SIGNATURE-----
279Version: FauxGPG v0.0.0 (FAUX/Linux)
280
281gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
282acmwXaWET20H0GeAGP+7vow=
283=agpO
284-----END PGP SIGNATURE-----
285'
286test_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 &&
3af82863 293 test_cmp expect actual
1bf6551e
BC
294'
295
a5a4b3ff
JM
296test_expect_success GPG 'Filtering retains message of gpg signed commit' '
297 mkdir gpg &&
298 touch gpg/foo &&
299 git add gpg &&
300 test_tick &&
301 git commit -S -m "Adding gpg" &&
302
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
307'
308
a9da1663
JS
309test_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
315'
377a3543
DP
316test_expect_success 'setup --prune-empty comparisons' '
317 git checkout --orphan master-no-a &&
318 git rm -rf . &&
319 unset test_tick &&
320 test_tick &&
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 &&
325 mkdir dir &&
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" &&
332 git tag Fx &&
333 test_commit G G.t G Gx &&
334 test_commit H H.t H Hx &&
335 git checkout branch
336'
a9da1663 337
d3240d93
PH
338test_expect_success 'Prune empty commits' '
339 git rev-list HEAD > expect &&
77f2e4f5
BG
340 test_commit to_remove &&
341 git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
d3240d93
PH
342 git rev-list HEAD > actual &&
343 test_cmp expect actual
344'
345
79bc4ef3
CB
346test_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
355'
356
1dc413eb
JK
357test_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
363'
364
377a3543
DP
365test_expect_failure '--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
372'
373
f2f3a6b8
TR
374test_expect_success '--remap-to-ancestor with filename filters' '
375 git checkout master &&
376 git reset --hard A &&
377 test_commit add-foo foo 1 &&
378 git branch moved-foo &&
379 test_commit add-bar bar a &&
380 git branch invariant &&
381 orig_invariant=$(git rev-parse invariant) &&
382 git branch moved-bar &&
383 test_commit change-foo foo 2 &&
384 git filter-branch -f --remap-to-ancestor \
385 moved-foo moved-bar A..master \
386 -- -- foo &&
387 test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
388 test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
389 test $orig_invariant = $(git rev-parse invariant)
390'
391
7ec344d8
CH
392test_expect_success 'automatic remapping to ancestor with filename filters' '
393 git checkout master &&
394 git reset --hard A &&
395 test_commit add-foo2 foo 1 &&
396 git branch moved-foo2 &&
397 test_commit add-bar2 bar a &&
398 git branch invariant2 &&
399 orig_invariant=$(git rev-parse invariant2) &&
400 git branch moved-bar2 &&
401 test_commit change-foo2 foo 2 &&
402 git filter-branch -f \
403 moved-foo2 moved-bar2 A..master \
404 -- -- foo &&
405 test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
406 test $(git rev-parse moved-foo2) = $(git rev-parse master^) &&
407 test $orig_invariant = $(git rev-parse invariant2)
408'
409
1f7d57ff
MS
410test_expect_success 'setup submodule' '
411 rm -fr ?* .git &&
412 git init &&
413 test_commit file &&
414 mkdir submod &&
415 submodurl="$PWD/submod" &&
416 ( cd submod &&
417 git init &&
418 test_commit file-in-submod ) &&
419 git submodule add "$submodurl" &&
420 git commit -m "added submodule" &&
421 test_commit add-file &&
422 ( cd submod && test_commit add-in-submodule ) &&
423 git add submod &&
424 git commit -m "changed submodule" &&
425 git branch original HEAD
426'
427
99485194 428orig_head=$(git show-ref --hash --head HEAD)
1f7d57ff
MS
429
430test_expect_success 'rewrite submodule with another content' '
431 git filter-branch --tree-filter "test -d submod && {
432 rm -rf submod &&
433 git rm -rf --quiet submod &&
434 mkdir submod &&
435 : > submod/file
436 } || :" HEAD &&
99485194 437 test $orig_head != $(git show-ref --hash --head HEAD)
1f7d57ff
MS
438'
439
440test_expect_success 'replace submodule revision' '
441 git reset --hard original &&
442 git filter-branch -f --tree-filter \
443 "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
444 then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
445 fi" HEAD &&
99485194 446 test $orig_head != $(git show-ref --hash --head HEAD)
1f7d57ff
MS
447'
448
df062010
JK
449test_expect_success 'filter commit message without trailing newline' '
450 git reset --hard original &&
451 commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
452 git update-ref refs/heads/no-newline $commit &&
453 git filter-branch -f refs/heads/no-newline &&
454 echo $commit >expect &&
455 git rev-parse refs/heads/no-newline >actual &&
456 test_cmp expect actual
457'
458
4d2a3646
SG
459test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
460 test_when_finished "git reset --hard original" &&
461 ambiguous=$(git rev-list -1 HEAD) &&
462 git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
463 git show HEAD:$ambiguous
464'
465
6f6826c5 466test_done