Skip to content

Commit

Permalink
feat(log): add log filter
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 12, 2020
1 parent 88f6f2b commit 3ebaac0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
8 changes: 8 additions & 0 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ fn main() -> Result<()> {
.takes_value(true)
.multiple(true)
.long("scope"))
.arg(Arg::with_name("no-error")
.help("omit error on the commit log")
.short("e")
.long("no-error"))
)

.subcommand(
Expand Down Expand Up @@ -218,6 +222,10 @@ fn main() -> Result<()> {
filters.push(CommitFilter::BreakingChange);
}

if subcommand.is_present("no-error") {
filters.push(CommitFilter::NoError);
}

let filters = CommitFilters(filters);

let mut content = cocogitto.get_log(filters)?;
Expand Down
55 changes: 29 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,37 @@ pub enum CommitFilter {
Scope(String),
Author(String),
BreakingChange,
NoError,
}

pub struct CommitFilters(pub Vec<CommitFilter>);

impl CommitFilters {
pub fn no_error(&self) -> bool {
!self.0.contains(&CommitFilter::NoError)
}

pub fn filter_git2_commit(&self, commit: &Git2Commit) -> bool {
// Author filters
let authors = self
.0
.iter()
.filter_map(|filter| match filter {
CommitFilter::Author(author) => Some(author),
_ => None,
})
.collect::<Vec<&String>>();

let filter_authors = if authors.is_empty() {
true
} else {
authors
.iter()
.any(|author| Some(author.as_str()) == commit.author().name())
};

filter_authors
}
pub fn filters(&self, commit: &Commit) -> bool {
// Commit type filters
let types = self
Expand Down Expand Up @@ -89,30 +115,14 @@ impl CommitFilters {
.any(|scope| Some(*scope) == commit.message.scope.as_ref())
};

// Author filters
let authors = self
.0
.iter()
.filter_map(|filter| match filter {
CommitFilter::Author(author) => Some(author),
_ => None,
})
.collect::<Vec<&String>>();

let filter_authors = if authors.is_empty() {
true
} else {
authors.iter().any(|author| *author == &commit.author)
};

// Breaking changes filters
let filter_breaking_changes = if self.0.contains(&CommitFilter::BreakingChange) {
commit.message.is_breaking_change
} else {
true
};

filter_type && filter_authors && filter_scopes && filter_breaking_changes
filter_type && filter_scopes && filter_breaking_changes
}
}

Expand Down Expand Up @@ -250,19 +260,12 @@ impl CocoGitto {
.iter()
// Remove merge commits
.filter(|commit| !commit.message().unwrap_or("").starts_with("Merge"))
.filter(|commit| filters.filter_git2_commit(&commit))
.map(|commit| Commit::from_git_commit(commit))
// Remove errors if we have filters
.filter(|commit| {
if !filters.0.is_empty() {
commit.is_ok()
} else {
true
}
})
// Apply filters
.filter(|commit| match commit {
Ok(commit) => filters.filters(commit),
Err(_) => false,
Err(_) => filters.no_error(),
})
// Format
.map(|commit| match commit {
Expand Down

0 comments on commit 3ebaac0

Please sign in to comment.