Merge branch 'rs/diff-parseopts-cleanup'
[git/gitster.git] / t / t3402-rebase-merge.sh
blob5c67d07ba3ecf6333d1967c341d9bfb75fc33769
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_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
14 T="A quick brown fox
15 jumps over the lazy dog."
16 for i in 1 2 3 4 5 6 7 8 9 10
18 echo "$i $T"
19 done >original
21 test_expect_success setup '
22 git add original &&
23 git commit -m"initial" &&
24 git branch side &&
25 echo "11 $T" >>original &&
26 git commit -a -m"main updates a bit." &&
28 echo "12 $T" >>original &&
29 git commit -a -m"main updates a bit more." &&
31 git checkout side &&
32 (echo "0 $T" && cat original) >renamed &&
33 git add renamed &&
34 git update-index --force-remove original &&
35 git commit -a -m"side renames and edits." &&
37 tr "[a-z]" "[A-Z]" <original >newfile &&
38 git add newfile &&
39 git commit -a -m"side edits further." &&
40 git branch second-side &&
42 tr "[a-m]" "[A-M]" <original >newfile &&
43 rm -f original &&
44 git commit -a -m"side edits once again." &&
46 git branch test-rebase side &&
47 git branch test-rebase-pick side &&
48 git branch test-reference-pick side &&
49 git branch test-conflicts side &&
50 git checkout -b test-merge side
53 test_expect_success 'reference merge' '
54 git merge -s recursive -m "reference merge" main
57 PRE_REBASE=$(git rev-parse test-rebase)
58 test_expect_success rebase '
59 git checkout test-rebase &&
60 GIT_TRACE=1 git rebase --merge main
63 test_expect_success 'test-rebase@{1} is pre rebase' '
64 test $PRE_REBASE = $(git rev-parse test-rebase@{1})
67 test_expect_success 'merge and rebase should match' '
68 git diff-tree -r test-rebase test-merge >difference &&
69 if test -s difference
70 then
71 cat difference
72 false
73 else
74 echo happy
78 test_expect_success 'rebase the other way' '
79 git reset --hard main &&
80 git rebase --merge side
83 test_expect_success 'rebase -Xtheirs' '
84 git checkout -b conflicting main~2 &&
85 echo "AB $T" >> original &&
86 git commit -mconflicting original &&
87 git rebase -Xtheirs main &&
88 grep AB original &&
89 ! grep 11 original
92 test_expect_success 'rebase -Xtheirs from orphan' '
93 git checkout --orphan orphan-conflicting main~2 &&
94 echo "AB $T" >> original &&
95 git commit -morphan-conflicting original &&
96 git rebase -Xtheirs main &&
97 grep AB original &&
98 ! grep 11 original
101 test_expect_success 'merge and rebase should match' '
102 git diff-tree -r test-rebase test-merge >difference &&
103 if test -s difference
104 then
105 cat difference
106 false
107 else
108 echo happy
112 test_expect_success 'picking rebase' '
113 git reset --hard side &&
114 git rebase --merge --onto main side^^ &&
115 mb=$(git merge-base main HEAD) &&
116 if test "$mb" = "$(git rev-parse main)"
117 then
118 echo happy
119 else
120 git show-branch
121 false
122 fi &&
123 f=$(git diff-tree --name-only HEAD^ HEAD) &&
124 g=$(git diff-tree --name-only HEAD^^ HEAD^) &&
125 case "$f,$g" in
126 newfile,newfile)
127 echo happy ;;
129 echo "$f"
130 echo "$g"
131 false
132 esac
135 test_expect_success 'rebase --skip works with two conflicts in a row' '
136 git checkout second-side &&
137 tr "[A-Z]" "[a-z]" <newfile >tmp &&
138 mv tmp newfile &&
139 git commit -a -m"edit conflicting with side" &&
140 tr "[d-f]" "[D-F]" <newfile >tmp &&
141 mv tmp newfile &&
142 git commit -a -m"another edit conflicting with side" &&
143 test_must_fail git rebase --merge test-conflicts &&
144 test_must_fail git rebase --skip &&
145 git rebase --skip
148 test_expect_success '--reapply-cherry-picks' '
149 git init repo &&
151 # O(1-10) -- O(1-11) -- O(0-10) main
153 # -- O(1-11) -- O(1-12) otherbranch
155 printf "Line %d\n" $(test_seq 1 10) >repo/file.txt &&
156 git -C repo add file.txt &&
157 git -C repo commit -m "base commit" &&
159 printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
160 git -C repo commit -a -m "add 11" &&
162 printf "Line %d\n" $(test_seq 0 10) >repo/file.txt &&
163 git -C repo commit -a -m "add 0 delete 11" &&
165 git -C repo checkout -b otherbranch HEAD^^ &&
166 printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
167 git -C repo commit -a -m "add 11 in another branch" &&
169 printf "Line %d\n" $(test_seq 1 12) >repo/file.txt &&
170 git -C repo commit -a -m "add 12 in another branch" &&
172 # Regular rebase fails, because the 1-11 commit is deduplicated
173 test_must_fail git -C repo rebase --merge main 2> err &&
174 test_grep "error: could not apply.*add 12 in another branch" err &&
175 git -C repo rebase --abort &&
177 # With --reapply-cherry-picks, it works
178 git -C repo rebase --merge --reapply-cherry-picks main
181 test_expect_success '--reapply-cherry-picks refrains from reading unneeded blobs' '
182 git init server &&
184 # O(1-10) -- O(1-11) -- O(1-12) main
186 # -- O(0-10) otherbranch
188 printf "Line %d\n" $(test_seq 1 10) >server/file.txt &&
189 git -C server add file.txt &&
190 git -C server commit -m "merge base" &&
192 printf "Line %d\n" $(test_seq 1 11) >server/file.txt &&
193 git -C server commit -a -m "add 11" &&
195 printf "Line %d\n" $(test_seq 1 12) >server/file.txt &&
196 git -C server commit -a -m "add 12" &&
198 git -C server checkout -b otherbranch HEAD^^ &&
199 printf "Line %d\n" $(test_seq 0 10) >server/file.txt &&
200 git -C server commit -a -m "add 0" &&
202 test_config -C server uploadpack.allowfilter 1 &&
203 test_config -C server uploadpack.allowanysha1inwant 1 &&
205 git clone --filter=blob:none "file://$(pwd)/server" client &&
206 git -C client checkout origin/main &&
207 git -C client checkout origin/otherbranch &&
209 # Sanity check to ensure that the blobs from the merge base and "add
210 # 11" are missing
211 git -C client rev-list --objects --all --missing=print >missing_list &&
212 MERGE_BASE_BLOB=$(git -C server rev-parse main^^:file.txt) &&
213 ADD_11_BLOB=$(git -C server rev-parse main^:file.txt) &&
214 grep "[?]$MERGE_BASE_BLOB" missing_list &&
215 grep "[?]$ADD_11_BLOB" missing_list &&
217 git -C client rebase --merge --reapply-cherry-picks origin/main &&
219 # The blob from the merge base had to be fetched, but not "add 11"
220 git -C client rev-list --objects --all --missing=print >missing_list &&
221 ! grep "[?]$MERGE_BASE_BLOB" missing_list &&
222 grep "[?]$ADD_11_BLOB" missing_list
225 test_done