]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7003-filter-branch.sh
disallow providing multiple upstream branches to rebase, pull --rebase
[thirdparty/git.git] / t / t7003-filter-branch.sh
CommitLineData
6f6826c5
JS
1#!/bin/sh
2
d592b315 3test_description='git filter-branch'
6f6826c5
JS
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' '
d592b315 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
a4661b01 41test_expect_success 'rewrite bare repository identically' '
9273b562
EK
42 (git config core.bare true && cd .git &&
43 git filter-branch branch > filter-output 2>&1 &&
44 ! fgrep fatal filter-output)
a4661b01
PB
45'
46git config core.bare false
47test_expect_success 'result is really identical' '
48 test $H = $(git rev-parse HEAD)
49'
50
0ea29cce
EK
51test_expect_success 'Fail if commit filter fails' '
52 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
53'
54
6f6826c5 55test_expect_success 'rewrite, renaming a specific file' '
d592b315 56 git filter-branch -f --tree-filter "mv d doh || :" HEAD
6f6826c5
JS
57'
58
59test_expect_success 'test that the file was renamed' '
90356287
JS
60 test d = "$(git show HEAD:doh --)" &&
61 ! test -f d &&
46eb449c 62 test -f doh &&
90356287
JS
63 test d = "$(cat doh)"
64'
65
66test_expect_success 'rewrite, renaming a specific directory' '
d592b315 67 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
90356287
JS
68'
69
6a589fda 70test_expect_success 'test that the directory was renamed' '
90356287
JS
71 test dir/d = "$(git show HEAD:diroh/d --)" &&
72 ! test -d dir &&
73 test -d diroh &&
74 ! test -d diroh/dir &&
75 test -f diroh/d &&
76 test dir/d = "$(cat diroh/d)"
6f6826c5
JS
77'
78
dfd05e38 79git tag oldD HEAD~4
98409060 80test_expect_success 'rewrite one branch, keeping a side branch' '
dfd05e38 81 git branch modD oldD &&
d592b315 82 git filter-branch -f --tree-filter "mv b boh || :" D..modD
98409060
JS
83'
84
85test_expect_success 'common ancestor is still common (unchanged)' '
5be60078 86 test "$(git merge-base modD D)" = "$(git rev-parse B)"
98409060
JS
87'
88
685ef546
JS
89test_expect_success 'filter subdirectory only' '
90 mkdir subdir &&
91 touch subdir/new &&
92 git add subdir/new &&
93 test_tick &&
94 git commit -m "subdir" &&
95 echo H > a &&
96 test_tick &&
97 git commit -m "not subdir" a &&
98 echo A > subdir/new &&
99 test_tick &&
100 git commit -m "again subdir" subdir/new &&
101 git rm a &&
102 test_tick &&
103 git commit -m "again not subdir" &&
dfd05e38 104 git branch sub &&
6e84b712 105 git branch sub-earlier HEAD~2 &&
9b8ae93a 106 git filter-branch -f --subdirectory-filter subdir \
6e84b712 107 refs/heads/sub refs/heads/sub-earlier
685ef546
JS
108'
109
110test_expect_success 'subdirectory filter result looks okay' '
5be60078 111 test 2 = $(git rev-list sub | wc -l) &&
685ef546 112 git show sub:new &&
6e84b712
TR
113 test_must_fail git show sub:subdir &&
114 git show sub-earlier:new &&
115 test_must_fail git show sub-earlier:subdir
685ef546
JS
116'
117
a17171b4 118test_expect_success 'more setup' '
cfabd6ee
JS
119 git checkout master &&
120 mkdir subdir &&
121 echo A > subdir/new &&
122 git add subdir/new &&
123 test_tick &&
124 git commit -m "subdir on master" subdir/new &&
125 git rm a &&
126 test_tick &&
127 git commit -m "again subdir on master" &&
a17171b4 128 git merge branch
cfabd6ee
JS
129'
130
55f22ff2 131test_expect_success 'use index-filter to move into a subdirectory' '
dfd05e38 132 git branch directorymoved &&
d592b315 133 git filter-branch -f --index-filter \
5be60078 134 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
55f22ff2 135 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
5be60078 136 git update-index --index-info &&
f69e836f 137 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
55f22ff2
JS
138 test -z "$(git diff HEAD directorymoved:newsubdir)"'
139
8c1ce0f4 140test_expect_success 'stops when msg filter fails' '
dfd05e38 141 old=$(git rev-parse HEAD) &&
d592b315 142 test_must_fail git filter-branch -f --msg-filter false HEAD &&
dfd05e38
JS
143 test $old = $(git rev-parse HEAD) &&
144 rm -rf .git-rewrite
8c1ce0f4
JS
145'
146
f6b78c6e
JS
147test_expect_success 'author information is preserved' '
148 : > i &&
149 git add i &&
150 test_tick &&
151 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
dfd05e38 152 git branch preserved-author &&
d592b315 153 git filter-branch -f --msg-filter "cat; \
8c1ce0f4 154 test \$GIT_COMMIT != $(git rev-parse master) || \
f6b78c6e
JS
155 echo Hallo" \
156 preserved-author &&
157 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
158'
159
160test_expect_success "remove a certain author's commits" '
161 echo i > i &&
162 test_tick &&
163 git commit -m i i &&
dfd05e38 164 git branch removed-author &&
d592b315 165 git filter-branch -f --commit-filter "\
f6b78c6e
JS
166 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
167 then\
f95eef15 168 skip_commit \"\$@\";
f6b78c6e
JS
169 else\
170 git commit-tree \"\$@\";\
171 fi" removed-author &&
172 cnt1=$(git rev-list master | wc -l) &&
173 cnt2=$(git rev-list removed-author | wc -l) &&
174 test $cnt1 -eq $(($cnt2 + 1)) &&
175 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
176'
177
dfd05e38 178test_expect_success 'barf on invalid name' '
c8a08692
BC
179 test_must_fail git filter-branch -f master xy-problem &&
180 test_must_fail git filter-branch -f HEAD^
dfd05e38
JS
181'
182
7e0f1704
JS
183test_expect_success '"map" works in commit filter' '
184 git filter-branch -f --commit-filter "\
185 parent=\$(git rev-parse \$GIT_COMMIT^) &&
186 mapped=\$(map \$parent) &&
187 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
188 test \$mapped = \$actual &&
189 git commit-tree \"\$@\";" master~2..master &&
190 git rev-parse --verify master
191'
192
1fe32cb9
JH
193test_expect_success 'Name needing quotes' '
194
195 git checkout -b rerere A &&
196 mkdir foo &&
197 name="れれれ" &&
198 >foo/$name &&
199 git add foo &&
200 git commit -m "Adding a file" &&
201 git filter-branch --tree-filter "rm -fr foo" &&
c8a08692 202 test_must_fail git ls-files --error-unmatch "foo/$name" &&
1fe32cb9
JH
203 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
204
205'
206
5b044ac3
JH
207test_expect_success 'Subdirectory filter with disappearing trees' '
208 git reset --hard &&
209 git checkout master &&
210
211 mkdir foo &&
212 touch foo/bar &&
213 git add foo &&
214 test_tick &&
215 git commit -m "Adding foo" &&
216
217 git rm -r foo &&
218 test_tick &&
219 git commit -m "Removing foo" &&
220
221 mkdir foo &&
222 touch foo/bar &&
223 git add foo &&
224 test_tick &&
225 git commit -m "Re-adding foo" &&
226
227 git filter-branch -f --subdirectory-filter foo &&
228 test $(git rev-list master | wc -l) = 3
229'
230
1bf6551e
BC
231test_expect_success 'Tag name filtering retains tag message' '
232 git tag -m atag T &&
233 git cat-file tag T > expect &&
234 git filter-branch -f --tag-name-filter cat &&
235 git cat-file tag T > actual &&
3af82863 236 test_cmp expect actual
1bf6551e
BC
237'
238
239faux_gpg_tag='object XXXXXX
240type commit
241tag S
242tagger T A Gger <tagger@example.com> 1206026339 -0500
243
244This is a faux gpg signed tag.
245-----BEGIN PGP SIGNATURE-----
246Version: FauxGPG v0.0.0 (FAUX/Linux)
247
248gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
249acmwXaWET20H0GeAGP+7vow=
250=agpO
251-----END PGP SIGNATURE-----
252'
253test_expect_success 'Tag name filtering strips gpg signature' '
254 sha1=$(git rev-parse HEAD) &&
255 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
256 git update-ref "refs/tags/S" "$sha1t" &&
257 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
258 git filter-branch -f --tag-name-filter cat &&
259 git cat-file tag S > actual &&
3af82863 260 test_cmp expect actual
1bf6551e
BC
261'
262
a9da1663
JS
263test_expect_success 'Tag name filtering allows slashes in tag names' '
264 git tag -m tag-with-slash X/1 &&
265 git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
266 git filter-branch -f --tag-name-filter "echo X/2" &&
267 git cat-file tag X/2 > actual &&
268 test_cmp expect actual
269'
270
d3240d93
PH
271test_expect_success 'Prune empty commits' '
272 git rev-list HEAD > expect &&
273 make_commit to_remove &&
274 git filter-branch -f --index-filter "git update-index --remove to_remove" --prune-empty HEAD &&
275 git rev-list HEAD > actual &&
276 test_cmp expect actual
277'
278
6f6826c5 279test_done