Skip to content

Commit 5335c84

Browse files
authored
Merge pull request #2071 from cruessler/add-accessors-to-change-ref
Add explicit accessors to `gix_diff::index::ChangeRef``
2 parents f87967d + 79b8f06 commit 5335c84

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

gix-diff/src/index/change.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ impl ChangeRef<'_, '_> {
8282
/// Return all shared fields among all variants: `(location, index, entry_mode, id)`
8383
///
8484
/// In case of rewrites, the fields return to the current change.
85+
///
86+
/// Note that there are also more specific accessors in case you only need to access to one of
87+
/// these fields individually.
88+
///
89+
/// See [`ChangeRef::location()`], [`ChangeRef::index()`], [`ChangeRef::entry_mode()`] and
90+
/// [`ChangeRef::id()`].
8591
pub fn fields(&self) -> (&BStr, usize, gix_index::entry::Mode, &gix_hash::oid) {
8692
match self {
8793
ChangeRef::Addition {
@@ -114,6 +120,46 @@ impl ChangeRef<'_, '_> {
114120
} => (location.as_ref(), *index, *entry_mode, id),
115121
}
116122
}
123+
124+
/// Return the `location`, in the case of rewrites referring to the current change.
125+
pub fn location(&self) -> &BStr {
126+
match self {
127+
ChangeRef::Addition { location, .. }
128+
| ChangeRef::Deletion { location, .. }
129+
| ChangeRef::Modification { location, .. }
130+
| ChangeRef::Rewrite { location, .. } => location.as_ref(),
131+
}
132+
}
133+
134+
/// Return the `index`, in the case of rewrites referring to the current change.
135+
pub fn index(&self) -> usize {
136+
match self {
137+
ChangeRef::Addition { index, .. }
138+
| ChangeRef::Deletion { index, .. }
139+
| ChangeRef::Modification { index, .. }
140+
| ChangeRef::Rewrite { index, .. } => *index,
141+
}
142+
}
143+
144+
/// Return the `entry_mode`, in the case of rewrites referring to the current change.
145+
pub fn entry_mode(&self) -> gix_index::entry::Mode {
146+
match self {
147+
ChangeRef::Addition { entry_mode, .. }
148+
| ChangeRef::Deletion { entry_mode, .. }
149+
| ChangeRef::Modification { entry_mode, .. }
150+
| ChangeRef::Rewrite { entry_mode, .. } => *entry_mode,
151+
}
152+
}
153+
154+
/// Return the `id`, in the case of rewrites referring to the current change.
155+
pub fn id(&self) -> &gix_hash::oid {
156+
match self {
157+
ChangeRef::Addition { id, .. }
158+
| ChangeRef::Deletion { id, .. }
159+
| ChangeRef::Modification { id, .. }
160+
| ChangeRef::Rewrite { id, .. } => id,
161+
}
162+
}
117163
}
118164

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

gix-diff/tests/diff/index.rs

Lines changed: 27 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,30 @@ 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+
319+
use gix_diff::index::ChangeRef;
320+
321+
let actual_locations: Vec<_> = changes.iter().map(ChangeRef::location).collect();
322+
assert_eq!(actual_locations, ["f1", "f1-renamed", "f2", "f2-renamed"]);
323+
324+
let actual_indices: Vec<_> = changes.iter().map(ChangeRef::index).collect();
325+
assert_eq!(actual_indices, [6, 6, 7, 7]);
326+
327+
use gix_index::entry::Mode;
328+
329+
let actual_entry_modes: Vec<_> = changes.iter().map(ChangeRef::entry_mode).collect();
330+
assert_eq!(actual_entry_modes, [Mode::FILE, Mode::FILE, Mode::FILE, Mode::FILE]);
331+
332+
let actual_ids: Vec<_> = changes.iter().map(ChangeRef::id).collect();
333+
assert_eq!(
334+
actual_ids,
335+
[
336+
gix_hash::ObjectId::from_str("f00c965d8307308469e537302baa73048488f162")?,
337+
gix_hash::ObjectId::from_str("683cfcc0f47566c332aa45d81c5cc98acb4aab49")?,
338+
gix_hash::ObjectId::from_str("3bb459b831ea471b9cd1cbb7c6d54a74251a711b")?,
339+
gix_hash::ObjectId::from_str("0a805f8e02d72bd354c1f00607906de2e49e00d6")?,
340+
]
341+
);
318342

319343
let out = out.expect("tracking enabled");
320344
assert_eq!(out.num_similarity_checks, 0);
@@ -481,7 +505,7 @@ fn copies_in_entire_tree_by_similarity() -> crate::Result {
481505
0,
482506
"needs --find-copies-harder to detect rewrites here"
483507
);
484-
let actual: Vec<_> = changes.iter().map(|c| c.fields().0).collect();
508+
let actual: Vec<_> = changes.iter().map(gix_diff::index::ChangeRef::location).collect();
485509
assert_eq!(actual, ["b", "c6", "c7", "newly-added"]);
486510

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

gix/src/status/iter/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl Item {
135135
pub fn location(&self) -> &BStr {
136136
match self {
137137
Item::IndexWorktree(change) => change.rela_path(),
138-
Item::TreeIndex(change) => change.fields().0,
138+
Item::TreeIndex(change) => change.location(),
139139
}
140140
}
141141
}

0 commit comments

Comments
 (0)