As the fastest way of rename tracking, we now offer support for tracking renames and copies, that is a file was renamed or copied without modification.
Unified · Split
| @@ -69,6 +69,16 @@ impl Cache { · 68 unmodified lines | ||
| 69 | 69 | .get_or_try_init(|| remote::url::SchemePermission::from_config(&self.resolved, self.filter_config_section)) |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | + pub(crate) fn diff_renames( | |
| 73 | + &self, | |
| 74 | + ) -> Result<Option<crate::object::tree::diff::Renames>, crate::object::tree::diff::renames::Error> { | |
| 75 | + self.diff_renames | |
| 76 | + .get_or_try_init(|| { | |
| 77 | + crate::object::tree::diff::Renames::try_from_config(&self.resolved, self.lenient_config) | |
| 78 | + }) | |
| 79 | + .copied() | |
| 80 | + } | |
| 81 | + | |
| 72 | 82 | /// Returns (file-timeout, pack-refs timeout) |
| 73 | 83 | pub(crate) fn lock_timeout( |
| 74 | 84 | &self, |
| @@ -179,6 +179,7 @@ impl Cache { · 178 unmodified lines | ||
| 179 | 179 | user_agent: Default::default(), |
| 180 | 180 | personas: Default::default(), |
| 181 | 181 | url_rewrite: Default::default(), |
| 182 | + diff_renames: Default::default(), | |
| 182 | 183 | #[cfg(any(feature = "blocking-network-client", feature = "async-network-client"))] |
| 183 | 184 | url_scheme: Default::default(), |
| 184 | 185 | diff_algorithm: Default::default(), |
| @@ -226,6 +227,7 @@ impl Cache { · 41 unmodified lines | ||
| 226 | 227 | self.user_agent = Default::default(); |
| 227 | 228 | self.personas = Default::default(); |
| 228 | 229 | self.url_rewrite = Default::default(); |
| 230 | + self.diff_renames = Default::default(); | |
| 229 | 231 | self.diff_algorithm = Default::default(); |
| 230 | 232 | (self.pack_cache_bytes, self.object_cache_bytes) = |
| 231 | 233 | util::parse_object_caches(config, self.lenient_config, self.filter_config_section)?; |
| @@ -425,6 +425,8 @@ pub(crate) struct Cache { · 424 unmodified lines | ||
| 425 | 425 | pub(crate) personas: OnceCell<identity::Personas>, |
| 426 | 426 | /// A lazily loaded rewrite list for remote urls |
| 427 | 427 | pub(crate) url_rewrite: OnceCell<crate::remote::url::Rewrite>, |
| 428 | + /// The lazy-loaded rename information for diffs. | |
| 429 | + pub(crate) diff_renames: OnceCell<Option<crate::object::tree::diff::Renames>>, | |
| 428 | 430 | /// A lazily loaded mapping to know which url schemes to allow |
| 429 | 431 | #[cfg(any(feature = "blocking-network-client", feature = "async-network-client"))] |
| 430 | 432 | pub(crate) url_scheme: OnceCell<crate::remote::url::SchemePermission>, |
| @@ -8,8 +8,13 @@ impl Diff { · 7 unmodified lines | ||
| 8 | 8 | pub const ALGORITHM: Algorithm = Algorithm::new_with_validate("algorithm", &config::Tree::DIFF, validate::Algorithm) |
| 9 | 9 | .with_deviation("'patience' diff is not implemented and can default to 'histogram' if lenient config is used, and defaults to histogram if unset for fastest and best results"); |
| 10 | 10 | /// The `diff.renameLimit` key. |
| 11 | − pub const RENAME_LIMIT: keys::UnsignedInteger = | |
| 12 | − keys::UnsignedInteger::new_unsigned_integer("renameLimit", &config::Tree::DIFF); | |
| 11 | + pub const RENAME_LIMIT: keys::UnsignedInteger = keys::UnsignedInteger::new_unsigned_integer( | |
| 12 | + "renameLimit", | |
| 13 | + &config::Tree::DIFF, | |
| 14 | + ) | |
| 15 | + .with_note( | |
| 16 | + "The limit is actually squared, so 1000 stands for up to 1 million diffs if fuzzy rename tracking is enabled", | |
| 17 | + ); | |
| 13 | 18 | /// The `diff.renames` key. |
| 14 | 19 | pub const RENAMES: Renames = Renames::new_renames("renames", &config::Tree::DIFF); |
| 15 | 20 | } |
| @@ -90,7 +90,7 @@ pub mod diff { · 89 unmodified lines | ||
| 90 | 90 | FnH: FnMut(line::Change<'_, '_>) -> Result<(), E>, |
| 91 | 91 | E: std::error::Error, |
| 92 | 92 | { |
| 93 | − let input = git_diff::blob::intern::InternedInput::new(self.old.data.as_bytes(), self.new.data.as_bytes()); | |
| 93 | + let input = self.line_tokens(); | |
| 94 | 94 | let mut err = None; |
| 95 | 95 | let mut lines = Vec::new(); |
| 96 | 96 | git_diff::blob::diff(self.algo, &input, |before: Range<u32>, after: Range<u32>| { |
| @@ -1,10 +1,11 @@ | ||
| 1 | +use crate::bstr::BStr; | |
| 1 | 2 | use git_object::tree::EntryMode; |
| 2 | 3 | |
| 3 | 4 | use crate::Id; |
| 4 | 5 | |
| 5 | 6 | /// An event emitted when finding differences between two trees. |
| 6 | 7 | #[derive(Debug, Clone, Copy)] |
| 7 | −pub enum Event<'old, 'new> { | |
| 8 | +pub enum Event<'a, 'old, 'new> { | |
| 8 | 9 | /// An entry was added, like the addition of a file or directory. |
| 9 | 10 | Addition { |
| 10 | 11 | /// The mode of the added entry. |
| @@ -32,9 +33,59 @@ pub enum Event<'old, 'new> { · 21 unmodified lines | ||
| 32 | 33 | /// The object id after the modification. |
| 33 | 34 | id: Id<'new>, |
| 34 | 35 | }, |
| 36 | + /// Entries are considered renamed if they are not trees and they, according to some understanding of identity, appeared | |
| 37 | + /// as [`Deletion`][Event::Deletion] in case of the previous source of the rename as well as [`Addition`][Event::Addition] | |
| 38 | + /// acting as destination all the while [rename tracking][super::Platform::track_renames()] is enabled. | |
| 39 | + /// | |
| 40 | + /// Note that mode changes may have occurred as well, i.e. changes from executable to non-executable or vice-versa. | |
| 41 | + Rename { | |
| 42 | + /// The location of the source of the rename operation. | |
| 43 | + /// | |
| 44 | + /// It may be empty if neither [file names][super::Platform::track_filename()] nor [file paths][super::Platform::track_path()] | |
| 45 | + /// are tracked. | |
| 46 | + source_location: &'a BStr, | |
| 47 | + /// The mode of the entry before the rename. | |
| 48 | + source_entry_mode: git_object::tree::EntryMode, | |
| 49 | + /// The object id of the entry before the rename. | |
| 50 | + /// | |
| 51 | + /// Note that this is the same as `id` if we require the [similarity to be 100%][super::Renames::percentage], but may | |
| 52 | + /// be different otherwise. | |
| 53 | + source_id: Id<'old>, | |
| 54 | + | |
| 55 | + /// The mode of the entry after the rename. | |
| 56 | + /// It could differ but still be considered a rename as we are concerned only about content. | |
| 57 | + entry_mode: git_object::tree::EntryMode, | |
| 58 | + /// The object id after the rename. | |
| 59 | + id: Id<'new>, | |
| 60 | + }, | |
| 61 | + /// This entry is considered to be a copy of another, according to some understanding of identity, as its source still exists. | |
| 62 | + /// If the source wouldn't exist, it would be considered a [rename][Event::Rename]. | |
| 63 | + /// | |
| 64 | + /// This variant may only occur if [rename tracking][super::Platform::track_renames()] is enabled, otherwise copies appear to be | |
| 65 | + /// plain [additions][Event::Addition]. | |
| 66 | + Copy { | |
| 67 | + /// The location of the source of the copy operation. | |
| 68 | + /// | |
| 69 | + /// It may be empty if neither [file names][super::Platform::track_filename()] nor [file paths][super::Platform::track_path()] | |
| 70 | + /// are tracked. | |
| 71 | + source_location: &'a BStr, | |
| 72 | + /// The mode of the entry that is considered the source. | |
| 73 | + source_entry_mode: git_object::tree::EntryMode, | |
| 74 | + /// The object id of the source of the copy. | |
| 75 | + /// | |
| 76 | + /// Note that this is the same as `id` if we require the [similarity to be 100%][super::Renames::percentage], but may | |
| 77 | + /// be different otherwise. | |
| 78 | + source_id: Id<'old>, | |
| 79 | + | |
| 80 | + /// The mode of the entry after the copy, or the destination of it. | |
| 81 | + /// It could differ but still be considered a copy as we are concerned only about content. | |
| 82 | + entry_mode: git_object::tree::EntryMode, | |
| 83 | + /// The object id after the copy, or the destination of it. | |
| 84 | + id: Id<'new>, | |
| 85 | + }, | |
| 35 | 86 | } |
| 36 | 87 | |
| 37 | −impl<'old, 'new> Event<'old, 'new> { | |
| 88 | +impl<'a, 'old, 'new> Event<'a, 'old, 'new> { | |
| 38 | 89 | /// Produce a platform for performing a line-diff, or `None` if this is not a [`Modification`][Event::Modification] |
| 39 | 90 | /// or one of the entries to compare is not a blob. |
| 40 | 91 | pub fn diff( |
| @@ -50,4 +101,15 @@ impl<'old, 'new> Event<'old, 'new> { · 9 unmodified lines | ||
| 50 | 101 | _ => None, |
| 51 | 102 | } |
| 52 | 103 | } |
| 104 | + | |
| 105 | + /// Return the current mode of this instance. | |
| 106 | + pub fn entry_mode(&self) -> git_object::tree::EntryMode { | |
| 107 | + match self { | |
| 108 | + Event::Addition { entry_mode, .. } | |
| 109 | + | Event::Deletion { entry_mode, .. } | |
| 110 | + | Event::Modification { entry_mode, .. } | |
| 111 | + | Event::Rename { entry_mode, .. } => *entry_mode, | |
| 112 | + Event::Copy { entry_mode, .. } => *entry_mode, | |
| 113 | + } | |
| 114 | + } | |
| 53 | 115 | } |
| @@ -4,6 +4,7 @@ use git_object::TreeRefIter; · 3 unmodified lines | ||
| 4 | 4 | use git_odb::FindExt; |
| 5 | 5 | |
| 6 | 6 | use super::{change, Action, Change, Platform, Tracking}; |
| 7 | +use crate::object::tree::diff::Renames; | |
| 7 | 8 | use crate::{ |
| 8 | 9 | bstr::{BStr, BString, ByteSlice, ByteVec}, |
| 9 | 10 | ext::ObjectIdExt, |
| @@ -39,6 +40,7 @@ impl<'a, 'old> Platform<'a, 'old> { · 29 unmodified lines | ||
| 39 | 40 | repo: self.lhs.repo, |
| 40 | 41 | other_repo: other.repo, |
| 41 | 42 | tracking: self.tracking, |
| 43 | + _renames: self.renames, | |
| 42 | 44 | location: BString::default(), |
| 43 | 45 | path_deque: Default::default(), |
| 44 | 46 | visit: for_each, |
| @@ -67,6 +69,7 @@ struct Delegate<'old, 'new, VisitFn, E> { · 22 unmodified lines | ||
| 67 | 69 | repo: &'old Repository, |
| 68 | 70 | other_repo: &'new Repository, |
| 69 | 71 | tracking: Option<Tracking>, |
| 72 | + _renames: Option<Renames>, | |
| 70 | 73 | location: BString, |
| 71 | 74 | path_deque: VecDeque<BString>, |
| 72 | 75 | visit: VisitFn, |
| @@ -23,7 +23,7 @@ pub struct Change<'a, 'old, 'new> { · 22 unmodified lines | ||
| 23 | 23 | /// Otherwise this value is always an empty path. |
| 24 | 24 | pub location: &'a BStr, |
| 25 | 25 | /// The diff event itself to provide information about what would need to change. |
| 26 | − pub event: change::Event<'old, 'new>, | |
| 26 | + pub event: change::Event<'a, 'old, 'new>, | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /// |
| @@ -36,12 +36,15 @@ impl<'repo> Tree<'repo> { · 6 unmodified lines | ||
| 36 | 36 | /// # Performance |
| 37 | 37 | /// |
| 38 | 38 | /// It's highly recommended to set an object cache to avoid extracting the same object multiple times. |
| 39 | − pub fn changes<'a>(&'a self) -> Platform<'a, 'repo> { | |
| 40 | − Platform { | |
| 39 | + /// By default, similar to `git diff`, rename tracking will be enabled if it is not configured. | |
| 40 | + #[allow(clippy::result_large_err)] | |
| 41 | + pub fn changes<'a>(&'a self) -> Result<Platform<'a, 'repo>, renames::Error> { | |
| 42 | + Ok(Platform { | |
| 41 | 43 | state: Default::default(), |
| 42 | 44 | lhs: self, |
| 43 | 45 | tracking: None, |
| 44 | − } | |
| 46 | + renames: self.repo.config.diff_renames()?.unwrap_or_default().into(), | |
| 47 | + }) | |
| 45 | 48 | } |
| 46 | 49 | } |
| 47 | 50 | |
| @@ -51,6 +54,7 @@ pub struct Platform<'a, 'repo> { · 3 unmodified lines | ||
| 51 | 54 | state: git_diff::tree::State, |
| 52 | 55 | lhs: &'a Tree<'repo>, |
| 53 | 56 | tracking: Option<Tracking>, |
| 57 | + renames: Option<Renames>, | |
| 54 | 58 | } |
| 55 | 59 | |
| 56 | 60 | #[derive(Clone, Copy)] |
| @@ -62,29 +66,23 @@ enum Tracking { · 5 unmodified lines | ||
| 62 | 66 | /// A structure to capture how to perform rename tracking |
| 63 | 67 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 64 | 68 | pub struct Renames { |
| 65 | − /// If true, default false, copies will be detected as well. | |
| 69 | + /// If `Some(…)`, do also find copies. `None` is the default which does not try to detect copies at all. | |
| 66 | 70 | /// |
| 67 | − /// Note that this is an even more expensive operation then detecting renames as files which were | |
| 68 | − /// determined as renamed can't be removed from the set. | |
| 69 | − pub copies: bool, | |
| 71 | + /// Note that this is an even more expensive operation than detecting renames as files. | |
| 72 | + pub copies: Option<renames::Copies>, | |
| 70 | 73 | /// The percentage of similarity needed for files to be considered renamed or copied, defaulting to `Some(0.5)`. |
| 74 | + /// This field is similar to `git diff -M50%`. | |
| 71 | 75 | /// |
| 72 | 76 | /// If `None`, files are only considered equal if their content matches 100%. |
| 77 | + /// Note that values greater than 1.0 have no different effect than 1.0. | |
| 73 | 78 | pub percentage: Option<f32>, |
| 74 | 79 | /// The amount of files to consider for rename or copy tracking. Defaults to 1000. |
| 75 | 80 | /// If 0, there is no limit. |
| 76 | 81 | pub limit: usize, |
| 77 | 82 | } |
| 78 | 83 | |
| 79 | −impl Default for Renames { | |
| 80 | − fn default() -> Self { | |
| 81 | − Renames { | |
| 82 | − copies: false, | |
| 83 | − percentage: Some(0.5), | |
| 84 | − limit: 1000, | |
| 85 | − } | |
| 86 | − } | |
| 87 | −} | |
| 84 | +/// | |
| 85 | +pub mod renames; | |
| 88 | 86 | |
| 89 | 87 | /// Configuration |
| 90 | 88 | impl<'a, 'repo> Platform<'a, 'repo> { |
| @@ -101,6 +99,16 @@ impl<'a, 'repo> Platform<'a, 'repo> { · 10 unmodified lines | ||
| 101 | 99 | self.tracking = Some(Tracking::Path); |
| 102 | 100 | self |
| 103 | 101 | } |
| 102 | + | |
| 103 | + /// Provide `None` to disable rename tracking entirely, or pass `Some(<configuration>)` to control to | |
| 104 | + /// what extend rename tracking is performed. | |
| 105 | + /// | |
| 106 | + /// Note that by default, the configuration determines rename tracking and standard git defaults are used | |
| 107 | + /// if nothing is configured, which turns on rename tracking with `-M50%`. | |
| 108 | + pub fn track_renames(&mut self, renames: Option<Renames>) -> &mut Self { | |
| 109 | + self.renames = renames; | |
| 110 | + self | |
| 111 | + } | |
| 104 | 112 | } |
| 105 | 113 | |
| 106 | 114 | /// |
| @@ -0,0 +1,68 @@ | ||
| 1 | +use crate::config::cache::util::ApplyLeniency; | |
| 2 | +use crate::config::tree::Diff; | |
| 3 | +use crate::diff::rename::Tracking; | |
| 4 | +use crate::object::tree::diff::Renames; | |
| 5 | + | |
| 6 | +/// The way copies are located. | |
| 7 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] | |
| 8 | +pub enum Copies { | |
| 9 | + /// Find copies from the set of changed files only. | |
| 10 | + FromSetOfChangedFiles, | |
| 11 | +} | |
| 12 | + | |
| 13 | +/// The error returned by [`Renames::try_from_config()]. | |
| 14 | +#[derive(Debug, thiserror::Error)] | |
| 15 | +#[allow(missing_docs)] | |
| 16 | +pub enum Error { | |
| 17 | + #[error(transparent)] | |
| 18 | + DiffRenames(#[from] crate::config::key::GenericError), | |
| 19 | + #[error(transparent)] | |
| 20 | + DiffRenameLimit(#[from] crate::config::unsigned_integer::Error), | |
| 21 | +} | |
| 22 | + | |
| 23 | +impl Default for Renames { | |
| 24 | + fn default() -> Self { | |
| 25 | + Renames { | |
| 26 | + copies: None, | |
| 27 | + percentage: Some(0.5), | |
| 28 | + limit: 1000, | |
| 29 | + } | |
| 30 | + } | |
| 31 | +} | |
| 32 | + | |
| 33 | +impl Renames { | |
| 34 | + /// Create an instance by reading all relevant information from the `config`uration, while being `lenient` or not. | |
| 35 | + /// Returns `Ok(None)` if nothing is configured. | |
| 36 | + /// | |
| 37 | + /// Note that missing values will be defaulted similar to what git does. | |
| 38 | + #[allow(clippy::result_large_err)] | |
| 39 | + pub fn try_from_config(config: &git_config::File<'static>, lenient: bool) -> Result<Option<Self>, Error> { | |
| 40 | + let key = "diff.renames"; | |
| 41 | + let copies = match config | |
| 42 | + .boolean_by_key(key) | |
| 43 | + .map(|value| Diff::RENAMES.try_into_renames(value, || config.string_by_key(key))) | |
| 44 | + .transpose() | |
| 45 | + .with_leniency(lenient)? | |
| 46 | + { | |
| 47 | + Some(renames) => match renames { | |
| 48 | + Tracking::Disabled => return Ok(None), | |
| 49 | + Tracking::Renames => None, | |
| 50 | + Tracking::RenamesAndCopies => Some(Copies::FromSetOfChangedFiles), | |
| 51 | + }, | |
| 52 | + None => return Ok(None), | |
| 53 | + }; | |
| 54 | + | |
| 55 | + let default = Self::default(); | |
| 56 | + Ok(Renames { | |
| 57 | + copies, | |
| 58 | + limit: config | |
| 59 | + .integer_by_key("diff.renameLimit") | |
| 60 | + .map(|value| Diff::RENAME_LIMIT.try_into_usize(value)) | |
| 61 | + .transpose() | |
| 62 | + .with_leniency(lenient)? | |
| 63 | + .unwrap_or(default.limit), | |
| 64 | + ..default | |
| 65 | + } | |
| 66 | + .into()) | |
| 67 | + } | |
| 68 | +} | |
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | version https://git-lfs.github.com/spec/v1 |
| 2 | −oid sha256:a057f8e02b4fa2743d74c84a68ace9ce2c3a051527afa8d9a5cb671b48e36868 | |
| 3 | −size 11272 | |
| 2 | +oid sha256:8173f507161b35b1164e05e08034ee5476466fbe3e897523d8c73a1426dbe581 | |
| 3 | +size 11768 | |
| @@ -17,3 +17,6 @@ git commit -q -am c2 · 16 unmodified lines | ||
| 17 | 17 | |
| 18 | 18 | echo a1 >> a |
| 19 | 19 | git commit -q -am c3 |
| 20 | + | |
| 21 | +git mv a dir/a-moved | |
| 22 | +git commit -q -am r1-identity | |
| @@ -1,100 +0,0 @@ | ||
| 1 | −mod diff { | |
| 2 | − use std::convert::Infallible; | |
| 3 | − | |
| 4 | − use git_object::{bstr::ByteSlice, tree::EntryMode}; | |
| 5 | − use git_repository as git; | |
| 6 | − use git_repository::object::{blob::diff::line::Change, tree::diff::change::Event}; | |
| 7 | − | |
| 8 | − use crate::named_repo; | |
| 9 | − | |
| 10 | − #[test] | |
| 11 | − fn changes_against_tree_modified() { | |
| 12 | − let repo = named_repo("make_diff_repo.sh").unwrap(); | |
| 13 | − let from = tree_named(&repo, "@^{/c3}~1"); | |
| 14 | − let to = tree_named(&repo, ":/c3"); | |
| 15 | − from.changes() | |
| 16 | − .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 17 | − assert_eq!(change.location, "", "without configuration the location field is empty"); | |
| 18 | − match change.event { | |
| 19 | − Event::Modification { | |
| 20 | − previous_entry_mode, | |
| 21 | − previous_id, | |
| 22 | − entry_mode, | |
| 23 | − id, | |
| 24 | − } => { | |
| 25 | − assert_eq!(previous_entry_mode, EntryMode::Blob); | |
| 26 | − assert_eq!(entry_mode, EntryMode::Blob); | |
| 27 | − assert_eq!(previous_id.object().unwrap().data.as_bstr(), "a\n"); | |
| 28 | − assert_eq!(id.object().unwrap().data.as_bstr(), "a\na1\n"); | |
| 29 | − } | |
| 30 | − Event::Deletion { .. } | Event::Addition { .. } => unreachable!("only modification is expected"), | |
| 31 | − }; | |
| 32 | − | |
| 33 | − let diff = change.event.diff().expect("changed file").expect("objects available"); | |
| 34 | − let count = diff.line_counts(); | |
| 35 | − assert_eq!(count.insertions, 1); | |
| 36 | − assert_eq!(count.removals, 0); | |
| 37 | − diff.lines(|hunk| { | |
| 38 | − match hunk { | |
| 39 | − Change::Deletion { .. } => unreachable!("there was no deletion"), | |
| 40 | − Change::Addition { lines } => assert_eq!(lines, vec!["a1".as_bytes().as_bstr()]), | |
| 41 | − Change::Modification { .. } => unreachable!("there was no modification"), | |
| 42 | − }; | |
| 43 | − Ok::<_, Infallible>(()) | |
| 44 | − }) | |
| 45 | − .expect("infallible"); | |
| 46 | − Ok(Default::default()) | |
| 47 | − }) | |
| 48 | − .unwrap(); | |
| 49 | − } | |
| 50 | − | |
| 51 | − #[test] | |
| 52 | − fn changes_against_tree_with_filename_tracking() { | |
| 53 | − let repo = named_repo("make_diff_repo.sh").unwrap(); | |
| 54 | − let from = repo.empty_tree(); | |
| 55 | − let to = tree_named(&repo, ":/c1"); | |
| 56 | − | |
| 57 | − let mut expected = vec!["a", "b", "c", "d"]; | |
| 58 | − from.changes() | |
| 59 | − .track_filename() | |
| 60 | − .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 61 | − expected.retain(|name| name != change.location); | |
| 62 | − Ok(Default::default()) | |
| 63 | − }) | |
| 64 | − .unwrap(); | |
| 65 | − assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen"); | |
| 66 | − | |
| 67 | − let mut expected = vec!["a", "b", "dir/c", "d"]; | |
| 68 | − from.changes() | |
| 69 | − .track_path() | |
| 70 | − .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 71 | − expected.retain(|name| name != change.location); | |
| 72 | − Ok(Default::default()) | |
| 73 | − }) | |
| 74 | − .unwrap(); | |
| 75 | − assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen"); | |
| 76 | − | |
| 77 | − let err = from | |
| 78 | − .changes() | |
| 79 | − .track_path() | |
| 80 | − .for_each_to_obtain_tree(&to, |_change| { | |
| 81 | − Err(std::io::Error::new(std::io::ErrorKind::Other, "custom error")) | |
| 82 | − }) | |
| 83 | − .unwrap_err(); | |
| 84 | − assert_eq!( | |
| 85 | − err.to_string(), | |
| 86 | − "The user-provided callback failed", | |
| 87 | − "custom errors made visible and not squelched" | |
| 88 | − ); | |
| 89 | − } | |
| 90 | − | |
| 91 | − fn tree_named<'repo>(repo: &'repo git::Repository, rev_spec: &str) -> git::Tree<'repo> { | |
| 92 | − repo.rev_parse_single(rev_spec) | |
| 93 | − .unwrap() | |
| 94 | − .object() | |
| 95 | − .unwrap() | |
| 96 | − .peel_to_kind(git::object::Kind::Tree) | |
| 97 | − .unwrap() | |
| 98 | − .into_tree() | |
| 99 | − } | |
| 100 | −} | |
| @@ -0,0 +1,130 @@ | ||
| 1 | +use std::convert::Infallible; | |
| 2 | + | |
| 3 | +use git_object::{bstr::ByteSlice, tree::EntryMode}; | |
| 4 | +use git_repository as git; | |
| 5 | +use git_repository::object::{blob::diff::line::Change, tree::diff::change::Event}; | |
| 6 | + | |
| 7 | +use crate::named_repo; | |
| 8 | + | |
| 9 | +#[test] | |
| 10 | +fn changes_against_tree_modified() -> crate::Result { | |
| 11 | + let repo = named_repo("make_diff_repo.sh")?; | |
| 12 | + let from = tree_named(&repo, "@^{/c3}~1"); | |
| 13 | + let to = tree_named(&repo, ":/c3"); | |
| 14 | + from.changes()? | |
| 15 | + .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 16 | + assert_eq!(change.location, "", "without configuration the location field is empty"); | |
| 17 | + match change.event { | |
| 18 | + Event::Modification { | |
| 19 | + previous_entry_mode, | |
| 20 | + previous_id, | |
| 21 | + entry_mode, | |
| 22 | + id, | |
| 23 | + } => { | |
| 24 | + assert_eq!(previous_entry_mode, EntryMode::Blob); | |
| 25 | + assert_eq!(entry_mode, EntryMode::Blob); | |
| 26 | + assert_eq!(previous_id.object().unwrap().data.as_bstr(), "a\n"); | |
| 27 | + assert_eq!(id.object().unwrap().data.as_bstr(), "a\na1\n"); | |
| 28 | + } | |
| 29 | + Event::Copy { .. } | Event::Rename { .. } | Event::Deletion { .. } | Event::Addition { .. } => { | |
| 30 | + unreachable!("only modification is expected") | |
| 31 | + } | |
| 32 | + }; | |
| 33 | + | |
| 34 | + let diff = change.event.diff().expect("changed file").expect("objects available"); | |
| 35 | + let count = diff.line_counts(); | |
| 36 | + assert_eq!(count.insertions, 1); | |
| 37 | + assert_eq!(count.removals, 0); | |
| 38 | + diff.lines(|hunk| { | |
| 39 | + match hunk { | |
| 40 | + Change::Deletion { .. } => unreachable!("there was no deletion"), | |
| 41 | + Change::Addition { lines } => assert_eq!(lines, vec!["a1".as_bytes().as_bstr()]), | |
| 42 | + Change::Modification { .. } => unreachable!("there was no modification"), | |
| 43 | + }; | |
| 44 | + Ok::<_, Infallible>(()) | |
| 45 | + }) | |
| 46 | + .expect("infallible"); | |
| 47 | + Ok(Default::default()) | |
| 48 | + })?; | |
| 49 | + Ok(()) | |
| 50 | +} | |
| 51 | + | |
| 52 | +#[test] | |
| 53 | +fn changes_against_tree_with_filename_tracking() -> crate::Result { | |
| 54 | + let repo = named_repo("make_diff_repo.sh")?; | |
| 55 | + let from = repo.empty_tree(); | |
| 56 | + let to = tree_named(&repo, ":/c1"); | |
| 57 | + | |
| 58 | + let mut expected = vec!["a", "b", "c", "d"]; | |
| 59 | + from.changes()? | |
| 60 | + .track_filename() | |
| 61 | + .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 62 | + expected.retain(|name| name != change.location); | |
| 63 | + Ok(Default::default()) | |
| 64 | + })?; | |
| 65 | + assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen"); | |
| 66 | + | |
| 67 | + let mut expected = vec!["a", "b", "dir/c", "d"]; | |
| 68 | + from.changes()? | |
| 69 | + .track_path() | |
| 70 | + .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 71 | + expected.retain(|name| name != change.location); | |
| 72 | + Ok(Default::default()) | |
| 73 | + })?; | |
| 74 | + assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen"); | |
| 75 | + | |
| 76 | + let err = from | |
| 77 | + .changes()? | |
| 78 | + .track_path() | |
| 79 | + .for_each_to_obtain_tree(&to, |_change| { | |
| 80 | + Err(std::io::Error::new(std::io::ErrorKind::Other, "custom error")) | |
| 81 | + }) | |
| 82 | + .unwrap_err(); | |
| 83 | + assert_eq!( | |
| 84 | + err.to_string(), | |
| 85 | + "The user-provided callback failed", | |
| 86 | + "custom errors made visible and not squelched" | |
| 87 | + ); | |
| 88 | + Ok(()) | |
| 89 | +} | |
| 90 | + | |
| 91 | +fn tree_named<'repo>(repo: &'repo git::Repository, rev_spec: &str) -> git::Tree<'repo> { | |
| 92 | + repo.rev_parse_single(rev_spec) | |
| 93 | + .unwrap() | |
| 94 | + .object() | |
| 95 | + .unwrap() | |
| 96 | + .peel_to_kind(git::object::Kind::Tree) | |
| 97 | + .unwrap() | |
| 98 | + .into_tree() | |
| 99 | +} | |
| 100 | + | |
| 101 | +mod renames { | |
| 102 | + use crate::object::tree::diff::tree_named; | |
| 103 | + use crate::util::named_repo; | |
| 104 | + use git_ref::bstr::BStr; | |
| 105 | + use git_repository::object::tree::diff::change::Event; | |
| 106 | + use std::convert::Infallible; | |
| 107 | + | |
| 108 | + #[test] | |
| 109 | + #[ignore] | |
| 110 | + fn identity() -> crate::Result { | |
| 111 | + let repo = named_repo("make_diff_repo.sh")?; | |
| 112 | + let from = tree_named(&repo, "@^{/r1-identity}~1"); | |
| 113 | + let to = tree_named(&repo, ":/r1-identity"); | |
| 114 | + | |
| 115 | + let mut actual = Vec::new(); | |
| 116 | + from.changes()? | |
| 117 | + .track_path() | |
| 118 | + .for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> { | |
| 119 | + if !change.event.entry_mode().is_tree() { | |
| 120 | + if let Event::Rename { source_location, .. } = change.event { | |
| 121 | + actual.push(source_location.to_owned()); | |
| 122 | + actual.push(change.location.to_owned()); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + Ok(Default::default()) | |
| 126 | + })?; | |
| 127 | + assert_eq!(actual, vec![BStr::new("a"), "dir/a-moved".into()]); | |
| 128 | + Ok(()) | |
| 129 | + } | |
| 130 | +} | |
| @@ -0,0 +1 @@ | ||
| 1 | +mod diff; | |
| @@ -11,7 +11,7 @@ mod worktree; · 10 unmodified lines | ||
| 11 | 11 | #[test] |
| 12 | 12 | fn size_in_memory() { |
| 13 | 13 | let actual_size = std::mem::size_of::<Repository>(); |
| 14 | − let limit = 1000; | |
| 14 | + let limit = 1200; | |
| 15 | 15 | assert!( |
| 16 | 16 | actual_size <= limit, |
| 17 | 17 | "size of Repository shouldn't change without us noticing, it's meant to be cloned: should have been below {:?}, was {} (bigger on windows)", |