Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion gix-diff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ autotests = false
[features]
default = ["blob", "index"]
## Enable diffing of blobs using imara-diff.
blob = ["dep:imara-diff", "dep:gix-filter", "dep:gix-worktree", "dep:gix-path", "dep:gix-fs", "dep:gix-command", "dep:gix-tempfile", "dep:gix-trace", "dep:gix-traverse"]
blob = [
"dep:imara-diff",
"dep:gix-filter",
"dep:gix-worktree",
"dep:gix-path",
"dep:gix-fs",
"dep:gix-command",
"dep:gix-tempfile",
"dep:gix-trace",
"dep:gix-traverse"
]
## An experimental use of the v0.2 branch of `imara-diff` to allow trying it out, and for writing tests against it more easily.
## We will decide later how it should actually be exposed.
blob-experimental = ["dep:imara-diff-v2"]
## Enable diffing of two indices, which also allows for a generic rewrite tracking implementation.
index = ["dep:gix-index", "dep:gix-pathspec", "dep:gix-attributes"]
## Data structures implement `serde::Serialize` and `serde::Deserialize`.
Expand Down Expand Up @@ -43,6 +56,7 @@ gix-traverse = { version = "^0.49.0", path = "../gix-traverse", optional = true

thiserror = "2.0.17"
imara-diff = { version = "0.1.8", optional = true }
imara-diff-v2 = { version = "0.2.0", optional = true, package = "imara-diff" }
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
getrandom = { version = "0.2.8", optional = true, default-features = false, features = ["js"] }
bstr = { version = "1.12.0", default-features = false }
Expand Down
36 changes: 36 additions & 0 deletions gix-diff/src/blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ use std::{collections::HashMap, path::PathBuf};
use bstr::BString;
pub use imara_diff::*;

/// Re-export imara-diff v0.2 types for use with slider heuristics.
///
/// This module provides access to the v0.2 API of imara-diff, which includes
/// support for Git's slider heuristics to produce more intuitive diffs.
#[cfg(feature = "blob-experimental")]
pub use imara_diff_v2 as v2;

/// Compute a diff with Git's slider heuristics to produce more intuitive diffs.
///
/// This function uses `imara-diff` v0.2 which provides the [`v2::Diff`] structure
/// that supports postprocessing with slider heuristics. The slider heuristics move
/// diff hunks to more intuitive locations based on indentation and other factors,
/// resulting in diffs that are more readable and match Git's output more closely.
///
/// # Examples
///
/// ```
/// use gix_diff::blob::{diff_with_slider_heuristics, v2::{Algorithm, InternedInput}};
///
/// let before = "fn foo() {\n let x = 1;\n}\n";
/// let after = "fn foo() {\n let x = 2;\n}\n";
///
/// let input = InternedInput::new(before, after);
/// let diff = diff_with_slider_heuristics(Algorithm::Histogram, &input);
///
/// // The diff now has slider heuristics applied
/// assert_eq!(diff.count_removals(), 1);
/// assert_eq!(diff.count_additions(), 1);
Comment on lines +33 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Byron Counting removals and additions probably is not the best example to showcase slider heuristics as line numbers are not affected by moving sliders up and down. :-)

/// ```
#[cfg(feature = "blob-experimental")]
pub fn diff_with_slider_heuristics<T: AsRef<[u8]>>(algorithm: v2::Algorithm, input: &v2::InternedInput<T>) -> v2::Diff {
let mut diff = v2::Diff::compute(algorithm, input);
diff.postprocess_lines(input);
diff
}

///
pub mod pipeline;

Expand Down
2 changes: 1 addition & 1 deletion gix-diff/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "diff"
path = "diff/main.rs"

[dev-dependencies]
gix-diff = { path = ".." }
gix-diff = { path = "..", features = ["blob-experimental"] }
gix-index = { path = "../../gix-index" }
gix-pathspec = { path = "../../gix-pathspec" }
gix-hash = { path = "../../gix-hash" }
Expand Down
1 change: 1 addition & 0 deletions gix-diff/tests/diff/blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub(crate) mod pipeline;
mod platform;
mod slider;
mod unified_diff;
mod v2;
Loading
Loading