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