### Try Mergiraf with example repository
Source: https://mergiraf.org/print.html
Clone and test Mergiraf using the provided example repository.
```bash
$ git clone https://codeberg.org/mergiraf/example-repo
$ cd example-repo
$ git merge origin/other-branch
```
```bash
$ jj git clone https://codeberg.org/mergiraf/example-repo
$ cd example-repo
$ jj new main other-branch@origin
$ jj resolve --tool mergiraf
```
--------------------------------
### Clone Mergiraf Example Repository
Source: https://mergiraf.org/usage.html
Clone this repository to try out Mergiraf with various conflict resolution examples.
```bash
$ git clone https://codeberg.org/mergiraf/example-repo
$ cd example-repo
$ git merge origin/other-branch
```
--------------------------------
### Install Mergiraf from source or crates.io
Source: https://mergiraf.org/installation.html
Use Cargo to build from a local clone or install directly from the crates.io registry.
```bash
cargo install --path .
```
```bash
cargo install --locked mergiraf
```
--------------------------------
### Clone Mergiraf Example Repository with Jujutsu
Source: https://mergiraf.org/usage.html
Clone the example repository using Jujutsu and resolve conflicts with Mergiraf.
```bash
$ jj git clone https://codeberg.org/mergiraf/example-repo
$ cd example-repo
$ jj new main other-branch@origin
$ jj resolve --tool mergiraf
```
--------------------------------
### Install Mergiraf via cargo-binstall
Source: https://mergiraf.org/installation.html
Use the cargo-binstall utility to fetch pre-compiled binaries from Codeberg.
```bash
cargo binstall mergiraf
```
--------------------------------
### Define Node Signatures
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Example configuration for defining signatures for using directives, field declarations, and method declarations.
```rust
__
signature("using_directive", vec![vec![]]),
signature("field_declaration", vec![vec![Field("name")]]),
signature("method_declaration", vec![
vec![Field("name")],
vec![Field("parameters"), ChildType("parameter"), Field("type")]
]),
```
--------------------------------
### Tree to PCS Triples Encoding
Source: https://mergiraf.org/architecture.html
This example shows how a tree structure is converted into a set of Parent-Child-Successor (PCS) triples. Each triple `(p,c,s)` indicates that `p` is the parent of `c` and `s`, and `s` immediately follows `c` in the child list of `p`. Sentinel nodes `⊣` and `⊢`, and a virtual root `⊥` are used.
```plaintext
(⊥, ⊣, source_file)
(⊥, source_file, ⊢)
(source_file, ⊣, struct_item)
(source_file, struct_item, ⊢)
(struct_item, ⊣, "struct")
(struct_item, "struct", "MyStruct")
(struct_item, "MyStruct", field_declaration_list)
(struct_item, field_declaration_list, ⊢)
(field_declaration_list, ⊣, "{")
(field_declaration_list, "{", field_declaration)
(field_declaration_list, field_declaration, "}")
(field_declaration_list, "}", ⊢)
(field_declaration, ⊣, "first")
(field_declaration, "first", ":")
(field_declaration, ":", "usize")
(field_declaration, "usize", ⊢)
```
--------------------------------
### Git Merge Conflict Example with Mergiraf
Source: https://mergiraf.org/
This output shows a typical Git merge conflict scenario where Mergiraf has been used. It highlights the differences between the HEAD version and the incoming commit, indicating a structured resolution process.
```diff
Renamed from /home/user/.local/share/mergiraf/merges/Parser.java_o0i2JL8B/line_based.java to /home/user/.local/share/mergiraf/merges/Parser.java_o0i2JL8B/structured_resolution.java 1 1 <<<<<<< HEAD 2 . import java.lang.IllegalArgumentException; 3 2 import org.codeberg.musicparser.Formats; 4 . ||||||| parent of 23d19ad (Commit on my_branch) 5 . import java.lang.IllegalArgumentException; 6 . ======= 7 . >>>>>>> 23d19ad (Commit on my_branch) 8 3 import org.testng.annotations.Test; 9 4 10 5 class ParserTests { /home/user/.local/share/mergiraf/merges/Parser.java_o0i2JL8B/structured_resolution.java --- 2/2 --- Text (13 Java parse errors, exceeded DFT_PARSE_ERROR_LIMIT) 15 void parseValidInput() throws Exception 10 void parseValidInput() throws Exception .. { .. { 16 String input = "fis g a b a b g c b 11 String input = "fis g a b a b g c b .. c a d es d c"; .. c a d es d c"; 17 12 18 <<<<<<< HEAD 13 Melody melody = SUT.parse(input, For.. .. mats.LILYPOND); 19 Melody melody = null; .. 20 try { .. 21 melody = SUT.parse(input, Format .. .. s.LILYPOND); .. :
```
--------------------------------
### Example of conflicting revisions in Mergiraf
Source: https://mergiraf.org/print.html
Demonstrates a scenario where one revision modifies an element while another deletes it, highlighting a known limitation of the 3DM merge algorithm.
```cpp
__
dismountTire(&wheel);
applyGlueAroundPuncture(&wheel.innerTube);
wait(Duration::minutes(5)); // better wait a bit longer
applyPatch(&wheel.innerTube);
```
```cpp
__
dismountTire(&wheel);
applyGlueAroundPuncture(&wheel.innerTube);
wait(Duration::minutes(2));
applyPatch(&wheel.innerTube);
```
```cpp
__
dismountTire(&wheel);
wheel.innerTube = InnerTube::new(); // otherwise I'll be late to pick up the kids
```
--------------------------------
### Defining Commutative Parent in Mergiraf Language Profile
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Example of how to define a 'compilation_unit' as a commutative parent in a Mergiraf language profile, specifying newline as a separator. This allows reordering of top-level elements like using directives.
```rust
LangProfile {
commutative_parents: vec![
CommutativeParent::without_delimiters("compilation_unit", "\n"),
],
..
},
```
--------------------------------
### C# Using Directives Sample
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
A sample C# file demonstrating the use of 'using' directives. This code is used to illustrate how Mergiraf parses and can potentially reorder these directives.
```csharp
using System;
using System.Collections.Generic;
using System.IO;
namespace HelloWorld {
public class SomeName {
}
}
```
--------------------------------
### Define TypeScript Interface
Source: https://mergiraf.org/adding-a-language/advanced-configuration.html
Example of a TypeScript interface containing a union type for string literals.
```typescript
export interface MyInterface {
level: 'debug' | 'info' | 'warn' | 'error';
}
```
--------------------------------
### C# Using Directives Sample
Source: https://mergiraf.org/print.html
A sample C# file demonstrating the use of 'using' directives. These are often candidates for commutative parents due to their order being semantically irrelevant.
```csharp
using System;
using System.Collections.Generic;
using System.IO;
namespace HelloWorld {
public class SomeName {
}
}
```
--------------------------------
### Generate Gitattributes for Supported Languages
Source: https://mergiraf.org/usage.html
Run this command to generate a list of supported file extensions for inclusion in your `.gitattributes` file.
```bash
$ mergiraf languages --gitattributes
```
--------------------------------
### Define Test Directory Structure
Source: https://mergiraf.org/adding-a-language/testing.html
Create a directory following this naming convention to add a new test case.
```text
examples/csharp/working/add_imports
```
--------------------------------
### Build Mergiraf
Source: https://mergiraf.org/adding-a-language.html
Compile the updated Mergiraf project to generate the binary with support for the new language.
```bash
$ cargo build
```
--------------------------------
### Configure Conflict Display
Source: https://mergiraf.org/print.html
Comparison between default line-aligned conflict markers and the compact mode which highlights only mismatched parts.
```text
<<<<<<< HEAD
||||||| 15b798c
=======
>>>>>>> origin/main
```
```text
>>>>>> origin/main
>
```
--------------------------------
### Define pyproject.toml Test Structure
Source: https://mergiraf.org/adding-a-language/testing.html
Include a language file when testing configuration files like pyproject.toml.
```text
Base.toml
Left.toml
Right.toml
Expected.toml
language // contains "pyproject.toml"
```
--------------------------------
### Define Makefile Test Structure
Source: https://mergiraf.org/adding-a-language/testing.html
Include a language file when testing full file names like Makefile.
```text
Base
Left
Right
Expected
language // contains "Makefile" (without the quotes)
```
--------------------------------
### Enable Mergiraf for All Files
Source: https://mergiraf.org/usage.html
Add this line to your global gitattributes file to enable Mergiraf for all files in your repositories.
```gitattributes
* merge=mergiraf
```
--------------------------------
### Run Individual Test
Source: https://mergiraf.org/adding-a-language/testing.html
Execute the inspection helper to compare actual merge output against expected results.
```bash
$ helpers/inspect.sh examples/csharp/working/add_imports
```
--------------------------------
### Configure gitattributes for Mergiraf
Source: https://mergiraf.org/print.html
Define which files should be processed by the Mergiraf merge driver.
```text
* merge=mergiraf
```
```text
*.py merge=mergiraf
*.java merge=mergiraf
```
--------------------------------
### Define Test File Requirements
Source: https://mergiraf.org/adding-a-language/testing.html
Each test directory must contain these four files representing the merge states.
```text
Base.cs
Left.cs
Right.cs
Expected.cs
```
--------------------------------
### Compact Conflict Presentation
Source: https://mergiraf.org/usage.html
Use the `--compact` or `-c` option with `mergiraf merge` for a more compact conflict presentation that highlights only mismatching parts. Note that reformatting may be required after resolving conflicts in this mode.
```html
>>>>>> origin/main
>
```
--------------------------------
### Register Mergiraf as Git Merge Driver via CLI
Source: https://mergiraf.org/usage.html
Use these commands to register Mergiraf as a merge driver globally via the command line.
```bash
$ git config --global merge.mergiraf.name mergiraf
$ git config --global merge.mergiraf.driver 'mergiraf merge --git %O %A %B -s %S -x %X -y %Y -p %P -l %L'
```
--------------------------------
### Tree-sitter Method Declaration Structure
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Visual representation of a method declaration node structure as parsed by tree-sitter.
```text
└method_declaration
├type: predefined_type void
├name: identifier Run
├parameters: parameter_list
│ ├(
│ ├parameter
│ │ ├type: predefined_type int
│ │ └name: identifier times
│ ├,
│ ├parameter
│ │ ├type: predefined_type bool
│ │ └name: identifier fast
│ └)
└body: block
├{
└}
```
--------------------------------
### Enable diff3 Conflict Style via CLI
Source: https://mergiraf.org/usage.html
Alternatively, run this command to enable the diff3 merge conflict style globally.
```bash
$ git config --global merge.conflictStyle diff3
```
--------------------------------
### Identify conflicting instruction additions
Source: https://mergiraf.org/conflicts.html
Illustrates a scenario where human intervention is required due to conflicting order of operations in a loop.
```java
while (true) {
mowTheLawn();
rechargeBatteries();
}
```
```java
while (true) {
mowTheLawn();
}
```
```java
while (true) {
mowTheLawn();
returnToHomeBase();
}
```
--------------------------------
### Define Test Case Directory Structure
Source: https://mergiraf.org/print.html
The required directory structure for adding a new end-to-end test case.
```text
examples/csharp/working/add_imports
```
```text
Base.cs
Left.cs
Right.cs
Expected.cs
```
```text
Base
Left
Right
Expected
language // contains "Makefile" (without the quotes)
```
```text
Base.toml
Left.toml
Right.toml
Expected.toml
language // contains "pyproject.toml"
```
--------------------------------
### Register Mergiraf as Git Merge Driver
Source: https://mergiraf.org/usage.html
Add this section to your `~/.config/git/config` file to register Mergiraf as a merge driver. This enables Mergiraf during merge operations.
```gitconfig
[merge "mergiraf"]
name = mergiraf
driver = mergiraf merge --git %O %A %B -s %S -x %X -y %Y -p %P -l %L
```
--------------------------------
### Specify Language for Mergiraf
Source: https://mergiraf.org/usage.html
Use the `--language` option to specify the language for Mergiraf if it does not recognize the file's extension. This can be a file extension or a language name.
```bash
*.myjs linguist-language=javascript
```
--------------------------------
### Define Language Profile
Source: https://mergiraf.org/adding-a-language.html
Register the new language in src/supported_langs.rs by defining a LangProfile struct with the necessary parser and configuration details.
```rust
LangProfile {
name: "C#", // used for the --language CLI option, and generating the list of supported languages
alternate_names: vec![], // other possible values for --language
extensions: vec!["cs"], // all file extensions for this language (note the lack of `.`!)
file_names: vec![], // the full file names which should be handled with this language
language: tree_sitter_c_sharp::LANGUAGE.into(), // the tree-sitter parser
// optional settings, explained below
commutative_parents: vec![],
signatures: vec![],
atomic_nodes: vec![],
injections: None,
flattened_nodes: &[],
comment_nodes: &[],
},
```
--------------------------------
### Parsing a C# File with Mergiraf
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Command to parse a C# file using Mergiraf's command-line interface. This helps in visualizing the syntax tree and understanding how the code is structured.
```bash
$ cargo parse test_file.cs
```
--------------------------------
### Register Mergiraf as a Git merge driver
Source: https://mergiraf.org/print.html
Configure Git to use Mergiraf as the default driver for merge operations.
```ini
[merge "mergiraf"]
name = mergiraf
driver = mergiraf merge --git %O %A %B -s %S -x %X -y %Y -p %P -l %L
```
```bash
$ git config --global merge.mergiraf.name mergiraf
$ git config --global merge.mergiraf.driver 'mergiraf merge --git %O %A %B -s %S -x %X -y %Y -p %P -l %L'
```
--------------------------------
### Enable diff3 conflict style
Source: https://mergiraf.org/print.html
Configure Git to use the diff3 conflict style for better merge conflict resolution.
```ini
[merge]
conflictStyle = "diff3"
```
```bash
$ git config --global merge.conflictStyle diff3
```
--------------------------------
### Enable diff3 Conflict Style
Source: https://mergiraf.org/usage.html
Add this section to your `~/.gitconfig` file to enable the diff3 merge conflict style, which is recommended for Mergiraf.
```gitconfig
[merge]
conflictStyle = "diff3"
```
--------------------------------
### Merging Conflicting Using Statements
Source: https://mergiraf.org/print.html
Demonstrates a conflict scenario with three different sets of 'using' statements (Left, Base, Right) that Mergiraf can resolve by merging them when 'compilation_unit' is a commutative parent.
```text
Left
```
using System;
using System.Collections.Generic;
```
Base
```
using System;
```
Right
```
using System;
using System.IO;
```
```
--------------------------------
### Merge Rust formatting and content changes
Source: https://mergiraf.org/conflicts.html
Shows how Mergiraf attempts to preserve formatting while applying content changes to a function signature.
```rust
fn plan_route(
start: &Location,
end: &Location,
settings: &RouteSettings,
) -> Route {
todo!();
}
```
```rust
fn plan_route(start: &Location, end: &Location, settings: &RouteSettings) -> Route {
todo!();
}
```
```rust
fn plan_route(start: &Location, end: &Location, settings: Option<&RouteSettings>) -> Route {
todo!();
}
```
```rust
fn plan_route(
start: &Location,
end: &Location,
settings: Option<&RouteSettings>,
) -> Route {
todo!();
}
```
--------------------------------
### Solve Conflicts Automatically
Source: https://mergiraf.org/print.html
Workflow for identifying, solving, and verifying conflict resolution in a Git repository.
```bash
$ git merge origin/main
Auto-merging config.yml
CONFLICT (content): Merge conflict in config.yml
Automatic merge failed; fix conflicts and then commit the result.
$ cat config.yml
<<<<<<< HEAD
restaurant:
tasks:
plates: 1
bowls: 2
||||||| 15b798c
tasks:
plates: 1
bowls: 2
=======
tasks:
plates: 1
bowls: 4
>>>>>>> origin/main
```
```bash
$ mergiraf solve config.yml
Solved 1 conflict(s)
```
```text
restaurant:
tasks:
plates: 1
bowls: 4
```
--------------------------------
### Report a Merge Bug
Source: https://mergiraf.org/print.html
Commands to generate a reproduction archive for merge issues, using either a merge ID or a file path.
```bash
$ git rebase origin/master
INFO Mergiraf: Solved 2 conflicts. Review with: mergiraf review geolocation.cpp_o0i2JL8B
Successfully rebased and updated refs/heads/my_branch.
$ mergiraf review geolocation.cpp_o0i2JL8B
$ mergiraf report geolocation.cpp_xyuSMcme
Bug report archive created:
mergiraf_report_6weNKAXO.zip
Please submit it to https://codeberg.org/mergiraf/mergiraf/issues if you are happy with its contents being published,
or reach out privately to a contributor if not.
Thank you for helping Mergiraf improve!
```
```bash
$ mergiraf report src/lib/geolocation.cpp
```
--------------------------------
### Enable Mergiraf for Specific Languages
Source: https://mergiraf.org/usage.html
To enable Mergiraf for specific languages, add lines like these to your global gitattributes file, specifying the file extension.
```gitattributes
*.py merge=mergiraf
*.java merge=mergiraf
```
--------------------------------
### Defining JSON Object as Commutative Parent
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Illustrates how to define a JSON object as a commutative parent using its node type, opening delimiter '{', closing delimiter '}', and the separator ', '.
```rust
CommutativeParent::new("object", "{", ", ", "}")
```
--------------------------------
### Merging Conflicting Using Directives
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Demonstrates a conflict scenario with three different sets of 'using' directives from left, base, and right versions. Mergiraf, with commutative parent definitions, can merge these to include all unique directives.
```text
Left
```
using System;
using System.Collections.Generic;
```
Base
```
using System;
```
Right
```
using System;
using System.IO;
```
```
--------------------------------
### Syntax Tree Representation
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
The output of parsing a C# file, showing the hierarchical structure of the code as a syntax tree. This visualization is key to identifying nodes like 'using_directive'.
```text
└compilation_unit
├using_directive
│ ├using
│ ├identifier System
│ └;
├using_directive
│ ├using
│ ├qualified_name
│ │ ├qualifier: qualified_name
│ │ │ ├qualifier: identifier System
│ │ │ ├.
│ │ │ └name: identifier Collections
│ │ ├.
│ │ └name: identifier Generic
│ └;
├using_directive
│ ├using
│ ├qualified_name
│ │ ├qualifier: identifier System
│ │ ├.
│ │ └name: identifier IO
│ └;
└namespace_declaration
├namespace
├name: identifier HelloWorld
└body: declaration_list
├{
├class_declaration
│ ├modifier
│ │ └public
│ ├class
│ ├name: identifier SomeName
│ └body: declaration_list
│ ├{
│ └}
└}
```
--------------------------------
### Resolve C function signature conflicts
Source: https://mergiraf.org/conflicts.html
Demonstrates independent changes to function return types and argument types being merged automatically.
```c
void notify_attendees(long status_code);
```
```c
void notify_attendees(int status_code);
```
```c
int notify_attendees(int status_code);
```
```c
int notify_attendees(long status_code);
```
--------------------------------
### Add Tree-sitter Parser Dependency
Source: https://mergiraf.org/adding-a-language.html
Add the required tree-sitter parser crate to the Cargo.toml file, ensuring version compatibility with the current Mergiraf tree-sitter version.
```toml
[dependencies]
tree-sitter-csharp = "0.21.3"
```
--------------------------------
### Report a Faulty Merge (With Conflicts)
Source: https://mergiraf.org/usage.html
When a merge has conflicts, use the file path instead of the merge id with `mergiraf report` to generate a bug report archive.
```bash
$ mergiraf report src/lib/geolocation.cpp
```
--------------------------------
### Syntax Tree Representation
Source: https://mergiraf.org/print.html
The resulting syntax tree structure after parsing a C# file with Mergiraf. It highlights how 'using' statements are represented as 'using_directive' nodes.
```text
└compilation_unit
├using_directive
│ ├using
│ ├identifier System
│ └;
├using_directive
│ ├using
│ ├qualified_name
│ │ ├qualifier: qualified_name
│ │ │ ├qualifier: identifier System
│ │ │ ├.
│ │ │ └name: identifier Collections
│ │ ├.
│ │ └name: identifier Generic
│ └;
├using_directive
│ ├using
│ ├qualified_name
│ │ ├qualifier: identifier System
│ │ ├.
│ │ └name: identifier IO
│ └;
└namespace_declaration
├namespace
├name: identifier HelloWorld
└body: declaration_list
├{
├class_declaration
│ ├modifier
│ │ └public
│ ├class
│ ├name: identifier SomeName
│ └body: declaration_list
│ ├{
│ └}
└}
```
--------------------------------
### Automatic Conflict Resolution
Source: https://mergiraf.org/usage.html
When a merge conflict occurs, you can use `mergiraf solve ` to attempt automatic resolution. After resolution, inspect the file and continue the merge process with Git.
```bash
$ git merge origin/main
Auto-merging config.yml
CONFLICT (content): Merge conflict in config.yml
Automatic merge failed; fix conflicts and then commit the result.
$ cat config.yml
<<<<<<< HEAD
restaurant:
tasks:
plates: 1
bowls: 2
||||||| 15b798c
tasks:
plates: 1
bowls: 2
=======
tasks:
plates: 1
bowls: 4
>>>>>>> origin/main
$ mergiraf solve config.yml
Solved 1 conflict(s)
restaurant:
tasks:
plates: 1
bowls: 4
$ git add config.yml
$ git merge --continue
```
--------------------------------
### Semantic Merge of Moved and Modified Rust Code
Source: https://mergiraf.org/print.html
Demonstrates Mergiraf replaying changes from a right branch onto a refactored method in a left branch.
```rust
__
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
self.is_suitable_home(station)
})
}
fn is_suitable_home(&self, station: &Station) -> bool {
station.id == self.home_station_id
&& !station.occupied
&& station.color == StationColor::Red
}
}
```
```rust
__
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
station.id == "home"
&& !station.occupied
&& station.color == StationColor::Red
})
}
}
```
```rust
__
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
station.id == "home"
&& !station.occupied
&& station.color == StationColor::Blue
})
}
}
```
```rust
__
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
self.is_suitable_home(station)
})
}
fn is_suitable_home(&self, station: &Station) -> bool {
station.id == self.home_station_id
&& !station.occupied
&& station.color == StationColor::Blue
}
}
```
--------------------------------
### Resolve Java class attribute additions
Source: https://mergiraf.org/conflicts.html
Shows how Mergiraf merges non-conflicting additions of class attributes in Java.
```java
class Bird {
String species;
int weight;
}
```
```java
class Bird {
String species;
}
```
```java
class Bird {
String species;
double wingspan;
}
```
```java
class Bird {
String species;
int weight;
double wingspan;
}
```
--------------------------------
### Report a Faulty Merge (No Conflicts)
Source: https://mergiraf.org/usage.html
If a merge produces an odd output without conflicts, use the merge id from Git's output with `mergiraf report` to generate a bug report archive. Submit this archive to help improve Mergiraf.
```bash
$ git rebase origin/master
INFO Mergiraf: Solved 2 conflicts. Review with: mergiraf review geolocation.cpp_o0i2JL8B
Successfully rebased and updated refs/heads/my_branch.
$ mergiraf review geolocation.cpp_o0i2JL8B
$ mergiraf report geolocation.cpp_xyuSMcme
Bug report archive created:
mergiraf_report_6weNKAXO.zip
Please submit it to https://codeberg.org/mergiraf/mergiraf/issues if you are happy with its contents being published,
or reach out privately to a contributor if not.
Thank you for helping Mergiraf improve!
```
--------------------------------
### Resolve All Merge Conflicts in Jujutsu
Source: https://mergiraf.org/usage.html
In Jujutsu, merges never fail. To resolve all conflicts in your working copy at once, use `jj resolve --tool mergiraf`. This command prepares files with Git-style conflict markers for Mergiraf.
```bash
jj resolve --tool mergiraf
```
--------------------------------
### Define Commutative Parent using Tree-sitter Query
Source: https://mergiraf.org/adding-a-language/enabling-commutative-merging.html
Use `CommutativeParent::from_query` with a tree-sitter query to define commutative parents based on more complex contextual patterns. The `@commutative` capture identifies the node to be treated as commutative.
```tree-sitter query
(expression_statement (assignment
left: (identifier) @variable (#eq? @variable "__all__")
right: (list) @commutative
))
```
```rust
CommutativeParent::from_query(
r#"(expression_statement (assignment
left: (identifier) @variable (#eq? @variable "__all__")
right: (list) @commutative)
)"#,
"[", ", ", "]",
)
```
--------------------------------
### JSON Key Conflict Resolution
Source: https://mergiraf.org/print.html
Shows how Mergiraf detects duplicate keys in JSON objects that line-based merge tools would incorrectly merge.
```json
__
{
"new_letter": "left value",
"alpha": "α",
"beta": "β",
"gamma": "γ",
"delta": "δ"
}
```
```json
__
{
"alpha": "α",
"beta": "β",
"gamma": "γ",
"delta": "δ"
}
```
```json
__
{
"alpha": "α",
"beta": "β",
"gamma": "γ",
"delta": "δ",
"new_letter": "right value"
}
```
```json
__
{
"new_letter": "left value",
"alpha": "α",
"beta": "β",
"gamma": "γ",
"delta": "δ",
"new_letter": "right value"
}
```
```json
__
{
<<<<<<< LEFT
"new_letter": "left value",
||||||| BASE
=======
"new_letter": "right value",
>>>>>>> RIGHT
"alpha": "α",
"beta": "β",
"gamma": "γ",
"delta": "δ"
}
```
--------------------------------
### Replaying Changes on Moved Code Sections
Source: https://mergiraf.org/conflicts.html
Mergiraf can replay changes from one branch onto a code section that has been moved in another. This requires access to base, left, and right revisions.
```rust
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
self.is_suitable_home(station)
})
}
fn is_suitable_home(&self, station: &Station) -> bool {
station.id == self.home_station_id
&& !station.occupied
&& station.color == StationColor::Red
}
}
```
```rust
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
station.id == "home"
&& !station.occupied
&& station.color == StationColor::Red
})
}
}
```
```rust
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
station.id == "home"
&& !station.occupied
&& station.color == StationColor::Blue
})
}
}
```
```rust
impl Lawnmower {
fn find_home_station(&self) -> Option<&Station> {
self.neighbouring_stations().iter().find(|station| {
self.is_suitable_home(station)
})
}
fn is_suitable_home(&self, station: &Station) -> bool {
station.id == self.home_station_id
&& !station.occupied
&& station.color == StationColor::Blue
}
}
```
--------------------------------
### Resolve HTML attribute conflicts
Source: https://mergiraf.org/conflicts.html
Demonstrates merging of attributes in self-closing tags where order is irrelevant.
```html
```
```html
```
```html
```
```html
```
--------------------------------
### Debug Test Case
Source: https://mergiraf.org/adding-a-language/testing.html
Use the Rust integration test to debug specific test cases with an IDE.
```rust
// use this test to debug a specific test case by changing the path in it.
#[test]
fn debug_test() {
run_test_from_dir(Path::new("examples/go/working/remove_and_add_imports"))
}
```
--------------------------------
### Resolve Single File Merge Conflict in Jujutsu
Source: https://mergiraf.org/usage.html
To resolve a specific file's merge conflicts in Jujutsu, use `jj resolve --tool mergiraf `. Jujutsu automatically configures Mergiraf as a merge tool.
```bash
jj resolve --tool mergiraf
```
--------------------------------
### Merge Conflict Scenario: Base Revision
Source: https://mergiraf.org/architecture.html
Represents the base state of the code before modifications.
```cpp
__
dismountTire(&wheel);
applyGlueAroundPuncture(&wheel.innerTube);
wait(Duration::minutes(2));
applyPatch(&wheel.innerTube);
```
--------------------------------
### Default Conflict Alignment
Source: https://mergiraf.org/usage.html
Mergiraf aligns conflicts to line boundaries by default, which aids resolution in standard merge tools.
```html
<<<<<<< HEAD