t5[6-9]*: adjust the references to the default branch name "main"
[git] / t / t3402-rebase-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git rebase --merge test'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12
13 T="A quick brown fox
14 jumps over the lazy dog."
15 for i in 1 2 3 4 5 6 7 8 9 10
16 do
17         echo "$i $T"
18 done >original
19
20 test_expect_success setup '
21         git add original &&
22         git commit -m"initial" &&
23         git branch side &&
24         echo "11 $T" >>original &&
25         git commit -a -m"main updates a bit." &&
26
27         echo "12 $T" >>original &&
28         git commit -a -m"main updates a bit more." &&
29
30         git checkout side &&
31         (echo "0 $T" && cat original) >renamed &&
32         git add renamed &&
33         git update-index --force-remove original &&
34         git commit -a -m"side renames and edits." &&
35
36         tr "[a-z]" "[A-Z]" <original >newfile &&
37         git add newfile &&
38         git commit -a -m"side edits further." &&
39         git branch second-side &&
40
41         tr "[a-m]" "[A-M]" <original >newfile &&
42         rm -f original &&
43         git commit -a -m"side edits once again." &&
44
45         git branch test-rebase side &&
46         git branch test-rebase-pick side &&
47         git branch test-reference-pick side &&
48         git branch test-conflicts side &&
49         git checkout -b test-merge side
50 '
51
52 test_expect_success 'reference merge' '
53         git merge -s recursive -m "reference merge" main
54 '
55
56 PRE_REBASE=$(git rev-parse test-rebase)
57 test_expect_success rebase '
58         git checkout test-rebase &&
59         GIT_TRACE=1 git rebase --merge main
60 '
61
62 test_expect_success 'test-rebase@{1} is pre rebase' '
63         test $PRE_REBASE = $(git rev-parse test-rebase@{1})
64 '
65
66 test_expect_success 'merge and rebase should match' '
67         git diff-tree -r test-rebase test-merge >difference &&
68         if test -s difference
69         then
70                 cat difference
71                 (exit 1)
72         else
73                 echo happy
74         fi
75 '
76
77 test_expect_success 'rebase the other way' '
78         git reset --hard main &&
79         git rebase --merge side
80 '
81
82 test_expect_success 'rebase -Xtheirs' '
83         git checkout -b conflicting main~2 &&
84         echo "AB $T" >> original &&
85         git commit -mconflicting original &&
86         git rebase -Xtheirs main &&
87         grep AB original &&
88         ! grep 11 original
89 '
90
91 test_expect_success 'rebase -Xtheirs from orphan' '
92         git checkout --orphan orphan-conflicting main~2 &&
93         echo "AB $T" >> original &&
94         git commit -morphan-conflicting original &&
95         git rebase -Xtheirs main &&
96         grep AB original &&
97         ! grep 11 original
98 '
99
100 test_expect_success 'merge and rebase should match' '
101         git diff-tree -r test-rebase test-merge >difference &&
102         if test -s difference
103         then
104                 cat difference
105                 (exit 1)
106         else
107                 echo happy
108         fi
109 '
110
111 test_expect_success 'picking rebase' '
112         git reset --hard side &&
113         git rebase --merge --onto main side^^ &&
114         mb=$(git merge-base main HEAD) &&
115         if test "$mb" = "$(git rev-parse main)"
116         then
117                 echo happy
118         else
119                 git show-branch
120                 (exit 1)
121         fi &&
122         f=$(git diff-tree --name-only HEAD^ HEAD) &&
123         g=$(git diff-tree --name-only HEAD^^ HEAD^) &&
124         case "$f,$g" in
125         newfile,newfile)
126                 echo happy ;;
127         *)
128                 echo "$f"
129                 echo "$g"
130                 (exit 1)
131         esac
132 '
133
134 test_expect_success 'rebase -s funny -Xopt' '
135         test_when_finished "rm -fr test-bin funny.was.run" &&
136         mkdir test-bin &&
137         cat >test-bin/git-merge-funny <<-EOF &&
138         #!$SHELL_PATH
139         case "\$1" in --opt) ;; *) exit 2 ;; esac
140         shift &&
141         >funny.was.run &&
142         exec git merge-recursive "\$@"
143         EOF
144         chmod +x test-bin/git-merge-funny &&
145         git reset --hard &&
146         git checkout -b test-funny main^ &&
147         test_commit funny &&
148         (
149                 PATH=./test-bin:$PATH &&
150                 git rebase -s funny -Xopt main
151         ) &&
152         test -f funny.was.run
153 '
154
155 test_expect_success 'rebase --skip works with two conflicts in a row' '
156         git checkout second-side  &&
157         tr "[A-Z]" "[a-z]" <newfile >tmp &&
158         mv tmp newfile &&
159         git commit -a -m"edit conflicting with side" &&
160         tr "[d-f]" "[D-F]" <newfile >tmp &&
161         mv tmp newfile &&
162         git commit -a -m"another edit conflicting with side" &&
163         test_must_fail git rebase --merge test-conflicts &&
164         test_must_fail git rebase --skip &&
165         git rebase --skip
166 '
167
168 test_expect_success '--reapply-cherry-picks' '
169         git init repo &&
170
171         # O(1-10) -- O(1-11) -- O(0-10) main
172         #        \
173         #         -- O(1-11) -- O(1-12) otherbranch
174
175         printf "Line %d\n" $(test_seq 1 10) >repo/file.txt &&
176         git -C repo add file.txt &&
177         git -C repo commit -m "base commit" &&
178
179         printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
180         git -C repo commit -a -m "add 11" &&
181
182         printf "Line %d\n" $(test_seq 0 10) >repo/file.txt &&
183         git -C repo commit -a -m "add 0 delete 11" &&
184
185         git -C repo checkout -b otherbranch HEAD^^ &&
186         printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
187         git -C repo commit -a -m "add 11 in another branch" &&
188
189         printf "Line %d\n" $(test_seq 1 12) >repo/file.txt &&
190         git -C repo commit -a -m "add 12 in another branch" &&
191
192         # Regular rebase fails, because the 1-11 commit is deduplicated
193         test_must_fail git -C repo rebase --merge main 2> err &&
194         test_i18ngrep "error: could not apply.*add 12 in another branch" err &&
195         git -C repo rebase --abort &&
196
197         # With --reapply-cherry-picks, it works
198         git -C repo rebase --merge --reapply-cherry-picks main
199 '
200
201 test_expect_success '--reapply-cherry-picks refrains from reading unneeded blobs' '
202         git init server &&
203
204         # O(1-10) -- O(1-11) -- O(1-12) main
205         #        \
206         #         -- O(0-10) otherbranch
207
208         printf "Line %d\n" $(test_seq 1 10) >server/file.txt &&
209         git -C server add file.txt &&
210         git -C server commit -m "merge base" &&
211
212         printf "Line %d\n" $(test_seq 1 11) >server/file.txt &&
213         git -C server commit -a -m "add 11" &&
214
215         printf "Line %d\n" $(test_seq 1 12) >server/file.txt &&
216         git -C server commit -a -m "add 12" &&
217
218         git -C server checkout -b otherbranch HEAD^^ &&
219         printf "Line %d\n" $(test_seq 0 10) >server/file.txt &&
220         git -C server commit -a -m "add 0" &&
221
222         test_config -C server uploadpack.allowfilter 1 &&
223         test_config -C server uploadpack.allowanysha1inwant 1 &&
224
225         git clone --filter=blob:none "file://$(pwd)/server" client &&
226         git -C client checkout origin/main &&
227         git -C client checkout origin/otherbranch &&
228
229         # Sanity check to ensure that the blobs from the merge base and "add
230         # 11" are missing
231         git -C client rev-list --objects --all --missing=print >missing_list &&
232         MERGE_BASE_BLOB=$(git -C server rev-parse main^^:file.txt) &&
233         ADD_11_BLOB=$(git -C server rev-parse main^:file.txt) &&
234         grep "[?]$MERGE_BASE_BLOB" missing_list &&
235         grep "[?]$ADD_11_BLOB" missing_list &&
236
237         git -C client rebase --merge --reapply-cherry-picks origin/main &&
238
239         # The blob from the merge base had to be fetched, but not "add 11"
240         git -C client rev-list --objects --all --missing=print >missing_list &&
241         ! grep "[?]$MERGE_BASE_BLOB" missing_list &&
242         grep "[?]$ADD_11_BLOB" missing_list
243 '
244
245 test_done