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