checkout/restore: add basic tests for --merge

Even though "checkout --merge -- paths" had some tests, we never made sure it worked to recreate the conflicted state _after_ the resolution was recorded in the index. Also "restore --merge" did not even have any tests. Currently these commands use the unmerge_marked_index() interface that cannot handle paths that have been resolved as removal, and tests for that case are marked with test_expect_failure; these should eventually be fixed, but not in this patch. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 31, 2023 at 15:44 UTC ed3789f2f0ab8b8d5e325e47f0caf02b6bd9edfc
2 files changed +106
t/t2070-restore.sh
+64
@@ -137,6 +137,70 @@ test_expect_success 'restore --staged invalidates cache tree for deletions' '
137 test_must_fail git rev-parse HEAD:new1
138 '
139
140 +test_expect_success 'restore --merge to unresolve' '
141 + O=$(echo original | git hash-object -w --stdin) &&
142 + A=$(echo ourside | git hash-object -w --stdin) &&
143 + B=$(echo theirside | git hash-object -w --stdin) &&
144 + {
145 + echo "100644 $O 1 file" &&
146 + echo "100644 $A 2 file" &&
147 + echo "100644 $B 3 file"
148 + } | git update-index --index-info &&
149 + echo nothing >file &&
150 + git restore --worktree --merge file &&
151 + cat >expect <<-\EOF &&
152 + <<<<<<< ours
153 + ourside
154 + =======
155 + theirside
156 + >>>>>>> theirs
157 + EOF
158 + test_cmp expect file
159 +'
160 +
161 +test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
162 + O=$(echo original | git hash-object -w --stdin) &&
163 + A=$(echo ourside | git hash-object -w --stdin) &&
164 + B=$(echo theirside | git hash-object -w --stdin) &&
165 + {
166 + echo "100644 $O 1 file" &&
167 + echo "100644 $A 2 file" &&
168 + echo "100644 $B 3 file"
169 + } | git update-index --index-info &&
170 + echo nothing >file &&
171 + git add file &&
172 + git restore --worktree --merge file &&
173 + cat >expect <<-\EOF &&
174 + <<<<<<< ours
175 + ourside
176 + =======
177 + theirside
178 + >>>>>>> theirs
179 + EOF
180 + test_cmp expect file
181 +'
182 +
183 +test_expect_failure 'restore --merge to unresolve after (mistaken) resolution' '
184 + O=$(echo original | git hash-object -w --stdin) &&
185 + A=$(echo ourside | git hash-object -w --stdin) &&
186 + B=$(echo theirside | git hash-object -w --stdin) &&
187 + {
188 + echo "100644 $O 1 file" &&
189 + echo "100644 $A 2 file" &&
190 + echo "100644 $B 3 file"
191 + } | git update-index --index-info &&
192 + git rm -f file &&
193 + git restore --worktree --merge file &&
194 + cat >expect <<-\EOF &&
195 + <<<<<<< ours
196 + ourside
197 + =======
198 + theirside
199 + >>>>>>> theirs
200 + EOF
201 + test_cmp expect file
202 +'
203 +
204 test_expect_success 'restore with merge options are incompatible with certain options' '
205 for opts in \
206 "--staged --ours" \
t/t7201-co.sh
+42
@@ -522,6 +522,48 @@ test_expect_success 'checkout with --merge' '
522 test_cmp merged file
523 '
524
525 +test_expect_success 'checkout -m works after (mistaken) resolution' '
526 + setup_conflicting_index &&
527 + echo "none of the above" >sample &&
528 + cat sample >fild &&
529 + cat sample >file &&
530 + cat sample >filf &&
531 + # resolve to something
532 + git add file &&
533 + git checkout --merge -- fild file filf &&
534 + {
535 + echo "<<<<<<< ours" &&
536 + echo ourside &&
537 + echo "=======" &&
538 + echo theirside &&
539 + echo ">>>>>>> theirs"
540 + } >merged &&
541 + test_cmp expect fild &&
542 + test_cmp expect filf &&
543 + test_cmp merged file
544 +'
545 +
546 +test_expect_failure 'checkout -m works after (mistaken) resolution to remove' '
547 + setup_conflicting_index &&
548 + echo "none of the above" >sample &&
549 + cat sample >fild &&
550 + cat sample >file &&
551 + cat sample >filf &&
552 + # resolve to remove
553 + git rm file &&
554 + git checkout --merge -- fild file filf &&
555 + {
556 + echo "<<<<<<< ours" &&
557 + echo ourside &&
558 + echo "=======" &&
559 + echo theirside &&
560 + echo ">>>>>>> theirs"
561 + } >merged &&
562 + test_cmp expect fild &&
563 + test_cmp expect filf &&
564 + test_cmp merged file
565 +'
566 +
567 test_expect_success 'checkout with --merge, in diff3 -m style' '
568 git config merge.conflictstyle diff3 &&
569 setup_conflicting_index &&