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