refactoring Revision and ToCommit traits

- Added a generic impl for &Revision
- Updated ToCommit for easier usage.

Signed-off-by: Han Xu <keepsimple@gmail.com>
This commit is contained in:
Han Xu
2022-12-05 21:07:56 -08:00
parent 761e14e7cc
commit a16a627151
8 changed files with 49 additions and 50 deletions
+18 -34
View File
@@ -127,14 +127,6 @@ impl Revision for RefString {
}
}
impl Revision for &RefString {
type Error = git2::Error;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
repo.refname_to_id(self)
}
}
impl Revision for Qualified<'_> {
type Error = git2::Error;
@@ -143,14 +135,6 @@ impl Revision for Qualified<'_> {
}
}
impl Revision for &Qualified<'_> {
type Error = git2::Error;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
repo.refname_to_id(self)
}
}
impl Revision for Oid {
type Error = Infallible;
@@ -170,14 +154,6 @@ impl Revision for &str {
impl Revision for Branch {
type Error = Error;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
(&self).object_id(repo)
}
}
impl Revision for &Branch {
type Error = Error;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
let refname = repo.namespaced_refname(&self.refname())?;
Ok(repo.refname_to_id(&refname)?)
@@ -187,16 +163,24 @@ impl Revision for &Branch {
impl Revision for Tag {
type Error = Infallible;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
(&self).object_id(repo)
fn object_id(&self, _repo: &Repository) -> Result<Oid, Self::Error> {
Ok(self.id())
}
}
impl Revision for &Tag {
type Error = Infallible;
impl Revision for String {
type Error = git2::Error;
fn object_id(&self, _repo: &Repository) -> Result<Oid, Self::Error> {
Ok(self.id())
Oid::from_str(self).map(Oid::from)
}
}
impl<R: Revision> Revision for &R {
type Error = R::Error;
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
(*self).object_id(repo)
}
}
@@ -213,22 +197,22 @@ pub trait ToCommit {
type Error: std::error::Error + Send + Sync + 'static;
/// Converts to a commit in `repo`.
fn to_commit(&self, repo: &Repository) -> Result<Commit, Self::Error>;
fn to_commit(self, repo: &Repository) -> Result<Commit, Self::Error>;
}
impl ToCommit for Commit {
type Error = Infallible;
fn to_commit(&self, _repo: &Repository) -> Result<Commit, Self::Error> {
Ok(self.clone())
fn to_commit(self, _repo: &Repository) -> Result<Commit, Self::Error> {
Ok(self)
}
}
impl<R: Revision> ToCommit for R {
type Error = Error;
fn to_commit(&self, repo: &Repository) -> Result<Commit, Self::Error> {
let oid = repo.object_id(self)?;
fn to_commit(self, repo: &Repository) -> Result<Commit, Self::Error> {
let oid = repo.object_id(&self)?;
let commit = repo.find_commit(oid)?;
Ok(Commit::try_from(commit)?)
}
+1 -1
View File
@@ -38,7 +38,7 @@ enum FilterBy {
impl<'a> History<'a> {
/// Creates a new history starting from `head`, in `repo`.
pub fn new<C: ToCommit>(repo: &'a Repository, head: &C) -> Result<Self, Error> {
pub fn new<C: ToCommit>(repo: &'a Repository, head: C) -> Result<Self, Error> {
let head = head
.to_commit(repo)
.map_err(|err| Error::ToCommit(err.into()))?;
+4 -4
View File
@@ -243,7 +243,7 @@ impl Repository {
///
/// To visit inside any nested sub-directories, call `directory.get(&repo)`
/// on the sub-directory.
pub fn root_dir<C: ToCommit>(&self, commit: &C) -> Result<Directory, Error> {
pub fn root_dir<C: ToCommit>(&self, commit: C) -> Result<Directory, Error> {
let commit = commit
.to_commit(self)
.map_err(|err| Error::ToCommit(err.into()))?;
@@ -254,7 +254,7 @@ impl Repository {
/// Returns the last commit, if exists, for a `path` in the history of
/// `rev`.
pub fn last_commit<P, C>(&self, path: P, rev: &C) -> Result<Option<Commit>, Error>
pub fn last_commit<P, C>(&self, path: P, rev: C) -> Result<Option<Commit>, Error>
where
P: AsRef<Path>,
C: ToCommit,
@@ -271,7 +271,7 @@ impl Repository {
/// Gets the [`Stats`] of this repository.
pub fn stats(&self) -> Result<Stats, Error> {
let branches = self.branches(Glob::all_heads())?.count();
let mut history = self.history(&self.head()?)?;
let mut history = self.history(self.head()?)?;
let (commits, contributors) = history.try_fold(
(0, BTreeSet::new()),
|(commits, mut contributors), commit| {
@@ -343,7 +343,7 @@ impl Repository {
}
/// Returns the history with the `head` commit.
pub fn history<C: ToCommit>(&self, head: &C) -> Result<History, Error> {
pub fn history<C: ToCommit>(&self, head: C) -> Result<History, Error> {
History::new(self, head)
}
}