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