gix_diff/blob/platform.rs
1use std::{cmp::Ordering, io::Write, process::Stdio};
2
3use bstr::{BStr, BString, ByteSlice};
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 bstr::BStr;
217
218 use crate::blob::platform::Resource;
219
220 /// The kind of operation that should be performed based on the configuration of the resources involved in the diff.
221 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
222 pub enum Operation<'a> {
223 /// The [internal diff algorithm](imara_diff::diff) should be called with the provided arguments.
224 /// This only happens if none of the resources are binary, and if there is no external diff program configured via git-attributes
225 /// *or* [Options::skip_internal_diff_if_external_is_configured](super::Options::skip_internal_diff_if_external_is_configured)
226 /// is `false`.
227 ///
228 /// Use [`Outcome::interned_input()`] to easily obtain an interner for use with [`imara_diff::diff()`], or maintain one yourself
229 /// for greater reuse.
230 InternalDiff {
231 /// The algorithm we determined should be used, which is one of (in order, first set one wins):
232 ///
233 /// * the driver's override
234 /// * the platforms own configuration (typically from git-config)
235 /// * the default algorithm
236 algorithm: imara_diff::Algorithm,
237 },
238 /// Run the external diff program according as configured in the `source`-resources driver.
239 /// This only happens if [Options::skip_internal_diff_if_external_is_configured](super::Options::skip_internal_diff_if_external_is_configured)
240 /// was `true`, preventing the usage of the internal diff implementation.
241 ExternalCommand {
242 /// The command as extracted from [Driver::command](super::super::Driver::command).
243 /// Use it in [`Platform::prepare_diff_command`](super::Platform::prepare_diff_command()) to easily prepare a compatible invocation.
244 command: &'a BStr,
245 },
246 /// One of the involved resources, [`old`](Outcome::old) or [`new`](Outcome::new), were binary and thus no diff
247 /// cannot be performed.
248 SourceOrDestinationIsBinary,
249 }
250
251 /// The outcome of a [`prepare_diff`](super::Platform::prepare_diff()) operation.
252 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
253 pub struct Outcome<'a> {
254 /// The kind of diff that was actually performed. This may include skipping the internal diff as well.
255 pub operation: Operation<'a>,
256 /// If `true`, a [binary to text filter](super::super::Driver::binary_to_text_command) was used to obtain the buffer
257 /// of `old` or `new`, making it a derived value.
258 ///
259 /// Applications should check for this to avoid treating the buffer content as (original) resource content.
260 pub old_or_new_is_derived: bool,
261 /// The old or source of the diff operation.
262 pub old: Resource<'a>,
263 /// The new or destination of the diff operation.
264 pub new: Resource<'a>,
265 }
266
267 impl<'a> Outcome<'a> {
268 /// Produce an instance of an interner which `git` would use to perform diffs.
269 ///
270 /// Note that newline separators will be removed to improve diff quality
271 /// at the end of files that didn't have a newline, but had lines added
272 /// past the end.
273 pub fn interned_input(&self) -> imara_diff::intern::InternedInput<&'a [u8]> {
274 crate::blob::intern::InternedInput::new(
275 self.old.intern_source_strip_newline_separators(),
276 self.new.intern_source_strip_newline_separators(),
277 )
278 }
279 }
280
281 /// The error returned by [Platform::prepare_diff()](super::Platform::prepare_diff()).
282 #[derive(Debug, thiserror::Error)]
283 #[allow(missing_docs)]
284 pub enum Error {
285 #[error("Either the source or the destination of the diff operation were not set")]
286 SourceOrDestinationUnset,
287 #[error("Tried to diff resources that are both considered removed")]
288 SourceAndDestinationRemoved,
289 }
290}
291
292///
293pub mod prepare_diff_command {
294 use std::ops::{Deref, DerefMut};
295
296 use bstr::BString;
297
298 /// The error returned by [Platform::prepare_diff_command()](super::Platform::prepare_diff_command()).
299 #[derive(Debug, thiserror::Error)]
300 #[allow(missing_docs)]
301 pub enum Error {
302 #[error("Either the source or the destination of the diff operation were not set")]
303 SourceOrDestinationUnset,
304 #[error("Binary resources can't be diffed with an external command (as we don't have the data anymore)")]
305 SourceOrDestinationBinary,
306 #[error(
307 "Tempfile to store content of '{rela_path}' for passing to external diff command could not be created"
308 )]
309 CreateTempfile { rela_path: BString, source: std::io::Error },
310 #[error("Could not write content of '{rela_path}' to tempfile for passing to external diff command")]
311 WriteTempfile { rela_path: BString, source: std::io::Error },
312 }
313
314 /// The outcome of a [`prepare_diff_command`](super::Platform::prepare_diff_command()) operation.
315 ///
316 /// This type acts like [`std::process::Command`], ready to run, with `stdin`, `stdout` and `stderr` set to *inherit*
317 /// all handles as this is expected to be for visual inspection.
318 pub struct Command {
319 pub(crate) cmd: std::process::Command,
320 /// Possibly a tempfile to be removed after the run, or `None` if there is no old version.
321 pub(crate) old: Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>,
322 /// Possibly a tempfile to be removed after the run, or `None` if there is no new version.
323 pub(crate) new: Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>,
324 }
325
326 impl Deref for Command {
327 type Target = std::process::Command;
328
329 fn deref(&self) -> &Self::Target {
330 &self.cmd
331 }
332 }
333
334 impl DerefMut for Command {
335 fn deref_mut(&mut self) -> &mut Self::Target {
336 &mut self.cmd
337 }
338 }
339}
340
341/// Options for use in [Platform::new()].
342#[derive(Default, Copy, Clone)]
343pub struct Options {
344 /// The algorithm to use when diffing.
345 /// If unset, it uses the [default algorithm](Algorithm::default()).
346 pub algorithm: Option<Algorithm>,
347 /// If `true`, default `false`, then an external `diff` configured using gitattributes and drivers,
348 /// will cause the built-in diff [to be skipped](prepare_diff::Operation::ExternalCommand).
349 /// Otherwise, the internal diff is called despite the configured external diff, which is
350 /// typically what callers expect by default.
351 pub skip_internal_diff_if_external_is_configured: bool,
352}
353
354/// Lifecycle
355impl Platform {
356 /// Create a new instance with `options`, and a way to `filter` data from the object database to data that is diff-able.
357 /// `filter_mode` decides how to do that specifically.
358 /// Use `attr_stack` to access attributes pertaining worktree filters and diff settings.
359 pub fn new(
360 options: Options,
361 filter: Pipeline,
362 filter_mode: pipeline::Mode,
363 attr_stack: gix_worktree::Stack,
364 ) -> Self {
365 Platform {
366 old: None,
367 new: None,
368 diff_cache: Default::default(),
369 free_list: Vec::with_capacity(2),
370 options,
371 filter,
372 filter_mode,
373 attr_stack,
374 }
375 }
376}
377
378/// Conversions
379impl Platform {
380 /// Store enough information about a resource to eventually diff it, where…
381 ///
382 /// * `id` is the hash of the resource. If it [is null](gix_hash::ObjectId::is_null()), it should either
383 /// be a resource in the worktree, or it's considered a non-existing, deleted object.
384 /// If an `id` is known, as the hash of the object as (would) be stored in `git`, then it should be provided
385 /// for completeness.
386 /// * `mode` is the kind of object (only blobs and links are allowed)
387 /// * `rela_path` is the relative path as seen from the (work)tree root.
388 /// * `kind` identifies the side of the diff this resource will be used for.
389 /// A diff needs both `OldOrSource` *and* `NewOrDestination`.
390 /// * `objects` provides access to the object database in case the resource can't be read from a worktree.
391 ///
392 /// Note that it's assumed that either `id + mode (` or `rela_path` can serve as unique identifier for the resource,
393 /// depending on whether or not a [worktree root](pipeline::WorktreeRoots) is set for the resource of `kind`,
394 /// with resources with worktree roots using the `rela_path` as unique identifier.
395 ///
396 /// ### Important
397 ///
398 /// If an error occurs, the previous resource of `kind` will be cleared, preventing further diffs
399 /// unless another attempt succeeds.
400 pub fn set_resource(
401 &mut self,
402 id: gix_hash::ObjectId,
403 mode: gix_object::tree::EntryKind,
404 rela_path: &BStr,
405 kind: ResourceKind,
406 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
407 ) -> Result<(), set_resource::Error> {
408 let res = self.set_resource_inner(id, mode, rela_path, kind, objects);
409 if res.is_err() {
410 *match kind {
411 ResourceKind::OldOrSource => &mut self.old,
412 ResourceKind::NewOrDestination => &mut self.new,
413 } = None;
414 }
415 res
416 }
417
418 /// Given `diff_command` and `context`, typically obtained from git-configuration, and the currently set diff-resources,
419 /// prepare the invocation and temporary files needed to launch it according to protocol.
420 /// `count` / `total` are used for progress indication passed as environment variables `GIT_DIFF_PATH_(COUNTER|TOTAL)`
421 /// respectively (0-based), so the first path has `count=0` and `total=1` (assuming there is only one path).
422 /// Returns `None` if at least one resource is unset, see [`set_resource()`](Self::set_resource()).
423 ///
424 /// Please note that this is an expensive operation this will always create up to two temporary files to hold the data
425 /// for the old and new resources.
426 ///
427 /// ### Deviation
428 ///
429 /// If one of the resources is binary, the operation reports an error as such resources don't make their data available
430 /// which is required for the external diff to run.
431 // TODO: fix this - the diff shouldn't fail if binary (or large) files are used, just copy them into tempfiles.
432 pub fn prepare_diff_command(
433 &self,
434 diff_command: BString,
435 context: gix_command::Context,
436 count: usize,
437 total: usize,
438 ) -> Result<prepare_diff_command::Command, prepare_diff_command::Error> {
439 fn add_resource(
440 cmd: &mut std::process::Command,
441 res: Resource<'_>,
442 ) -> Result<Option<gix_tempfile::Handle<gix_tempfile::handle::Closed>>, prepare_diff_command::Error> {
443 let tmpfile = match res.data {
444 resource::Data::Missing => {
445 cmd.args(["/dev/null", ".", "."]);
446 None
447 }
448 resource::Data::Buffer { buf, is_derived: _ } => {
449 let mut tmp = gix_tempfile::new(
450 std::env::temp_dir(),
451 gix_tempfile::ContainingDirectory::Exists,
452 gix_tempfile::AutoRemove::Tempfile,
453 )
454 .map_err(|err| prepare_diff_command::Error::CreateTempfile {
455 rela_path: res.rela_path.to_owned(),
456 source: err,
457 })?;
458 tmp.write_all(buf)
459 .map_err(|err| prepare_diff_command::Error::WriteTempfile {
460 rela_path: res.rela_path.to_owned(),
461 source: err,
462 })?;
463 tmp.with_mut(|f| {
464 cmd.arg(f.path());
465 })
466 .map_err(|err| prepare_diff_command::Error::WriteTempfile {
467 rela_path: res.rela_path.to_owned(),
468 source: err,
469 })?;
470 cmd.arg(res.id.to_string()).arg(res.mode.as_octal_str().to_string());
471 let tmp = tmp.close().map_err(|err| prepare_diff_command::Error::WriteTempfile {
472 rela_path: res.rela_path.to_owned(),
473 source: err,
474 })?;
475 Some(tmp)
476 }
477 resource::Data::Binary { .. } => return Err(prepare_diff_command::Error::SourceOrDestinationBinary),
478 };
479 Ok(tmpfile)
480 }
481
482 let (old, new) = self
483 .resources()
484 .ok_or(prepare_diff_command::Error::SourceOrDestinationUnset)?;
485 let mut cmd: std::process::Command = gix_command::prepare(gix_path::from_bstring(diff_command))
486 .with_context(context)
487 .env("GIT_DIFF_PATH_COUNTER", (count + 1).to_string())
488 .env("GIT_DIFF_PATH_TOTAL", total.to_string())
489 .stdin(Stdio::inherit())
490 .stdout(Stdio::inherit())
491 .stderr(Stdio::inherit())
492 .into();
493
494 cmd.arg(gix_path::from_bstr(old.rela_path).into_owned());
495 let mut out = prepare_diff_command::Command {
496 cmd,
497 old: None,
498 new: None,
499 };
500
501 out.old = add_resource(&mut out.cmd, old)?;
502 out.new = add_resource(&mut out.cmd, new)?;
503
504 if old.rela_path != new.rela_path {
505 out.cmd.arg(gix_path::from_bstr(new.rela_path).into_owned());
506 }
507
508 Ok(out)
509 }
510
511 /// Returns the resource of the given kind if it was set.
512 pub fn resource(&self, kind: ResourceKind) -> Option<Resource<'_>> {
513 let key = match kind {
514 ResourceKind::OldOrSource => self.old.as_ref(),
515 ResourceKind::NewOrDestination => self.new.as_ref(),
516 }?;
517 Resource::new(key, self.diff_cache.get(key)?).into()
518 }
519
520 /// Obtain the two resources that were previously set as `(OldOrSource, NewOrDestination)`, if both are set and available.
521 ///
522 /// This is useful if one wishes to manually prepare the diff, maybe for invoking external programs, instead of relying on
523 /// [`Self::prepare_diff()`].
524 pub fn resources(&self) -> Option<(Resource<'_>, Resource<'_>)> {
525 let key = &self.old.as_ref()?;
526 let value = self.diff_cache.get(key)?;
527 let old = Resource::new(key, value);
528
529 let key = &self.new.as_ref()?;
530 let value = self.diff_cache.get(key)?;
531 let new = Resource::new(key, value);
532 Some((old, new))
533 }
534
535 /// Prepare a diff operation on the [previously set](Self::set_resource()) [old](ResourceKind::OldOrSource) and
536 /// [new](ResourceKind::NewOrDestination) resources.
537 ///
538 /// The returned outcome allows to easily perform diff operations, based on the [`prepare_diff::Outcome::operation`] field,
539 /// which hints at what should be done.
540 pub fn prepare_diff(&mut self) -> Result<prepare_diff::Outcome<'_>, prepare_diff::Error> {
541 let old_key = &self.old.as_ref().ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
542 let old = self
543 .diff_cache
544 .get(old_key)
545 .ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
546 let new_key = &self.new.as_ref().ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
547 let new = self
548 .diff_cache
549 .get(new_key)
550 .ok_or(prepare_diff::Error::SourceOrDestinationUnset)?;
551 let mut out = {
552 let old = Resource::new(old_key, old);
553 let new = Resource::new(new_key, new);
554 prepare_diff::Outcome {
555 operation: prepare_diff::Operation::SourceOrDestinationIsBinary,
556 old_or_new_is_derived: old.data.is_derived() || new.data.is_derived(),
557 old,
558 new,
559 }
560 };
561
562 match (old.conversion.data, new.conversion.data) {
563 (None, None) => return Err(prepare_diff::Error::SourceAndDestinationRemoved),
564 (Some(pipeline::Data::Binary { .. }), _) | (_, Some(pipeline::Data::Binary { .. })) => return Ok(out),
565 _either_missing_or_non_binary => {
566 if let Some(command) = old
567 .conversion
568 .driver_index
569 .and_then(|idx| self.filter.drivers[idx].command.as_deref())
570 .filter(|_| self.options.skip_internal_diff_if_external_is_configured)
571 {
572 out.operation = prepare_diff::Operation::ExternalCommand {
573 command: command.as_bstr(),
574 };
575 return Ok(out);
576 }
577 }
578 }
579
580 out.operation = prepare_diff::Operation::InternalDiff {
581 algorithm: old
582 .conversion
583 .driver_index
584 .and_then(|idx| self.filter.drivers[idx].algorithm)
585 .or(self.options.algorithm)
586 .unwrap_or_default(),
587 };
588 Ok(out)
589 }
590
591 /// Every call to [set_resource()](Self::set_resource()) will keep the diffable data in memory, and that will never be cleared.
592 ///
593 /// Use this method to clear the cache, releasing memory. Note that this will also lose all information about resources
594 /// which means diffs would fail unless the resources are set again.
595 ///
596 /// Note that this also has to be called if the same resource is going to be diffed in different states, i.e. using different
597 /// `id`s, but the same `rela_path`.
598 pub fn clear_resource_cache(&mut self) {
599 self.old = None;
600 self.new = None;
601 self.diff_cache.clear();
602 self.free_list.clear();
603 }
604
605 /// Every call to [set_resource()](Self::set_resource()) will keep the diffable data in memory, and that will never be cleared.
606 ///
607 /// Use this method to clear the cache, but keep the previously used buffers around for later re-use.
608 ///
609 /// If there are more buffers on the free-list than there are stored sources, we half that amount each time this method is called,
610 /// or keep as many resources as were previously stored, or 2 buffers, whatever is larger.
611 /// 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
612 /// number of previously stored resources.
613 ///
614 /// Returns the number of available buffers.
615 pub fn clear_resource_cache_keep_allocation(&mut self) -> usize {
616 self.old = None;
617 self.new = None;
618
619 let diff_cache = std::mem::take(&mut self.diff_cache);
620 match self.free_list.len().cmp(&diff_cache.len()) {
621 Ordering::Less => {
622 let to_take = diff_cache.len() - self.free_list.len();
623 self.free_list
624 .extend(diff_cache.into_values().map(|v| v.buffer).take(to_take));
625 }
626 Ordering::Equal => {}
627 Ordering::Greater => {
628 let new_len = (self.free_list.len() / 2).max(diff_cache.len()).max(2);
629 self.free_list.truncate(new_len);
630 }
631 }
632 self.free_list.len()
633 }
634}
635
636impl Platform {
637 fn set_resource_inner(
638 &mut self,
639 id: gix_hash::ObjectId,
640 mode: gix_object::tree::EntryKind,
641 rela_path: &BStr,
642 kind: ResourceKind,
643 objects: &impl gix_object::FindObjectOrHeader,
644 ) -> Result<(), set_resource::Error> {
645 if matches!(
646 mode,
647 gix_object::tree::EntryKind::Commit | gix_object::tree::EntryKind::Tree
648 ) {
649 return Err(set_resource::Error::InvalidMode { mode });
650 }
651 let storage = match kind {
652 ResourceKind::OldOrSource => &mut self.old,
653 ResourceKind::NewOrDestination => &mut self.new,
654 }
655 .get_or_insert_with(Default::default);
656
657 storage.id = id;
658 storage.set_location(rela_path);
659 storage.is_link = matches!(mode, gix_object::tree::EntryKind::Link);
660 storage.use_id = self.filter.roots.by_kind(kind).is_none();
661
662 if self.diff_cache.contains_key(storage) {
663 return Ok(());
664 }
665 let entry =
666 self.attr_stack
667 .at_entry(rela_path, None, objects)
668 .map_err(|err| set_resource::Error::Attributes {
669 source: err,
670 kind,
671 rela_path: rela_path.to_owned(),
672 })?;
673 let mut buf = self.free_list.pop().unwrap_or_default();
674 let out = self.filter.convert_to_diffable(
675 &id,
676 mode,
677 rela_path,
678 kind,
679 &mut |_, out| {
680 let _ = entry.matching_attributes(out);
681 },
682 objects,
683 self.filter_mode,
684 &mut buf,
685 )?;
686 let key = storage.clone();
687 assert!(
688 self.diff_cache
689 .insert(
690 key,
691 CacheValue {
692 conversion: out,
693 mode,
694 buffer: buf,
695 },
696 )
697 .is_none(),
698 "The key impl makes clashes impossible with our usage"
699 );
700 Ok(())
701 }
702}