From 16f2b82feb187e9ca6a9a59c9d631e9c7c47bc0a Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 20 Sep 2022 12:31:20 +0900 Subject: [PATCH] conflicts: change diff line marker to %%%%%%% I feel the original -------/+++++++ pair is slightly confusing because each half can be a separator by itself. I don't know what character other than '-'/'+' is preferred, but let's pick '%' (for "mod") per @martinvonz suggestion. --- docs/conflicts.md | 8 +++---- docs/tutorial.md | 3 +-- lib/src/conflicts.rs | 20 +++++++++++++---- lib/tests/test_conflicts.rs | 38 +++++++++++++++------------------ tests/test_interdiff_command.rs | 5 ++--- tests/test_obslog_command.rs | 12 +++++------ tests/test_print_command.rs | 3 +-- tests/test_touchup_command.rs | 3 +-- 8 files changed, 46 insertions(+), 46 deletions(-) diff --git a/docs/conflicts.md b/docs/conflicts.md index 4d2cf0b43..72bb018fb 100644 --- a/docs/conflicts.md +++ b/docs/conflicts.md @@ -87,8 +87,7 @@ how that would look for the same example as above: ``` <<<<<<< -------- -+++++++ +%%%%%%% apple -grape +grapefruit @@ -101,9 +100,8 @@ ORANGE ``` As in Git, the `<<<<<<<` and `>>>>>>>` lines mark the start and end of the -conflict. The `-------` followed by `+++++++` indicates the start of a diff -(there is never content between the two header lines). A header consisting of -only `+++++++` indicates the start of a snapshot (not a diff). +conflict. The `%%%%%%%` line indicates the start of a diff. The `+++++++` +line indicates the start of a snapshot (not a diff). There is another reason for this format (in addition to helping you spot the differences): The format supports more complex conflicts involving more than 3 diff --git a/docs/tutorial.md b/docs/tutorial.md index 650c58323..fcf7485c3 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -234,8 +234,7 @@ There are unresolved conflicts at these paths: file1 $ cat file1 <<<<<<< -------- -+++++++ +%%%%%%% -b1 +a +++++++ diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index 617a07b7e..fb7809503 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -25,6 +25,7 @@ use crate::store::Store; const CONFLICT_START_LINE: &[u8] = b"<<<<<<<\n"; const CONFLICT_END_LINE: &[u8] = b">>>>>>>\n"; +const CONFLICT_DIFF_LINE: &[u8] = b"%%%%%%%\n"; const CONFLICT_MINUS_LINE: &[u8] = b"-------\n"; const CONFLICT_PLUS_LINE: &[u8] = b"+++++++\n"; @@ -181,8 +182,7 @@ pub fn materialize_conflict( .iter() .position_min_by_key(|diff| diff_size(diff)) .unwrap(); - output.write_all(CONFLICT_MINUS_LINE)?; - output.write_all(CONFLICT_PLUS_LINE)?; + output.write_all(CONFLICT_DIFF_LINE)?; write_diff_hunks(&diffs[min_diff_index], output)?; removes.remove(0); adds.remove(min_diff_index); @@ -278,15 +278,26 @@ pub fn parse_conflict(input: &[u8], num_removes: usize, num_adds: usize) -> Opti } fn parse_conflict_hunk(input: &[u8]) -> MergeHunk { + let mut diff_seen = false; let mut minus_seen = false; let mut plus_seen = false; let mut body_seen = false; let mut removes = vec![]; let mut adds = vec![]; for line in input.split_inclusive(|b| *b == b'\n') { - if line == CONFLICT_MINUS_LINE { + if line == CONFLICT_DIFF_LINE { + diff_seen = true; + if body_seen { + minus_seen = false; + plus_seen = false; + body_seen = false; + } + removes.push(vec![]); + adds.push(vec![]); + } else if line == CONFLICT_MINUS_LINE { minus_seen = true; if body_seen { + diff_seen = false; plus_seen = false; body_seen = false; } @@ -294,11 +305,12 @@ fn parse_conflict_hunk(input: &[u8]) -> MergeHunk { } else if line == CONFLICT_PLUS_LINE { plus_seen = true; if body_seen { + diff_seen = false; minus_seen = false; body_seen = false; } adds.push(vec![]); - } else if minus_seen && plus_seen { + } else if diff_seen { body_seen = true; if let Some(rest) = line.strip_prefix(b"-") { removes.last_mut().unwrap().extend_from_slice(rest); diff --git a/lib/tests/test_conflicts.rs b/lib/tests/test_conflicts.rs index de2f6c8ab..b92d4b2e4 100644 --- a/lib/tests/test_conflicts.rs +++ b/lib/tests/test_conflicts.rs @@ -87,8 +87,7 @@ line 5 line 1 line 2 <<<<<<< - ------- - +++++++ + %%%%%%% -line 3 +right 3.1 +++++++ @@ -108,8 +107,7 @@ line 5 line 1 line 2 <<<<<<< - ------- - +++++++ + %%%%%%% -line 3 +right 3.1 +++++++ @@ -185,8 +183,7 @@ line 5 line 1 line 2 <<<<<<< - ------- - +++++++ + %%%%%%% -line 3 +++++++ left @@ -262,8 +259,7 @@ line 5 line 1 line 2 <<<<<<< - ------- - +++++++ + %%%%%%% -line 3 +++++++ right @@ -297,8 +293,7 @@ fn test_parse_conflict_simple() { parse_conflict( b"line 1 <<<<<<< -------- -+++++++ +%%%%%%% line 2 -line 3 +left @@ -328,16 +323,14 @@ fn test_parse_conflict_multi_way() { parse_conflict( b"line 1 <<<<<<< -------- -+++++++ +%%%%%%% line 2 -line 3 +left line 4 +++++++ right -------- -+++++++ +%%%%%%% line 2 +forward line 3 @@ -372,8 +365,7 @@ fn test_parse_conflict_different_wrong_arity() { parse_conflict( b"line 1 <<<<<<< -------- -+++++++ +%%%%%%% line 2 -line 3 +left @@ -392,8 +384,7 @@ line 5 #[test] fn test_parse_conflict_malformed_marker() { - // The conflict marker is missing `-------` and `+++++++` (it needs at least one - // of them) + // The conflict marker is missing `%%%%%%%` assert_eq!( parse_conflict( b"line 1 @@ -421,8 +412,7 @@ fn test_parse_conflict_malformed_diff() { parse_conflict( b"line 1 <<<<<<< -------- -+++++++ +%%%%%%% line 2 -line 3 +left @@ -490,7 +480,13 @@ fn test_update_conflict_from_content() { assert_eq!(result, None); // If the conflict is partially resolved, we get a new conflict back. - let result = update_conflict_from_content(store, &path, &conflict_id, b"resolved 1\nline 2\n<<<<<<<\n-------\n+++++++\n-line 3\n+left 3\n+++++++\nright 3\n>>>>>>>\n").unwrap(); + let result = update_conflict_from_content( + store, + &path, + &conflict_id, + b"resolved 1\nline 2\n<<<<<<<\n%%%%%%%\n-line 3\n+left 3\n+++++++\nright 3\n>>>>>>>\n", + ) + .unwrap(); assert_ne!(result, None); assert_ne!(result, Some(conflict_id)); let new_conflict = store.read_conflict(&path, &result.unwrap()).unwrap(); diff --git a/tests/test_interdiff_command.rs b/tests/test_interdiff_command.rs index 7b0339fb5..bdf04adce 100644 --- a/tests/test_interdiff_command.rs +++ b/tests/test_interdiff_command.rs @@ -154,10 +154,9 @@ fn test_interdiff_conflicting() { index f845ab93f0...24c5735c3e 100644 --- a/file +++ b/file - @@ -1,8 +1,1 @@ + @@ -1,7 +1,1 @@ -<<<<<<< - -------- - -+++++++ + -%%%%%%% --foo -+abc -+++++++ diff --git a/tests/test_obslog_command.rs b/tests/test_obslog_command.rs index 3f62f426d..bb8542fe7 100644 --- a/tests/test_obslog_command.rs +++ b/tests/test_obslog_command.rs @@ -52,10 +52,9 @@ fn test_obslog_with_or_without_diff() { | my description | Resolved conflict in file1: | 1 1: <<<<<<>>>>>> + | 2 : %%%%%%% + | 3 : +bar + | 4 : >>>>>>> o 813918f7b4e6 test.user@example.com 2001-02-03 04:05:08.000 +07:00 conflict | my description o 8f02f5470c55 test.user@example.com 2001-02-03 04:05:08.000 +07:00 @@ -91,10 +90,9 @@ fn test_obslog_with_or_without_diff() { index e155302a24...2ab19ae607 100644 --- a/file1 +++ b/file1 - @@ -1,5 +1,1 @@ + @@ -1,4 +1,1 @@ -<<<<<<< - -------- - -+++++++ + -%%%%%%% -+bar ->>>>>>> +resolved diff --git a/tests/test_print_command.rs b/tests/test_print_command.rs index f428635ca..9617024c6 100644 --- a/tests/test_print_command.rs +++ b/tests/test_print_command.rs @@ -61,8 +61,7 @@ fn test_print() { let stdout = test_env.jj_cmd_success(&repo_path, &["print", "file1"]); insta::assert_snapshot!(stdout, @r###" <<<<<<< - ------- - +++++++ + %%%%%%% -b +a +++++++ diff --git a/tests/test_touchup_command.rs b/tests/test_touchup_command.rs index a78a865ab..0bb3adc47 100644 --- a/tests/test_touchup_command.rs +++ b/tests/test_touchup_command.rs @@ -140,8 +140,7 @@ fn test_touchup_merge() { let stdout = test_env.jj_cmd_success(&repo_path, &["print", "file2"]); insta::assert_snapshot!(stdout, @r###" <<<<<<< - ------- - +++++++ + %%%%%%% -a +c +++++++