]>
Commit | Line | Data |
---|---|---|
6f6826c5 JS |
1 | #!/bin/sh |
2 | ||
d592b315 | 3 | test_description='git filter-branch' |
6f6826c5 JS |
4 | . ./test-lib.sh |
5 | ||
6 | make_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 | ||
15 | test_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 | 32 | H=$(git rev-parse H) |
6f6826c5 JS |
33 | |
34 | test_expect_success 'rewrite identically' ' | |
d592b315 | 35 | git filter-branch branch |
6f6826c5 | 36 | ' |
6f6826c5 | 37 | test_expect_success 'result is really identical' ' |
dfd05e38 | 38 | test $H = $(git rev-parse HEAD) |
6f6826c5 JS |
39 | ' |
40 | ||
a4661b01 | 41 | test_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 | ' |
46 | git config core.bare false | |
47 | test_expect_success 'result is really identical' ' | |
48 | test $H = $(git rev-parse HEAD) | |
49 | ' | |
50 | ||
88e38808 LN |
51 | TRASHDIR=$(pwd) |
52 | test_expect_success 'correct GIT_DIR while using -d' ' | |
53 | mkdir drepo && | |
54 | ( cd drepo && | |
55 | git init && | |
56 | test_commit drepo && | |
57 | git filter-branch -d "$TRASHDIR/dfoo" \ | |
58 | --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \ | |
59 | ) && | |
60 | grep drepo "$TRASHDIR/backup-refs" | |
61 | ' | |
62 | ||
0ea29cce EK |
63 | test_expect_success 'Fail if commit filter fails' ' |
64 | test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD | |
65 | ' | |
66 | ||
6f6826c5 | 67 | test_expect_success 'rewrite, renaming a specific file' ' |
d592b315 | 68 | git filter-branch -f --tree-filter "mv d doh || :" HEAD |
6f6826c5 JS |
69 | ' |
70 | ||
71 | test_expect_success 'test that the file was renamed' ' | |
90356287 JS |
72 | test d = "$(git show HEAD:doh --)" && |
73 | ! test -f d && | |
46eb449c | 74 | test -f doh && |
90356287 JS |
75 | test d = "$(cat doh)" |
76 | ' | |
77 | ||
78 | test_expect_success 'rewrite, renaming a specific directory' ' | |
d592b315 | 79 | git filter-branch -f --tree-filter "mv dir diroh || :" HEAD |
90356287 JS |
80 | ' |
81 | ||
6a589fda | 82 | test_expect_success 'test that the directory was renamed' ' |
90356287 JS |
83 | test dir/d = "$(git show HEAD:diroh/d --)" && |
84 | ! test -d dir && | |
85 | test -d diroh && | |
86 | ! test -d diroh/dir && | |
87 | test -f diroh/d && | |
88 | test dir/d = "$(cat diroh/d)" | |
6f6826c5 JS |
89 | ' |
90 | ||
dfd05e38 | 91 | git tag oldD HEAD~4 |
98409060 | 92 | test_expect_success 'rewrite one branch, keeping a side branch' ' |
dfd05e38 | 93 | git branch modD oldD && |
d592b315 | 94 | git filter-branch -f --tree-filter "mv b boh || :" D..modD |
98409060 JS |
95 | ' |
96 | ||
97 | test_expect_success 'common ancestor is still common (unchanged)' ' | |
5be60078 | 98 | test "$(git merge-base modD D)" = "$(git rev-parse B)" |
98409060 JS |
99 | ' |
100 | ||
685ef546 JS |
101 | test_expect_success 'filter subdirectory only' ' |
102 | mkdir subdir && | |
103 | touch subdir/new && | |
104 | git add subdir/new && | |
105 | test_tick && | |
106 | git commit -m "subdir" && | |
107 | echo H > a && | |
108 | test_tick && | |
109 | git commit -m "not subdir" a && | |
110 | echo A > subdir/new && | |
111 | test_tick && | |
112 | git commit -m "again subdir" subdir/new && | |
113 | git rm a && | |
114 | test_tick && | |
115 | git commit -m "again not subdir" && | |
dfd05e38 | 116 | git branch sub && |
6e84b712 | 117 | git branch sub-earlier HEAD~2 && |
9b8ae93a | 118 | git filter-branch -f --subdirectory-filter subdir \ |
6e84b712 | 119 | refs/heads/sub refs/heads/sub-earlier |
685ef546 JS |
120 | ' |
121 | ||
122 | test_expect_success 'subdirectory filter result looks okay' ' | |
5be60078 | 123 | test 2 = $(git rev-list sub | wc -l) && |
685ef546 | 124 | git show sub:new && |
6e84b712 TR |
125 | test_must_fail git show sub:subdir && |
126 | git show sub-earlier:new && | |
127 | test_must_fail git show sub-earlier:subdir | |
685ef546 JS |
128 | ' |
129 | ||
a17171b4 | 130 | test_expect_success 'more setup' ' |
cfabd6ee JS |
131 | git checkout master && |
132 | mkdir subdir && | |
133 | echo A > subdir/new && | |
134 | git add subdir/new && | |
135 | test_tick && | |
136 | git commit -m "subdir on master" subdir/new && | |
137 | git rm a && | |
138 | test_tick && | |
139 | git commit -m "again subdir on master" && | |
a17171b4 | 140 | git merge branch |
cfabd6ee JS |
141 | ' |
142 | ||
55f22ff2 | 143 | test_expect_success 'use index-filter to move into a subdirectory' ' |
dfd05e38 | 144 | git branch directorymoved && |
d592b315 | 145 | git filter-branch -f --index-filter \ |
5be60078 | 146 | "git ls-files -s | sed \"s-\\t-&newsubdir/-\" | |
55f22ff2 | 147 | GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \ |
5be60078 | 148 | git update-index --index-info && |
f69e836f | 149 | mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved && |
55f22ff2 JS |
150 | test -z "$(git diff HEAD directorymoved:newsubdir)"' |
151 | ||
8c1ce0f4 | 152 | test_expect_success 'stops when msg filter fails' ' |
dfd05e38 | 153 | old=$(git rev-parse HEAD) && |
d592b315 | 154 | test_must_fail git filter-branch -f --msg-filter false HEAD && |
dfd05e38 JS |
155 | test $old = $(git rev-parse HEAD) && |
156 | rm -rf .git-rewrite | |
8c1ce0f4 JS |
157 | ' |
158 | ||
f6b78c6e JS |
159 | test_expect_success 'author information is preserved' ' |
160 | : > i && | |
161 | git add i && | |
162 | test_tick && | |
163 | GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips && | |
dfd05e38 | 164 | git branch preserved-author && |
d592b315 | 165 | git filter-branch -f --msg-filter "cat; \ |
8c1ce0f4 | 166 | test \$GIT_COMMIT != $(git rev-parse master) || \ |
f6b78c6e JS |
167 | echo Hallo" \ |
168 | preserved-author && | |
169 | test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l) | |
170 | ' | |
171 | ||
172 | test_expect_success "remove a certain author's commits" ' | |
173 | echo i > i && | |
174 | test_tick && | |
175 | git commit -m i i && | |
dfd05e38 | 176 | git branch removed-author && |
d592b315 | 177 | git filter-branch -f --commit-filter "\ |
f6b78c6e JS |
178 | if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\ |
179 | then\ | |
f95eef15 | 180 | skip_commit \"\$@\"; |
f6b78c6e JS |
181 | else\ |
182 | git commit-tree \"\$@\";\ | |
183 | fi" removed-author && | |
184 | cnt1=$(git rev-list master | wc -l) && | |
185 | cnt2=$(git rev-list removed-author | wc -l) && | |
186 | test $cnt1 -eq $(($cnt2 + 1)) && | |
187 | test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l) | |
188 | ' | |
189 | ||
dfd05e38 | 190 | test_expect_success 'barf on invalid name' ' |
c8a08692 BC |
191 | test_must_fail git filter-branch -f master xy-problem && |
192 | test_must_fail git filter-branch -f HEAD^ | |
dfd05e38 JS |
193 | ' |
194 | ||
7e0f1704 JS |
195 | test_expect_success '"map" works in commit filter' ' |
196 | git filter-branch -f --commit-filter "\ | |
197 | parent=\$(git rev-parse \$GIT_COMMIT^) && | |
198 | mapped=\$(map \$parent) && | |
199 | actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") && | |
200 | test \$mapped = \$actual && | |
201 | git commit-tree \"\$@\";" master~2..master && | |
202 | git rev-parse --verify master | |
203 | ' | |
204 | ||
1fe32cb9 JH |
205 | test_expect_success 'Name needing quotes' ' |
206 | ||
207 | git checkout -b rerere A && | |
208 | mkdir foo && | |
209 | name="れれれ" && | |
210 | >foo/$name && | |
211 | git add foo && | |
212 | git commit -m "Adding a file" && | |
213 | git filter-branch --tree-filter "rm -fr foo" && | |
c8a08692 | 214 | test_must_fail git ls-files --error-unmatch "foo/$name" && |
1fe32cb9 JH |
215 | test $(git rev-parse --verify rerere) != $(git rev-parse --verify A) |
216 | ||
217 | ' | |
218 | ||
5b044ac3 JH |
219 | test_expect_success 'Subdirectory filter with disappearing trees' ' |
220 | git reset --hard && | |
221 | git checkout master && | |
222 | ||
223 | mkdir foo && | |
224 | touch foo/bar && | |
225 | git add foo && | |
226 | test_tick && | |
227 | git commit -m "Adding foo" && | |
228 | ||
229 | git rm -r foo && | |
230 | test_tick && | |
231 | git commit -m "Removing foo" && | |
232 | ||
233 | mkdir foo && | |
234 | touch foo/bar && | |
235 | git add foo && | |
236 | test_tick && | |
237 | git commit -m "Re-adding foo" && | |
238 | ||
239 | git filter-branch -f --subdirectory-filter foo && | |
240 | test $(git rev-list master | wc -l) = 3 | |
241 | ' | |
242 | ||
1bf6551e BC |
243 | test_expect_success 'Tag name filtering retains tag message' ' |
244 | git tag -m atag T && | |
245 | git cat-file tag T > expect && | |
246 | git filter-branch -f --tag-name-filter cat && | |
247 | git cat-file tag T > actual && | |
3af82863 | 248 | test_cmp expect actual |
1bf6551e BC |
249 | ' |
250 | ||
251 | faux_gpg_tag='object XXXXXX | |
252 | type commit | |
253 | tag S | |
254 | tagger T A Gger <tagger@example.com> 1206026339 -0500 | |
255 | ||
256 | This is a faux gpg signed tag. | |
257 | -----BEGIN PGP SIGNATURE----- | |
258 | Version: FauxGPG v0.0.0 (FAUX/Linux) | |
259 | ||
260 | gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ | |
261 | acmwXaWET20H0GeAGP+7vow= | |
262 | =agpO | |
263 | -----END PGP SIGNATURE----- | |
264 | ' | |
265 | test_expect_success 'Tag name filtering strips gpg signature' ' | |
266 | sha1=$(git rev-parse HEAD) && | |
267 | sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) && | |
268 | git update-ref "refs/tags/S" "$sha1t" && | |
269 | echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect && | |
270 | git filter-branch -f --tag-name-filter cat && | |
271 | git cat-file tag S > actual && | |
3af82863 | 272 | test_cmp expect actual |
1bf6551e BC |
273 | ' |
274 | ||
a9da1663 JS |
275 | test_expect_success 'Tag name filtering allows slashes in tag names' ' |
276 | git tag -m tag-with-slash X/1 && | |
277 | git cat-file tag X/1 | sed -e s,X/1,X/2, > expect && | |
278 | git filter-branch -f --tag-name-filter "echo X/2" && | |
279 | git cat-file tag X/2 > actual && | |
280 | test_cmp expect actual | |
281 | ' | |
282 | ||
d3240d93 PH |
283 | test_expect_success 'Prune empty commits' ' |
284 | git rev-list HEAD > expect && | |
285 | make_commit to_remove && | |
286 | git filter-branch -f --index-filter "git update-index --remove to_remove" --prune-empty HEAD && | |
287 | git rev-list HEAD > actual && | |
288 | test_cmp expect actual | |
289 | ' | |
290 | ||
f2f3a6b8 TR |
291 | test_expect_success '--remap-to-ancestor with filename filters' ' |
292 | git checkout master && | |
293 | git reset --hard A && | |
294 | test_commit add-foo foo 1 && | |
295 | git branch moved-foo && | |
296 | test_commit add-bar bar a && | |
297 | git branch invariant && | |
298 | orig_invariant=$(git rev-parse invariant) && | |
299 | git branch moved-bar && | |
300 | test_commit change-foo foo 2 && | |
301 | git filter-branch -f --remap-to-ancestor \ | |
302 | moved-foo moved-bar A..master \ | |
303 | -- -- foo && | |
304 | test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) && | |
305 | test $(git rev-parse moved-foo) = $(git rev-parse master^) && | |
306 | test $orig_invariant = $(git rev-parse invariant) | |
307 | ' | |
308 | ||
1f7d57ff MS |
309 | test_expect_success 'setup submodule' ' |
310 | rm -fr ?* .git && | |
311 | git init && | |
312 | test_commit file && | |
313 | mkdir submod && | |
314 | submodurl="$PWD/submod" && | |
315 | ( cd submod && | |
316 | git init && | |
317 | test_commit file-in-submod ) && | |
318 | git submodule add "$submodurl" && | |
319 | git commit -m "added submodule" && | |
320 | test_commit add-file && | |
321 | ( cd submod && test_commit add-in-submodule ) && | |
322 | git add submod && | |
323 | git commit -m "changed submodule" && | |
324 | git branch original HEAD | |
325 | ' | |
326 | ||
327 | orig_head=`git show-ref --hash --head HEAD` | |
328 | ||
329 | test_expect_success 'rewrite submodule with another content' ' | |
330 | git filter-branch --tree-filter "test -d submod && { | |
331 | rm -rf submod && | |
332 | git rm -rf --quiet submod && | |
333 | mkdir submod && | |
334 | : > submod/file | |
335 | } || :" HEAD && | |
336 | test $orig_head != `git show-ref --hash --head HEAD` | |
337 | ' | |
338 | ||
339 | test_expect_success 'replace submodule revision' ' | |
340 | git reset --hard original && | |
341 | git filter-branch -f --tree-filter \ | |
342 | "if git ls-files --error-unmatch -- submod > /dev/null 2>&1 | |
343 | then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod | |
344 | fi" HEAD && | |
345 | test $orig_head != `git show-ref --hash --head HEAD` | |
346 | ' | |
347 | ||
6f6826c5 | 348 | test_done |