| 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 | false |
| 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 | test_grep AB original && |
| 88 | test_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 | test_grep AB original && |
| 97 | test_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 | false |
| 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 | 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 ;; |
| 127 | *) |
| 128 | echo "$f" |
| 129 | echo "$g" |
| 130 | false |
| 131 | esac |
| 132 | ' |
| 133 | |
| 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 |
| 145 | ' |
| 146 | |
| 147 | test_expect_success '--reapply-cherry-picks' ' |
| 148 | git init repo && |
| 149 | |
| 150 | # O(1-10) -- O(1-11) -- O(0-10) main |
| 151 | # \ |
| 152 | # -- O(1-11) -- O(1-12) otherbranch |
| 153 | |
| 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" && |
| 157 | |
| 158 | printf "Line %d\n" $(test_seq 1 11) >repo/file.txt && |
| 159 | git -C repo commit -a -m "add 11" && |
| 160 | |
| 161 | printf "Line %d\n" $(test_seq 0 10) >repo/file.txt && |
| 162 | git -C repo commit -a -m "add 0 delete 11" && |
| 163 | |
| 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" && |
| 167 | |
| 168 | printf "Line %d\n" $(test_seq 1 12) >repo/file.txt && |
| 169 | git -C repo commit -a -m "add 12 in another branch" && |
| 170 | |
| 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_grep "error: could not apply.*add 12 in another branch" err && |
| 174 | git -C repo rebase --abort && |
| 175 | |
| 176 | # With --reapply-cherry-picks, it works |
| 177 | git -C repo rebase --merge --reapply-cherry-picks main |
| 178 | ' |
| 179 | |
| 180 | test_expect_success '--reapply-cherry-picks refrains from reading unneeded blobs' ' |
| 181 | git init server && |
| 182 | |
| 183 | # O(1-10) -- O(1-11) -- O(1-12) main |
| 184 | # \ |
| 185 | # -- O(0-10) otherbranch |
| 186 | |
| 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" && |
| 190 | |
| 191 | printf "Line %d\n" $(test_seq 1 11) >server/file.txt && |
| 192 | git -C server commit -a -m "add 11" && |
| 193 | |
| 194 | printf "Line %d\n" $(test_seq 1 12) >server/file.txt && |
| 195 | git -C server commit -a -m "add 12" && |
| 196 | |
| 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" && |
| 200 | |
| 201 | test_config -C server uploadpack.allowfilter 1 && |
| 202 | test_config -C server uploadpack.allowanysha1inwant 1 && |
| 203 | |
| 204 | git clone --filter=blob:none "file://$(pwd)/server" client && |
| 205 | git -C client checkout origin/main && |
| 206 | git -C client checkout origin/otherbranch && |
| 207 | |
| 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 | test_grep "[?]$MERGE_BASE_BLOB" missing_list && |
| 214 | test_grep "[?]$ADD_11_BLOB" missing_list && |
| 215 | |
| 216 | git -C client rebase --merge --reapply-cherry-picks origin/main && |
| 217 | |
| 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 | test_grep ! "[?]$MERGE_BASE_BLOB" missing_list && |
| 221 | test_grep "[?]$ADD_11_BLOB" missing_list |
| 222 | ' |
| 223 | |
| 224 | test_done |