diff --git a/Cargo.toml b/Cargo.toml index f94b45b..ec35dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "diffy" +name = "diffy-fork-filenames" version = "0.3.0" -authors = ["Brandon Williams "] +authors = ["Roben Kleene "] license = "MIT OR Apache-2.0" -description = "Tools for finding and manipulating differences between files" +description = "Fork of https://docs.rs/diffy that allows specifiying filenames" documentation = "https://docs.rs/diffy" -repository = "https://github.com/bmwill/diffy" +repository = "https://github.com/robenkleene/diffy" readme = "README.md" keywords = ["diff", "patch", "merge"] categories = ["text-processing"] diff --git a/README.md b/README.md index 4b9fce7..787fa4a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # diffy +**A fork of [diffy](https://github.com/robenkleene/diffy) that adds support for changing changing the filenames.** + [![diffy on crates.io](https://img.shields.io/crates/v/diffy)](https://crates.io/crates/diffy) [![Documentation (latest release)](https://docs.rs/diffy/badge.svg)](https://docs.rs/diffy/) [![Documentation (master)](https://img.shields.io/badge/docs-master-59f)](https://bmwill.github.io/diffy/diffy/) diff --git a/src/diff/mod.rs b/src/diff/mod.rs index a456c41..b74bac8 100644 --- a/src/diff/mod.rs +++ b/src/diff/mod.rs @@ -95,14 +95,12 @@ impl DiffOptions { /// Produce a Patch between two texts based on the configured options pub fn create_patch<'a>(&self, original: &'a str, modified: &'a str) -> Patch<'a, str> { - let mut classifier = Classifier::default(); - let (old_lines, old_ids) = classifier.classify_lines(original); - let (new_lines, new_ids) = classifier.classify_lines(modified); - - let solution = self.diff_slice(&old_ids, &new_ids); + self.construct_patch(original, modified, "original", "modified") + } - let hunks = to_hunks(&old_lines, &new_lines, &solution, self.context_len); - Patch::new(Some("original"), Some("modified"), hunks) + /// Produce a Patch between two texts from files based on the configured options + pub fn create_file_patch<'a>(&self, original: &'a str, modified: &'a str, original_filename: &'a str, modified_filename: &'a str) -> Patch<'a, str> { + self.construct_patch(original, modified, original_filename, modified_filename) } /// Create a patch between two potentially non-utf8 texts @@ -134,6 +132,17 @@ impl DiffOptions { solution } + + fn construct_patch<'a>(&self, original: &'a str, modified: &'a str, original_filename: &'a str, modified_filename: &'a str) -> Patch<'a, str> { + let mut classifier = Classifier::default(); + let (old_lines, old_ids) = classifier.classify_lines(original); + let (new_lines, new_ids) = classifier.classify_lines(modified); + + let solution = self.diff_slice(&old_ids, &new_ids); + + let hunks = to_hunks(&old_lines, &new_lines, &solution, self.context_len); + Patch::new(Some(original_filename), Some(modified_filename), hunks) + } } impl Default for DiffOptions { @@ -185,6 +194,10 @@ pub fn create_patch<'a>(original: &'a str, modified: &'a str) -> Patch<'a, str> DiffOptions::default().create_patch(original, modified) } +pub fn create_file_patch<'a>(original: &'a str, modified: &'a str, original_filename: &'a str, modified_filename: &'a str) -> Patch<'a, str> { + DiffOptions::default().create_file_patch(original, modified, original_filename, modified_filename) +} + /// Create a patch between two potentially non-utf8 texts pub fn create_patch_bytes<'a>(original: &'a [u8], modified: &'a [u8]) -> Patch<'a, [u8]> { DiffOptions::default().create_patch_bytes(original, modified) diff --git a/src/lib.rs b/src/lib.rs index 2d4b0dc..3eb1427 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -220,6 +220,6 @@ mod range; mod utils; pub use apply::{apply, apply_bytes, ApplyError}; -pub use diff::{create_patch, create_patch_bytes, DiffOptions}; +pub use diff::{create_patch, create_file_patch, create_patch_bytes, DiffOptions}; pub use merge::{merge, merge_bytes, ConflictStyle, MergeOptions}; pub use patch::{Hunk, HunkRange, Line, ParsePatchError, Patch, PatchFormatter};