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) &&
45test -f doh &&
46test d = $(cat doh)
47'
4849
git tag oldD HEAD~4
50test_expect_success 'rewrite one branch, keeping a side branch' '
51git branch modD oldD &&
52git-filter-branch -f --tree-filter "mv b boh || :" D..modD
53'
5455
test_expect_success 'common ancestor is still common (unchanged)' '
56test "$(git merge-base modD D)" = "$(git rev-parse B)"
57'
5859
test_expect_success 'filter subdirectory only' '
60mkdir subdir &&
61touch subdir/new &&
62git add subdir/new &&
63test_tick &&
64git commit -m "subdir" &&
65echo H > a &&
66test_tick &&
67git commit -m "not subdir" a &&
68echo A > subdir/new &&
69test_tick &&
70git commit -m "again subdir" subdir/new &&
71git rm a &&
72test_tick &&
73git commit -m "again not subdir" &&
74git branch sub &&
75git-filter-branch -f --subdirectory-filter subdir refs/heads/sub
76'
7778
test_expect_success 'subdirectory filter result looks okay' '
79test 2 = $(git rev-list sub | wc -l) &&
80git show sub:new &&
81! git show sub:subdir
82'
8384
test_expect_success 'setup and filter history that requires --full-history' '
85git checkout master &&
86mkdir subdir &&
87echo A > subdir/new &&
88git add subdir/new &&
89test_tick &&
90git commit -m "subdir on master" subdir/new &&
91git rm a &&
92test_tick &&
93git commit -m "again subdir on master" &&
94git merge branch &&
95git branch sub-master &&
96git-filter-branch -f --subdirectory-filter subdir sub-master
97'
9899
test_expect_success 'subdirectory filter result looks okay' '
100test 3 = $(git rev-list -1 --parents sub-master | wc -w) &&
101git show sub-master^:new &&
102git show sub-master^2:new &&
103! git show sub:subdir
104'
105106
test_expect_success 'use index-filter to move into a subdirectory' '
107git branch directorymoved &&
108git-filter-branch -f --index-filter \
109"git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
110GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
111git update-index --index-info &&
112mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" directorymoved &&
113test -z "$(git diff HEAD directorymoved:newsubdir)"'
114115
test_expect_success 'stops when msg filter fails' '
116old=$(git rev-parse HEAD) &&
117! git-filter-branch -f --msg-filter false HEAD &&
118test $old = $(git rev-parse HEAD) &&
119rm -rf .git-rewrite
120'
121122
test_expect_success 'author information is preserved' '
123: > i &&
124git add i &&
125test_tick &&
126GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
127git branch preserved-author &&
128git-filter-branch -f --msg-filter "cat; \
129test \$GIT_COMMIT != $(git rev-parse master) || \
130echo Hallo" \
131preserved-author &&
132test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
133'
134135
test_expect_success "remove a certain author's commits" '
136echo i > i &&
137test_tick &&
138git commit -m i i &&
139git branch removed-author &&
140git-filter-branch -f --commit-filter "\
141if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
142then\
143skip_commit \"\$@\";
144else\
145git commit-tree \"\$@\";\
146fi" removed-author &&
147cnt1=$(git rev-list master | wc -l) &&
148cnt2=$(git rev-list removed-author | wc -l) &&
149test $cnt1 -eq $(($cnt2 + 1)) &&
150test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
151'
152153
test_expect_success 'barf on invalid name' '
154! git filter-branch -f master xy-problem &&
155! git filter-branch -f HEAD^
156'
157158
test_expect_success '"map" works in commit filter' '
159git filter-branch -f --commit-filter "\
160parent=\$(git rev-parse \$GIT_COMMIT^) &&
161mapped=\$(map \$parent) &&
162actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
163test \$mapped = \$actual &&
164git commit-tree \"\$@\";" master~2..master &&
165git rev-parse --verify master
166'
167168
test_done