Debian Codegraph @

lib/tests/test_conflicts.rs

raw
1 // Copyright 2021 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use indoc::indoc;
16 use itertools::Itertools as _;
17 use jj_lib::backend::FileId;
18 use jj_lib::conflict_labels::ConflictLabels;
19 use jj_lib::conflicts::ConflictMarkerStyle;
20 use jj_lib::conflicts::ConflictMaterializeOptions;
21 use jj_lib::conflicts::MIN_CONFLICT_MARKER_LEN;
22 use jj_lib::conflicts::choose_materialized_conflict_marker_len;
23 use jj_lib::conflicts::extract_as_single_hunk;
24 use jj_lib::conflicts::materialize_merge_result_to_bytes;
25 use jj_lib::conflicts::parse_conflict;
26 use jj_lib::conflicts::update_from_content;
27 use jj_lib::files::FileMergeHunkLevel;
28 use jj_lib::merge::Merge;
29 use jj_lib::merge::SameChange;
30 use jj_lib::repo::Repo as _;
31 use jj_lib::repo_path::RepoPath;
32 use jj_lib::store::Store;
33 use jj_lib::tree_merge::MergeOptions;
34 use pollster::FutureExt as _;
35 use test_case::test_case;
36 use testutils::TestRepo;
37 use testutils::TestResult;
38 use testutils::read_file;
39 use testutils::repo_path;
40
41 #[test]
42 fn test_materialize_conflict_basic() {
43 let test_repo = TestRepo::init();
44 let store = test_repo.repo.store();
45
46 let path = repo_path("file");
47 let base_id = testutils::write_file(
48 store,
49 path,
50 indoc! {"
51 line 1
52 line 2
53 line 3
54 line 4
55 line 5
56 "},
57 );
58 let left_id = testutils::write_file(
59 store,
60 path,
61 indoc! {"
62 line 1
63 line 2
64 left 3.1
65 left 3.2
66 left 3.3
67 line 4
68 line 5
69 "},
70 );
71 let right_id = testutils::write_file(
72 store,
73 path,
74 indoc! {"
75 line 1
76 line 2
77 right 3.1
78 line 4
79 line 5
80 "},
81 );
82
83 // The left side should come first. The diff should be use the smaller (right)
84 // side, and the left side should be a snapshot.
85 let conflict = Merge::from_removes_adds(
86 vec![Some(base_id.clone())],
87 vec![Some(left_id.clone()), Some(right_id.clone())],
88 );
89 insta::assert_snapshot!(
90 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
91 @r"
92 line 1
93 line 2
94 <<<<<<< conflict 1 of 1
95 +++++++ side #1
96 left 3.1
97 left 3.2
98 left 3.3
99 %%%%%%% diff from: base
100 \\\\\\\ to: side #2
101 -line 3
102 +right 3.1
103 >>>>>>> conflict 1 of 1 ends
104 line 4
105 line 5
106 "
107 );
108 // Swap the positive terms in the conflict. The diff should still use the right
109 // side, but now the right side should come first.
110 let conflict = Merge::from_removes_adds(
111 vec![Some(base_id.clone())],
112 vec![Some(right_id.clone()), Some(left_id.clone())],
113 );
114 insta::assert_snapshot!(
115 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
116 @r"
117 line 1
118 line 2
119 <<<<<<< conflict 1 of 1
120 %%%%%%% diff from: base
121 \\\\\\\ to: side #1
122 -line 3
123 +right 3.1
124 +++++++ side #2
125 left 3.1
126 left 3.2
127 left 3.3
128 >>>>>>> conflict 1 of 1 ends
129 line 4
130 line 5
131 "
132 );
133 // Test materializing "snapshot" conflict markers
134 let conflict = Merge::from_removes_adds(
135 vec![Some(base_id.clone())],
136 vec![Some(left_id.clone()), Some(right_id.clone())],
137 );
138 insta::assert_snapshot!(
139 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Snapshot),
140 @"
141 line 1
142 line 2
143 <<<<<<< conflict 1 of 1
144 +++++++ side #1
145 left 3.1
146 left 3.2
147 left 3.3
148 ------- base
149 line 3
150 +++++++ side #2
151 right 3.1
152 >>>>>>> conflict 1 of 1 ends
153 line 4
154 line 5
155 "
156 );
157 // Test materializing "git" conflict markers
158 let conflict = Merge::from_removes_adds(
159 vec![Some(base_id.clone())],
160 vec![Some(left_id.clone()), Some(right_id.clone())],
161 );
162 insta::assert_snapshot!(
163 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Git),
164 @"
165 line 1
166 line 2
167 <<<<<<< side #1
168 left 3.1
169 left 3.2
170 left 3.3
171 ||||||| base
172 line 3
173 =======
174 right 3.1
175 >>>>>>> side #2
176 line 4
177 line 5
178 "
179 );
180 }
181
182 #[test]
183 fn test_materialize_conflict_three_sides() {
184 let test_repo = TestRepo::init();
185 let store = test_repo.repo.store();
186
187 let path = repo_path("file");
188 let base_1_id = testutils::write_file(
189 store,
190 path,
191 indoc! {"
192 line 1
193 line 2 base
194 line 3 base
195 line 4 base
196 line 5
197 "},
198 );
199 let base_2_id = testutils::write_file(
200 store,
201 path,
202 indoc! {"
203 line 1
204 line 2 base
205 line 5
206 "},
207 );
208 let a_id = testutils::write_file(
209 store,
210 path,
211 indoc! {"
212 line 1
213 line 2 a.1
214 line 3 a.2
215 line 4 base
216 line 5
217 "},
218 );
219 let b_id = testutils::write_file(
220 store,
221 path,
222 indoc! {"
223 line 1
224 line 2 b.1
225 line 3 base
226 line 4 b.2
227 line 5
228 "},
229 );
230 let c_id = testutils::write_file(
231 store,
232 path,
233 indoc! {"
234 line 1
235 line 2 base
236 line 3 c.2
237 line 5
238 "},
239 );
240
241 let conflict = Merge::from_removes_adds(
242 vec![Some(base_1_id.clone()), Some(base_2_id.clone())],
243 vec![Some(a_id.clone()), Some(b_id.clone()), Some(c_id.clone())],
244 );
245 // Test materializing "diff" conflict markers
246 insta::assert_snapshot!(
247 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
248 @r"
249 line 1
250 <<<<<<< conflict 1 of 1
251 %%%%%%% diff from: base #1
252 \\\\\\\ to: side #1
253 -line 2 base
254 -line 3 base
255 +line 2 a.1
256 +line 3 a.2
257 line 4 base
258 +++++++ side #2
259 line 2 b.1
260 line 3 base
261 line 4 b.2
262 %%%%%%% diff from: base #2
263 \\\\\\\ to: side #3
264 line 2 base
265 +line 3 c.2
266 >>>>>>> conflict 1 of 1 ends
267 line 5
268 "
269 );
270 // Test materializing "diff-experimental" conflict markers
271 insta::assert_snapshot!(
272 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::DiffExperimental),
273 @r"
274 line 1
275 <<<<<<< conflict 1 of 1
276 +++++++ side #1
277 line 2 a.1
278 line 3 a.2
279 line 4 base
280 %%%%%%% diff from: base #1
281 \\\\\\\ to: side #2
282 -line 2 base
283 +line 2 b.1
284 line 3 base
285 -line 4 base
286 +line 4 b.2
287 %%%%%%% diff from: base #2
288 \\\\\\\ to: side #3
289 line 2 base
290 +line 3 c.2
291 >>>>>>> conflict 1 of 1 ends
292 line 5
293 "
294 );
295 // Test materializing "snapshot" conflict markers
296 insta::assert_snapshot!(
297 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Snapshot),
298 @"
299 line 1
300 <<<<<<< conflict 1 of 1
301 +++++++ side #1
302 line 2 a.1
303 line 3 a.2
304 line 4 base
305 ------- base #1
306 line 2 base
307 line 3 base
308 line 4 base
309 +++++++ side #2
310 line 2 b.1
311 line 3 base
312 line 4 b.2
313 ------- base #2
314 line 2 base
315 +++++++ side #3
316 line 2 base
317 line 3 c.2
318 >>>>>>> conflict 1 of 1 ends
319 line 5
320 "
321 );
322 // Test materializing "git" conflict markers (falls back to "snapshot" since
323 // "git" conflict markers don't support more than 2 sides)
324 insta::assert_snapshot!(
325 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Git),
326 @"
327 line 1
328 <<<<<<< conflict 1 of 1
329 +++++++ side #1
330 line 2 a.1
331 line 3 a.2
332 line 4 base
333 ------- base #1
334 line 2 base
335 line 3 base
336 line 4 base
337 +++++++ side #2
338 line 2 b.1
339 line 3 base
340 line 4 b.2
341 ------- base #2
342 line 2 base
343 +++++++ side #3
344 line 2 base
345 line 3 c.2
346 >>>>>>> conflict 1 of 1 ends
347 line 5
348 "
349 );
350 }
351
352 #[test]
353 fn test_materialize_conflict_multi_rebase_conflicts() {
354 let test_repo = TestRepo::init();
355 let store = test_repo.repo.store();
356
357 // Create changes (a, b, c) on top of the base, and linearize them.
358 let path = repo_path("file");
359 let base_id = testutils::write_file(
360 store,
361 path,
362 indoc! {"
363 line 1
364 line 2 base
365 line 3
366 "},
367 );
368 let a_id = testutils::write_file(
369 store,
370 path,
371 indoc! {"
372 line 1
373 line 2 a.1
374 line 2 a.2
375 line 2 a.3
376 line 3
377 "},
378 );
379 let b_id = testutils::write_file(
380 store,
381 path,
382 indoc! {"
383 line 1
384 line 2 b.1
385 line 2 b.2
386 line 3
387 "},
388 );
389 let c_id = testutils::write_file(
390 store,
391 path,
392 indoc! {"
393 line 1
394 line 2 c.1
395 line 3
396 "},
397 );
398
399 // The order of (a, b, c) should be preserved. For all cases, the "a" side
400 // should be a snapshot.
401 let conflict = Merge::from_removes_adds(
402 vec![Some(base_id.clone()), Some(base_id.clone())],
403 vec![Some(a_id.clone()), Some(b_id.clone()), Some(c_id.clone())],
404 );
405 insta::assert_snapshot!(
406 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
407 @r"
408 line 1
409 <<<<<<< conflict 1 of 1
410 +++++++ side #1
411 line 2 a.1
412 line 2 a.2
413 line 2 a.3
414 %%%%%%% diff from: base #1
415 \\\\\\\ to: side #2
416 -line 2 base
417 +line 2 b.1
418 +line 2 b.2
419 %%%%%%% diff from: base #2
420 \\\\\\\ to: side #3
421 -line 2 base
422 +line 2 c.1
423 >>>>>>> conflict 1 of 1 ends
424 line 3
425 "
426 );
427 let conflict = Merge::from_removes_adds(
428 vec![Some(base_id.clone()), Some(base_id.clone())],
429 vec![Some(c_id.clone()), Some(b_id.clone()), Some(a_id.clone())],
430 );
431 insta::assert_snapshot!(
432 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
433 @r"
434 line 1
435 <<<<<<< conflict 1 of 1
436 %%%%%%% diff from: base #1
437 \\\\\\\ to: side #1
438 -line 2 base
439 +line 2 c.1
440 %%%%%%% diff from: base #2
441 \\\\\\\ to: side #2
442 -line 2 base
443 +line 2 b.1
444 +line 2 b.2
445 +++++++ side #3
446 line 2 a.1
447 line 2 a.2
448 line 2 a.3
449 >>>>>>> conflict 1 of 1 ends
450 line 3
451 "
452 );
453 let conflict = Merge::from_removes_adds(
454 vec![Some(base_id.clone()), Some(base_id.clone())],
455 vec![Some(c_id.clone()), Some(a_id.clone()), Some(b_id.clone())],
456 );
457 insta::assert_snapshot!(
458 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
459 @r"
460 line 1
461 <<<<<<< conflict 1 of 1
462 %%%%%%% diff from: base #1
463 \\\\\\\ to: side #1
464 -line 2 base
465 +line 2 c.1
466 +++++++ side #2
467 line 2 a.1
468 line 2 a.2
469 line 2 a.3
470 %%%%%%% diff from: base #2
471 \\\\\\\ to: side #3
472 -line 2 base
473 +line 2 b.1
474 +line 2 b.2
475 >>>>>>> conflict 1 of 1 ends
476 line 3
477 "
478 );
479 }
480
481 // TODO: With options
482 #[test]
483 fn test_materialize_parse_roundtrip() {
484 let test_repo = TestRepo::init();
485 let store = test_repo.repo.store();
486
487 let path = repo_path("file");
488 let base_id = testutils::write_file(
489 store,
490 path,
491 indoc! {"
492 line 1
493 line 2
494 line 3
495 line 4
496 line 5
497 "},
498 );
499 let left_id = testutils::write_file(
500 store,
501 path,
502 indoc! {"
503 line 1 left
504 line 2 left
505 line 3
506 line 4
507 line 5 left
508 "},
509 );
510 let right_id = testutils::write_file(
511 store,
512 path,
513 indoc! {"
514 line 1 right
515 line 2
516 line 3
517 line 4 right
518 line 5 right
519 "},
520 );
521
522 let conflict = Merge::from_removes_adds(
523 vec![Some(base_id.clone())],
524 vec![Some(left_id.clone()), Some(right_id.clone())],
525 );
526 let materialized =
527 materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
528 insta::assert_snapshot!(
529 materialized,
530 @r"
531 <<<<<<< conflict 1 of 2
532 +++++++ side #1
533 line 1 left
534 line 2 left
535 %%%%%%% diff from: base
536 \\\\\\\ to: side #2
537 -line 1
538 +line 1 right
539 line 2
540 >>>>>>> conflict 1 of 2 ends
541 line 3
542 <<<<<<< conflict 2 of 2
543 %%%%%%% diff from: base
544 \\\\\\\ to: side #1
545 line 4
546 -line 5
547 +line 5 left
548 +++++++ side #2
549 line 4 right
550 line 5 right
551 >>>>>>> conflict 2 of 2 ends
552 "
553 );
554
555 // The first add should always be from the left side
556 insta::assert_debug_snapshot!(
557 parse_conflict(materialized.as_bytes(), conflict.num_sides(), MIN_CONFLICT_MARKER_LEN),
558 @r#"
559 Some(
560 [
561 Conflicted(
562 [
563 "line 1 left\nline 2 left\n",
564 "line 1\nline 2\n",
565 "line 1 right\nline 2\n",
566 ],
567 ),
568 Resolved(
569 "line 3\n",
570 ),
571 Conflicted(
572 [
573 "line 4\nline 5 left\n",
574 "line 4\nline 5\n",
575 "line 4 right\nline 5 right\n",
576 ],
577 ),
578 ],
579 )
580 "#);
581 }
582
583 #[test_case(ConflictMarkerStyle::Diff)]
584 #[test_case(ConflictMarkerStyle::Snapshot)]
585 #[test_case(ConflictMarkerStyle::Git)]
586 fn test_materialize_update_roundtrip(style: ConflictMarkerStyle) -> TestResult {
587 let test_repo = TestRepo::init();
588 let store = test_repo.repo.store();
589
590 let path = repo_path("file");
591 let base_id = testutils::write_file(
592 store,
593 path,
594 indoc! {"
595 line 1
596 line 2 base
597 line 3 base
598 line 4 base
599 line 5
600 "},
601 );
602 let a_id = testutils::write_file(
603 store,
604 path,
605 indoc! {"
606 line 1
607 line 2 a.1
608 line 3 a.2
609 line 4 base
610 line 5
611 "},
612 );
613 let b_id = testutils::write_file(
614 store,
615 path,
616 indoc! {"
617 line 1
618 line 2 b.1
619 line 3 base
620 line 4 b.2
621 line 5
622 "},
623 );
624
625 let conflict = Merge::from_removes_adds(
626 vec![Some(base_id.clone())],
627 vec![Some(a_id.clone()), Some(b_id.clone())],
628 );
629
630 let materialized = materialize_conflict_string(store, path, &conflict, style);
631 let parsed = update_from_content(
632 &conflict,
633 store,
634 path,
635 materialized.as_bytes(),
636 MIN_CONFLICT_MARKER_LEN,
637 )
638 .block_on()?;
639
640 assert_eq!(parsed, conflict);
641 Ok(())
642 }
643
644 #[test]
645 fn test_materialize_conflict_no_newlines_at_eof() {
646 let test_repo = TestRepo::init();
647 let store = test_repo.repo.store();
648
649 let path = repo_path("file");
650 let base_id = testutils::write_file(store, path, "base");
651 let left_empty_id = testutils::write_file(store, path, "");
652 let right_id = testutils::write_file(store, path, "right");
653
654 let conflict = Merge::from_removes_adds(
655 vec![Some(base_id.clone())],
656 vec![Some(left_empty_id.clone()), Some(right_id.clone())],
657 );
658 let materialized =
659 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
660 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
661 @r"
662 <<<<<<< conflict 1 of 1
663 %%%%%%% diff from: base (no terminating newline)
664 \\\\\\\ to: side #1
665 -base
666 +
667 +++++++ side #2 (no terminating newline)
668 right
669 >>>>>>> conflict 1 of 1 ends[EOF]
670 "
671 );
672 // The conflict markers are parsed with the trailing newline, but it is removed
673 // by `update_from_content`
674 insta::assert_debug_snapshot!(
675 parse_conflict(
676 materialized.as_bytes(),
677 conflict.num_sides(),
678 MIN_CONFLICT_MARKER_LEN
679 ),
680 @r#"
681 Some(
682 [
683 Conflicted(
684 [
685 "",
686 "base",
687 "right",
688 ],
689 ),
690 ],
691 )
692 "#);
693 }
694
695 #[test]
696 fn test_materialize_conflict_modify_delete() {
697 let test_repo = TestRepo::init();
698 let store = test_repo.repo.store();
699
700 let path = repo_path("file");
701 let base_id = testutils::write_file(
702 store,
703 path,
704 indoc! {"
705 line 1
706 line 2
707 line 3
708 line 4
709 line 5
710 "},
711 );
712 let modified_id = testutils::write_file(
713 store,
714 path,
715 indoc! {"
716 line 1
717 line 2
718 modified
719 line 4
720 line 5
721 "},
722 );
723 let deleted_id = testutils::write_file(
724 store,
725 path,
726 indoc! {"
727 line 1
728 line 2
729 line 4
730 line 5
731 "},
732 );
733
734 // left modifies a line, right deletes the same line.
735 let conflict = Merge::from_removes_adds(
736 vec![Some(base_id.clone())],
737 vec![Some(modified_id.clone()), Some(deleted_id.clone())],
738 );
739 insta::assert_snapshot!(&materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff), @r"
740 line 1
741 line 2
742 <<<<<<< conflict 1 of 1
743 +++++++ side #1
744 modified
745 %%%%%%% diff from: base
746 \\\\\\\ to: side #2
747 -line 3
748 >>>>>>> conflict 1 of 1 ends
749 line 4
750 line 5
751 "
752 );
753
754 // right modifies a line, left deletes the same line.
755 let conflict = Merge::from_removes_adds(
756 vec![Some(base_id.clone())],
757 vec![Some(deleted_id.clone()), Some(modified_id.clone())],
758 );
759 insta::assert_snapshot!(&materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff), @r"
760 line 1
761 line 2
762 <<<<<<< conflict 1 of 1
763 %%%%%%% diff from: base
764 \\\\\\\ to: side #1
765 -line 3
766 +++++++ side #2
767 modified
768 >>>>>>> conflict 1 of 1 ends
769 line 4
770 line 5
771 "
772 );
773
774 // modify/delete conflict at the file level
775 let conflict = Merge::from_removes_adds(
776 vec![Some(base_id.clone())],
777 vec![Some(modified_id.clone()), None],
778 );
779 insta::assert_snapshot!(&materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff), @r"
780 <<<<<<< conflict 1 of 1
781 %%%%%%% diff from: base
782 \\\\\\\ to: side #1
783 line 1
784 line 2
785 -line 3
786 +modified
787 line 4
788 line 5
789 +++++++ side #2
790 >>>>>>> conflict 1 of 1 ends
791 "
792 );
793 }
794
795 #[test]
796 fn test_materialize_conflict_two_forward_diffs() {
797 let test_repo = TestRepo::init();
798 let store = test_repo.repo.store();
799
800 // Create conflict A-B+B-C+D-E+C. This is designed to tempt the algorithm to
801 // produce a negative snapshot at the end like this:
802 // <<<<
803 // ====
804 // A
805 // %%%%
806 // B
807 // ++++
808 // D
809 // %%%%
810 // C
811 // ----
812 // E
813 // >>>>
814 let path = repo_path("file");
815 let a_id = testutils::write_file(store, path, "A\n");
816 let b_id = testutils::write_file(store, path, "B\n");
817 let c_id = testutils::write_file(store, path, "C\n");
818 let d_id = testutils::write_file(store, path, "D\n");
819 let e_id = testutils::write_file(store, path, "E\n");
820
821 let conflict = Merge::from_removes_adds(
822 vec![Some(b_id.clone()), Some(c_id.clone()), Some(e_id.clone())],
823 vec![
824 Some(a_id.clone()),
825 Some(b_id.clone()),
826 Some(d_id.clone()),
827 Some(c_id.clone()),
828 ],
829 );
830 // The materialized conflict should still have exactly one snapshot despite our
831 // attempted temptation.
832 insta::assert_snapshot!(
833 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff),
834 @r"
835 <<<<<<< conflict 1 of 1
836 +++++++ side #1
837 A
838 %%%%%%% diff from: base #1
839 \\\\\\\ to: side #2
840 B
841 %%%%%%% diff from: base #2
842 \\\\\\\ to: side #3
843 -C
844 +D
845 %%%%%%% diff from: base #3
846 \\\\\\\ to: side #4
847 -E
848 +C
849 >>>>>>> conflict 1 of 1 ends
850 "
851 );
852 }
853
854 #[test]
855 fn test_materialize_conflict_with_labels() {
856 let test_repo = TestRepo::init();
857 let store = test_repo.repo.store();
858
859 let path = repo_path("file");
860 let side1 = testutils::write_file(store, path, "side 1\n");
861 let base1 = testutils::write_file(store, path, "base 1\n");
862 let side2 = testutils::write_file(store, path, "side 2\n");
863
864 let conflict = Merge::from_vec(vec![Some(side1), Some(base1), Some(side2)]);
865 let conflict_labels = ConflictLabels::from_vec(vec![
866 "side 1 conflict label".into(),
867 "base conflict label".into(),
868 "side 2 conflict label".into(),
869 ]);
870 insta::assert_snapshot!(
871 &materialize_conflict_string_with_labels(
872 store,
873 path,
874 &conflict,
875 &conflict_labels,
876 ConflictMarkerStyle::Diff,
877 ),
878 @r"
879 <<<<<<< conflict 1 of 1
880 %%%%%%% diff from: base conflict label
881 \\\\\\\ to: side 1 conflict label
882 -base 1
883 +side 1
884 +++++++ side 2 conflict label
885 side 2
886 >>>>>>> conflict 1 of 1 ends
887 "
888 );
889
890 insta::assert_snapshot!(
891 &materialize_conflict_string_with_labels(
892 store,
893 path,
894 &conflict,
895 &conflict_labels,
896 ConflictMarkerStyle::Snapshot,
897 ),
898 @"
899 <<<<<<< conflict 1 of 1
900 +++++++ side 1 conflict label
901 side 1
902 ------- base conflict label
903 base 1
904 +++++++ side 2 conflict label
905 side 2
906 >>>>>>> conflict 1 of 1 ends
907 "
908 );
909
910 insta::assert_snapshot!(
911 &materialize_conflict_string_with_labels(
912 store,
913 path,
914 &conflict,
915 &conflict_labels,
916 ConflictMarkerStyle::Git,
917 ),
918 @"
919 <<<<<<< side 1 conflict label
920 side 1
921 ||||||| base conflict label
922 base 1
923 =======
924 side 2
925 >>>>>>> side 2 conflict label
926 "
927 );
928 }
929
930 #[test]
931 fn test_parse_conflict_resolved() {
932 assert_eq!(
933 parse_conflict(
934 indoc! {b"
935 line 1
936 line 2
937 line 3
938 line 4
939 line 5
940 "},
941 2,
942 7
943 ),
944 None
945 );
946 }
947
948 #[test]
949 fn test_parse_conflict_simple() {
950 insta::assert_debug_snapshot!(
951 parse_conflict(indoc! {b"
952 line 1
953 <<<<<<<
954 %%%%%%%
955 line 2
956 -line 3
957 +left
958 line 4
959 +++++++
960 right
961 >>>>>>>
962 line 5
963 "},
964 2,
965 7
966 ),
967 @r#"
968 Some(
969 [
970 Resolved(
971 "line 1\n",
972 ),
973 Conflicted(
974 [
975 "line 2\nleft\nline 4\n",
976 "line 2\nline 3\nline 4\n",
977 "right\n",
978 ],
979 ),
980 Resolved(
981 "line 5\n",
982 ),
983 ],
984 )
985 "#
986 );
987 insta::assert_debug_snapshot!(
988 parse_conflict(indoc! {b"
989 line 1
990 <<<<<<< Text
991 %%%%%%% Different text
992 line 2
993 -line 3
994 +left
995 line 4
996 +++++++ Yet <><>< more text
997 right
998 >>>>>>> More and more text
999 line 5
1000 "},
1001 2,
1002 7
1003 ),
1004 @r#"
1005 Some(
1006 [
1007 Resolved(
1008 "line 1\n",
1009 ),
1010 Conflicted(
1011 [
1012 "line 2\nleft\nline 4\n",
1013 "line 2\nline 3\nline 4\n",
1014 "right\n",
1015 ],
1016 ),
1017 Resolved(
1018 "line 5\n",
1019 ),
1020 ],
1021 )
1022 "#
1023 );
1024 // Test "snapshot" style
1025 insta::assert_debug_snapshot!(
1026 parse_conflict(indoc! {b"
1027 line 1
1028 <<<<<<< Random text
1029 +++++++ Random text
1030 line 3.1
1031 line 3.2
1032 ------- Random text
1033 line 3
1034 line 4
1035 +++++++ Random text
1036 line 3
1037 line 4.1
1038 >>>>>>> Random text
1039 line 5
1040 "},
1041 2,
1042 7
1043 ),
1044 @r#"
1045 Some(
1046 [
1047 Resolved(
1048 "line 1\n",
1049 ),
1050 Conflicted(
1051 [
1052 "line 3.1\nline 3.2\n",
1053 "line 3\nline 4\n",
1054 "line 3\nline 4.1\n",
1055 ],
1056 ),
1057 Resolved(
1058 "line 5\n",
1059 ),
1060 ],
1061 )
1062 "#
1063 );
1064 // Test "snapshot" style with reordered sections
1065 insta::assert_debug_snapshot!(
1066 parse_conflict(indoc! {b"
1067 line 1
1068 <<<<<<< Random text
1069 ------- Random text
1070 line 3
1071 line 4
1072 +++++++ Random text
1073 line 3.1
1074 line 3.2
1075 +++++++ Random text
1076 line 3
1077 line 4.1
1078 >>>>>>> Random text
1079 line 5
1080 "},
1081 2,
1082 7
1083 ),
1084 @r#"
1085 Some(
1086 [
1087 Resolved(
1088 "line 1\n",
1089 ),
1090 Conflicted(
1091 [
1092 "line 3.1\nline 3.2\n",
1093 "line 3\nline 4\n",
1094 "line 3\nline 4.1\n",
1095 ],
1096 ),
1097 Resolved(
1098 "line 5\n",
1099 ),
1100 ],
1101 )
1102 "#
1103 );
1104 // Test "git" style
1105 insta::assert_debug_snapshot!(
1106 parse_conflict(indoc! {b"
1107 line 1
1108 <<<<<<< Side #1
1109 line 3.1
1110 line 3.2
1111 ||||||| Base
1112 line 3
1113 line 4
1114 ======= Side #2
1115 line 3
1116 line 4.1
1117 >>>>>>> End
1118 line 5
1119 "},
1120 2,
1121 7
1122 ),
1123 @r#"
1124 Some(
1125 [
1126 Resolved(
1127 "line 1\n",
1128 ),
1129 Conflicted(
1130 [
1131 "line 3.1\nline 3.2\n",
1132 "line 3\nline 4\n",
1133 "line 3\nline 4.1\n",
1134 ],
1135 ),
1136 Resolved(
1137 "line 5\n",
1138 ),
1139 ],
1140 )
1141 "#
1142 );
1143 // Test "git" style with empty side 1
1144 insta::assert_debug_snapshot!(
1145 parse_conflict(indoc! {b"
1146 line 1
1147 <<<<<<< Side #1
1148 ||||||| Base
1149 line 3
1150 line 4
1151 ======= Side #2
1152 line 3.1
1153 line 4.1
1154 >>>>>>> End
1155 line 5
1156 "},
1157 2,
1158 7
1159 ),
1160 @r#"
1161 Some(
1162 [
1163 Resolved(
1164 "line 1\n",
1165 ),
1166 Conflicted(
1167 [
1168 "",
1169 "line 3\nline 4\n",
1170 "line 3.1\nline 4.1\n",
1171 ],
1172 ),
1173 Resolved(
1174 "line 5\n",
1175 ),
1176 ],
1177 )
1178 "#
1179 );
1180 // The conflict markers are longer than the originally materialized markers, but
1181 // we allow them to parse anyway
1182 insta::assert_debug_snapshot!(
1183 parse_conflict(indoc! {b"
1184 line 1
1185 <<<<<<<<<<<
1186 %%%%%%%%%%%
1187 line 2
1188 -line 3
1189 +left
1190 line 4
1191 +++++++++++
1192 right
1193 >>>>>>>>>>>
1194 line 5
1195 "},
1196 2,
1197 7
1198 ),
1199 @r#"
1200 Some(
1201 [
1202 Resolved(
1203 "line 1\n",
1204 ),
1205 Conflicted(
1206 [
1207 "line 2\nleft\nline 4\n",
1208 "line 2\nline 3\nline 4\n",
1209 "right\n",
1210 ],
1211 ),
1212 Resolved(
1213 "line 5\n",
1214 ),
1215 ],
1216 )
1217 "#
1218 );
1219 }
1220
1221 #[test]
1222 fn test_parse_conflict_multi_way() {
1223 insta::assert_debug_snapshot!(
1224 parse_conflict(
1225 indoc! {b"
1226 line 1
1227 <<<<<<<
1228 %%%%%%%
1229 line 2
1230 -line 3
1231 +left
1232 line 4
1233 +++++++
1234 right
1235 %%%%%%%
1236 line 2
1237 +forward
1238 line 3
1239 line 4
1240 >>>>>>>
1241 line 5
1242 "},
1243 3,
1244 7
1245 ),
1246 @r#"
1247 Some(
1248 [
1249 Resolved(
1250 "line 1\n",
1251 ),
1252 Conflicted(
1253 [
1254 "line 2\nleft\nline 4\n",
1255 "line 2\nline 3\nline 4\n",
1256 "right\n",
1257 "line 2\nline 3\nline 4\n",
1258 "line 2\nforward\nline 3\nline 4\n",
1259 ],
1260 ),
1261 Resolved(
1262 "line 5\n",
1263 ),
1264 ],
1265 )
1266 "#
1267 );
1268 insta::assert_debug_snapshot!(
1269 parse_conflict(indoc! {b"
1270 line 1
1271 <<<<<<< Random text
1272 %%%%%%% Random text
1273 line 2
1274 -line 3
1275 +left
1276 line 4
1277 +++++++ Random text
1278 right
1279 %%%%%%% Random text
1280 line 2
1281 +forward
1282 line 3
1283 line 4
1284 >>>>>>> Random text
1285 line 5
1286 "},
1287 3,
1288 7
1289 ),
1290 @r#"
1291 Some(
1292 [
1293 Resolved(
1294 "line 1\n",
1295 ),
1296 Conflicted(
1297 [
1298 "line 2\nleft\nline 4\n",
1299 "line 2\nline 3\nline 4\n",
1300 "right\n",
1301 "line 2\nline 3\nline 4\n",
1302 "line 2\nforward\nline 3\nline 4\n",
1303 ],
1304 ),
1305 Resolved(
1306 "line 5\n",
1307 ),
1308 ],
1309 )
1310 "#
1311 );
1312 // Test "snapshot" style
1313 insta::assert_debug_snapshot!(
1314 parse_conflict(indoc! {b"
1315 line 1
1316 <<<<<<< Random text
1317 +++++++ Random text
1318 line 3.1
1319 line 3.2
1320 +++++++ Random text
1321 line 3
1322 line 4.1
1323 ------- Random text
1324 line 3
1325 line 4
1326 ------- Random text
1327 line 3
1328 +++++++ Random text
1329 line 3
1330 line 4
1331 >>>>>>> Random text
1332 line 5
1333 "},
1334 3,
1335 7
1336 ),
1337 @r#"
1338 Some(
1339 [
1340 Resolved(
1341 "line 1\n",
1342 ),
1343 Conflicted(
1344 [
1345 "line 3.1\nline 3.2\n",
1346 "line 3\nline 4\n",
1347 "line 3\nline 4.1\n",
1348 "line 3\n",
1349 "line 3\nline 4\n",
1350 ],
1351 ),
1352 Resolved(
1353 "line 5\n",
1354 ),
1355 ],
1356 )
1357 "#
1358 );
1359 }
1360
1361 #[test]
1362 fn test_parse_conflict_crlf_markers() {
1363 // Conflict markers should be recognized even with CRLF
1364 insta::assert_debug_snapshot!(
1365 parse_conflict(
1366 indoc! {b"
1367 line 1\r
1368 <<<<<<<\r
1369 +++++++\r
1370 left\r
1371 -------\r
1372 base\r
1373 +++++++\r
1374 right\r
1375 >>>>>>>\r
1376 line 5\r
1377 "},
1378 2,
1379 7
1380 ),
1381 @r#"
1382 Some(
1383 [
1384 Resolved(
1385 "line 1\r\n",
1386 ),
1387 Conflicted(
1388 [
1389 "left\r\n",
1390 "base\r\n",
1391 "right\r\n",
1392 ],
1393 ),
1394 Resolved(
1395 "line 5\r\n",
1396 ),
1397 ],
1398 )
1399 "#
1400 );
1401 }
1402
1403 #[test]
1404 fn test_parse_conflict_diff_stripped_whitespace() {
1405 // Should be able to parse conflict even if diff contains empty line (without
1406 // even a leading space, which is sometimes stripped by text editors)
1407 insta::assert_debug_snapshot!(
1408 parse_conflict(
1409 indoc! {b"
1410 line 1
1411 <<<<<<<
1412 %%%%%%%
1413 line 2
1414
1415 -line 3
1416 +left
1417 \r
1418 line 4
1419 +++++++
1420 right
1421 >>>>>>>
1422 line 5
1423 "},
1424 2,
1425 7
1426 ),
1427 @r#"
1428 Some(
1429 [
1430 Resolved(
1431 "line 1\n",
1432 ),
1433 Conflicted(
1434 [
1435 "line 2\n\nleft\n\r\nline 4\n",
1436 "line 2\n\nline 3\n\r\nline 4\n",
1437 "right\n",
1438 ],
1439 ),
1440 Resolved(
1441 "line 5\n",
1442 ),
1443 ],
1444 )
1445 "#
1446 );
1447 }
1448
1449 #[test]
1450 fn test_parse_conflict_wrong_arity() {
1451 // Valid conflict marker but it has fewer sides than the caller expected
1452 assert_eq!(
1453 parse_conflict(
1454 indoc! {b"
1455 line 1
1456 <<<<<<<
1457 %%%%%%%
1458 line 2
1459 -line 3
1460 +left
1461 line 4
1462 +++++++
1463 right
1464 >>>>>>>
1465 line 5
1466 "},
1467 3,
1468 7
1469 ),
1470 None
1471 );
1472 }
1473
1474 #[test]
1475 fn test_parse_conflict_malformed_missing_removes() {
1476 // Right number of adds but missing removes
1477 assert_eq!(
1478 parse_conflict(
1479 indoc! {b"
1480 line 1
1481 <<<<<<<
1482 +++++++
1483 left
1484 +++++++
1485 right
1486 >>>>>>>
1487 line 5
1488 "},
1489 2,
1490 7
1491 ),
1492 None
1493 );
1494 }
1495
1496 #[test]
1497 fn test_parse_conflict_malformed_marker() {
1498 // The conflict marker is missing `%%%%%%%`
1499 assert_eq!(
1500 parse_conflict(
1501 indoc! {b"
1502 line 1
1503 <<<<<<<
1504 line 2
1505 -line 3
1506 +left
1507 line 4
1508 +++++++
1509 right
1510 >>>>>>>
1511 line 5
1512 "},
1513 2,
1514 7
1515 ),
1516 None
1517 );
1518 }
1519
1520 #[test]
1521 fn test_parse_conflict_malformed_diff() {
1522 // The diff part is invalid (missing space before "line 4")
1523 assert_eq!(
1524 parse_conflict(
1525 indoc! {b"
1526 line 1
1527 <<<<<<<
1528 %%%%%%%
1529 line 2
1530 -line 3
1531 +left
1532 line 4
1533 +++++++
1534 right
1535 >>>>>>>
1536 line 5
1537 "},
1538 2,
1539 7
1540 ),
1541 None
1542 );
1543 }
1544
1545 #[test]
1546 fn test_parse_conflict_snapshot_missing_header() {
1547 // The "+++++++" header is missing
1548 assert_eq!(
1549 parse_conflict(
1550 indoc! {b"
1551 line 1
1552 <<<<<<<
1553 left
1554 -------
1555 base
1556 +++++++
1557 right
1558 >>>>>>>
1559 line 5
1560 "},
1561 2,
1562 7
1563 ),
1564 None
1565 );
1566 }
1567
1568 #[test]
1569 fn test_parse_conflict_wrong_git_style() {
1570 // The "|||||||" section is missing
1571 assert_eq!(
1572 parse_conflict(
1573 indoc! {b"
1574 line 1
1575 <<<<<<<
1576 left
1577 =======
1578 right
1579 >>>>>>>
1580 line 5
1581 "},
1582 2,
1583 7
1584 ),
1585 None
1586 );
1587 }
1588
1589 #[test]
1590 fn test_parse_conflict_git_reordered_headers() {
1591 // The "=======" header must come after the "|||||||" header
1592 assert_eq!(
1593 parse_conflict(
1594 indoc! {b"
1595 line 1
1596 <<<<<<<
1597 left
1598 =======
1599 right
1600 |||||||
1601 base
1602 >>>>>>>
1603 line 5
1604 "},
1605 2,
1606 7
1607 ),
1608 None
1609 );
1610 }
1611
1612 #[test]
1613 fn test_parse_conflict_git_too_many_sides() {
1614 // Git-style conflicts only allow 2 sides
1615 assert_eq!(
1616 parse_conflict(
1617 indoc! {b"
1618 line 1
1619 <<<<<<<
1620 a
1621 |||||||
1622 b
1623 =======
1624 c
1625 |||||||
1626 d
1627 =======
1628 e
1629 >>>>>>>
1630 line 5
1631 "},
1632 3,
1633 7
1634 ),
1635 None
1636 );
1637 }
1638
1639 #[test]
1640 fn test_parse_conflict_mixed_header_styles() {
1641 // "|||||||" can't be used in place of "-------"
1642 assert_eq!(
1643 parse_conflict(
1644 indoc! {b"
1645 line 1
1646 <<<<<<<
1647 +++++++
1648 left
1649 |||||||
1650 base
1651 +++++++
1652 right
1653 >>>>>>>
1654 line 5
1655 "},
1656 2,
1657 7
1658 ),
1659 None
1660 );
1661 // "+++++++" can't be used in place of "======="
1662 assert_eq!(
1663 parse_conflict(
1664 indoc! {b"
1665 line 1
1666 <<<<<<<
1667 left
1668 |||||||
1669 base
1670 +++++++
1671 right
1672 >>>>>>>
1673 line 5
1674 "},
1675 2,
1676 7
1677 ),
1678 None
1679 );
1680 // Test Git-style markers are ignored inside of JJ-style conflict
1681 insta::assert_debug_snapshot!(
1682 parse_conflict(indoc! {b"
1683 line 1
1684 <<<<<<< conflict 1 of 1
1685 +++++++ side #1
1686 ======= ignored
1687 ------- base
1688 ||||||| ignored
1689 +++++++ side #2
1690 >>>>>>> conflict 1 of 1 ends
1691 line 5
1692 "},
1693 2,
1694 7
1695 ),
1696 @r#"
1697 Some(
1698 [
1699 Resolved(
1700 "line 1\n",
1701 ),
1702 Conflicted(
1703 [
1704 "======= ignored\n",
1705 "||||||| ignored\n",
1706 "",
1707 ],
1708 ),
1709 Resolved(
1710 "line 5\n",
1711 ),
1712 ],
1713 )
1714 "#
1715 );
1716 // Test JJ-style markers are ignored inside of Git-style conflict
1717 insta::assert_debug_snapshot!(
1718 parse_conflict(indoc! {b"
1719 line 1
1720 <<<<<<< side #1
1721 ||||||| base
1722 ------- ignored
1723 %%%%%%% ignored
1724 =======
1725 +++++++ ignored
1726 >>>>>>> side #2
1727 line 5
1728 "},
1729 2,
1730 7
1731 ),
1732 @r#"
1733 Some(
1734 [
1735 Resolved(
1736 "line 1\n",
1737 ),
1738 Conflicted(
1739 [
1740 "",
1741 "------- ignored\n%%%%%%% ignored\n",
1742 "+++++++ ignored\n",
1743 ],
1744 ),
1745 Resolved(
1746 "line 5\n",
1747 ),
1748 ],
1749 )
1750 "#
1751 );
1752 }
1753
1754 #[test]
1755 fn test_update_conflict_from_content() -> TestResult {
1756 let test_repo = TestRepo::init();
1757 let store = test_repo.repo.store();
1758
1759 let path = repo_path("dir/file");
1760 let base_file_id = testutils::write_file(store, path, "line 1\nline 2\nline 3\n");
1761 let left_file_id = testutils::write_file(store, path, "left 1\nline 2\nleft 3\n");
1762 let right_file_id = testutils::write_file(store, path, "right 1\nline 2\nright 3\n");
1763 let conflict = Merge::from_removes_adds(
1764 vec![Some(base_file_id.clone())],
1765 vec![Some(left_file_id.clone()), Some(right_file_id.clone())],
1766 );
1767
1768 // If the content is unchanged compared to the materialized value, we get the
1769 // old conflict id back.
1770 let materialized =
1771 materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
1772 let parse = |content| {
1773 update_from_content(&conflict, store, path, content, MIN_CONFLICT_MARKER_LEN)
1774 .block_on()
1775 .unwrap()
1776 };
1777 assert_eq!(parse(materialized.as_bytes()), conflict);
1778
1779 // If the conflict is resolved, we get None back to indicate that.
1780 let expected_file_id = testutils::write_file(store, path, "resolved 1\nline 2\nresolved 3\n");
1781 assert_eq!(
1782 parse(b"resolved 1\nline 2\nresolved 3\n"),
1783 Merge::normal(expected_file_id)
1784 );
1785
1786 // If the conflict is partially resolved, we get a new conflict back.
1787 let new_conflict = parse(
1788 b"resolved 1\nline 2\n<<<<<<<\n%%%%%%%\n-line 3\n+left 3\n+++++++\nright 3\n>>>>>>>\n",
1789 );
1790 assert_ne!(new_conflict, conflict);
1791 // Calculate expected new FileIds
1792 let new_base_file_id = testutils::write_file(store, path, "resolved 1\nline 2\nline 3\n");
1793 let new_left_file_id = testutils::write_file(store, path, "resolved 1\nline 2\nleft 3\n");
1794 let new_right_file_id = testutils::write_file(store, path, "resolved 1\nline 2\nright 3\n");
1795 assert_eq!(
1796 new_conflict,
1797 Merge::from_removes_adds(
1798 vec![Some(new_base_file_id.clone())],
1799 vec![
1800 Some(new_left_file_id.clone()),
1801 Some(new_right_file_id.clone())
1802 ]
1803 )
1804 );
1805 Ok(())
1806 }
1807
1808 #[test]
1809 fn test_update_conflict_from_content_modify_delete() -> TestResult {
1810 let test_repo = TestRepo::init();
1811 let store = test_repo.repo.store();
1812
1813 let path = repo_path("dir/file");
1814 let before_file_id = testutils::write_file(store, path, "line 1\nline 2 before\nline 3\n");
1815 let after_file_id = testutils::write_file(store, path, "line 1\nline 2 after\nline 3\n");
1816 let conflict =
1817 Merge::from_removes_adds(vec![Some(before_file_id)], vec![Some(after_file_id), None]);
1818
1819 // If the content is unchanged compared to the materialized value, we get the
1820 // old conflict id back.
1821 let materialized =
1822 materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
1823 let parse = |content| {
1824 update_from_content(&conflict, store, path, content, MIN_CONFLICT_MARKER_LEN)
1825 .block_on()
1826 .unwrap()
1827 };
1828 assert_eq!(parse(materialized.as_bytes()), conflict);
1829
1830 // If the conflict is resolved, we get None back to indicate that.
1831 let expected_file_id = testutils::write_file(store, path, "resolved\n");
1832 assert_eq!(parse(b"resolved\n"), Merge::normal(expected_file_id));
1833
1834 // If the conflict is modified, we get a new conflict back.
1835 let new_conflict = parse(
1836 b"<<<<<<<\n%%%%%%%\n line 1\n-line 2 before\n+line 2 modified after\n line 3\n+++++++\n>>>>>>>\n",
1837 );
1838 // Calculate expected new FileIds
1839 let new_base_file_id = testutils::write_file(store, path, "line 1\nline 2 before\nline 3\n");
1840 let new_left_file_id =
1841 testutils::write_file(store, path, "line 1\nline 2 modified after\nline 3\n");
1842
1843 assert_eq!(
1844 new_conflict,
1845 Merge::from_removes_adds(
1846 vec![Some(new_base_file_id.clone())],
1847 vec![Some(new_left_file_id.clone()), None]
1848 )
1849 );
1850 Ok(())
1851 }
1852
1853 #[test]
1854 fn test_update_conflict_from_content_simplified_conflict() -> TestResult {
1855 let test_repo = TestRepo::init();
1856 let store = test_repo.repo.store();
1857
1858 let path = repo_path("dir/file");
1859 let base_file_id = testutils::write_file(store, path, "line 1\nline 2\nline 3\n");
1860 let left_file_id = testutils::write_file(store, path, "left 1\nline 2\nleft 3\n");
1861 let right_file_id = testutils::write_file(store, path, "right 1\nline 2\nright 3\n");
1862 // Conflict: left - base + base - base + right
1863 let conflict = Merge::from_removes_adds(
1864 vec![Some(base_file_id.clone()), Some(base_file_id.clone())],
1865 vec![
1866 Some(left_file_id.clone()),
1867 Some(base_file_id.clone()),
1868 Some(right_file_id.clone()),
1869 ],
1870 );
1871 let simplified_conflict = conflict.simplify();
1872
1873 // If the content is unchanged compared to the materialized value, we get the
1874 // old conflict id back.
1875 let materialized =
1876 materialize_conflict_string(store, path, &simplified_conflict, ConflictMarkerStyle::Diff);
1877 let parse = |content| {
1878 update_from_content(&conflict, store, path, content, MIN_CONFLICT_MARKER_LEN)
1879 .block_on()
1880 .unwrap()
1881 };
1882 insta::assert_snapshot!(
1883 materialized,
1884 @r"
1885 <<<<<<< conflict 1 of 2
1886 %%%%%%% diff from: base
1887 \\\\\\\ to: side #1
1888 -line 1
1889 +left 1
1890 +++++++ side #2
1891 right 1
1892 >>>>>>> conflict 1 of 2 ends
1893 line 2
1894 <<<<<<< conflict 2 of 2
1895 %%%%%%% diff from: base
1896 \\\\\\\ to: side #1
1897 -line 3
1898 +left 3
1899 +++++++ side #2
1900 right 3
1901 >>>>>>> conflict 2 of 2 ends
1902 "
1903 );
1904 assert_eq!(parse(materialized.as_bytes()), conflict);
1905
1906 // If the conflict is resolved, we get a normal merge back to indicate that.
1907 let expected_file_id = testutils::write_file(store, path, "resolved 1\nline 2\nresolved 3\n");
1908 assert_eq!(
1909 parse(b"resolved 1\nline 2\nresolved 3\n"),
1910 Merge::normal(expected_file_id)
1911 );
1912
1913 // If the conflict is partially resolved, we get a new conflict back.
1914 let new_conflict = parse(indoc! {b"
1915 resolved 1
1916 line 2
1917 <<<<<<< conflict 2 of 2
1918 %%%%%%% diff from base to side #1
1919 -edited line 3
1920 +edited left 3
1921 +++++++ side #2
1922 edited right 3
1923 >>>>>>> conflict 2 of 2 ends
1924 "});
1925 assert_ne!(new_conflict, conflict);
1926 // Calculate expected new FileIds
1927 let new_base_file_id =
1928 testutils::write_file(store, path, "resolved 1\nline 2\nedited line 3\n");
1929 let new_left_file_id =
1930 testutils::write_file(store, path, "resolved 1\nline 2\nedited left 3\n");
1931 let new_right_file_id =
1932 testutils::write_file(store, path, "resolved 1\nline 2\nedited right 3\n");
1933 assert_eq!(
1934 new_conflict,
1935 Merge::from_removes_adds(
1936 vec![Some(base_file_id.clone()), Some(new_base_file_id.clone())],
1937 vec![
1938 Some(new_left_file_id.clone()),
1939 Some(base_file_id.clone()),
1940 Some(new_right_file_id.clone())
1941 ]
1942 )
1943 );
1944 Ok(())
1945 }
1946
1947 #[test]
1948 fn test_update_conflict_from_content_with_long_markers() -> TestResult {
1949 let test_repo = TestRepo::init();
1950 let store = test_repo.repo.store();
1951
1952 // Create conflicts which contain conflict markers of varying lengths
1953 let path = repo_path("dir/file");
1954 let base_file_id = testutils::write_file(
1955 store,
1956 path,
1957 indoc! {"
1958 line 1
1959 line 2
1960 line 3
1961 "},
1962 );
1963 let left_file_id = testutils::write_file(
1964 store,
1965 path,
1966 indoc! {"
1967 <<<< left 1
1968 line 2
1969 <<<<<<<<<<<< left 3
1970 "},
1971 );
1972 let right_file_id = testutils::write_file(
1973 store,
1974 path,
1975 indoc! {"
1976 >>>>>>> right 1
1977 line 2
1978 >>>>>>>>>>>> right 3
1979 "},
1980 );
1981 let conflict = Merge::from_removes_adds(
1982 vec![Some(base_file_id.clone())],
1983 vec![Some(left_file_id.clone()), Some(right_file_id.clone())],
1984 );
1985
1986 // The conflict should be materialized using long conflict markers
1987 let materialized_marker_len = choose_materialized_conflict_marker_len(
1988 &extract_as_single_hunk(&conflict, store, path).block_on()?,
1989 );
1990 assert!(materialized_marker_len > MIN_CONFLICT_MARKER_LEN);
1991 let materialized =
1992 materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Snapshot);
1993 insta::assert_snapshot!(materialized, @"
1994 <<<<<<<<<<<<<<<< conflict 1 of 2
1995 ++++++++++++++++ side #1
1996 <<<< left 1
1997 ---------------- base
1998 line 1
1999 ++++++++++++++++ side #2
2000 >>>>>>> right 1
2001 >>>>>>>>>>>>>>>> conflict 1 of 2 ends
2002 line 2
2003 <<<<<<<<<<<<<<<< conflict 2 of 2
2004 ++++++++++++++++ side #1
2005 <<<<<<<<<<<< left 3
2006 ---------------- base
2007 line 3
2008 ++++++++++++++++ side #2
2009 >>>>>>>>>>>> right 3
2010 >>>>>>>>>>>>>>>> conflict 2 of 2 ends
2011 "
2012 );
2013
2014 let parse = |conflict, content| {
2015 update_from_content(conflict, store, path, content, materialized_marker_len)
2016 .block_on()
2017 .unwrap()
2018 };
2019 assert_eq!(parse(&conflict, materialized.as_bytes()), conflict);
2020
2021 // Test resolving the conflict, leaving some fake conflict markers which should
2022 // not be parsed since they are too short
2023 let resolved_file_contents = indoc! {"
2024 <<<<<<<<<<<< not a real conflict!
2025 ++++++++++++
2026 left
2027 ------------
2028 base
2029 ++++++++++++
2030 right
2031 >>>>>>>>>>>>
2032 "};
2033 let resolved_file_id = testutils::write_file(store, path, resolved_file_contents);
2034 assert_eq!(
2035 parse(&conflict, resolved_file_contents.as_bytes()),
2036 Merge::normal(resolved_file_id)
2037 );
2038
2039 // Resolve one of the conflicts, decreasing the minimum conflict marker length
2040 let new_conflict_contents = indoc! {"
2041 <<<<<<<<<<<<<<<< conflict 1 of 2
2042 ++++++++++++++++ side #1
2043 <<<< left 1
2044 ---------------- base
2045 line 1
2046 ++++++++++++++++ side #2
2047 >>>>>>> right 1
2048 >>>>>>>>>>>>>>>> conflict 1 of 2 ends
2049 line 2
2050 line 3
2051 "};
2052
2053 // Confirm that the new conflict parsed correctly
2054 let new_conflict = parse(&conflict, new_conflict_contents.as_bytes());
2055 assert_eq!(new_conflict.num_sides(), 2);
2056 let new_conflict_terms = new_conflict
2057 .iter()
2058 .map(|id| String::from_utf8(read_file(store, path, id.as_ref().unwrap())).unwrap())
2059 .collect_vec();
2060 let [new_left_side, new_base, new_right_side] = new_conflict_terms.as_slice() else {
2061 unreachable!()
2062 };
2063 insta::assert_snapshot!(new_left_side, @"
2064 <<<< left 1
2065 line 2
2066 line 3
2067 ");
2068 insta::assert_snapshot!(new_base, @"
2069 line 1
2070 line 2
2071 line 3
2072 ");
2073 insta::assert_snapshot!(new_right_side, @"
2074 >>>>>>> right 1
2075 line 2
2076 line 3
2077 ");
2078
2079 // The conflict markers should still parse in future snapshots even though
2080 // they're now longer than necessary
2081 assert_eq!(
2082 parse(&new_conflict, new_conflict_contents.as_bytes()),
2083 new_conflict
2084 );
2085
2086 // If we add back the second conflict, it should still be parsed correctly
2087 // (the fake conflict markers shouldn't be interpreted as conflict markers
2088 // still, since they aren't the longest ones in the file).
2089 assert_eq!(parse(&new_conflict, materialized.as_bytes()), conflict);
2090
2091 // If the new conflict is materialized again, it should have shorter
2092 // conflict markers now
2093 insta::assert_snapshot!(
2094 materialize_conflict_string(store, path, &new_conflict, ConflictMarkerStyle::Snapshot),
2095 @"
2096 <<<<<<<<<<< conflict 1 of 1
2097 +++++++++++ side #1
2098 <<<< left 1
2099 ----------- base
2100 line 1
2101 +++++++++++ side #2
2102 >>>>>>> right 1
2103 >>>>>>>>>>> conflict 1 of 1 ends
2104 line 2
2105 line 3
2106 "
2107 );
2108 Ok(())
2109 }
2110
2111 #[test]
2112 fn test_update_conflict_from_content_no_eol() -> TestResult {
2113 let test_repo = TestRepo::init();
2114 let store = test_repo.repo.store();
2115
2116 let path = repo_path("file");
2117 let base_id = testutils::write_file(store, path, "line 1\nline 2\nline 3\nbase");
2118 let left_empty_id =
2119 testutils::write_file(store, path, "line 1\nline 2 left\nline 3\nbase\nleft\n");
2120 let right_id = testutils::write_file(store, path, "line 1\nline 2 right\nline 3\nright");
2121
2122 let conflict = Merge::from_removes_adds(
2123 vec![Some(base_id)],
2124 vec![Some(left_empty_id), Some(right_id)],
2125 );
2126
2127 let materialized =
2128 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
2129 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
2130 @r"
2131 line 1
2132 <<<<<<< conflict 1 of 2
2133 %%%%%%% diff from: base
2134 \\\\\\\ to: side #1
2135 -line 2
2136 +line 2 left
2137 +++++++ side #2
2138 line 2 right
2139 >>>>>>> conflict 1 of 2 ends
2140 line 3
2141 <<<<<<< conflict 2 of 2
2142 %%%%%%% diff from: base (no terminating newline)
2143 \\\\\\\ to: side #1
2144 base
2145 +left
2146 +
2147 +++++++ side #2 (no terminating newline)
2148 right
2149 >>>>>>> conflict 2 of 2 ends[EOF]
2150 "
2151 );
2152 assert_eq!(
2153 update_from_content(
2154 &conflict,
2155 store,
2156 path,
2157 materialized.as_bytes(),
2158 MIN_CONFLICT_MARKER_LEN,
2159 )
2160 .block_on()?,
2161 conflict
2162 );
2163
2164 let materialized =
2165 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Snapshot);
2166 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
2167 @"
2168 line 1
2169 <<<<<<< conflict 1 of 2
2170 +++++++ side #1
2171 line 2 left
2172 ------- base
2173 line 2
2174 +++++++ side #2
2175 line 2 right
2176 >>>>>>> conflict 1 of 2 ends
2177 line 3
2178 <<<<<<< conflict 2 of 2
2179 +++++++ side #1
2180 base
2181 left
2182
2183 ------- base (no terminating newline)
2184 base
2185 +++++++ side #2 (no terminating newline)
2186 right
2187 >>>>>>> conflict 2 of 2 ends[EOF]
2188 "
2189 );
2190 assert_eq!(
2191 update_from_content(
2192 &conflict,
2193 store,
2194 path,
2195 materialized.as_bytes(),
2196 MIN_CONFLICT_MARKER_LEN,
2197 )
2198 .block_on()?,
2199 conflict
2200 );
2201
2202 let materialized =
2203 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Git);
2204 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
2205 @"
2206 line 1
2207 <<<<<<< side #1
2208 line 2 left
2209 ||||||| base
2210 line 2
2211 =======
2212 line 2 right
2213 >>>>>>> side #2
2214 line 3
2215 <<<<<<< side #1
2216 base
2217 left
2218
2219 ||||||| base (no terminating newline)
2220 base
2221 =======
2222 right
2223 >>>>>>> side #2 (no terminating newline)[EOF]
2224 "
2225 );
2226 assert_eq!(
2227 update_from_content(
2228 &conflict,
2229 store,
2230 path,
2231 materialized.as_bytes(),
2232 MIN_CONFLICT_MARKER_LEN,
2233 )
2234 .block_on()?,
2235 conflict
2236 );
2237 Ok(())
2238 }
2239
2240 #[test]
2241 fn test_update_conflict_from_content_no_eol_in_diff_hunk() -> TestResult {
2242 let test_repo = TestRepo::init();
2243 let store = test_repo.repo.store();
2244
2245 let path = repo_path("file");
2246 // Create a conflict with all 4 possible cases for diff "noeol" markers
2247 let side_1_id = testutils::write_file(store, path, "side\n");
2248 let base_1_id = testutils::write_file(store, path, "add newline\nline");
2249 let side_2_id = testutils::write_file(store, path, "add newline\nline\n");
2250 let base_2_id = testutils::write_file(store, path, "remove newline\nline\n");
2251 let side_3_id = testutils::write_file(store, path, "remove newline\nline");
2252 let base_3_id = testutils::write_file(store, path, "no newline\nline 1");
2253 let side_4_id = testutils::write_file(store, path, "no newline\nline 2");
2254 let base_4_id = testutils::write_file(store, path, "with newline\nline 1\n");
2255 let side_5_id = testutils::write_file(store, path, "with newline\nline 2\n");
2256
2257 let conflict = Merge::from_removes_adds(
2258 vec![
2259 Some(base_1_id),
2260 Some(base_2_id),
2261 Some(base_3_id),
2262 Some(base_4_id),
2263 ],
2264 vec![
2265 Some(side_1_id),
2266 Some(side_2_id),
2267 Some(side_3_id),
2268 Some(side_4_id),
2269 Some(side_5_id),
2270 ],
2271 );
2272
2273 let materialized =
2274 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
2275 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
2276 @r"
2277 <<<<<<< conflict 1 of 1
2278 +++++++ side #1
2279 side
2280
2281 %%%%%%% diff from: base #1 (no terminating newline)
2282 \\\\\\\ to: side #2
2283 add newline
2284 line
2285 +
2286 %%%%%%% diff from: base #2
2287 \\\\\\\ to: side #3 (no terminating newline)
2288 remove newline
2289 line
2290 -
2291 %%%%%%% diff from: base #3 (no terminating newline)
2292 \\\\\\\ to: side #4 (no terminating newline)
2293 no newline
2294 -line 1
2295 +line 2
2296 %%%%%%% diff from: base #4
2297 \\\\\\\ to: side #5
2298 with newline
2299 -line 1
2300 +line 2
2301
2302 >>>>>>> conflict 1 of 1 ends[EOF]
2303 "
2304 );
2305 assert_eq!(
2306 update_from_content(
2307 &conflict,
2308 store,
2309 path,
2310 materialized.as_bytes(),
2311 MIN_CONFLICT_MARKER_LEN,
2312 )
2313 .block_on()?,
2314 conflict
2315 );
2316 Ok(())
2317 }
2318
2319 #[test]
2320 fn test_update_conflict_from_content_only_no_eol_change() -> TestResult {
2321 let test_repo = TestRepo::init();
2322 let store = test_repo.repo.store();
2323
2324 let path = repo_path("file");
2325 // Create a conflict which would be resolved by the "A-B+A = A" rule if the
2326 // missing newline is wrongly ignored
2327 let left_id = testutils::write_file(store, path, "line 1\nline 2");
2328 let base_id = testutils::write_file(store, path, "line 1\n");
2329 let right_id = testutils::write_file(store, path, "line 1\nline 2\n");
2330
2331 let conflict =
2332 Merge::from_removes_adds(vec![Some(base_id)], vec![Some(left_id), Some(right_id)]);
2333
2334 let materialized =
2335 &materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
2336 insta::assert_snapshot!(materialized.to_owned() + "[EOF]",
2337 @r"
2338 line 1
2339 <<<<<<< conflict 1 of 1
2340 +++++++ side #1 (no terminating newline)
2341 line 2
2342 %%%%%%% diff from: base
2343 \\\\\\\ to: side #2
2344 +line 2
2345
2346 >>>>>>> conflict 1 of 1 ends[EOF]
2347 "
2348 );
2349 assert_eq!(
2350 update_from_content(
2351 &conflict,
2352 store,
2353 path,
2354 materialized.as_bytes(),
2355 MIN_CONFLICT_MARKER_LEN,
2356 )
2357 .block_on()?,
2358 conflict
2359 );
2360 Ok(())
2361 }
2362
2363 #[test]
2364 fn test_update_from_content_malformed_conflict() -> TestResult {
2365 let test_repo = TestRepo::init();
2366 let store = test_repo.repo.store();
2367
2368 let path = repo_path("dir/file");
2369 let base_file_id = testutils::write_file(
2370 store,
2371 path,
2372 indoc! {"
2373 line 1
2374 line 2
2375 line 3
2376 line 4
2377 line 5
2378 "},
2379 );
2380 let left_file_id = testutils::write_file(
2381 store,
2382 path,
2383 indoc! {"
2384 line 1
2385 line 2 left
2386 line 3
2387 line 4 left
2388 line 5
2389 "},
2390 );
2391 let right_file_id = testutils::write_file(
2392 store,
2393 path,
2394 indoc! {"
2395 line 1
2396 line 2 right
2397 line 3
2398 line 4 right
2399 line 5
2400 "},
2401 );
2402 let conflict = Merge::from_removes_adds(
2403 vec![Some(base_file_id.clone())],
2404 vec![Some(left_file_id.clone()), Some(right_file_id.clone())],
2405 );
2406
2407 // The conflict should be materialized with normal markers
2408 let materialized_marker_len = choose_materialized_conflict_marker_len(
2409 &extract_as_single_hunk(&conflict, store, path).block_on()?,
2410 );
2411 assert!(materialized_marker_len == MIN_CONFLICT_MARKER_LEN);
2412
2413 let materialized =
2414 materialize_conflict_string(store, path, &conflict, ConflictMarkerStyle::Diff);
2415 insta::assert_snapshot!(materialized, @r"
2416 line 1
2417 <<<<<<< conflict 1 of 2
2418 %%%%%%% diff from: base
2419 \\\\\\\ to: side #1
2420 -line 2
2421 +line 2 left
2422 +++++++ side #2
2423 line 2 right
2424 >>>>>>> conflict 1 of 2 ends
2425 line 3
2426 <<<<<<< conflict 2 of 2
2427 %%%%%%% diff from: base
2428 \\\\\\\ to: side #1
2429 -line 4
2430 +line 4 left
2431 +++++++ side #2
2432 line 4 right
2433 >>>>>>> conflict 2 of 2 ends
2434 line 5
2435 "
2436 );
2437
2438 let parse = |conflict, content| {
2439 update_from_content(conflict, store, path, content, materialized_marker_len)
2440 .block_on()
2441 .unwrap()
2442 };
2443 assert_eq!(parse(&conflict, materialized.as_bytes()), conflict);
2444
2445 // Make a change to the second conflict that causes it to become invalid
2446 let new_conflict_contents = indoc! {"
2447 line 1
2448 <<<<<<< conflict 1 of 2
2449 %%%%%%% diff from base to side #1
2450 -line 2
2451 +line 2 left
2452 +++++++ side #2
2453 line 2 right
2454 >>>>>>> conflict 1 of 2 ends
2455 line 3
2456 <<<<<<< conflict 2 of 2
2457 %%%%%%% diff from base to side #1
2458 -line 4
2459 +line 4 left
2460 line 4 right
2461 >>>>>>> conflict 2 of 2 ends
2462 line 5
2463 "};
2464 // On the first snapshot, it will parse as a conflict containing conflict
2465 // markers as text
2466 let new_conflict = parse(&conflict, new_conflict_contents.as_bytes());
2467 assert_eq!(new_conflict.num_sides(), 2);
2468 let new_conflict_terms = new_conflict
2469 .iter()
2470 .map(|id| String::from_utf8(read_file(store, path, id.as_ref().unwrap())).unwrap())
2471 .collect_vec();
2472 let [new_left_side, new_base, new_right_side] = new_conflict_terms.as_slice() else {
2473 unreachable!()
2474 };
2475 insta::assert_snapshot!(new_left_side, @"
2476 line 1
2477 line 2 left
2478 line 3
2479 <<<<<<< conflict 2 of 2
2480 %%%%%%% diff from base to side #1
2481 -line 4
2482 +line 4 left
2483 line 4 right
2484 >>>>>>> conflict 2 of 2 ends
2485 line 5
2486 ");
2487 insta::assert_snapshot!(new_base, @"
2488 line 1
2489 line 2
2490 line 3
2491 <<<<<<< conflict 2 of 2
2492 %%%%%%% diff from base to side #1
2493 -line 4
2494 +line 4 left
2495 line 4 right
2496 >>>>>>> conflict 2 of 2 ends
2497 line 5
2498 ");
2499 insta::assert_snapshot!(new_right_side, @"
2500 line 1
2501 line 2 right
2502 line 3
2503 <<<<<<< conflict 2 of 2
2504 %%%%%%% diff from base to side #1
2505 -line 4
2506 +line 4 left
2507 line 4 right
2508 >>>>>>> conflict 2 of 2 ends
2509 line 5
2510 ");
2511
2512 // Even though the file now contains markers of length 7, the materialized
2513 // markers of length 7 are still parsed
2514 let second_snapshot = parse(&new_conflict, new_conflict_contents.as_bytes());
2515 assert_eq!(second_snapshot, new_conflict);
2516 Ok(())
2517 }
2518
2519 fn materialize_conflict_string(
2520 store: &Store,
2521 path: &RepoPath,
2522 conflict: &Merge<Option<FileId>>,
2523 marker_style: ConflictMarkerStyle,
2524 ) -> String {
2525 materialize_conflict_string_with_labels(
2526 store,
2527 path,
2528 conflict,
2529 &ConflictLabels::unlabeled(),
2530 marker_style,
2531 )
2532 }
2533
2534 fn materialize_conflict_string_with_labels(
2535 store: &Store,
2536 path: &RepoPath,
2537 conflict: &Merge<Option<FileId>>,
2538 conflict_labels: &ConflictLabels,
2539 marker_style: ConflictMarkerStyle,
2540 ) -> String {
2541 let contents = extract_as_single_hunk(conflict, store, path)
2542 .block_on()
2543 .unwrap();
2544 let options = ConflictMaterializeOptions {
2545 marker_style,
2546 marker_len: None,
2547 merge: MergeOptions {
2548 hunk_level: FileMergeHunkLevel::Line,
2549 same_change: SameChange::Accept,
2550 },
2551 };
2552 String::from_utf8(
2553 materialize_merge_result_to_bytes(&contents, conflict_labels, &options).into(),
2554 )
2555 .unwrap()
2556 }