Skip to content

Commit 12251eb

Browse files
authored
Merge pull request #1521 from mvlabat/fix-dir-filename-tracking
Fix dir name tracking in the FileName mode
2 parents e249c34 + 63936e5 commit 12251eb

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

gix-diff/src/tree/recorder.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,16 @@ impl visit::Visit for Recorder {
101101
}
102102

103103
fn push_back_tracked_path_component(&mut self, component: &BStr) {
104-
if let Some(Location::Path) = self.location {
105-
self.push_element(component);
106-
self.path_deque.push_back(self.path.clone());
104+
match self.location {
105+
None => {}
106+
Some(Location::Path) => {
107+
self.push_element(component);
108+
self.path_deque.push_back(self.path.clone());
109+
}
110+
Some(Location::FileName) => {
111+
self.path.clear();
112+
self.path.extend_from_slice(component);
113+
}
107114
}
108115
}
109116

Binary file not shown.

gix/tests/fixtures/make_diff_repo.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ echo d >> d
3434
git commit -q -am "c2"
3535

3636
echo a1 >> a
37+
echo dir/c1 >> dir/c
3738
git commit -q -am "c3-modification"
3839

3940
git mv a dir/a-moved
@@ -46,7 +47,7 @@ git mv s1 z && git mv s2 b2 && git mv s3 b1
4647
git commit -m "r2-ambiguous"
4748

4849
git mv dir/c dir/c-moved
49-
echo n >> dir/c-moved
50+
echo dir/cn >> dir/c-moved
5051
echo n >> b
5152
git commit -am "r3-simple" # modified rename and normal modification
5253

gix/tests/object/tree/diff.rs

+74-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ fn changes_against_tree_modified() -> crate::Result {
1414
let from = tree_named(&repo, "@^{/c3-modification}~1");
1515
let to = tree_named(&repo, ":/c3-modification");
1616
let mut cache = repo.diff_resource_cache(gix_diff::blob::pipeline::Mode::ToGit, Default::default())?;
17+
18+
let expected_modifications = [
19+
(EntryKind::Blob, "a\n", EntryKind::Blob, "a\na1\n"),
20+
(EntryKind::Tree, "", EntryKind::Tree, ""),
21+
(EntryKind::Blob, "dir/c\n", EntryKind::Blob, "dir/c\ndir/c1\n"),
22+
];
23+
let mut i = 0;
24+
1725
from.changes()?
1826
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
27+
let (expected_previous_entry_mode, expected_previous_data, expected_entry_mode, expected_data) =
28+
expected_modifications[i];
29+
1930
assert_eq!(change.location, "", "without configuration the location field is empty");
2031
match change.event {
2132
Event::Modification {
@@ -24,10 +35,16 @@ fn changes_against_tree_modified() -> crate::Result {
2435
entry_mode,
2536
id,
2637
} => {
27-
assert_eq!(previous_entry_mode.kind(), EntryKind::Blob);
28-
assert_eq!(entry_mode.kind(), EntryKind::Blob);
29-
assert_eq!(previous_id.object().unwrap().data.as_bstr(), "a\n");
30-
assert_eq!(id.object().unwrap().data.as_bstr(), "a\na1\n");
38+
assert_eq!(previous_entry_mode.kind(), expected_previous_entry_mode);
39+
assert_eq!(entry_mode.kind(), expected_entry_mode);
40+
41+
if matches!(entry_mode.kind(), EntryKind::Tree) {
42+
i += 1;
43+
return Ok(Default::default());
44+
}
45+
46+
assert_eq!(previous_id.object().unwrap().data.as_bstr(), expected_previous_data);
47+
assert_eq!(id.object().unwrap().data.as_bstr(), expected_data);
3148
}
3249
Event::Rewrite { .. } | Event::Deletion { .. } | Event::Addition { .. } => {
3350
unreachable!("only modification is expected")
@@ -41,14 +58,20 @@ fn changes_against_tree_modified() -> crate::Result {
4158
diff.lines(|hunk| {
4259
match hunk {
4360
Change::Deletion { .. } => unreachable!("there was no deletion"),
44-
Change::Addition { lines } => assert_eq!(lines, vec!["a1\n".as_bytes().as_bstr()]),
61+
Change::Addition { lines } => assert_eq!(
62+
lines,
63+
vec![expected_data[expected_previous_data.len()..].as_bytes().as_bstr()]
64+
),
4565
Change::Modification { .. } => unreachable!("there was no modification"),
4666
};
4767
Ok::<_, Infallible>(())
4868
})
4969
.expect("infallible");
70+
71+
i += 1;
5072
Ok(Default::default())
5173
})?;
74+
assert_eq!(i, 3);
5275
Ok(())
5376
}
5477

@@ -91,6 +114,45 @@ fn changes_against_tree_with_filename_tracking() -> crate::Result {
91114
Ok(())
92115
}
93116

117+
#[test]
118+
fn changes_against_modified_tree_with_filename_tracking() -> crate::Result {
119+
let repo = named_repo("make_diff_repo.sh")?;
120+
let from = tree_named(&repo, "@^{/c3-modification}~1");
121+
let to = tree_named(&repo, ":/c3-modification");
122+
123+
let mut expected = vec!["a", "dir", "c"];
124+
from.changes()?
125+
.track_filename()
126+
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
127+
expected.retain(|name| name != change.location);
128+
Ok(Default::default())
129+
})?;
130+
assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen");
131+
132+
let mut expected = vec!["a", "dir", "dir/c"];
133+
from.changes()?
134+
.track_path()
135+
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
136+
expected.retain(|name| name != change.location);
137+
Ok(Default::default())
138+
})?;
139+
assert_eq!(expected, Vec::<&str>::new(), "all paths should have been seen");
140+
141+
let err = from
142+
.changes()?
143+
.track_path()
144+
.for_each_to_obtain_tree(&to, |_change| {
145+
Err(std::io::Error::new(std::io::ErrorKind::Other, "custom error"))
146+
})
147+
.unwrap_err();
148+
assert_eq!(
149+
err.to_string(),
150+
"The user-provided callback failed",
151+
"custom errors made visible and not squelched"
152+
);
153+
Ok(())
154+
}
155+
94156
fn tree_named(repo: &gix::Repository, rev_spec: impl AsRef<str>) -> gix::Tree {
95157
repo.rev_parse_single(rev_spec.as_ref())
96158
.unwrap()
@@ -246,7 +308,7 @@ mod track_rewrites {
246308
.track_path()
247309
.track_rewrites(
248310
Rewrites {
249-
percentage: Some(0.75),
311+
percentage: Some(0.6),
250312
limit: 1, // has no effect as it's just one item here.
251313
..Default::default()
252314
}
@@ -700,7 +762,7 @@ mod track_rewrites {
700762
#[cfg(not(windows))]
701763
{
702764
let actual = std::fs::read_to_string(repo.work_dir().expect("non-bare").join("baseline.with-renames"))?;
703-
let expected = r#"commit 6974f2b5181772977a9d7d34a566414508552650
765+
let expected = r#"commit 0231f5093bd3d760e7ee82984e0453da80e05c87
704766
Author: author <[email protected]>
705767
Date: Sat Jan 1 00:00:00 2000 +0000
706768
@@ -759,7 +821,7 @@ index e69de29..8ba3a16 100644
759821
#[cfg(not(windows))]
760822
{
761823
let actual = std::fs::read_to_string(repo.work_dir().expect("non-bare").join("baseline.no-renames"))?;
762-
let expected = r#"commit 6974f2b5181772977a9d7d34a566414508552650
824+
let expected = r#"commit 0231f5093bd3d760e7ee82984e0453da80e05c87
763825
Author: author <[email protected]>
764826
Date: Sat Jan 1 00:00:00 2000 +0000
765827
@@ -809,7 +871,7 @@ index e69de29..8ba3a16 100644
809871

810872
#[cfg(not(windows))]
811873
{
812-
let expected = r#"commit 72de3500e1bff816e56432bee8de02946d3e784b
874+
let expected = r#"commit d78c63c5ea3149040767e4387e7fc743cda118fd
813875
Author: author <[email protected]>
814876
Date: Sat Jan 1 00:00:00 2000 +0000
815877
@@ -917,7 +979,7 @@ index 0000000..e69de29
917979

918980
#[cfg(not(windows))]
919981
{
920-
let expected = r#"commit dee00f5a20957db20d8d2e0050210716d6b44879
982+
let expected = r#"commit 0cf7a4fe3ad6c49ae7beb394a1c1df7cc5173ce4
921983
Author: author <[email protected]>
922984
Date: Sat Jan 1 00:00:00 2000 +0000
923985
@@ -991,7 +1053,7 @@ index e69de29..0000000
9911053

9921054
#[cfg(not(windows))]
9931055
{
994-
let expected = r#"commit dee00f5a20957db20d8d2e0050210716d6b44879
1056+
let expected = r#"commit 0cf7a4fe3ad6c49ae7beb394a1c1df7cc5173ce4
9951057
Author: author <[email protected]>
9961058
Date: Sat Jan 1 00:00:00 2000 +0000
9971059
@@ -1060,7 +1122,7 @@ rename to src/gix.rs
10601122

10611123
#[cfg(not(windows))]
10621124
{
1063-
let expected = r#"commit 72de3500e1bff816e56432bee8de02946d3e784b
1125+
let expected = r#"commit d78c63c5ea3149040767e4387e7fc743cda118fd
10641126
Author: author <[email protected]>
10651127
Date: Sat Jan 1 00:00:00 2000 +0000
10661128

0 commit comments

Comments
 (0)