gix_diff/blob/
mod.rs

1//! For using text diffs, please have a look at the [`imara-diff` documentation](https://docs.rs/imara-diff),
2//! maintained by [Pascal Kuthe](https://github.com/pascalkuthe).
3use std::{collections::HashMap, path::PathBuf};
4
5use bstr::BString;
6pub use imara_diff::*;
7
8///
9pub mod pipeline;
10
11///
12pub mod platform;
13
14/// Information about the diff performed to detect similarity.
15#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
16pub struct DiffLineStats {
17    /// The amount of lines to remove from the source to get to the destination.
18    pub removals: u32,
19    /// The amount of lines to add to the source to get to the destination.
20    pub insertions: u32,
21    /// The amount of lines of the previous state, in the source.
22    pub before: u32,
23    /// The amount of lines of the new state, in the destination.
24    pub after: u32,
25    /// A range from 0 to 1.0, where 1.0 is a perfect match and 0.5 is a similarity of 50%.
26    /// Similarity is the ratio between all lines in the previous blob and the current blob,
27    /// calculated as `(old_lines_count - new_lines_count) as f32 / old_lines_count.max(new_lines_count) as f32`.
28    pub similarity: f32,
29}
30
31/// A way to classify a resource suitable for diffing.
32#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
33pub enum ResourceKind {
34    /// The source of a rewrite, rename or copy operation, or generally the old version of a resource.
35    OldOrSource,
36    /// The destination of a rewrite, rename or copy operation, or generally the new version of a resource.
37    NewOrDestination,
38}
39
40/// A set of values to define how to diff something that is associated with it using `git-attributes`, relevant for regular files.
41///
42/// Some values are related to diffing, some are related to conversions.
43#[derive(Default, Debug, Clone, PartialEq, Eq)]
44pub struct Driver {
45    /// The name of the driver, as referred to by `[diff "name"]` in the git configuration.
46    pub name: BString,
47    /// The command to execute to perform the diff entirely like `<command> old-file old-hex old-mode new-file new-hex new-mode`.
48    ///
49    /// Please note that we don't make this call ourselves, but use it to determine that we should not run the our standard
50    /// built-in algorithm but bail instead as the output of such a program isn't standardized.
51    pub command: Option<BString>,
52    /// The per-driver algorithm to use.
53    pub algorithm: Option<Algorithm>,
54    /// The external filter program to call like `<binary_to_text_command> /path/to/blob` which outputs a textual version of the provided
55    /// binary file.
56    /// Note that it's invoked with a shell if arguments are given.
57    /// Further, if present, it will always be executed, whether `is_binary` is set or not.
58    pub binary_to_text_command: Option<BString>,
59    /// `Some(true)` if this driver deals with binary files, which means that a `binary_to_text_command` should be used to convert binary
60    /// into a textual representation.
61    /// Without such a command, anything that is considered binary is not diffed, but only the size of its data is made available.
62    /// If `Some(false)`, it won't be considered binary, and the its data will not be sampled for the null-byte either.
63    /// Leaving it to `None` means binary detection is automatic, and is based on the presence of the `0` byte in the first 8kB of the buffer.
64    pub is_binary: Option<bool>,
65}
66
67/// A conversion pipeline to take an object or path from what's stored in `git` to what can be diffed, while
68/// following the guidance of git-attributes at the respective path to learn if diffing should happen or if
69/// the content is considered binary.
70///
71/// There are two different conversion flows, where the target of the flow is a buffer with diffable content:
72// TODO: update this with information about possible directions.
73///
74/// * `worktree on disk` -> `text conversion`
75/// * `object` -> `worktree-filters` -> `text conversion`
76#[derive(Clone)]
77pub struct Pipeline {
78    /// A way to read data directly from the worktree.
79    pub roots: pipeline::WorktreeRoots,
80    /// A pipeline to convert objects from what's stored in `git` to its worktree version.
81    pub worktree_filter: gix_filter::Pipeline,
82    /// Options affecting the way we read files.
83    pub options: pipeline::Options,
84    /// Drivers to help customize the conversion behaviour depending on the location of items.
85    drivers: Vec<Driver>,
86    /// Pre-configured attributes to obtain additional diff-related information.
87    attrs: gix_filter::attributes::search::Outcome,
88    /// A buffer to manipulate paths
89    path: PathBuf,
90}
91
92/// A utility for performing a diff of two blobs, including flexible conversions, conversion-caching
93/// acquisition of diff information.
94/// Note that this instance will not call external filters as their output can't be known programmatically,
95/// but it allows to prepare their input if the caller wishes to perform this task.
96///
97/// Optimized for NxM lookups with built-in caching.
98#[derive(Clone)]
99pub struct Platform {
100    /// The old version of a diff-able blob, if set.
101    old: Option<platform::CacheKey>,
102    /// The new version of a diff-able blob, if set.
103    new: Option<platform::CacheKey>,
104
105    /// Options to alter how diffs should be performed.
106    pub options: platform::Options,
107    /// A way to convert objects into a diff-able format.
108    pub filter: Pipeline,
109    /// A way to access .gitattributes
110    pub attr_stack: gix_worktree::Stack,
111
112    /// The way we convert resources into diffable states.
113    filter_mode: pipeline::Mode,
114    /// A continuously growing cache keeping ready-for-diff blobs by their path in the worktree,
115    /// as that is what affects their final diff-able state.
116    ///
117    /// That way, expensive rewrite-checks with NxM matrix checks would be as fast as possible,
118    /// avoiding duplicate work.
119    diff_cache: HashMap<platform::CacheKey, platform::CacheValue>,
120    /// A list of previously used buffers, ready for re-use.
121    free_list: Vec<Vec<u8>>,
122}
123
124mod impls {
125    use crate::blob::ResourceKind;
126
127    impl std::fmt::Display for ResourceKind {
128        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129            f.write_str(match self {
130                ResourceKind::OldOrSource => "old",
131                ResourceKind::NewOrDestination => "new",
132            })
133        }
134    }
135}