Skip to content

Commit 88c4a57

Browse files
committed
Support for file-name tracking (#470)
1 parent 7fd9b0e commit 88c4a57

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

git-repository/src/object/tree/mod.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'repo> Tree<'repo> {
6565
#[allow(missing_docs)]
6666
///
6767
pub mod diff {
68-
use crate::bstr::{BStr, BString};
68+
use crate::bstr::{BStr, BString, ByteVec};
6969
use crate::ext::ObjectIdExt;
7070
use crate::{Id, Repository, Tree};
7171
use git_object::TreeRefIter;
@@ -131,13 +131,31 @@ pub mod diff {
131131
Platform {
132132
state: Default::default(),
133133
lhs: self,
134+
tracking: None,
134135
}
135136
}
136137
}
137138

139+
/// The diffing platform returned by [`Tree::changes()`].
140+
#[derive(Clone)]
138141
pub struct Platform<'a, 'repo> {
139142
state: git_diff::tree::State,
140143
lhs: &'a Tree<'repo>,
144+
tracking: Option<Tracking>,
145+
}
146+
147+
#[derive(Clone, Copy)]
148+
enum Tracking {
149+
FileName,
150+
}
151+
152+
/// Configuration
153+
impl<'a, 'repo> Platform<'a, 'repo> {
154+
/// Keep track of file-names, which makes the [`location`][Change::location] field usable with the filename of the changed item.
155+
pub fn track_filename(&mut self) -> &mut Self {
156+
self.tracking = Some(Tracking::FileName);
157+
self
158+
}
141159
}
142160

143161
/// Add the item to compare to.
@@ -155,6 +173,7 @@ pub mod diff {
155173
let mut delegate = Delegate {
156174
repo: self.lhs.repo,
157175
other_repo: other.repo,
176+
tracking: self.tracking,
158177
location: BString::default(),
159178
visit: for_each,
160179
err: None,
@@ -175,6 +194,7 @@ pub mod diff {
175194
struct Delegate<'repo, 'other_repo, VisitFn, E> {
176195
repo: &'repo Repository,
177196
other_repo: &'other_repo Repository,
197+
tracking: Option<Tracking>,
178198
location: BString,
179199
visit: VisitFn,
180200
err: Option<E>,
@@ -192,7 +212,15 @@ pub mod diff {
192212
{}
193213
}
194214

195-
fn push_path_component(&mut self, _component: &BStr) {}
215+
fn push_path_component(&mut self, component: &BStr) {
216+
match self.tracking {
217+
Some(Tracking::FileName) => {
218+
self.location.clear();
219+
self.location.push_str(component);
220+
}
221+
None => {}
222+
}
223+
}
196224

197225
fn pop_path_component(&mut self) {}
198226

git-repository/tests/object/tree.rs

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ mod diff {
3131
}
3232
})
3333
.unwrap();
34+
35+
from.changes()
36+
.track_filename()
37+
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
38+
assert_eq!(change.location, "file");
39+
Ok(git::diff::tree::visit::Action::Continue)
40+
})
41+
.unwrap();
3442
}
3543

3644
fn tree_named<'repo>(repo: &'repo git::Repository, rev_spec: &str) -> git::Tree<'repo> {

0 commit comments

Comments
 (0)