gix_diff/blob/
platform.rs

1use bstr::{BStr, BString, ByteSlice};
2use std::cmp::Ordering;
3use std::{io::Write, process::Stdio};
4
5use super::Algorithm;
6use crate::blob::{pipeline, Pipeline, Platform, ResourceKind};
7
8/// A key to uniquely identify either a location in the worktree, or in the object database.
9#[derive(Clone)]
10pub(crate) struct CacheKey {
11    id: gix_hash::ObjectId,
12    location: BString,
13    /// If `true`, this is an `id` based key, otherwise it's location based.
14    use_id: bool,
15    /// Only relevant when `id` is not null, to further differentiate content and allow us to
16    /// keep track of both links and blobs with the same content (rare, but possible).
17    is_link: bool,
18}
19
20/// A stored value representing a diffable resource.
21#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
22pub(crate) struct CacheValue {
23    /// The outcome of converting a resource into a diffable format using [Pipeline::convert_to_diffable()].
24    conversion: pipeline::Outcome,
25    /// The kind of the resource we are looking at. Only possible values are `Blob`, `BlobExecutable` and `Link`.
26    mode: gix_object::tree::EntryKind,
27    /// A possibly empty buffer, depending on `conversion.data` which may indicate the data is considered binary.
28    buffer: Vec<u8>,
29}
30
31impl std::hash::Hash for CacheKey {
32    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
33        if self.use_id {
34            self.id.hash(state);
35            self.is_link.hash(state);
36        } else {
37            self.location.hash(state);
38        }
39    }
40}
41
42impl PartialEq for CacheKey {
43    fn eq(&self, other: &Self) -> bool {
44        match (self.use_id, other.use_id) {
45            (false, false) => self.location.eq(&other.location),
46            (true, true) => self.id.eq(&other.id) && self.is_link.eq(&other.is_link),
47            _ => false,
48        }
49    }
50}
51
52impl Eq for CacheKey {}
53
54impl Default for CacheKey {
55    fn default() -> Self {
56        CacheKey {
57            id: gix_hash::Kind::Sha1.null(),
58            use_id: false,
59            is_link: false,
60            location: BString::default(),
61        }
62    }
63}
64
65impl CacheKey {
66    fn set_location(&mut self, rela_path: &BStr) {
67        self.location.clear();
68        self.location.extend_from_slice(rela_path);
69    }
70}
71
72/// A resource ready to be diffed in one way or another.
73#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
74pub struct Resource<'a> {
75    /// If available, an index into the `drivers` field to access more diff-related information of the driver for items
76    /// at the given path, as previously determined by git-attributes.
77    ///
78    /// Note that drivers are queried even if there is no object available.
79    pub driver_index: Option<usize>,
80    /// The data itself, suitable for diffing, and if the object or worktree item is present at all.
81    pub data: resource::Data<'a>,
82    /// The kind of the resource we are looking at. Only possible values are `Blob`, `BlobExecutable` and `Link`.
83    pub mode: gix_object::tree::EntryKind,
84    /// The location of the resource, relative to the working tree.
85    pub rela_path: &'a BStr,
86    /// The id of the content as it would be stored in `git`, or `null` if the content doesn't exist anymore at
87    /// `rela_path` or if it was never computed. This can happen with content read from the worktree, which has to
88    /// go through a filter to be converted back to what `git` would store.
89    pub id: &'a gix_hash::oid,
90}
91
92///
93pub mod resource {
94    use crate::blob::{
95        pipeline,
96        platform::{CacheKey, CacheValue, Resource},
97    };
98
99    impl<'a> Resource<'a> {
100        pub(crate) fn new(key: &'a CacheKey, value: &'a CacheValue) -> Self {
101            Resource {
102                driver_index: value.conversion.driver_index,
103                data: value.conversion.data.map_or(Data::Missing, |data| match data {
104                    pipeline::Data::Buffer { is_derived } => Data::Buffer {
105                        buf: &value.buffer,
106                        is_derived,
107                    },
108                    pipeline::Data::Binary { size } => Data::Binary { size },
109                }),
110                mode: value.mode,
111                rela_path: key.location.as_ref(),
112                id: &key.id,
113            }
114        }
115
116        /// Produce an iterator over lines, separated by LF or CRLF and thus keeping newlines.
117        ///
118        /// Note that this will cause unusual diffs if a file didn't end in newline but lines were added
119        /// on the other side.
120        ///
121        /// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
122        pub fn intern_source(&self) -> imara_diff::sources::ByteLines<'a, true> {
123            crate::blob::sources::byte_lines_with_terminator(self.data.as_slice().unwrap_or_default())
124        }
125
126        /// Produce an iterator over lines, but remove LF or CRLF.
127        ///
128        /// This produces the expected diffs when lines were added at the end of a file that didn't end
129        /// with a newline before the change.
130        ///
131        /// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
132        pub fn intern_source_strip_newline_separators(&self) -> imara_diff::sources::ByteLines<'a, false> {
133            crate::blob::sources::byte_lines(self.data.as_slice().unwrap_or_default())
134        }
135    }
136
137    /// The data of a diffable resource, as it could be determined and computed previously.
138    #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
139    pub enum Data<'a> {
140        /// The object is missing, either because it didn't exist in the working tree or because its `id` was null.
141        Missing,
142        /// The textual data as processed to be in a diffable state.
143        Buffer {
144            /// The buffer bytes.
145            buf: &'a [u8],
146            /// If `true`, a [binary to text filter](super::super::Driver::binary_to_text_command) was used to obtain the buffer,
147            /// making it a derived value.
148            ///
149            /// Applications should check for this to avoid treating the buffer content as (original) resource content.
150            is_derived: bool,
151        },
152        /// The size that the binary blob had at the given revision, without having applied filters, as it's either
153        /// considered binary or above the big-file threshold.
154        ///
155        /// In this state, the binary file cannot be diffed.
156        Binary {
157            /// The size of the object prior to performing any filtering or as it was found on disk.
158            ///
159            /// Note that technically, the size isn't always representative of the same 'state' of the
160            /// content, as once it can be the size of the blob in git, and once it's the size of file
161            /// in the worktree.
162            size: u64,
163        },
164    }
165
166    impl<'a> Data<'a> {
167        /// Return ourselves as slice of bytes if this instance stores data.
168        pub fn as_slice(&self) -> Option<&'a [u8]> {
169            match self {
170                Data::Buffer { buf, .. } => Some(buf),
171                Data::Binary { .. } | Data::Missing => None,
172            }
173        }
174
175        /// Returns `true` if the data in this instance is derived.
176        pub fn is_derived(&self) -> bool {
177            match self {
178                Data::Missing | Data::Binary { .. } => false,
179                Data::Buffer { is_derived, .. } => *is_derived,
180            }
181        }
182    }
183}
184
185///
186pub mod set_resource {
187    use bstr::BString;
188
189    use crate::blob::{pipeline, ResourceKind};
190
191    /// The error returned by [Platform::set_resource](super::Platform::set_resource).
192    #[derive(Debug, thiserror::Error)]
193    #[allow(missing_docs)]
194    pub enum Error {
195        #[error("Can only diff blobs and links, not {mode:?}")]
196        InvalidMode { mode: gix_object::tree::EntryKind },
197        #[error("Failed to read {kind} worktree data from '{rela_path}'")]
198        Io {
199            rela_path: BString,
200            kind: ResourceKind,
201            source: std::io::Error,
202        },
203        #[error("Failed to obtain attributes for {kind} resource at '{rela_path}'")]
204        Attributes {
205            rela_path: BString,
206            kind: ResourceKind,
207            source: std::io::Error,
208        },
209        #[error(transparent)]
210        ConvertToDiffable(#[from] pipeline::convert_to_diffable::Error),
211    }
212}
213
214///
215pub mod prepare_diff {
216    use crate::blob::platform::Resource;
217    use bstr::BStr;
218
219    /// The kind of operation that should be performed based on the configuration of the resources involved in the diff.
220    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
221    pub enum Operation<'a> {
222        /// The [internal diff algorithm](imara_diff::diff) should be called with the provided arguments.
223        /// This only happens if none of the resources are binary, and if there is no external diff program configured via git-attributes
224        /// *or* [Options::skip_internal_diff_if_external_is_configured](super::Options::skip_internal_diff_if_external_is_configured)
225        /// is `false`.
226        ///
227        /// Use [`Outcome::interned_input()`] to easily obtain an interner for use with [`imara_diff::diff()`], or maintain one yourself
228        /// for greater reuse.
229        InternalDiff {
230            /// The algorithm we determined should be used, which is one of (in order, first set one wins):
231            ///
232            /// * the driver's override
233            /// * the platforms own configuration (typically from git-config)
234            /// * the default algorithm
235            algorithm: imara_diff::Algorithm,
236        },
237        /// Run the external diff program according as configured in the `source`-resources driver.
238        /// This only happens if [Options::skip_internal_diff_if_external_is_configured](super::Options::skip_internal_diff_if_external_is_configured)
239        /// was `true`, preventing the usage of the internal diff implementation.
240        ExternalCommand {
241            /// The command as extracted from [Driver::command](super::super::Driver::command).
242            /// Use it in [`Platform::prepare_diff_command`](super::Platform::prepare_diff_command()) to easily prepare a compatible invocation.
243            command: &'a BStr,
244        },
245        /// One of the involved resources, [`old`](Outcome::old) or [`new`](Outcome::new), were binary and thus no diff
246        /// cannot be performed.
247        SourceOrDestinationIsBinary,
248    }
249
250    /// The outcome of a [`prepare_diff`](super::Platform::prepare_diff()) operation.
251    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
252    pub struct Outcome<'a> {
253        /// The kind of diff that was actually performed. This may include skipping the internal diff as well.
254        pub operation: Operation<'a>,
255        /// If `true`, a [binary to text filter](super::super::Driver::binary_to_text_command) was used to obtain the buffer
256        /// of `old` or `new`, making it a derived value.
257        ///
258        /// Applications should check for this to avoid treating the buffer content as (original) resource content.
259        pub old_or_new_is_derived: bool,
260        /// The old or source of the diff operation.
261        pub old: Resource<'a>,
262        /// The new or destination of the diff operation.
263        pub new: Resource<'a>,
264    }
265
266    impl<'a> Outcome<'a> {
267        /// Produce an instance of an interner which `git` would use to perform diffs.
268        ///
269        /// Note that newline separators will be removed to improve diff quality
270        /// at the end of files that didn't have a newline, but had lines added
271        /// past the end.
272        pub fn interned_input(&self) -> imara_diff::intern::InternedInput<&'a [u8]> {
273            crate::blob::intern::InternedInput::new(
274                self.old.intern_source_strip_newline_separators(),
275                self.new.intern_source_strip_newline_separators(),
276            )
277        }
278    }
279
280    /// The error returned by [Platform::prepare_diff()](super::Platform::prepare_diff()).
281    #[derive(Debug, thiserror::Error)]
282    #[allow(missing_docs)]
283    pub enum Error {
284        #[error("Either the source or the destination of the diff operation were not set")]
285        SourceOrDestinationUnset,
286        #[error("Tried to diff resources that are both considered removed")]
287        SourceAndDestinationRemoved,
288    }
289}
290
291///
292pub mod prepare_diff_command {
293    use std::ops::{Deref, DerefMut};
294
295    use bstr::BString;
296
297    /// The error returned by [Platform::prepare_diff_command()](super::Platform::prepare_diff_command()).
298    #[derive(Debug, thiserror::Error)]
299    #[allow(missing_docs)]
300    pub enum Error {
301        #[error("Either the source or the destination of the diff operation were not set")]
302        SourceOrDestinationUnset,
303        #[error("Binary resources can't be diffed with an external command (as we don't have the data anymore)")]
304        SourceOrDestinationBinary,
305        #[error(
306            "Tempfile to store content of '{rela_path}' for passing to external diff command could not be created"
307        )]
308        CreateTempfile { rela_path: BString, source: std::io::Error },
309        #[error("Could not write content of '{rela_path}' to tempfile for passing to external diff command")]
310        WriteTempfile { rela_path: BString, source: std::io::Error },
311    }
312
313    /// The outcome of a [`prepare_diff_command`](super::Platform::prepare_diff_command()) operation.
314    ///
315    /// This type acts like [`std::process::Command`], ready to run, with `stdin`, `stdout` and `stderr` set to *inherit*
316    /// all handles as this is expected to be for visual inspection.
317    pub struct Command {
318        pub(crate) cmd: std::process::Command,
319        /// Possibly a tempfile to be removed after the run, or `None` if there is no old version.
320        pub(crate) old: Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>,
321        /// Possibly a tempfile to be removed after the run, or `None` if there is no new version.
322        pub(crate) new: Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>,
323    }
324
325    impl Deref for Command {
326        type Target = std::process::Command;
327
328        fn deref(&self) -> &Self::Target {
329            &self.cmd
330        }
331    }
332
333    impl DerefMut for Command {
334        fn deref_mut(&mut self) -> &mut Self::Target {
335            &mut self.cmd
336        }
337    }
338}
339
340/// Options for use in [Platform::new()].
341#[derive(Default, Copy, Clone)]
342pub struct Options {
343    /// The algorithm to use when diffing.
344    /// If unset, it uses the [default algorithm](Algorithm::default()).
345    pub algorithm: Option<Algorithm>,
346    /// If `true`, default `false`, then an external `diff` configured using gitattributes and drivers,
347    /// will cause the built-in diff [to be skipped](prepare_diff::Operation::ExternalCommand).
348    /// Otherwise, the internal diff is called despite the configured external diff, which is
349    /// typically what callers expect by default.
350    pub skip_internal_diff_if_external_is_configured: bool,
351}
352
353/// Lifecycle
354impl Platform {
355    /// Create a new instance with `options`, and a way to `filter` data from the object database to data that is diff-able.
356    /// `filter_mode` decides how to do that specifically.
357    /// Use `attr_stack` to access attributes pertaining worktree filters and diff settings.
358    pub fn new(
359        options: Options,
360        filter: Pipeline,
361        filter_mode: pipeline::Mode,
362        attr_stack: gix_worktree::Stack,
363    ) -> Self {
364        Platform {
365            old: None,
366            new: None,
367            diff_cache: Default::default(),
368            free_list: Vec::with_capacity(2),
369            options,
370            filter,
371            filter_mode,
372            attr_stack,
373        }
374    }
375}
376
377/// Conversions
378impl Platform {
379    /// Store enough information about a resource to eventually diff it, where…
380    ///
381    /// * `id` is the hash of the resource. If it [is null](gix_hash::ObjectId::is_null()), it should either
382    ///   be a resource in the worktree, or it's considered a non-existing, deleted object.
383    ///   If an `id` is known, as the hash of the object as (would) be stored in `git`, then it should be provided
384    ///   for completeness.
385    /// * `mode` is the kind of object (only blobs and links are allowed)
386    /// * `rela_path` is the relative path as seen from the (work)tree root.
387    /// * `kind` identifies the side of the diff this resource will be used for.
388    ///   A diff needs both `OldOrSource` *and* `NewOrDestination`.
389    /// * `objects` provides access to the object database in case the resource can't be read from a worktree.
390    ///
391    /// Note that it's assumed that either `id + mode (` or `rela_path` can serve as unique identifier for the resource,
392    /// depending on whether or not a [worktree root](pipeline::WorktreeRoots) is set for the resource of `kind`,
393    /// with resources with worktree roots using the `rela_path` as unique identifier.
394    ///
395    /// ### Important
396    ///
397    /// If an error occurs, the previous resource of `kind` will be cleared, preventing further diffs
398    /// unless another attempt succeeds.
399    pub fn set_resource(
400        &mut self,
401        id: gix_hash::ObjectId,
402        mode: gix_object::tree::EntryKind,
403        rela_path: &BStr,
404        kind: ResourceKind,
405        objects: &impl gix_object::FindObjectOrHeader, // TODO: make this `dyn` once https://github.com/rust-lang/rust/issues/65991 is stable, then also make tracker.rs `objects` dyn
406    ) -> Result<(), set_resource::Error> {
407        let res = self.set_resource_inner(id, mode, rela_path, kind, objects);
408        if res.is_err() {
409            *match kind {
410                ResourceKind::OldOrSource => &mut self.old,
411                ResourceKind::NewOrDestination => &mut self.new,
412            } = None;
413        }
414        res
415    }
416
417    /// Given `diff_command` and `context`, typically obtained from git-configuration, and the currently set diff-resources,
418    /// prepare the invocation and temporary files needed to launch it according to protocol.
419    /// `count` / `total` are used for progress indication passed as environment variables `GIT_DIFF_PATH_(COUNTER|TOTAL)`
420    /// respectively (0-based), so the first path has `count=0` and `total=1` (assuming there is only one path).
421    /// Returns `None` if at least one resource is unset, see [`set_resource()`](Self::set_resource()).
422    ///
423    /// Please note that this is an expensive operation this will always create up to two temporary files to hold the data
424    /// for the old and new resources.
425    ///
426    /// ### Deviation
427    ///
428    /// If one of the resources is binary, the operation reports an error as such resources don't make their data available
429    /// which is required for the external diff to run.
430    // TODO: fix this - the diff shouldn't fail if binary (or large) files are used, just copy them into tempfiles.
431    pub fn prepare_diff_command(
432        &self,
433        diff_command: BString,
434        context: gix_command::Context,
435        count: usize,
436        total: usize,
437    ) -> Result<prepare_diff_command::Command, prepare_diff_command::Error> {
438        fn add_resource(
439            cmd: &mut std::process::Command,
440            res: Resource<'_>,
441        ) -> Result<Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>, prepare_diff_command::Error> {
442            let tmpfile = match res.data {
443                resource::Data::Missing => {
444                    cmd.args(["/dev/null", ".", "."]);
445                    None
446                }
447                resource::Data::Buffer { buf, is_derived: _ } => {
448                    let mut tmp = gix_tempfile::new(
449                        std::env::temp_dir(),
450                        gix_tempfile::ContainingDirectory::Exists,
451                        gix_tempfile::AutoRemove::Tempfile,
452                    )
453                    .map_err(|err| prepare_diff_command::Error::CreateTempfile {
454                        rela_path: res.rela_path.to_owned(),
455                        source: err,
456                    })?;
457                    tmp.write_all(buf)
458                        .map_err(|err| prepare_diff_command::Error::WriteTempfile {
459                            rela_path: res.rela_path.to_owned(),
460                            source: err,
461                        })?;
462                    tmp.with_mut(|f| {
463                        cmd.arg(f.path());
464                    })
465                    .map_err(|err| prepare_diff_command::Error::WriteTempfile {
466                        rela_path: res.rela_path.to_owned(),
467                        source: err,
468                    })?;
469                    cmd.arg(res.id.to_string()).arg(res.mode.as_octal_str().to_string());
470                    let tmp = tmp.close().map_err(|err| prepare_diff_command::Error::WriteTempfile {
471                        rela_path: res.rela_path.to_owned(),
472                        source: err,
473                    })?;
474                    Some(tmp)
475                }
476                resource::Data::Binary { .. } => return Err(prepare_diff_command::Error::SourceOrDestinationBinary),
477            };
478            Ok(tmpfile)
479        }
480
481        let (old, new) = self
482            .resources()
483            .ok_or(prepare_diff_command::Error::SourceOrDestinationUnset)?;
484        let mut cmd: std::process::Command = gix_command::prepare(gix_path::from_bstring(diff_command))
485            .with_context(context)
486            .env("GIT_DIFF_PATH_COUNTER", (count + 1).to_string())
487            .env("GIT_DIFF_PATH_TOTAL", total.to_string())
488            .stdin(Stdio::inherit())
489            .stdout(Stdio::inherit())
490            .stderr(Stdio::inherit())
491            .into();
492
493        cmd.arg(gix_path::from_bstr(old.rela_path).into_owned());
494        let mut out = prepare_diff_command::Command {
495            cmd,
496            old: None,
497            new: None,
498        };
499
500        out.old = add_resource(&mut out.cmd, old)?;
501        out.new = add_resource(&mut out.cmd, new)?;
502
503        if old.rela_path != new.rela_path {
504            out.cmd.arg(gix_path::from_bstr(new.rela_path).into_owned());
505        }
506
507        Ok(out)
508    }
509
510    /// Returns the resource of the given kind if it was set.
511    pub fn resource(&self, kind: ResourceKind) -> Option<Resource<'_>> {
512        let key = match kind {
513            ResourceKind::OldOrSource => self.old.as_ref(),
514            ResourceKind::NewOrDestination => self.new.as_ref(),
515        }?;
516        Resource::new(key, self.diff_cache.get(key)?).into()
517    }
518
519    /// Obtain the two resources that were previously set as `(OldOrSource, NewOrDestination)`, if both are set and available.
520    ///
521    /// This is useful if one wishes to manually prepare the diff, maybe for invoking external programs, instead of relying on
522    /// [`Self::prepare_diff()`].
523    pub fn resources(&self) -> Option<(Resource<'_>, Resource<'_>)> {
524        let key = &self.old.as_ref()?;
525        let value = self.diff_cache.get(key)?;
526        let old = Resource::new(key, value);
527
528        let key = &self.new.as_ref()?;
529        let value = self.diff_cache.get(key)?;
530        let new = Resource::new(key, value);
531        Some((old, new))
532    }
533
534    /// Prepare a diff operation on the [previously set](Self::set_resource()) [old](ResourceKind::OldOrSource) and
535    /// [new](ResourceKind::NewOrDestination) resources.
536    ///
537    /// The returned outcome allows to easily perform diff operations, based on the [`prepare_diff::Outcome::operation`] field,
538    /// which hints at what should be done.
539    pub fn prepare_diff(&mut self) -> Result<prepare_diff::Outcome<'_>, prepare_diff::Error> {
540        let old_key = &self.old.as_ref().ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
541        let old = self
542            .diff_cache
543            .get(old_key)
544            .ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
545        let new_key = &self.new.as_ref().ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
546        let new = self
547            .diff_cache
548            .get(new_key)
549            .ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
550        let mut out = {
551            let old = Resource::new(old_key, old);
552            let new = Resource::new(new_key, new);
553            prepare_diff::Outcome {
554                operation: prepare_diff::Operation::SourceOrDestinationIsBinary,
555                old_or_new_is_derived: old.data.is_derived() || new.data.is_derived(),
556                old,
557                new,
558            }
559        };
560
561        match (old.conversion.data, new.conversion.data) {
562            (None, None) => return Err(prepare_diff::Error::SourceAndDestinationRemoved),
563            (Some(pipeline::Data::Binary { .. }), _) | (_, Some(pipeline::Data::Binary { .. })) => return Ok(out),
564            _either_missing_or_non_binary => {
565                if let Some(command) = old
566                    .conversion
567                    .driver_index
568                    .and_then(|idx| self.filter.drivers[idx].command.as_deref())
569                    .filter(|_| self.options.skip_internal_diff_if_external_is_configured)
570                {
571                    out.operation = prepare_diff::Operation::ExternalCommand {
572                        command: command.as_bstr(),
573                    };
574                    return Ok(out);
575                }
576            }
577        }
578
579        out.operation = prepare_diff::Operation::InternalDiff {
580            algorithm: old
581                .conversion
582                .driver_index
583                .and_then(|idx| self.filter.drivers[idx].algorithm)
584                .or(self.options.algorithm)
585                .unwrap_or_default(),
586        };
587        Ok(out)
588    }
589
590    /// Every call to [set_resource()](Self::set_resource()) will keep the diffable data in memory, and that will never be cleared.
591    ///
592    /// Use this method to clear the cache, releasing memory. Note that this will also lose all information about resources
593    /// which means diffs would fail unless the resources are set again.
594    ///
595    /// Note that this also has to be called if the same resource is going to be diffed in different states, i.e. using different
596    /// `id`s, but the same `rela_path`.
597    pub fn clear_resource_cache(&mut self) {
598        self.old = None;
599        self.new = None;
600        self.diff_cache.clear();
601        self.free_list.clear();
602    }
603
604    /// Every call to [set_resource()](Self::set_resource()) will keep the diffable data in memory, and that will never be cleared.
605    ///
606    /// Use this method to clear the cache, but keep the previously used buffers around for later re-use.
607    ///
608    /// If there are more buffers on the free-list than there are stored sources, we half that amount each time this method is called,
609    /// or keep as many resources as were previously stored, or 2 buffers, whatever is larger.
610    /// If there are fewer buffers in the free-list than are in the resource cache, we will keep as many as needed to match the
611    /// number of previously stored resources.
612    ///
613    /// Returns the number of available buffers.
614    pub fn clear_resource_cache_keep_allocation(&mut self) -> usize {
615        self.old = None;
616        self.new = None;
617
618        let diff_cache = std::mem::take(&mut self.diff_cache);
619        match self.free_list.len().cmp(&diff_cache.len()) {
620            Ordering::Less => {
621                let to_take = diff_cache.len() - self.free_list.len();
622                self.free_list
623                    .extend(diff_cache.into_values().map(|v| v.buffer).take(to_take));
624            }
625            Ordering::Equal => {}
626            Ordering::Greater => {
627                let new_len = (self.free_list.len() / 2).max(diff_cache.len()).max(2);
628                self.free_list.truncate(new_len);
629            }
630        }
631        self.free_list.len()
632    }
633}
634
635impl Platform {
636    fn set_resource_inner(
637        &mut self,
638        id: gix_hash::ObjectId,
639        mode: gix_object::tree::EntryKind,
640        rela_path: &BStr,
641        kind: ResourceKind,
642        objects: &impl gix_object::FindObjectOrHeader,
643    ) -> Result<(), set_resource::Error> {
644        if matches!(
645            mode,
646            gix_object::tree::EntryKind::Commit | gix_object::tree::EntryKind::Tree
647        ) {
648            return Err(set_resource::Error::InvalidMode { mode });
649        }
650        let storage = match kind {
651            ResourceKind::OldOrSource => &mut self.old,
652            ResourceKind::NewOrDestination => &mut self.new,
653        }
654        .get_or_insert_with(Default::default);
655
656        storage.id = id;
657        storage.set_location(rela_path);
658        storage.is_link = matches!(mode, gix_object::tree::EntryKind::Link);
659        storage.use_id = self.filter.roots.by_kind(kind).is_none();
660
661        if self.diff_cache.contains_key(storage) {
662            return Ok(());
663        }
664        let entry =
665            self.attr_stack
666                .at_entry(rela_path, None, objects)
667                .map_err(|err| set_resource::Error::Attributes {
668                    source: err,
669                    kind,
670                    rela_path: rela_path.to_owned(),
671                })?;
672        let mut buf = self.free_list.pop().unwrap_or_default();
673        let out = self.filter.convert_to_diffable(
674            &id,
675            mode,
676            rela_path,
677            kind,
678            &mut |_, out| {
679                let _ = entry.matching_attributes(out);
680            },
681            objects,
682            self.filter_mode,
683            &mut buf,
684        )?;
685        let key = storage.clone();
686        assert!(
687            self.diff_cache
688                .insert(
689                    key,
690                    CacheValue {
691                        conversion: out,
692                        mode,
693                        buffer: buf,
694                    },
695                )
696                .is_none(),
697            "The key impl makes clashes impossible with our usage"
698        );
699        Ok(())
700    }
701}