Skip to content

Commit

Permalink
Support for file-name tracking (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 19, 2022
1 parent 7fd9b0e commit 88c4a57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
32 changes: 30 additions & 2 deletions git-repository/src/object/tree/mod.rs
Expand Up @@ -65,7 +65,7 @@ impl<'repo> Tree<'repo> {
#[allow(missing_docs)]
///
pub mod diff {
use crate::bstr::{BStr, BString};
use crate::bstr::{BStr, BString, ByteVec};
use crate::ext::ObjectIdExt;
use crate::{Id, Repository, Tree};
use git_object::TreeRefIter;
Expand Down Expand Up @@ -131,13 +131,31 @@ pub mod diff {
Platform {
state: Default::default(),
lhs: self,
tracking: None,
}
}
}

/// The diffing platform returned by [`Tree::changes()`].
#[derive(Clone)]
pub struct Platform<'a, 'repo> {
state: git_diff::tree::State,
lhs: &'a Tree<'repo>,
tracking: Option<Tracking>,
}

#[derive(Clone, Copy)]
enum Tracking {
FileName,
}

/// Configuration
impl<'a, 'repo> Platform<'a, 'repo> {
/// Keep track of file-names, which makes the [`location`][Change::location] field usable with the filename of the changed item.
pub fn track_filename(&mut self) -> &mut Self {
self.tracking = Some(Tracking::FileName);
self
}
}

/// Add the item to compare to.
Expand All @@ -155,6 +173,7 @@ pub mod diff {
let mut delegate = Delegate {
repo: self.lhs.repo,
other_repo: other.repo,
tracking: self.tracking,
location: BString::default(),
visit: for_each,
err: None,
Expand All @@ -175,6 +194,7 @@ pub mod diff {
struct Delegate<'repo, 'other_repo, VisitFn, E> {
repo: &'repo Repository,
other_repo: &'other_repo Repository,
tracking: Option<Tracking>,
location: BString,
visit: VisitFn,
err: Option<E>,
Expand All @@ -192,7 +212,15 @@ pub mod diff {
{}
}

fn push_path_component(&mut self, _component: &BStr) {}
fn push_path_component(&mut self, component: &BStr) {
match self.tracking {
Some(Tracking::FileName) => {
self.location.clear();
self.location.push_str(component);
}
None => {}
}
}

fn pop_path_component(&mut self) {}

Expand Down
8 changes: 8 additions & 0 deletions git-repository/tests/object/tree.rs
Expand Up @@ -31,6 +31,14 @@ mod diff {
}
})
.unwrap();

from.changes()
.track_filename()
.for_each_to_obtain_tree(&to, |change| -> Result<_, Infallible> {
assert_eq!(change.location, "file");
Ok(git::diff::tree::visit::Action::Continue)
})
.unwrap();
}

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

0 comments on commit 88c4a57

Please sign in to comment.