blob: 5f60b22d872b5d034e137e0c6c7c5a9d664e57ee [file] [log] [blame]
Johannes Schindelin6f6826c2007-06-03 01:31:28 +01001#!/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
Junio C Hamanoaee078b2007-06-05 00:07:31 -070010 test_tick
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010011 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
Junio C Hamano5be60072007-07-02 22:52:14 -070030H=$(git rev-parse H)
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010031
32test_expect_success 'rewrite identically' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +010033 git-filter-branch branch
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010034'
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010035test_expect_success 'result is really identical' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +010036 test $H = $(git rev-parse HEAD)
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010037'
38
39test_expect_success 'rewrite, renaming a specific file' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +010040 git-filter-branch -f --tree-filter "mv d doh || :" HEAD
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010041'
42
43test_expect_success 'test that the file was renamed' '
Johannes Schindelin46eb4492007-10-17 03:23:10 +010044 test d = $(git show HEAD:doh) &&
45 test -f doh &&
46 test d = $(cat doh)
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010047'
48
Johannes Schindelindfd05e32007-07-23 18:34:13 +010049git tag oldD HEAD~4
Johannes Schindelin98409062007-06-05 16:58:13 +010050test_expect_success 'rewrite one branch, keeping a side branch' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +010051 git branch modD oldD &&
52 git-filter-branch -f --tree-filter "mv b boh || :" D..modD
Johannes Schindelin98409062007-06-05 16:58:13 +010053'
54
55test_expect_success 'common ancestor is still common (unchanged)' '
Junio C Hamano5be60072007-07-02 22:52:14 -070056 test "$(git merge-base modD D)" = "$(git rev-parse B)"
Johannes Schindelin98409062007-06-05 16:58:13 +010057'
58
Johannes Schindelin685ef542007-06-08 01:30:35 +010059test_expect_success 'filter subdirectory only' '
60 mkdir subdir &&
61 touch subdir/new &&
62 git add subdir/new &&
63 test_tick &&
64 git commit -m "subdir" &&
65 echo H > a &&
66 test_tick &&
67 git commit -m "not subdir" a &&
68 echo A > subdir/new &&
69 test_tick &&
70 git commit -m "again subdir" subdir/new &&
71 git rm a &&
72 test_tick &&
73 git commit -m "again not subdir" &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +010074 git branch sub &&
75 git-filter-branch -f --subdirectory-filter subdir refs/heads/sub
Johannes Schindelin685ef542007-06-08 01:30:35 +010076'
77
78test_expect_success 'subdirectory filter result looks okay' '
Junio C Hamano5be60072007-07-02 22:52:14 -070079 test 2 = $(git rev-list sub | wc -l) &&
Johannes Schindelin685ef542007-06-08 01:30:35 +010080 git show sub:new &&
81 ! git show sub:subdir
82'
83
Johannes Sixtcfabd6e2007-06-08 23:28:50 +020084test_expect_success 'setup and filter history that requires --full-history' '
85 git checkout master &&
86 mkdir subdir &&
87 echo A > subdir/new &&
88 git add subdir/new &&
89 test_tick &&
90 git commit -m "subdir on master" subdir/new &&
91 git rm a &&
92 test_tick &&
93 git commit -m "again subdir on master" &&
94 git merge branch &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +010095 git branch sub-master &&
96 git-filter-branch -f --subdirectory-filter subdir sub-master
Johannes Sixtcfabd6e2007-06-08 23:28:50 +020097'
98
99test_expect_success 'subdirectory filter result looks okay' '
Junio C Hamano5be60072007-07-02 22:52:14 -0700100 test 3 = $(git rev-list -1 --parents sub-master | wc -w) &&
Johannes Sixtcfabd6e2007-06-08 23:28:50 +0200101 git show sub-master^:new &&
102 git show sub-master^2:new &&
103 ! git show sub:subdir
104'
105
Johannes Schindelin55f22ff2007-06-21 18:05:30 +0100106test_expect_success 'use index-filter to move into a subdirectory' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100107 git branch directorymoved &&
108 git-filter-branch -f --index-filter \
Junio C Hamano5be60072007-07-02 22:52:14 -0700109 "git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
Johannes Schindelin55f22ff2007-06-21 18:05:30 +0100110 GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
Junio C Hamano5be60072007-07-02 22:52:14 -0700111 git update-index --index-info &&
Johannes Schindelin55f22ff2007-06-21 18:05:30 +0100112 mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" directorymoved &&
113 test -z "$(git diff HEAD directorymoved:newsubdir)"'
114
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100115test_expect_success 'stops when msg filter fails' '
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100116 old=$(git rev-parse HEAD) &&
Johannes Sixtfdd7d482007-11-27 13:10:19 +0100117 ! git-filter-branch -f --msg-filter false HEAD &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100118 test $old = $(git rev-parse HEAD) &&
119 rm -rf .git-rewrite
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100120'
121
Johannes Schindelinf6b78c62007-07-03 17:50:19 +0100122test_expect_success 'author information is preserved' '
123 : > i &&
124 git add i &&
125 test_tick &&
126 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100127 git branch preserved-author &&
128 git-filter-branch -f --msg-filter "cat; \
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100129 test \$GIT_COMMIT != $(git rev-parse master) || \
Johannes Schindelinf6b78c62007-07-03 17:50:19 +0100130 echo Hallo" \
131 preserved-author &&
132 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
133'
134
135test_expect_success "remove a certain author's commits" '
136 echo i > i &&
137 test_tick &&
138 git commit -m i i &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100139 git branch removed-author &&
140 git-filter-branch -f --commit-filter "\
Johannes Schindelinf6b78c62007-07-03 17:50:19 +0100141 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
142 then\
Johannes Schindelinf95eef12007-08-31 20:06:27 +0100143 skip_commit \"\$@\";
Johannes Schindelinf6b78c62007-07-03 17:50:19 +0100144 else\
145 git commit-tree \"\$@\";\
146 fi" removed-author &&
147 cnt1=$(git rev-list master | wc -l) &&
148 cnt2=$(git rev-list removed-author | wc -l) &&
149 test $cnt1 -eq $(($cnt2 + 1)) &&
150 test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
151'
152
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100153test_expect_success 'barf on invalid name' '
154 ! git filter-branch -f master xy-problem &&
155 ! git filter-branch -f HEAD^
156'
157
Johannes Schindelin7e0f1702007-08-31 20:05:36 +0100158test_expect_success '"map" works in commit filter' '
159 git filter-branch -f --commit-filter "\
160 parent=\$(git rev-parse \$GIT_COMMIT^) &&
161 mapped=\$(map \$parent) &&
162 actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
163 test \$mapped = \$actual &&
164 git commit-tree \"\$@\";" master~2..master &&
165 git rev-parse --verify master
166'
167
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100168test_done