use std::str::FromStr; use gix_diff::{ index::Change, rewrites::{Copies, CopySource}, Rewrites,};use gix_object::bstr::BStr; #[test]fn empty_to_new_tree_without_rename_tracking() -> crate::Result { let changes = collect_changes_no_renames(None, "c1 - initial").expect("really just an addition - nothing to track"); insta::assert_debug_snapshot!(changes, @r#" [ Addition { location: "a", index: 0, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "b", index: 1, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "d", index: 2, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "dir/c", index: 3, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, ] "#); { let (lhs, rhs, _cache, _odb, mut pathspec) = repo_with_indices(None, "c1 - initial", None)?; let err = gix_diff::index( &lhs, &rhs, |_change| Err(std::io::Error::other("custom error")), None::<gix_diff::index::RewriteOptions<'_, gix_odb::Handle>>, &mut pathspec, &mut |_, _, _, _| true, ) .unwrap_err(); assert_eq!( format!("{err:?}"), r#"Callback(Custom { kind: Other, error: "custom error" })"#, "custom errors made visible and not squelched" ); } Ok(())} #[test]fn changes_against_modified_tree_with_filename_tracking() -> crate::Result { let changes = collect_changes_no_renames("c2", "c3-modification")?; insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "a", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(78981922613b2afb6025042ff6bd878ac1994e85), index: 0, entry_mode: Mode( FILE, ), id: Sha1(b4f17b61de71d9b2e54ac9e62b1629ae2d97a6a7), }, Modification { location: "dir/c", previous_index: 3, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(6695780ceb14b05e076a99bbd2babf34723b3464), index: 3, entry_mode: Mode( FILE, ), id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), }, ] "#); Ok(())} #[test]fn renames_by_identity() -> crate::Result { for (from, to, expected, assert_msg, track_empty) in [ ( "c3-modification", "r1-identity", vec![BStr::new("a"), "dir/a-moved".into()], "one rename and nothing else", false, ), ( "c4 - add identical files", "r2-ambiguous", vec![ "s1".into(), "b1".into(), "s2".into(), "b2".into(), "s3".into(), "z".into(), ], "multiple possible sources decide by ordering everything lexicographically", true, ), ( "c4 - add identical files", "r2-ambiguous", vec![], "nothing is tracked with `track_empty = false`", false, ), ( "c5 - add links", "r4-symlinks", vec!["link-1".into(), "renamed-link-1".into()], "symlinks are only tracked by identity", false, ), ( "r1-identity", "c4 - add identical files", vec![], "not having any renames is OK as well", false, ), ( "tc1-identity", "tc1-identity", vec![], "copy tracking is off by default", false, ), ] { for percentage in [None, Some(0.5)] { let (changes, out) = collect_changes_opts( from, to, Some(Rewrites { percentage, track_empty, ..Default::default() }), )?; let actual: Vec<_> = changes .into_iter() .flat_map(|c| match c { Change::Rewrite { source_location, location, copy, .. } => { assert!(!copy); vec![source_location, location] } _ => vec![], }) .collect(); assert_eq!(actual, expected, "{assert_msg}"); #[cfg(not(windows))] assert_eq!( out.expect("present as rewrites are configured").num_similarity_checks, 0, "there are no fuzzy checks in if everything was resolved by identity only" ); } } Ok(())} #[test]fn rename_by_similarity() -> crate::Result { insta::allow_duplicates! { for percentage in [ None, Some(0.76), /*cutoff point where git stops seeing it as equal */ ] { let (changes, out) = collect_changes_opts( "r2-ambiguous", "r3-simple", Some(Rewrites { percentage, ..Default::default() }), ).expect("errors can only happen with IO or ODB access fails"); insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "b", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(61780798228d17af2d34fce4cfbdf35556832472), index: 0, entry_mode: Mode( FILE, ), id: Sha1(54781fa52cf133fa9d0bf59cfe2ef2621b5ad29f), }, Deletion { location: "dir/c", index: 5, entry_mode: Mode( FILE, ), id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), }, Addition { location: "dir/c-moved", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f01e8ddf5adc56985b9a1cda6d7c7ef9e3abe034), }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, if percentage.is_some() { 1 } else { 0 }); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); } } let (changes, out) = collect_changes_opts( "r2-ambiguous", "r3-simple", Some(Rewrites { percentage: Some(0.6), limit: 1, // has no effect as it's just one item here. ..Default::default() }), ) .expect("it found all items at the cut-off point, similar to git"); insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "b", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(61780798228d17af2d34fce4cfbdf35556832472), index: 0, entry_mode: Mode( FILE, ), id: Sha1(54781fa52cf133fa9d0bf59cfe2ef2621b5ad29f), }, Rewrite { source_location: "dir/c", source_index: 5, source_entry_mode: Mode( FILE, ), source_id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), location: "dir/c-moved", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f01e8ddf5adc56985b9a1cda6d7c7ef9e3abe034), copy: false, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 1); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(())} #[test]fn renames_by_similarity_with_limit() -> crate::Result { let (changes, out) = collect_changes_opts( "c6", "r5", Some(Rewrites { limit: 1, // prevent fuzzy tracking from happening ..Default::default() }), )?; assert_eq!( changes.iter().filter(|c| matches!(c, Change::Rewrite { .. })).count(), 0, "fuzzy tracking is effectively disabled due to limit" ); use gix_diff::index::ChangeRef; let actual_locations: Vec<_> = changes.iter().map(ChangeRef::location).collect(); assert_eq!(actual_locations, ["f1", "f1-renamed", "f2", "f2-renamed"]); let actual_indices: Vec<_> = changes.iter().map(ChangeRef::index).collect(); assert_eq!(actual_indices, [6, 6, 7, 7]); use gix_index::entry::Mode; let actual_entry_modes: Vec<_> = changes.iter().map(ChangeRef::entry_mode).collect(); assert_eq!(actual_entry_modes, [Mode::FILE, Mode::FILE, Mode::FILE, Mode::FILE]); let actual_ids: Vec<_> = changes.iter().map(ChangeRef::id).collect(); assert_eq!( actual_ids, [ gix_hash::ObjectId::from_str("f00c965d8307308469e537302baa73048488f162")?, gix_hash::ObjectId::from_str("683cfcc0f47566c332aa45d81c5cc98acb4aab49")?, gix_hash::ObjectId::from_str("3bb459b831ea471b9cd1cbb7c6d54a74251a711b")?, gix_hash::ObjectId::from_str("0a805f8e02d72bd354c1f00607906de2e49e00d6")?, ] ); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 0); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 4); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(())} #[test]fn copies_by_identity() -> crate::Result { let (changes, out) = collect_changes_opts( "c7", "tc1-identity", Some(Rewrites { copies: Some(Copies { source: CopySource::FromSetOfModifiedFiles, percentage: None, }), limit: 1, // the limit isn't actually used for identity based checks ..Default::default() }), )?; insta::assert_debug_snapshot!(changes, @r#" [ Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "c1", index: 4, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "c2", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "dir/c3", index: 9, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 0); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(())} #[test]fn copies_by_similarity() -> crate::Result { let (changes, out) = collect_changes_opts( "tc1-identity", "tc2-similarity", Some(Rewrites { copies: Some(Copies::default()), ..Default::default() }), )?; insta::assert_debug_snapshot!(changes, @r#" [ Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "c4", index: 6, entry_mode: Mode( FILE, ), id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "c5", index: 7, entry_mode: Mode( FILE, ), id: Sha1(08fe19ca4d2f79624f35333157d610811efc1aed), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "dir/c6", index: 12, entry_mode: Mode( FILE, ), id: Sha1(cf7a729ca69bfabd0995fc9b083e86a18215bd91), copy: true, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!( out.num_similarity_checks, 2, "two are similar, the other one is identical" ); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(())} #[test]fn copies_in_entire_tree_by_similarity() -> crate::Result { let (changes, out) = collect_changes_opts( "tc2-similarity", "tc3-find-harder", Some(Rewrites { copies: Some(Copies::default()), ..Default::default()Lines 1–500 of 1352. Load more
use std::str::FromStr; use gix_diff::{ index::Change, rewrites::{Copies, CopySource}, Rewrites, }; use gix_object::bstr::BStr; #[test] fn empty_to_new_tree_without_rename_tracking() -> crate::Result { let changes = collect_changes_no_renames(None, "c1 - initial").expect("really just an addition - nothing to track"); insta::assert_debug_snapshot!(changes, @r#" [ Addition { location: "a", index: 0, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "b", index: 1, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "d", index: 2, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, Addition { location: "dir/c", index: 3, entry_mode: Mode( FILE, ), id: Sha1(e69de29bb2d1d6434b8b29ae775ad8c2e48c5391), }, ] "#); { let (lhs, rhs, _cache, _odb, mut pathspec) = repo_with_indices(None, "c1 - initial", None)?; let err = gix_diff::index( &lhs, &rhs, |_change| Err(std::io::Error::other("custom error")), None::<gix_diff::index::RewriteOptions<'_, gix_odb::Handle>>, &mut pathspec, &mut |_, _, _, _| true, ) .unwrap_err(); assert_eq!( format!("{err:?}"), r#"Callback(Custom { kind: Other, error: "custom error" })"#, "custom errors made visible and not squelched" ); } Ok(()) } #[test] fn changes_against_modified_tree_with_filename_tracking() -> crate::Result { let changes = collect_changes_no_renames("c2", "c3-modification")?; insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "a", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(78981922613b2afb6025042ff6bd878ac1994e85), index: 0, entry_mode: Mode( FILE, ), id: Sha1(b4f17b61de71d9b2e54ac9e62b1629ae2d97a6a7), }, Modification { location: "dir/c", previous_index: 3, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(6695780ceb14b05e076a99bbd2babf34723b3464), index: 3, entry_mode: Mode( FILE, ), id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), }, ] "#); Ok(()) } #[test] fn renames_by_identity() -> crate::Result { for (from, to, expected, assert_msg, track_empty) in [ ( "c3-modification", "r1-identity", vec![BStr::new("a"), "dir/a-moved".into()], "one rename and nothing else", false, ), ( "c4 - add identical files", "r2-ambiguous", vec![ "s1".into(), "b1".into(), "s2".into(), "b2".into(), "s3".into(), "z".into(), ], "multiple possible sources decide by ordering everything lexicographically", true, ), ( "c4 - add identical files", "r2-ambiguous", vec![], "nothing is tracked with `track_empty = false`", false, ), ( "c5 - add links", "r4-symlinks", vec!["link-1".into(), "renamed-link-1".into()], "symlinks are only tracked by identity", false, ), ( "r1-identity", "c4 - add identical files", vec![], "not having any renames is OK as well", false, ), ( "tc1-identity", "tc1-identity", vec![], "copy tracking is off by default", false, ), ] { for percentage in [None, Some(0.5)] { let (changes, out) = collect_changes_opts( from, to, Some(Rewrites { percentage, track_empty, ..Default::default() }), )?; let actual: Vec<_> = changes .into_iter() .flat_map(|c| match c { Change::Rewrite { source_location, location, copy, .. } => { assert!(!copy); vec![source_location, location] } _ => vec![], }) .collect(); assert_eq!(actual, expected, "{assert_msg}"); #[cfg(not(windows))] assert_eq!( out.expect("present as rewrites are configured").num_similarity_checks, 0, "there are no fuzzy checks in if everything was resolved by identity only" ); } } Ok(()) } #[test] fn rename_by_similarity() -> crate::Result { insta::allow_duplicates! { for percentage in [ None, Some(0.76), /*cutoff point where git stops seeing it as equal */ ] { let (changes, out) = collect_changes_opts( "r2-ambiguous", "r3-simple", Some(Rewrites { percentage, ..Default::default() }), ).expect("errors can only happen with IO or ODB access fails"); insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "b", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(61780798228d17af2d34fce4cfbdf35556832472), index: 0, entry_mode: Mode( FILE, ), id: Sha1(54781fa52cf133fa9d0bf59cfe2ef2621b5ad29f), }, Deletion { location: "dir/c", index: 5, entry_mode: Mode( FILE, ), id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), }, Addition { location: "dir/c-moved", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f01e8ddf5adc56985b9a1cda6d7c7ef9e3abe034), }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, if percentage.is_some() { 1 } else { 0 }); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); } } let (changes, out) = collect_changes_opts( "r2-ambiguous", "r3-simple", Some(Rewrites { percentage: Some(0.6), limit: 1, // has no effect as it's just one item here. ..Default::default() }), ) .expect("it found all items at the cut-off point, similar to git"); insta::assert_debug_snapshot!(changes, @r#" [ Modification { location: "b", previous_index: 0, previous_entry_mode: Mode( FILE, ), previous_id: Sha1(61780798228d17af2d34fce4cfbdf35556832472), index: 0, entry_mode: Mode( FILE, ), id: Sha1(54781fa52cf133fa9d0bf59cfe2ef2621b5ad29f), }, Rewrite { source_location: "dir/c", source_index: 5, source_entry_mode: Mode( FILE, ), source_id: Sha1(40006fcef15a8853a1b7ae186d93b7d680fd29cf), location: "dir/c-moved", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f01e8ddf5adc56985b9a1cda6d7c7ef9e3abe034), copy: false, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 1); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(()) } #[test] fn renames_by_similarity_with_limit() -> crate::Result { let (changes, out) = collect_changes_opts( "c6", "r5", Some(Rewrites { limit: 1, // prevent fuzzy tracking from happening ..Default::default() }), )?; assert_eq!( changes.iter().filter(|c| matches!(c, Change::Rewrite { .. })).count(), 0, "fuzzy tracking is effectively disabled due to limit" ); use gix_diff::index::ChangeRef; let actual_locations: Vec<_> = changes.iter().map(ChangeRef::location).collect(); assert_eq!(actual_locations, ["f1", "f1-renamed", "f2", "f2-renamed"]); let actual_indices: Vec<_> = changes.iter().map(ChangeRef::index).collect(); assert_eq!(actual_indices, [6, 6, 7, 7]); use gix_index::entry::Mode; let actual_entry_modes: Vec<_> = changes.iter().map(ChangeRef::entry_mode).collect(); assert_eq!(actual_entry_modes, [Mode::FILE, Mode::FILE, Mode::FILE, Mode::FILE]); let actual_ids: Vec<_> = changes.iter().map(ChangeRef::id).collect(); assert_eq!( actual_ids, [ gix_hash::ObjectId::from_str("f00c965d8307308469e537302baa73048488f162")?, gix_hash::ObjectId::from_str("683cfcc0f47566c332aa45d81c5cc98acb4aab49")?, gix_hash::ObjectId::from_str("3bb459b831ea471b9cd1cbb7c6d54a74251a711b")?, gix_hash::ObjectId::from_str("0a805f8e02d72bd354c1f00607906de2e49e00d6")?, ] ); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 0); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 4); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(()) } #[test] fn copies_by_identity() -> crate::Result { let (changes, out) = collect_changes_opts( "c7", "tc1-identity", Some(Rewrites { copies: Some(Copies { source: CopySource::FromSetOfModifiedFiles, percentage: None, }), limit: 1, // the limit isn't actually used for identity based checks ..Default::default() }), )?; insta::assert_debug_snapshot!(changes, @r#" [ Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "c1", index: 4, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "c2", index: 5, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(f00c965d8307308469e537302baa73048488f162), location: "dir/c3", index: 9, entry_mode: Mode( FILE, ), id: Sha1(f00c965d8307308469e537302baa73048488f162), copy: true, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!(out.num_similarity_checks, 0); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(()) } #[test] fn copies_by_similarity() -> crate::Result { let (changes, out) = collect_changes_opts( "tc1-identity", "tc2-similarity", Some(Rewrites { copies: Some(Copies::default()), ..Default::default() }), )?; insta::assert_debug_snapshot!(changes, @r#" [ Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "c4", index: 6, entry_mode: Mode( FILE, ), id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "c5", index: 7, entry_mode: Mode( FILE, ), id: Sha1(08fe19ca4d2f79624f35333157d610811efc1aed), copy: true, }, Rewrite { source_location: "base", source_index: 3, source_entry_mode: Mode( FILE, ), source_id: Sha1(3bb459b831ea471b9cd1cbb7c6d54a74251a711b), location: "dir/c6", index: 12, entry_mode: Mode( FILE, ), id: Sha1(cf7a729ca69bfabd0995fc9b083e86a18215bd91), copy: true, }, ] "#); let out = out.expect("tracking enabled"); assert_eq!( out.num_similarity_checks, 2, "two are similar, the other one is identical" ); assert_eq!(out.num_similarity_checks_skipped_for_rename_tracking_due_to_limit, 0); assert_eq!(out.num_similarity_checks_skipped_for_copy_tracking_due_to_limit, 0); Ok(()) } #[test] fn copies_in_entire_tree_by_similarity() -> crate::Result { let (changes, out) = collect_changes_opts( "tc2-similarity", "tc3-find-harder", Some(Rewrites { copies: Some(Copies::default()), ..Default::default()