Files
radicle-git/radicle-surf/src/git/history.rs
T
Fintan Halpenny c6c8fe3164 radicle-surf: move vcs::git to git
Other VCS systems are no longer expected to be supported.

Remove the git hierarchy up to the top-level and remove the vcs
hierarchy.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
2022-11-18 10:09:43 +00:00

104 lines
3.3 KiB
Rust

// This file is part of radicle-surf
// <https://github.com/radicle-dev/radicle-git>
//
// Copyright (C) 2022 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{
file_system,
git::{commit::ToCommit, Commit, Error, RepositoryRef},
};
use std::convert::TryFrom;
/// An iterator that produces the history of commits for a given `head`,
/// in the `repo`.
pub struct History<'a> {
repo: RepositoryRef<'a>,
head: Commit,
revwalk: git2::Revwalk<'a>,
filter_by: Option<FilterBy>,
}
/// Internal implementation, subject to refactoring.
enum FilterBy {
File { path: file_system::Path },
}
impl<'a> History<'a> {
/// Creates a new history starting from `head`, in `repo`.
pub fn new<C: ToCommit>(repo: RepositoryRef<'a>, head: C) -> Result<Self, Error> {
let head = head.to_commit(&repo)?;
let mut revwalk = repo.repo_ref.revwalk()?;
revwalk.push(head.id.into())?;
let history = Self {
repo,
head,
revwalk,
filter_by: None,
};
Ok(history)
}
/// Returns the first commit (i.e. the head) in the history.
pub fn head(&self) -> &Commit {
&self.head
}
/// Returns a modified `History` filtered by `path`.
///
/// Note that it is possible that a filtered History becomes empty,
/// even though calling `.head()` still returns the original head.
pub fn by_path(mut self, path: file_system::Path) -> Self {
self.filter_by = Some(FilterBy::File { path });
self
}
}
impl<'a> Iterator for History<'a> {
type Item = Result<Commit, Error>;
fn next(&mut self) -> Option<Self::Item> {
// Loop through the commits with the optional filtering.
while let Some(oid) = self.revwalk.next() {
let found = oid
.map_err(Error::Git)
.and_then(|oid| {
let git2_commit = self.repo.repo_ref.find_commit(oid)?;
// Handles the optional filter_by.
if let Some(FilterBy::File { path }) = &self.filter_by {
let path_opt = self.repo.diff_commit_and_parents(path, &git2_commit)?;
if path_opt.is_none() {
return Ok(None); // Filter out this commit.
}
}
let commit = Commit::try_from(git2_commit)?;
Ok(Some(commit))
})
.transpose();
if found.is_some() {
return found;
}
}
None
}
}
impl<'a> std::fmt::Debug for History<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "History of {}", self.head.id)
}
}