]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7003-filter-branch.sh
builtin-branch.c: optimize --merged and --no-merged
[thirdparty/git.git] / t / t7003-filter-branch.sh
CommitLineData
6f6826c5
JS
1#!/bin/sh
2
3test_description='git-filter-branch'
4. ./test-lib.sh
5
6make_commit () {
40a7ce64 7 lower=$(echo $1 | tr '[A-Z]' '[a-z]')
6f6826c5
JS
8 echo $lower > $lower
9 git add $lower
aee078bf 10 test_tick
6f6826c5
JS
11 git commit -m $1
12 git tag $1
13}
14
15test_expect_success 'setup' '
16 make_commit A
17 make_commit B
18 git checkout -b branch B
19 make_commit D
90356287
JS
20 mkdir dir
21 make_commit dir/D
6f6826c5
JS
22 make_commit E
23 git checkout master
24 make_commit C
25 git checkout branch
26 git merge C
27 git tag F
28 make_commit G
29 make_commit H
30'
31
5be60078 32H=$(git rev-parse H)
6f6826c5
JS
33
34test_expect_success 'rewrite identically' '
dfd05e38 35 git-filter-branch branch
6f6826c5 36'
6f6826c5 37test_expect_success 'result is really identical' '
dfd05e38 38 test $H = $(git rev-parse HEAD)
6f6826c5
JS
39'
40
41test_expect_success 'rewrite, renaming a specific file' '
dfd05e38 42 git-filter-branch -f --tree-filter "mv d doh || :" HEAD
6f6826c5
JS
43'
44
45test_expect_success 'test that the file was renamed' '
90356287
JS
46 test d = "$(git show HEAD:doh --)" &&
47 ! test -f d &&
46eb449c 48 test -f doh &&
90356287
JS
49 test d = "$(cat doh)"
50'
51
52test_expect_success 'rewrite, renaming a specific directory' '
53 git-filter-branch -f --tree-filter "mv dir diroh || :" HEAD
54'
55
6a589fda 56test_expect_success 'test that the directory was renamed' '
90356287
JS
57 test dir/d = "$(git show HEAD:diroh/d --)" &&
58 ! test -d dir &&
59 test -d diroh &&
60 ! test -d diroh/dir &&
61 test -f diroh/d &&
62 test dir/d = "$(cat diroh/d)"
6f6826c5
JS
63'
64
dfd05e38 65git tag oldD HEAD~4
98409060 66test_expect_success 'rewrite one branch, keeping a side branch' '
dfd05e38
JS
67 git branch modD oldD &&
68 git-filter-branch -f --tree-filter "mv b boh || :" D..modD
98409060
JS
69'
70
71test_expect_success 'common ancestor is still common (unchanged)' '
5be60078 72 test "$(git merge-base modD D)" = "$(git rev-parse B)"
98409060
JS
73'
74
685ef546
JS
75test_expect_success 'filter subdirectory only' '
76 mkdir subdir &&
77 touch subdir/new &&
78 git add subdir/new &&
79 test_tick &&
80 git commit -m "subdir" &&
81 echo H > a &&
82 test_tick &&
83 git commit -m "not subdir" a &&
84 echo A > subdir/new &&
85 test_tick &&
86 git commit -m "again subdir" subdir/new &&
87 git rm a &&
88 test_tick &&
89 git commit -m "again not subdir" &&
dfd05e38
JS
90 git branch sub &&
91 git-filter-branch -f --subdirectory-filter subdir refs/heads/sub
685ef546
JS
92'
93
94test_expect_success 'subdirectory filter result looks okay' '
5be60078 95 test 2 = $(git rev-list sub | wc -l) &&
685ef546 96 git show sub:new &&
c8a08692 97 test_must_fail git show sub:subdir
685ef546
JS
98'
99
a17171b4 100test_expect_success 'more setup' '
cfabd6ee
JS
101 git checkout master &&
102 mkdir subdir &&
103 echo A > subdir/new &&
104 git add subdir/new &&
105 test_tick &&
106 git commit -m "subdir on master" subdir/new &&
107 git rm a &&
108 test_tick &&
109 git commit -m "again subdir on master" &&
a17171b4 110 git merge branch
cfabd6ee
JS
111'
112
55f22ff2 113test_expect_success 'use index-filter to move into a subdirectory' '
dfd05e38
JS
114 git branch directorymoved &&
115 git-filter-branch -f --index-filter \
5be60078 116 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
55f22ff2 117 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
5be60078 118 git update-index --index-info &&
f69e836f 119 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
55f22ff2
JS
120 test -z "$(git diff HEAD directorymoved:newsubdir)"'
121
8c1ce0f4 122test_expect_success 'stops when msg filter fails' '
dfd05e38 123 old=$(git rev-parse HEAD) &&
c8a08692 124 test_must_fail git-filter-branch -f --msg-filter false HEAD &&
dfd05e38
JS
125 test $old = $(git rev-parse HEAD) &&
126 rm -rf .git-rewrite
8c1ce0f4
JS
127'
128
f6b78c6e
JS
129test_expect_success 'author information is preserved' '
130 : > i &&
131 git add i &&
132 test_tick &&
133 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
dfd05e38
JS
134 git branch preserved-author &&
135 git-filter-branch -f --msg-filter "cat; \
8c1ce0f4 136 test \$GIT_COMMIT != $(git rev-parse master) || \
f6b78c6e
JS
137 echo Hallo" \
138 preserved-author &&
139 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
140'
141
142test_expect_success "remove a certain author's commits" '
143 echo i > i &&
144 test_tick &&
145 git commit -m i i &&
dfd05e38
JS
146 git branch removed-author &&
147 git-filter-branch -f --commit-filter "\
f6b78c6e
JS
148 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
149 then\
f95eef15 150 skip_commit \"\$@\";
f6b78c6e
JS
151 else\
152 git commit-tree \"\$@\";\
153 fi" removed-author &&
154 cnt1=$(git rev-list master | wc -l) &&
155 cnt2=$(git rev-list removed-author | wc -l) &&
156 test $cnt1 -eq $(($cnt2 + 1)) &&
157 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
158'
159
dfd05e38 160test_expect_success 'barf on invalid name' '
c8a08692
BC
161 test_must_fail git filter-branch -f master xy-problem &&
162 test_must_fail git filter-branch -f HEAD^
dfd05e38
JS
163'
164
7e0f1704
JS
165test_expect_success '"map" works in commit filter' '
166 git filter-branch -f --commit-filter "\
167 parent=\$(git rev-parse \$GIT_COMMIT^) &&
168 mapped=\$(map \$parent) &&
169 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
170 test \$mapped = \$actual &&
171 git commit-tree \"\$@\";" master~2..master &&
172 git rev-parse --verify master
173'
174
1fe32cb9
JH
175test_expect_success 'Name needing quotes' '
176
177 git checkout -b rerere A &&
178 mkdir foo &&
179 name="れれれ" &&
180 >foo/$name &&
181 git add foo &&
182 git commit -m "Adding a file" &&
183 git filter-branch --tree-filter "rm -fr foo" &&
c8a08692 184 test_must_fail git ls-files --error-unmatch "foo/$name" &&
1fe32cb9
JH
185 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
186
187'
188
5b044ac3
JH
189test_expect_success 'Subdirectory filter with disappearing trees' '
190 git reset --hard &&
191 git checkout master &&
192
193 mkdir foo &&
194 touch foo/bar &&
195 git add foo &&
196 test_tick &&
197 git commit -m "Adding foo" &&
198
199 git rm -r foo &&
200 test_tick &&
201 git commit -m "Removing foo" &&
202
203 mkdir foo &&
204 touch foo/bar &&
205 git add foo &&
206 test_tick &&
207 git commit -m "Re-adding foo" &&
208
209 git filter-branch -f --subdirectory-filter foo &&
210 test $(git rev-list master | wc -l) = 3
211'
212
1bf6551e
BC
213test_expect_success 'Tag name filtering retains tag message' '
214 git tag -m atag T &&
215 git cat-file tag T > expect &&
216 git filter-branch -f --tag-name-filter cat &&
217 git cat-file tag T > actual &&
3af82863 218 test_cmp expect actual
1bf6551e
BC
219'
220
221faux_gpg_tag='object XXXXXX
222type commit
223tag S
224tagger T A Gger <tagger@example.com> 1206026339 -0500
225
226This is a faux gpg signed tag.
227-----BEGIN PGP SIGNATURE-----
228Version: FauxGPG v0.0.0 (FAUX/Linux)
229
230gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
231acmwXaWET20H0GeAGP+7vow=
232=agpO
233-----END PGP SIGNATURE-----
234'
235test_expect_success 'Tag name filtering strips gpg signature' '
236 sha1=$(git rev-parse HEAD) &&
237 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
238 git update-ref "refs/tags/S" "$sha1t" &&
239 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
240 git filter-branch -f --tag-name-filter cat &&
241 git cat-file tag S > actual &&
3af82863 242 test_cmp expect actual
1bf6551e
BC
243'
244
6f6826c5 245test_done