Code

56b5eccdb4a0563b4df22e2af679b08b66ef6ae1
[git.git] / t / t7003-filter-branch.sh
1 #!/bin/sh
3 test_description='git filter-branch'
4 . ./test-lib.sh
6 make_commit () {
7         lower=$(echo $1 | tr '[A-Z]' '[a-z]')
8         echo $lower > $lower
9         git add $lower
10         test_tick
11         git commit -m $1
12         git tag $1
13 }
15 test_expect_success 'setup' '
16         make_commit A
17         make_commit B
18         git checkout -b branch B
19         make_commit D
20         mkdir dir
21         make_commit dir/D
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 '
32 H=$(git rev-parse H)
34 test_expect_success 'rewrite identically' '
35         git filter-branch branch
36 '
37 test_expect_success 'result is really identical' '
38         test $H = $(git rev-parse HEAD)
39 '
41 test_expect_success 'rewrite bare repository identically' '
42         (git config core.bare true && cd .git &&
43          git filter-branch branch > filter-output 2>&1 &&
44         ! fgrep fatal filter-output)
45 '
46 git config core.bare false
47 test_expect_success 'result is really identical' '
48         test $H = $(git rev-parse HEAD)
49 '
51 test_expect_success 'Fail if commit filter fails' '
52         test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
53 '
55 test_expect_success 'rewrite, renaming a specific file' '
56         git filter-branch -f --tree-filter "mv d doh || :" HEAD
57 '
59 test_expect_success 'test that the file was renamed' '
60         test d = "$(git show HEAD:doh --)" &&
61         ! test -f d &&
62         test -f doh &&
63         test d = "$(cat doh)"
64 '
66 test_expect_success 'rewrite, renaming a specific directory' '
67         git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
68 '
70 test_expect_success 'test that the directory was renamed' '
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)"
77 '
79 git tag oldD HEAD~4
80 test_expect_success 'rewrite one branch, keeping a side branch' '
81         git branch modD oldD &&
82         git filter-branch -f --tree-filter "mv b boh || :" D..modD
83 '
85 test_expect_success 'common ancestor is still common (unchanged)' '
86         test "$(git merge-base modD D)" = "$(git rev-parse B)"
87 '
89 test_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" &&
104         git branch sub &&
105         git branch sub-earlier HEAD~2 &&
106         git filter-branch -f --subdirectory-filter subdir \
107                 refs/heads/sub refs/heads/sub-earlier
110 test_expect_success 'subdirectory filter result looks okay' '
111         test 2 = $(git rev-list sub | wc -l) &&
112         git show sub:new &&
113         test_must_fail git show sub:subdir &&
114         git show sub-earlier:new &&
115         test_must_fail git show sub-earlier:subdir
118 test_expect_success 'more setup' '
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" &&
128         git merge branch
131 test_expect_success 'use index-filter to move into a subdirectory' '
132         git branch directorymoved &&
133         git filter-branch -f --index-filter \
134                  "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
135                   GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
136                         git update-index --index-info &&
137                   mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
138         test -z "$(git diff HEAD directorymoved:newsubdir)"'
140 test_expect_success 'stops when msg filter fails' '
141         old=$(git rev-parse HEAD) &&
142         test_must_fail git filter-branch -f --msg-filter false HEAD &&
143         test $old = $(git rev-parse HEAD) &&
144         rm -rf .git-rewrite
147 test_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 &&
152         git branch preserved-author &&
153         git filter-branch -f --msg-filter "cat; \
154                         test \$GIT_COMMIT != $(git rev-parse master) || \
155                         echo Hallo" \
156                 preserved-author &&
157         test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
160 test_expect_success "remove a certain author's commits" '
161         echo i > i &&
162         test_tick &&
163         git commit -m i i &&
164         git branch removed-author &&
165         git filter-branch -f --commit-filter "\
166                 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
167                 then\
168                         skip_commit \"\$@\";
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)
178 test_expect_success 'barf on invalid name' '
179         test_must_fail git filter-branch -f master xy-problem &&
180         test_must_fail git filter-branch -f HEAD^
183 test_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
193 test_expect_success 'Name needing quotes' '
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" &&
202         test_must_fail git ls-files --error-unmatch "foo/$name" &&
203         test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
207 test_expect_success 'Subdirectory filter with disappearing trees' '
208         git reset --hard &&
209         git checkout master &&
211         mkdir foo &&
212         touch foo/bar &&
213         git add foo &&
214         test_tick &&
215         git commit -m "Adding foo" &&
217         git rm -r foo &&
218         test_tick &&
219         git commit -m "Removing foo" &&
221         mkdir foo &&
222         touch foo/bar &&
223         git add foo &&
224         test_tick &&
225         git commit -m "Re-adding foo" &&
227         git filter-branch -f --subdirectory-filter foo &&
228         test $(git rev-list master | wc -l) = 3
231 test_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 &&
236         test_cmp expect actual
239 faux_gpg_tag='object XXXXXX
240 type commit
241 tag S
242 tagger T A Gger <tagger@example.com> 1206026339 -0500
244 This is a faux gpg signed tag.
245 -----BEGIN PGP SIGNATURE-----
246 Version: FauxGPG v0.0.0 (FAUX/Linux)
248 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
249 acmwXaWET20H0GeAGP+7vow=
250 =agpO
251 -----END PGP SIGNATURE-----
253 test_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 &&
260         test_cmp expect actual
263 test_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
271 test_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
279 test_done