Skip to content

Commit

Permalink
Do not require issues for creation of CollectableRefs
Browse files Browse the repository at this point in the history
Instead, require issues only for `into_refs()` and `into_collector()`.
This is a breaking change.
  • Loading branch information
neithernut committed Apr 30, 2018
1 parent 467dcd7 commit 16e3df3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
33 changes: 16 additions & 17 deletions lib/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,26 @@ pub enum ReferenceCollectionSpec {
/// Use this type in order to compute dit-references which are no longer
/// required and thus may be collected.
///
pub struct CollectableRefs<'r, I, J = Issue<'r>>
where I: Iterator<Item = J>,
J: Borrow<Issue<'r>>
pub struct CollectableRefs<'r>
{
repo: &'r git2::Repository,
issues: I,
/// Should remote references be considered during collection?
consider_remote_refs: bool,
/// Under what circumstances should local heads be collected?
collect_heads: ReferenceCollectionSpec,
}

impl<'r, I, J> CollectableRefs<'r, I, J>
where I: Iterator<Item = J>,
J: Borrow<Issue<'r>>
impl<'r> CollectableRefs<'r>
{
/// Create a new CollectableRefs object
///
/// By default only local references are considered, e.g. references which
/// are unnecessary due to remote references are not reported.
///
pub fn new<K>(repo: &'r git2::Repository, issues: K) -> Self
where K: IntoIterator<Item = J, IntoIter = I>
pub fn new(repo: &'r git2::Repository) -> Self
{
CollectableRefs {
repo: repo,
issues: issues.into_iter(),
consider_remote_refs: false,
collect_heads: ReferenceCollectionSpec::Never,
}
Expand Down Expand Up @@ -176,11 +169,14 @@ impl<'r, I, J> CollectableRefs<'r, I, J>

/// Perform the computation of references to collect.
///
pub fn into_refs(mut self) -> Result<Vec<Reference<'r>>> {
pub fn into_refs<I, J, K>(self, issues: I) -> Result<Vec<Reference<'r>>>
where I: IntoIterator<Item = K, IntoIter = J>,
J: Iterator<Item = K>,
K: Borrow<Issue<'r>>
{
// in this function, we assemble a list of references to collect
let mut retval = Vec::new();

let issues: Vec<_> = self.issues.by_ref().collect();
for item in issues {
self.for_issue(item.borrow())?.collect_result_into(&mut retval)?;
}
Expand All @@ -190,9 +186,12 @@ impl<'r, I, J> CollectableRefs<'r, I, J>

/// Transform directly into a reference collection iterator
///
pub fn into_collector(self) -> Result<ReferenceCollector<'r>> {
self.into_refs()
.map(ReferenceCollector::from)
pub fn into_collector<I, J, K>(self, issues: I) -> Result<ReferenceCollector<'r>>
where I: IntoIterator<Item = K, IntoIter = J>,
J: Iterator<Item = K>,
K: Borrow<Issue<'r>>
{
self.into_refs(issues).map(ReferenceCollector::from)
}

/// Push the parents of a referred commit to a revwalk
Expand Down Expand Up @@ -283,9 +282,9 @@ mod tests {

refs_to_collect.sort();

let mut collected: Vec<_> = CollectableRefs::new(repo, issues)
let mut collected: Vec<_> = CollectableRefs::new(repo)
.collect_heads(ReferenceCollectionSpec::BackedByRemoteHead)
.into_refs()
.into_refs(issues)
.expect("Error during collection")
.into_iter()
.map(|r| r.peel(git2::ObjectType::Commit).expect("Could not peel ref").id())
Expand Down
11 changes: 3 additions & 8 deletions lib/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ use error::ErrorKind as EK;
///
pub type UniqueIssues<'a> = HashSet<Issue<'a>>;

/// Convenience alias for easier use of the CollectableRefs type
///
type CollectableRefs<'a> = gc::CollectableRefs<'a, <UniqueIssues<'a> as IntoIterator>::IntoIter>;


/// Extension trait for Repositories
///
Expand Down Expand Up @@ -102,7 +98,7 @@ pub trait RepositoryExt {

/// Produce a CollectableRefs for all issues known to the repository
///
fn collectable_refs<'a>(&'a self) -> Result<CollectableRefs<'a>>;
fn collectable_refs<'a>(&'a self) -> gc::CollectableRefs<'a>;

/// Get an empty tree
///
Expand Down Expand Up @@ -205,9 +201,8 @@ impl RepositoryExt for git2::Repository {
.chain_err(|| EK::CannotGetCommitForRev(id.to_string()))
}

fn collectable_refs<'a>(&'a self) -> Result<CollectableRefs<'a>> {
self.issues()
.map(|issues| gc::CollectableRefs::new(self, issues))
fn collectable_refs<'a>(&'a self) -> gc::CollectableRefs<'a> {
gc::CollectableRefs::new(self)
}

fn issue_messages_iter<'a>(&'a self, commit: Commit<'a>) -> Result<iter::IssueMessagesIter<'a>> {
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ fn gc_impl(matches: &clap::ArgMatches) {

let refs = repo
.collectable_refs()
.unwrap_or_abort()
.consider_remote_refs(matches.is_present("consider-remote"))
.collect_heads(collect_heads)
.into_refs()
.into_refs(repo.issues().unwrap_or_abort())
.unwrap_or_abort();

if matches.is_present("dry-run") {
Expand Down

0 comments on commit 16e3df3

Please sign in to comment.