Skip to content

Commit

Permalink
conflicts: add "snapshot" conflict marker style
Browse files Browse the repository at this point in the history
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 jj-vcs#1
fn example(word: String) {
    println!("word is {word}");
------- Contents of base
fn example(w: String) {
    println!("word is {w}");
+++++++ Contents of side jj-vcs#2
fn example(w: &str) {
    println!("word is {w}");
>>>>>>> Conflict 1 of 1 ends
}
```
  • Loading branch information
scott2000 committed Nov 22, 2024
1 parent 1d39636 commit c118f74
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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

Expand Down
3 changes: 2 additions & 1 deletion cli/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
13 changes: 12 additions & 1 deletion lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ async fn materialize_tree_value_no_access_denied(
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,
}

pub fn materialize_merge_result<T: AsRef<[u8]>>(
Expand Down Expand Up @@ -272,7 +274,7 @@ pub fn materialize_merge_result_to_bytes<T: AsRef<[u8]>>(

fn materialize_conflict_hunks(
hunks: &[Merge<BString>],
_conflict_marker_style: ConflictMarkerStyle,
conflict_marker_style: ConflictMarkerStyle,
output: &mut dyn Write,
) -> io::Result<()> {
let num_conflicts = hunks
Expand Down Expand Up @@ -304,6 +306,15 @@ fn materialize_conflict_hunks(
write_base(&base_str, left, output)?;
continue;
};

// For any style other than "diff", always emit sides and bases separately
if conflict_marker_style != ConflictMarkerStyle::Diff {
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
Expand Down
Loading

0 comments on commit c118f74

Please sign in to comment.