1#!/bin/sh
2
3test_description='git-filter-branch'
4. ./test-lib.sh
5
6make_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}
14
15test_expect_success 'setup' '
16 make_commit A
17 make_commit B
18 git checkout -b branch B
19 make_commit D
20 make_commit E
21 git checkout master
22 make_commit C
23 git checkout branch
24 git merge C
25 git tag F
26 make_commit G
27 make_commit H
28'
29
30H=$(git rev-parse H)
31
32test_expect_success 'rewrite identically' '
33 git-filter-branch H2
34'
35
36test_expect_success 'result is really identical' '
37 test $H = $(git rev-parse H2)
38'
39
40test_expect_success 'rewrite, renaming a specific file' '
41 git-filter-branch --tree-filter "mv d doh || :" H3
42'
43
44test_expect_success 'test that the file was renamed' '
45 test d = $(git show H3:doh)
46'
47
48git tag oldD H3~4
49test_expect_success 'rewrite one branch, keeping a side branch' '
50 git-filter-branch --tree-filter "mv b boh || :" modD D..oldD
51'
52
53test_expect_success 'common ancestor is still common (unchanged)' '
54 test "$(git merge-base modD D)" = "$(git rev-parse B)"
55'
56
57test_expect_success 'filter subdirectory only' '
58 mkdir subdir &&
59 touch subdir/new &&
60 git add subdir/new &&
61 test_tick &&
62 git commit -m "subdir" &&
63 echo H > a &&
64 test_tick &&
65 git commit -m "not subdir" a &&
66 echo A > subdir/new &&
67 test_tick &&
68 git commit -m "again subdir" subdir/new &&
69 git rm a &&
70 test_tick &&
71 git commit -m "again not subdir" &&
72 git-filter-branch --subdirectory-filter subdir sub
73'
74
75test_expect_success 'subdirectory filter result looks okay' '
76 test 2 = $(git rev-list sub | wc -l) &&
77 git show sub:new &&
78 ! git show sub:subdir
79'
80
81test_expect_success 'setup and filter history that requires --full-history' '
82 git checkout master &&
83 mkdir subdir &&
84 echo A > subdir/new &&
85 git add subdir/new &&
86 test_tick &&
87 git commit -m "subdir on master" subdir/new &&
88 git rm a &&
89 test_tick &&
90 git commit -m "again subdir on master" &&
91 git merge branch &&
92 git-filter-branch --subdirectory-filter subdir sub-master
93'
94
95test_expect_success 'subdirectory filter result looks okay' '
96 test 3 = $(git rev-list -1 --parents sub-master | wc -w) &&
97 git show sub-master^:new &&
98 git show sub-master^2:new &&
99 ! git show sub:subdir
100'
101
102test_expect_success 'use index-filter to move into a subdirectory' '
103 git-filter-branch --index-filter \
104 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
105 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
106 git update-index --index-info &&
107 mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" directorymoved &&
108 test -z "$(git diff HEAD directorymoved:newsubdir)"'
109
110test_expect_success 'author information is preserved' '
111 : > i &&
112 git add i &&
113 test_tick &&
114 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
115 git-filter-branch --msg-filter "cat; \
116 test \$GIT_COMMIT = $(git rev-parse master) && \
117 echo Hallo" \
118 preserved-author &&
119 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
120'
121
122test_expect_success "remove a certain author's commits" '
123 echo i > i &&
124 test_tick &&
125 git commit -m i i &&
126 git-filter-branch --commit-filter "\
127 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
128 then\
129 shift;\
130 while [ -n \"\$1\" ];\
131 do\
132 shift;\
133 echo \"\$1\";\
134 shift;\
135 done;\
136 else\
137 git commit-tree \"\$@\";\
138 fi" removed-author &&
139 cnt1=$(git rev-list master | wc -l) &&
140 cnt2=$(git rev-list removed-author | wc -l) &&
141 test $cnt1 -eq $(($cnt2 + 1)) &&
142 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
143'
144
145test_done