Crates.io | threeway_merge |
lib.rs | threeway_merge |
version | 0.1.5 |
created_at | 2025-08-27 08:19:20.725371+00 |
updated_at | 2025-08-27 14:29:15.088995+00 |
description | Git-style 3-way string merging using proven algorithms from libgit2/xdiff. |
homepage | https://github.com/levish0/threeway-merge-rs |
repository | https://github.com/levish0/threeway-merge-rs |
max_upload_size | |
id | 1812298 |
size | 163,276 |
threeway-merge-rs
is a Rust library for string-based 3-way merging using Git's proven merge algorithms.
It uses libgit2/xdiff via safe FFI bindings, providing the same merge behavior as git merge-file
but with a pure string API perfect for applications that need to merge text content programmatically.
&str
inputs, no file I/O requiredgit merge-file
(576+ test combinations)Myers
โ Default, standard diffMinimal
โ Minimal changesPatience
โ Patience diff algorithmHistogram
โ Histogram-based diffMinimal
โ Conservative mergingEager
โ Slightly more aggressiveZealous
โ Aggressive mergeZealousAlnum
โ Aggressive with alphanumeric heuristicsNone
โ No automatic favorOurs
โ Prefer "ours" changesTheirs
โ Prefer "theirs" changesUnion
โ Combine changes when possibleNormal
โ Default 3-way mergeDiff3
โ Include base lines in conflictsZealousDiff3
โ Diff3 style with aggressive merging<<<<<<<
, =======
, >>>>>>>
)Add to your Cargo.toml
:
[dependencies]
threeway_merge = "0.1"
use threeway_merge::{merge_strings, MergeOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let base = "Hello world\nSecond line";
let ours = "Hello Rust world\nSecond line";
let theirs = "Hello beautiful world\nSecond line";
let result = merge_strings(base, ours, theirs, &MergeOptions::default())?;
println!("Conflicts: {}", result.conflicts);
println!("Result:\n{}", result.content);
Ok(())
}
use threeway_merge::{
merge_strings, MergeOptions, DiffAlgorithm, MergeStyle, MergeFavor
};
let mut options = MergeOptions::default();
options.algorithm = DiffAlgorithm::Histogram;
options.style = MergeStyle::ZealousDiff3;
options.favor = Some(MergeFavor::Union);
options.base_label = Some("original".to_string());
options.ours_label = Some("mine".to_string());
options.theirs_label = Some("theirs".to_string());
options.marker_size = 10;
let result = merge_strings(base, ours, theirs, &options)?;
This Rust code:
let result = merge_strings(base, ours, theirs, &options)?;
Is equivalent to:
git merge-file --diff-algorithm histogram --zdiff3 \
-L "mine" ours.txt -L "original" base.txt -L "theirs" theirs.txt --stdout
This library achieves 100% compatibility with git merge-file
through comprehensive testing:
# Run all tests
cargo test
# Run Git compatibility tests specifically
cargo test test_comprehensive_git_comparison
# Run with output visible
cargo test -- --nocapture
threeway-merge-rs