1 #!/bin/sh
3 test_description='git rebase --root
5 Tests if git rebase --root --onto <newparent> can rebase the root commit.
6 '
7 . ./test-lib.sh
9 test_expect_success 'prepare repository' '
10 test_commit 1 A &&
11 test_commit 2 A &&
12 git symbolic-ref HEAD refs/heads/other &&
13 rm .git/index &&
14 test_commit 3 B &&
15 test_commit 1b A 1 &&
16 test_commit 4 B
17 '
19 test_expect_success 'rebase --root expects --onto' '
20 test_must_fail git rebase --root
21 '
23 test_expect_success 'setup pre-rebase hook' '
24 mkdir -p .git/hooks &&
25 cat >.git/hooks/pre-rebase <<EOF &&
26 #!$SHELL_PATH
27 echo "\$1,\$2" >.git/PRE-REBASE-INPUT
28 EOF
29 chmod +x .git/hooks/pre-rebase
30 '
31 cat > expect <<EOF
32 4
33 3
34 2
35 1
36 EOF
38 test_expect_success 'rebase --root --onto <newbase>' '
39 git checkout -b work &&
40 git rebase --root --onto master &&
41 git log --pretty=tformat:"%s" > rebased &&
42 test_cmp expect rebased
43 '
45 test_expect_success 'pre-rebase got correct input (1)' '
46 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
47 '
49 test_expect_success 'rebase --root --onto <newbase> <branch>' '
50 git branch work2 other &&
51 git rebase --root --onto master work2 &&
52 git log --pretty=tformat:"%s" > rebased2 &&
53 test_cmp expect rebased2
54 '
56 test_expect_success 'pre-rebase got correct input (2)' '
57 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
58 '
60 test_expect_success 'rebase -i --root --onto <newbase>' '
61 git checkout -b work3 other &&
62 GIT_EDITOR=: git rebase -i --root --onto master &&
63 git log --pretty=tformat:"%s" > rebased3 &&
64 test_cmp expect rebased3
65 '
67 test_expect_success 'pre-rebase got correct input (3)' '
68 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
69 '
71 test_expect_success 'rebase -i --root --onto <newbase> <branch>' '
72 git branch work4 other &&
73 GIT_EDITOR=: git rebase -i --root --onto master work4 &&
74 git log --pretty=tformat:"%s" > rebased4 &&
75 test_cmp expect rebased4
76 '
78 test_expect_success 'pre-rebase got correct input (4)' '
79 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
80 '
82 test_expect_success 'rebase -i -p with linear history' '
83 git checkout -b work5 other &&
84 GIT_EDITOR=: git rebase -i -p --root --onto master &&
85 git log --pretty=tformat:"%s" > rebased5 &&
86 test_cmp expect rebased5
87 '
89 test_expect_success 'pre-rebase got correct input (5)' '
90 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
91 '
93 test_expect_success 'set up merge history' '
94 git checkout other^ &&
95 git checkout -b side &&
96 test_commit 5 C &&
97 git checkout other &&
98 git merge side
99 '
101 sed 's/#/ /g' > expect-side <<'EOF'
102 * Merge branch 'side' into other
103 |\##
104 | * 5
105 * | 4
106 |/##
107 * 3
108 * 2
109 * 1
110 EOF
112 test_expect_success 'rebase -i -p with merge' '
113 git checkout -b work6 other &&
114 GIT_EDITOR=: git rebase -i -p --root --onto master &&
115 git log --graph --topo-order --pretty=tformat:"%s" > rebased6 &&
116 test_cmp expect-side rebased6
117 '
119 test_expect_success 'set up second root and merge' '
120 git symbolic-ref HEAD refs/heads/third &&
121 rm .git/index &&
122 rm A B C &&
123 test_commit 6 D &&
124 git checkout other &&
125 git merge third
126 '
128 sed 's/#/ /g' > expect-third <<'EOF'
129 * Merge branch 'third' into other
130 |\##
131 | * 6
132 * | Merge branch 'side' into other
133 |\ \##
134 | * | 5
135 * | | 4
136 |/ /##
137 * | 3
138 |/##
139 * 2
140 * 1
141 EOF
143 test_expect_success 'rebase -i -p with two roots' '
144 git checkout -b work7 other &&
145 GIT_EDITOR=: git rebase -i -p --root --onto master &&
146 git log --graph --topo-order --pretty=tformat:"%s" > rebased7 &&
147 test_cmp expect-third rebased7
148 '
150 test_expect_success 'setup pre-rebase hook that fails' '
151 mkdir -p .git/hooks &&
152 cat >.git/hooks/pre-rebase <<EOF &&
153 #!$SHELL_PATH
154 false
155 EOF
156 chmod +x .git/hooks/pre-rebase
157 '
159 test_expect_success 'pre-rebase hook stops rebase' '
160 git checkout -b stops1 other &&
161 (
162 GIT_EDITOR=:
163 export GIT_EDITOR
164 test_must_fail git rebase --root --onto master
165 ) &&
166 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1
167 test 0 = $(git rev-list other...stops1 | wc -l)
168 '
170 test_expect_success 'pre-rebase hook stops rebase -i' '
171 git checkout -b stops2 other &&
172 (
173 GIT_EDITOR=:
174 export GIT_EDITOR
175 test_must_fail git rebase --root --onto master
176 ) &&
177 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2
178 test 0 = $(git rev-list other...stops2 | wc -l)
179 '
181 test_expect_success 'remove pre-rebase hook' '
182 rm -f .git/hooks/pre-rebase
183 '
185 test_expect_success 'set up a conflict' '
186 git checkout master &&
187 echo conflict > B &&
188 git add B &&
189 git commit -m conflict
190 '
192 test_expect_success 'rebase --root with conflict (first part)' '
193 git checkout -b conflict1 other &&
194 test_must_fail git rebase --root --onto master &&
195 git ls-files -u | grep "B$"
196 '
198 test_expect_success 'fix the conflict' '
199 echo 3 > B &&
200 git add B
201 '
203 cat > expect-conflict <<EOF
204 6
205 5
206 4
207 3
208 conflict
209 2
210 1
211 EOF
213 test_expect_success 'rebase --root with conflict (second part)' '
214 git rebase --continue &&
215 git log --pretty=tformat:"%s" > conflict1 &&
216 test_cmp expect-conflict conflict1
217 '
219 test_expect_success 'rebase -i --root with conflict (first part)' '
220 git checkout -b conflict2 other &&
221 (
222 GIT_EDITOR=:
223 export GIT_EDITOR
224 test_must_fail git rebase -i --root --onto master
225 ) &&
226 git ls-files -u | grep "B$"
227 '
229 test_expect_success 'fix the conflict' '
230 echo 3 > B &&
231 git add B
232 '
234 test_expect_success 'rebase -i --root with conflict (second part)' '
235 git rebase --continue &&
236 git log --pretty=tformat:"%s" > conflict2 &&
237 test_cmp expect-conflict conflict2
238 '
240 cat >expect-conflict-p <<\EOF
241 commit conflict3 conflict3~1 conflict3^2
242 Merge branch 'third' into other
243 commit conflict3^2 conflict3~4
244 6
245 commit conflict3~1 conflict3~2 conflict3~1^2
246 Merge branch 'side' into other
247 commit conflict3~1^2 conflict3~3
248 5
249 commit conflict3~2 conflict3~3
250 4
251 commit conflict3~3 conflict3~4
252 3
253 commit conflict3~4 conflict3~5
254 conflict
255 commit conflict3~5 conflict3~6
256 2
257 commit conflict3~6
258 1
259 EOF
261 test_expect_success 'rebase -i -p --root with conflict (first part)' '
262 git checkout -b conflict3 other &&
263 (
264 GIT_EDITOR=:
265 export GIT_EDITOR
266 test_must_fail git rebase -i -p --root --onto master
267 ) &&
268 git ls-files -u | grep "B$"
269 '
271 test_expect_success 'fix the conflict' '
272 echo 3 > B &&
273 git add B
274 '
276 test_expect_success 'rebase -i -p --root with conflict (second part)' '
277 git rebase --continue &&
278 git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
279 git name-rev --stdin --name-only --refs=refs/heads/conflict3 >out &&
280 test_cmp expect-conflict-p out
281 '
283 test_done