3 test_description
='git rebase --root
5 Tests if git rebase --root --onto <newparent> can rebase the root commit.
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13 git rev-list
--topo-order --parents --pretty="tformat:%s" HEAD |
14 git name-rev
--stdin --name-only --refs=refs
/heads
/$1
18 test_expect_success
'prepare repository' '
21 git symbolic-ref HEAD refs/heads/other &&
28 test_expect_success
'rebase --root fails with too many args' '
29 git checkout -B fail other &&
30 test_must_fail git rebase --onto main --root fail fail
33 test_expect_success
'setup pre-rebase hook' '
34 mkdir -p .git/hooks &&
35 cat >.git/hooks/pre-rebase <<EOF &&
37 echo "\$1,\$2" >.git/PRE-REBASE-INPUT
39 chmod +x .git/hooks/pre-rebase
48 test_expect_success
'rebase --root --onto <newbase>' '
49 git checkout -b work other &&
50 git rebase --root --onto main &&
51 git log --pretty=tformat:"%s" > rebased &&
52 test_cmp expect rebased
55 test_expect_success
'pre-rebase got correct input (1)' '
56 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
59 test_expect_success
'rebase --root --onto <newbase> <branch>' '
60 git branch work2 other &&
61 git rebase --root --onto main work2 &&
62 git log --pretty=tformat:"%s" > rebased2 &&
63 test_cmp expect rebased2
66 test_expect_success
'pre-rebase got correct input (2)' '
67 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
70 test_expect_success
'rebase -i --root --onto <newbase>' '
71 git checkout -b work3 other &&
72 git rebase -i --root --onto main &&
73 git log --pretty=tformat:"%s" > rebased3 &&
74 test_cmp expect rebased3
77 test_expect_success
'pre-rebase got correct input (3)' '
78 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
81 test_expect_success
'rebase -i --root --onto <newbase> <branch>' '
82 git branch work4 other &&
83 git rebase -i --root --onto main work4 &&
84 git log --pretty=tformat:"%s" > rebased4 &&
85 test_cmp expect rebased4
88 test_expect_success
'pre-rebase got correct input (4)' '
89 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
92 test_expect_success REBASE_P
'rebase -i -p with linear history' '
93 git checkout -b work5 other &&
94 git rebase -i -p --root --onto main &&
95 git log --pretty=tformat:"%s" > rebased5 &&
96 test_cmp expect rebased5
99 test_expect_success REBASE_P
'pre-rebase got correct input (5)' '
100 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
103 test_expect_success
'set up merge history' '
104 git checkout other^ &&
105 git checkout -b side &&
107 git checkout other &&
111 cat > expect-side
<<'EOF'
112 commit work6 work6~1 work6^2
113 Merge branch 'side' into other
114 commit work6^2 work6~2
116 commit work6~1 work6~2
118 commit work6~2 work6~3
120 commit work6~3 work6~4
126 test_expect_success REBASE_P
'rebase -i -p with merge' '
127 git checkout -b work6 other &&
128 git rebase -i -p --root --onto main &&
129 log_with_names work6 > rebased6 &&
130 test_cmp expect-side rebased6
133 test_expect_success
'set up second root and merge' '
134 git symbolic-ref HEAD refs/heads/third &&
138 git checkout other &&
139 git merge --allow-unrelated-histories third
142 cat > expect-third
<<'EOF'
143 commit work7 work7~1 work7^2
144 Merge branch 'third' into other
145 commit work7^2 work7~4
147 commit work7~1 work7~2 work7~1^2
148 Merge branch 'side' into other
149 commit work7~1^2 work7~3
151 commit work7~2 work7~3
153 commit work7~3 work7~4
155 commit work7~4 work7~5
161 test_expect_success REBASE_P
'rebase -i -p with two roots' '
162 git checkout -b work7 other &&
163 git rebase -i -p --root --onto main &&
164 log_with_names work7 > rebased7 &&
165 test_cmp expect-third rebased7
168 test_expect_success
'setup pre-rebase hook that fails' '
169 mkdir -p .git/hooks &&
170 cat >.git/hooks/pre-rebase <<EOF &&
174 chmod +x .git/hooks/pre-rebase
177 test_expect_success
'pre-rebase hook stops rebase' '
178 git checkout -b stops1 other &&
179 test_must_fail git rebase --root --onto main &&
180 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1 &&
181 test 0 = $(git rev-list other...stops1 | wc -l)
184 test_expect_success
'pre-rebase hook stops rebase -i' '
185 git checkout -b stops2 other &&
186 test_must_fail git rebase --root --onto main &&
187 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2 &&
188 test 0 = $(git rev-list other...stops2 | wc -l)
191 test_expect_success
'remove pre-rebase hook' '
192 rm -f .git/hooks/pre-rebase
195 test_expect_success
'set up a conflict' '
199 git commit -m conflict
202 test_expect_success
'rebase --root with conflict (first part)' '
203 git checkout -b conflict1 other &&
204 test_must_fail git rebase --root --onto main &&
205 git ls-files -u | grep "B$"
208 test_expect_success
'fix the conflict' '
213 cat > expect-conflict
<<EOF
223 test_expect_success
'rebase --root with conflict (second part)' '
224 git rebase --continue &&
225 git log --pretty=tformat:"%s" > conflict1 &&
226 test_cmp expect-conflict conflict1
229 test_expect_success
'rebase -i --root with conflict (first part)' '
230 git checkout -b conflict2 other &&
231 test_must_fail git rebase -i --root --onto main &&
232 git ls-files -u | grep "B$"
235 test_expect_success
'fix the conflict' '
240 test_expect_success
'rebase -i --root with conflict (second part)' '
241 git rebase --continue &&
242 git log --pretty=tformat:"%s" > conflict2 &&
243 test_cmp expect-conflict conflict2
246 cat >expect-conflict-p
<<\EOF
247 commit conflict3 conflict3~
1 conflict3^
2
248 Merge branch
'third' into other
249 commit conflict3^
2 conflict3~
4
251 commit conflict3~
1 conflict3~
2 conflict3~
1^
2
252 Merge branch
'side' into other
253 commit conflict3~
1^
2 conflict3~
3
255 commit conflict3~
2 conflict3~
3
257 commit conflict3~
3 conflict3~
4
259 commit conflict3~
4 conflict3~
5
261 commit conflict3~
5 conflict3~
6
267 test_expect_success REBASE_P
'rebase -i -p --root with conflict (first part)' '
268 git checkout -b conflict3 other &&
269 test_must_fail git rebase -i -p --root --onto main &&
270 git ls-files -u | grep "B$"
273 test_expect_success
'fix the conflict' '
278 test_expect_success REBASE_P
'rebase -i -p --root with conflict (second part)' '
279 git rebase --continue &&
280 log_with_names conflict3 >out &&
281 test_cmp expect-conflict-p out