1#!/bin/sh
23
test_description='git-filter-branch'
4. ./test-lib.sh
56
make_commit () {
7lower=$(echo $1 | tr A-Z a-z)
8echo $lower > $lower
9git add $lower
10test_tick
11git commit -m $1
12git tag $1
13}
1415
test_expect_success 'setup' '
16make_commit A
17make_commit B
18git checkout -b branch B
19make_commit D
20make_commit E
21git checkout master
22make_commit C
23git checkout branch
24git merge C
25git tag F
26make_commit G
27make_commit H
28'
2930
H=$(git rev-parse H)
3132
test_expect_success 'rewrite identically' '
33git-filter-branch branch
34'
35test_expect_success 'result is really identical' '
36test $H = $(git rev-parse HEAD)
37'
3839
test_expect_success 'rewrite, renaming a specific file' '
40git-filter-branch -f --tree-filter "mv d doh || :" HEAD
41'
4243
test_expect_success 'test that the file was renamed' '
44test d = $(git show HEAD:doh)
45'
4647
git tag oldD HEAD~4
48test_expect_success 'rewrite one branch, keeping a side branch' '
49git branch modD oldD &&
50git-filter-branch -f --tree-filter "mv b boh || :" D..modD
51'
5253
test_expect_success 'common ancestor is still common (unchanged)' '
54test "$(git merge-base modD D)" = "$(git rev-parse B)"
55'
5657
test_expect_success 'filter subdirectory only' '
58mkdir subdir &&
59touch subdir/new &&
60git add subdir/new &&
61test_tick &&
62git commit -m "subdir" &&
63echo H > a &&
64test_tick &&
65git commit -m "not subdir" a &&
66echo A > subdir/new &&
67test_tick &&
68git commit -m "again subdir" subdir/new &&
69git rm a &&
70test_tick &&
71git commit -m "again not subdir" &&
72git branch sub &&
73git-filter-branch -f --subdirectory-filter subdir refs/heads/sub
74'
7576
test_expect_success 'subdirectory filter result looks okay' '
77test 2 = $(git rev-list sub | wc -l) &&
78git show sub:new &&
79! git show sub:subdir
80'
8182
test_expect_success 'setup and filter history that requires --full-history' '
83git checkout master &&
84mkdir subdir &&
85echo A > subdir/new &&
86git add subdir/new &&
87test_tick &&
88git commit -m "subdir on master" subdir/new &&
89git rm a &&
90test_tick &&
91git commit -m "again subdir on master" &&
92git merge branch &&
93git branch sub-master &&
94git-filter-branch -f --subdirectory-filter subdir sub-master
95'
9697
test_expect_success 'subdirectory filter result looks okay' '
98test 3 = $(git rev-list -1 --parents sub-master | wc -w) &&
99git show sub-master^:new &&
100git show sub-master^2:new &&
101! git show sub:subdir
102'
103104
test_expect_success 'use index-filter to move into a subdirectory' '
105git branch directorymoved &&
106git-filter-branch -f --index-filter \
107"git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
108GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
109git update-index --index-info &&
110mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" directorymoved &&
111test -z "$(git diff HEAD directorymoved:newsubdir)"'
112113
test_expect_success 'stops when msg filter fails' '
114old=$(git rev-parse HEAD) &&
115! git-filter-branch -f --msg-filter false &&
116test $old = $(git rev-parse HEAD) &&
117rm -rf .git-rewrite
118'
119120
test_expect_success 'author information is preserved' '
121: > i &&
122git add i &&
123test_tick &&
124GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
125git branch preserved-author &&
126git-filter-branch -f --msg-filter "cat; \
127test \$GIT_COMMIT != $(git rev-parse master) || \
128echo Hallo" \
129preserved-author &&
130test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
131'
132133
test_expect_success "remove a certain author's commits" '
134echo i > i &&
135test_tick &&
136git commit -m i i &&
137git branch removed-author &&
138git-filter-branch -f --commit-filter "\
139if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
140then\
141shift;\
142while [ -n \"\$1\" ];\
143do\
144shift;\
145echo \"\$1\";\
146shift;\
147done;\
148else\
149git commit-tree \"\$@\";\
150fi" removed-author &&
151cnt1=$(git rev-list master | wc -l) &&
152cnt2=$(git rev-list removed-author | wc -l) &&
153test $cnt1 -eq $(($cnt2 + 1)) &&
154test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
155'
156157
test_expect_success 'barf on invalid name' '
158! git filter-branch -f master xy-problem &&
159! git filter-branch -f HEAD^
160'
161162
test_expect_success '"map" works in commit filter' '
163git filter-branch -f --commit-filter "\
164parent=\$(git rev-parse \$GIT_COMMIT^) &&
165mapped=\$(map \$parent) &&
166actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
167test \$mapped = \$actual &&
168git commit-tree \"\$@\";" master~2..master &&
169git rev-parse --verify master
170'
171172
test_done