From 0d17ddc7e7fb756cb64eb1f9efac85a25999ac7a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 16 Nov 2024 11:25:30 -0600 Subject: [PATCH] conflicts: add "snapshot" conflict marker style Adds a new "snapshot" conflict marker style which returns a series of snapshots, similar to Git's "diff3" conflict style. The "snapshot" option uses a subset of the conflict hunk headers as the "diff" option (it just doesn't use "%%%%%%%"), meaning that the two options are trivially compatible with each other (i.e. a file materialized with "snapshot" can be parsed with "diff" and vice versa). Example of "snapshot" conflict markers: ``` <<<<<<< Conflict 1 of 1 +++++++ Contents of side #1 fn example(word: String) { println!("word is {word}"); ------- Contents of base fn example(w: String) { println!("word is {w}"); +++++++ Contents of side #2 fn example(w: &str) { println!("word is {w}"); >>>>>>> Conflict 1 of 1 ends } ``` --- CHANGELOG.md | 2 ++ cli/src/config-schema.json | 3 ++- lib/src/conflicts.rs | 13 ++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b74375d52..568458d42b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). * New `ui.conflict-marker-style` config option to change how conflicts are materialized in the working copy. The default option ("diff") renders conflicts as a snapshot with a list of diffs to apply to the snapshot. + The new "snapshot" option just renders conflicts as a series of snapshots, + showing each side of the conflict and the base(s). ### Fixed bugs diff --git a/cli/src/config-schema.json b/cli/src/config-schema.json index b9c347018c..0468e95c35 100644 --- a/cli/src/config-schema.json +++ b/cli/src/config-schema.json @@ -163,7 +163,8 @@ "type": "string", "description": "Conflict marker style to use when materializing conflicts in the working copy", "enum": [ - "diff" + "diff", + "snapshot" ], "default": "diff" } diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index df2cde1b72..f5f1fb9016 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -140,6 +140,8 @@ pub async fn extract_as_single_hunk( pub enum ConflictMarkerStyle { /// Style which shows a snapshot and a series of diffs to apply Diff, + /// Style which shows a snapshot for each base and side + Snapshot, } /// A type similar to `MergedTreeValue` but with associated data to include in @@ -241,7 +243,7 @@ async fn materialize_tree_value_no_access_denied( pub fn materialize_merge_result( single_hunk: &Merge, - _conflict_marker_style: ConflictMarkerStyle, + conflict_marker_style: ConflictMarkerStyle, output: &mut dyn Write, ) -> std::io::Result<()> { let merge_result = files::merge(single_hunk); @@ -280,6 +282,15 @@ pub fn materialize_merge_result( write_base(&base_str, left, output)?; continue; }; + + // For snapshot style, always emit sides and bases separately + if conflict_marker_style == ConflictMarkerStyle::Snapshot { + write_side(add_index, right1, output)?; + write_base(&base_str, left, output)?; + add_index += 1; + continue; + } + let diff1 = Diff::by_line([&left, &right1]).hunks().collect_vec(); // Check if the diff against the next positive term is better. Since // we want to preserve the order of the terms, we don't match against