Add remove_path: a function to remove as much as possible of a path
[git/spearce.git] / t / t7003-filter-branch.sh
blob182aea4267c33fd0f737c9028cedcb635e3348e2
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
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
32 H=$(git rev-parse H)
34 test_expect_success 'rewrite identically' '
35 git filter-branch branch
37 test_expect_success 'result is really identical' '
38 test $H = $(git rev-parse HEAD)
41 test_expect_success 'rewrite bare repository identically' '
42 (git config core.bare true && cd .git && git filter-branch branch)
44 git config core.bare false
45 test_expect_success 'result is really identical' '
46 test $H = $(git rev-parse HEAD)
49 test_expect_success 'rewrite, renaming a specific file' '
50 git filter-branch -f --tree-filter "mv d doh || :" HEAD
53 test_expect_success 'test that the file was renamed' '
54 test d = "$(git show HEAD:doh --)" &&
55 ! test -f d &&
56 test -f doh &&
57 test d = "$(cat doh)"
60 test_expect_success 'rewrite, renaming a specific directory' '
61 git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
64 test_expect_success 'test that the directory was renamed' '
65 test dir/d = "$(git show HEAD:diroh/d --)" &&
66 ! test -d dir &&
67 test -d diroh &&
68 ! test -d diroh/dir &&
69 test -f diroh/d &&
70 test dir/d = "$(cat diroh/d)"
73 git tag oldD HEAD~4
74 test_expect_success 'rewrite one branch, keeping a side branch' '
75 git branch modD oldD &&
76 git filter-branch -f --tree-filter "mv b boh || :" D..modD
79 test_expect_success 'common ancestor is still common (unchanged)' '
80 test "$(git merge-base modD D)" = "$(git rev-parse B)"
83 test_expect_success 'filter subdirectory only' '
84 mkdir subdir &&
85 touch subdir/new &&
86 git add subdir/new &&
87 test_tick &&
88 git commit -m "subdir" &&
89 echo H > a &&
90 test_tick &&
91 git commit -m "not subdir" a &&
92 echo A > subdir/new &&
93 test_tick &&
94 git commit -m "again subdir" subdir/new &&
95 git rm a &&
96 test_tick &&
97 git commit -m "again not subdir" &&
98 git branch sub &&
99 git filter-branch -f --subdirectory-filter subdir refs/heads/sub
102 test_expect_success 'subdirectory filter result looks okay' '
103 test 2 = $(git rev-list sub | wc -l) &&
104 git show sub:new &&
105 test_must_fail git show sub:subdir
108 test_expect_success 'more setup' '
109 git checkout master &&
110 mkdir subdir &&
111 echo A > subdir/new &&
112 git add subdir/new &&
113 test_tick &&
114 git commit -m "subdir on master" subdir/new &&
115 git rm a &&
116 test_tick &&
117 git commit -m "again subdir on master" &&
118 git merge branch
121 test_expect_success 'use index-filter to move into a subdirectory' '
122 git branch directorymoved &&
123 git filter-branch -f --index-filter \
124 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
125 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
126 git update-index --index-info &&
127 mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
128 test -z "$(git diff HEAD directorymoved:newsubdir)"'
130 test_expect_success 'stops when msg filter fails' '
131 old=$(git rev-parse HEAD) &&
132 test_must_fail git filter-branch -f --msg-filter false HEAD &&
133 test $old = $(git rev-parse HEAD) &&
134 rm -rf .git-rewrite
137 test_expect_success 'author information is preserved' '
138 : > i &&
139 git add i &&
140 test_tick &&
141 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
142 git branch preserved-author &&
143 git filter-branch -f --msg-filter "cat; \
144 test \$GIT_COMMIT != $(git rev-parse master) || \
145 echo Hallo" \
146 preserved-author &&
147 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
150 test_expect_success "remove a certain author's commits" '
151 echo i > i &&
152 test_tick &&
153 git commit -m i i &&
154 git branch removed-author &&
155 git filter-branch -f --commit-filter "\
156 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
157 then\
158 skip_commit \"\$@\";
159 else\
160 git commit-tree \"\$@\";\
161 fi" removed-author &&
162 cnt1=$(git rev-list master | wc -l) &&
163 cnt2=$(git rev-list removed-author | wc -l) &&
164 test $cnt1 -eq $(($cnt2 + 1)) &&
165 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
168 test_expect_success 'barf on invalid name' '
169 test_must_fail git filter-branch -f master xy-problem &&
170 test_must_fail git filter-branch -f HEAD^
173 test_expect_success '"map" works in commit filter' '
174 git filter-branch -f --commit-filter "\
175 parent=\$(git rev-parse \$GIT_COMMIT^) &&
176 mapped=\$(map \$parent) &&
177 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
178 test \$mapped = \$actual &&
179 git commit-tree \"\$@\";" master~2..master &&
180 git rev-parse --verify master
183 test_expect_success 'Name needing quotes' '
185 git checkout -b rerere A &&
186 mkdir foo &&
187 name="れれれ" &&
188 >foo/$name &&
189 git add foo &&
190 git commit -m "Adding a file" &&
191 git filter-branch --tree-filter "rm -fr foo" &&
192 test_must_fail git ls-files --error-unmatch "foo/$name" &&
193 test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
197 test_expect_success 'Subdirectory filter with disappearing trees' '
198 git reset --hard &&
199 git checkout master &&
201 mkdir foo &&
202 touch foo/bar &&
203 git add foo &&
204 test_tick &&
205 git commit -m "Adding foo" &&
207 git rm -r foo &&
208 test_tick &&
209 git commit -m "Removing foo" &&
211 mkdir foo &&
212 touch foo/bar &&
213 git add foo &&
214 test_tick &&
215 git commit -m "Re-adding foo" &&
217 git filter-branch -f --subdirectory-filter foo &&
218 test $(git rev-list master | wc -l) = 3
221 test_expect_success 'Tag name filtering retains tag message' '
222 git tag -m atag T &&
223 git cat-file tag T > expect &&
224 git filter-branch -f --tag-name-filter cat &&
225 git cat-file tag T > actual &&
226 test_cmp expect actual
229 faux_gpg_tag='object XXXXXX
230 type commit
231 tag S
232 tagger T A Gger <tagger@example.com> 1206026339 -0500
234 This is a faux gpg signed tag.
235 -----BEGIN PGP SIGNATURE-----
236 Version: FauxGPG v0.0.0 (FAUX/Linux)
238 gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
239 acmwXaWET20H0GeAGP+7vow=
240 =agpO
241 -----END PGP SIGNATURE-----
243 test_expect_success 'Tag name filtering strips gpg signature' '
244 sha1=$(git rev-parse HEAD) &&
245 sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
246 git update-ref "refs/tags/S" "$sha1t" &&
247 echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
248 git filter-branch -f --tag-name-filter cat &&
249 git cat-file tag S > actual &&
250 test_cmp expect actual
253 test_done