Sync with Git 2.40.1
[git/gitster.git] / t / t3402-rebase-merge.sh
blob79b0640c004e23f5346d9bfe1ba9998791633887
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git rebase --merge test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 . ./test-lib.sh
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
17 echo "$i $T"
18 done >original
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." &&
27 echo "12 $T" >>original &&
28 git commit -a -m"main updates a bit more." &&
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." &&
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 &&
41 tr "[a-m]" "[A-M]" <original >newfile &&
42 rm -f original &&
43 git commit -a -m"side edits once again." &&
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
52 test_expect_success 'reference merge' '
53 git merge -s recursive -m "reference merge" main
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
62 test_expect_success 'test-rebase@{1} is pre rebase' '
63 test $PRE_REBASE = $(git rev-parse test-rebase@{1})
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 false
72 else
73 echo happy
77 test_expect_success 'rebase the other way' '
78 git reset --hard main &&
79 git rebase --merge side
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
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
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 false
106 else
107 echo happy
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 false
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 ;;
128 echo "$f"
129 echo "$g"
130 false
131 esac
134 test_expect_success 'rebase --skip works with two conflicts in a row' '
135 git checkout second-side &&
136 tr "[A-Z]" "[a-z]" <newfile >tmp &&
137 mv tmp newfile &&
138 git commit -a -m"edit conflicting with side" &&
139 tr "[d-f]" "[D-F]" <newfile >tmp &&
140 mv tmp newfile &&
141 git commit -a -m"another edit conflicting with side" &&
142 test_must_fail git rebase --merge test-conflicts &&
143 test_must_fail git rebase --skip &&
144 git rebase --skip
147 test_expect_success '--reapply-cherry-picks' '
148 git init repo &&
150 # O(1-10) -- O(1-11) -- O(0-10) main
152 # -- O(1-11) -- O(1-12) otherbranch
154 printf "Line %d\n" $(test_seq 1 10) >repo/file.txt &&
155 git -C repo add file.txt &&
156 git -C repo commit -m "base commit" &&
158 printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
159 git -C repo commit -a -m "add 11" &&
161 printf "Line %d\n" $(test_seq 0 10) >repo/file.txt &&
162 git -C repo commit -a -m "add 0 delete 11" &&
164 git -C repo checkout -b otherbranch HEAD^^ &&
165 printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
166 git -C repo commit -a -m "add 11 in another branch" &&
168 printf "Line %d\n" $(test_seq 1 12) >repo/file.txt &&
169 git -C repo commit -a -m "add 12 in another branch" &&
171 # Regular rebase fails, because the 1-11 commit is deduplicated
172 test_must_fail git -C repo rebase --merge main 2> err &&
173 test_i18ngrep "error: could not apply.*add 12 in another branch" err &&
174 git -C repo rebase --abort &&
176 # With --reapply-cherry-picks, it works
177 git -C repo rebase --merge --reapply-cherry-picks main
180 test_expect_success '--reapply-cherry-picks refrains from reading unneeded blobs' '
181 git init server &&
183 # O(1-10) -- O(1-11) -- O(1-12) main
185 # -- O(0-10) otherbranch
187 printf "Line %d\n" $(test_seq 1 10) >server/file.txt &&
188 git -C server add file.txt &&
189 git -C server commit -m "merge base" &&
191 printf "Line %d\n" $(test_seq 1 11) >server/file.txt &&
192 git -C server commit -a -m "add 11" &&
194 printf "Line %d\n" $(test_seq 1 12) >server/file.txt &&
195 git -C server commit -a -m "add 12" &&
197 git -C server checkout -b otherbranch HEAD^^ &&
198 printf "Line %d\n" $(test_seq 0 10) >server/file.txt &&
199 git -C server commit -a -m "add 0" &&
201 test_config -C server uploadpack.allowfilter 1 &&
202 test_config -C server uploadpack.allowanysha1inwant 1 &&
204 git clone --filter=blob:none "file://$(pwd)/server" client &&
205 git -C client checkout origin/main &&
206 git -C client checkout origin/otherbranch &&
208 # Sanity check to ensure that the blobs from the merge base and "add
209 # 11" are missing
210 git -C client rev-list --objects --all --missing=print >missing_list &&
211 MERGE_BASE_BLOB=$(git -C server rev-parse main^^:file.txt) &&
212 ADD_11_BLOB=$(git -C server rev-parse main^:file.txt) &&
213 grep "[?]$MERGE_BASE_BLOB" missing_list &&
214 grep "[?]$ADD_11_BLOB" missing_list &&
216 git -C client rebase --merge --reapply-cherry-picks origin/main &&
218 # The blob from the merge base had to be fetched, but not "add 11"
219 git -C client rev-list --objects --all --missing=print >missing_list &&
220 ! grep "[?]$MERGE_BASE_BLOB" missing_list &&
221 grep "[?]$ADD_11_BLOB" missing_list
224 test_done