Skip to content

Commit bd01893

Browse files
committed
feat: add explicit accessors for common fields
1 parent dab97f7 commit bd01893

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

gix-diff/src/index/change.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,46 @@ impl ChangeRef<'_, '_> {
114114
} => (location.as_ref(), *index, *entry_mode, id),
115115
}
116116
}
117+
118+
/// Return the `location`, in the case of rewrites referring to the current change.
119+
pub fn location(&self) -> &BStr {
120+
match self {
121+
ChangeRef::Addition { location, .. }
122+
| ChangeRef::Deletion { location, .. }
123+
| ChangeRef::Modification { location, .. }
124+
| ChangeRef::Rewrite { location, .. } => location.as_ref(),
125+
}
126+
}
127+
128+
/// Return the `index`, in the case of rewrites referring to the current change.
129+
pub fn index(&self) -> usize {
130+
match self {
131+
ChangeRef::Addition { index, .. }
132+
| ChangeRef::Deletion { index, .. }
133+
| ChangeRef::Modification { index, .. }
134+
| ChangeRef::Rewrite { index, .. } => *index,
135+
}
136+
}
137+
138+
/// Return the `entry_mode`, in the case of rewrites referring to the current change.
139+
pub fn entry_mode(&self) -> gix_index::entry::Mode {
140+
match self {
141+
ChangeRef::Addition { entry_mode, .. }
142+
| ChangeRef::Deletion { entry_mode, .. }
143+
| ChangeRef::Modification { entry_mode, .. }
144+
| ChangeRef::Rewrite { entry_mode, .. } => *entry_mode,
145+
}
146+
}
147+
148+
/// Return the `id`, in the case of rewrites referring to the current change.
149+
pub fn id(&self) -> &gix_hash::oid {
150+
match self {
151+
ChangeRef::Addition { id, .. }
152+
| ChangeRef::Deletion { id, .. }
153+
| ChangeRef::Modification { id, .. }
154+
| ChangeRef::Rewrite { id, .. } => id,
155+
}
156+
}
117157
}
118158

119159
impl rewrites::tracker::Change for ChangeRef<'_, '_> {

gix-diff/tests/diff/index.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
use gix_diff::{
24
index::Change,
35
rewrites::{Copies, CopySource},
@@ -313,8 +315,27 @@ fn renames_by_similarity_with_limit() -> crate::Result {
313315
0,
314316
"fuzzy tracking is effectively disabled due to limit"
315317
);
316-
let actual: Vec<_> = changes.iter().map(|c| c.fields().0).collect();
317-
assert_eq!(actual, ["f1", "f1-renamed", "f2", "f2-renamed"]);
318+
let actual_locations: Vec<_> = changes.iter().map(|c| c.location()).collect();
319+
assert_eq!(actual_locations, ["f1", "f1-renamed", "f2", "f2-renamed"]);
320+
321+
let actual_indices: Vec<_> = changes.iter().map(|c| c.index()).collect();
322+
assert_eq!(actual_indices, [6, 6, 7, 7]);
323+
324+
use gix_index::entry::Mode;
325+
326+
let actual_entry_modes: Vec<_> = changes.iter().map(|c| c.entry_mode()).collect();
327+
assert_eq!(actual_entry_modes, [Mode::FILE, Mode::FILE, Mode::FILE, Mode::FILE]);
328+
329+
let actual_ids: Vec<_> = changes.iter().map(|c| c.id()).collect();
330+
assert_eq!(
331+
actual_ids,
332+
[
333+
gix_hash::ObjectId::from_str("f00c965d8307308469e537302baa73048488f162")?,
334+
gix_hash::ObjectId::from_str("683cfcc0f47566c332aa45d81c5cc98acb4aab49")?,
335+
gix_hash::ObjectId::from_str("3bb459b831ea471b9cd1cbb7c6d54a74251a711b")?,
336+
gix_hash::ObjectId::from_str("0a805f8e02d72bd354c1f00607906de2e49e00d6")?,
337+
]
338+
);
318339

319340
let out = out.expect("tracking enabled");
320341
assert_eq!(out.num_similarity_checks, 0);
@@ -481,7 +502,7 @@ fn copies_in_entire_tree_by_similarity() -> crate::Result {
481502
0,
482503
"needs --find-copies-harder to detect rewrites here"
483504
);
484-
let actual: Vec<_> = changes.iter().map(|c| c.fields().0).collect();
505+
let actual: Vec<_> = changes.iter().map(|c| c.location()).collect();
485506
assert_eq!(actual, ["b", "c6", "c7", "newly-added"]);
486507

487508
let out = out.expect("tracking enabled");

0 commit comments

Comments
 (0)