Skip to content

Commit

Permalink
feat(log): add multiple args for log filters
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 11, 2020
1 parent 44bc3f3 commit 88f6f2b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
21 changes: 15 additions & 6 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,19 @@ fn main() -> Result<()> {
.help("filter on commit type")
.short("t")
.takes_value(true)
.multiple(true)
.long("type"))
.arg(Arg::with_name("author")
.help("filter on commit author")
.short("a")
.takes_value(true)
.multiple(true)
.long("author"))
.arg(Arg::with_name("scope")
.help("filter on commit scope")
.short("s")
.takes_value(true)
.multiple(true)
.long("scope"))
)

Expand Down Expand Up @@ -193,16 +196,22 @@ fn main() -> Result<()> {
let subcommand = matches.subcommand_matches(LOG).unwrap();

let mut filters = vec![];
if let Some(commit_type) = subcommand.value_of("type") {
filters.push(CommitFilter::Type(CommitType::from(commit_type)));
if let Some(commit_types) = subcommand.values_of("type") {
commit_types.for_each(|commit_type| {
filters.push(CommitFilter::Type(CommitType::from(commit_type)));
});
}

if let Some(scope) = subcommand.value_of("scope") {
filters.push(CommitFilter::Scope(scope.to_string()));
if let Some(scopes) = subcommand.values_of("scope") {
scopes.for_each(|scope| {
filters.push(CommitFilter::Scope(scope.to_string()));
});
}

if let Some(author) = subcommand.value_of("author") {
filters.push(CommitFilter::Author(author.to_string()));
if let Some(authors) = subcommand.values_of("author") {
authors.for_each(|author| {
filters.push(CommitFilter::Author(author.to_string()));
});
}

if subcommand.is_present("breaking-change") {
Expand Down
2 changes: 1 addition & 1 deletion src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl fmt::Display for Commit {
}
}

#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum CommitType {
Feature,
BugFix,
Expand Down
85 changes: 73 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum VersionIncrement {
Manual(String),
}

#[derive(Eq, PartialEq)]
pub enum CommitFilter {
Type(CommitType),
Scope(String),
Expand All @@ -52,17 +53,66 @@ pub struct CommitFilters(pub Vec<CommitFilter>);

impl CommitFilters {
pub fn filters(&self, commit: &Commit) -> bool {
let mut take = true;
for filter in self.0.iter() {
take = take
&& match filter {
CommitFilter::Type(commit_type) => commit_type == &commit.message.commit_type,
CommitFilter::Scope(scope) => Some(scope) == commit.message.scope.as_ref(),
CommitFilter::Author(author) => author == &commit.author,
CommitFilter::BreakingChange => commit.message.is_breaking_change,
}
}
take
// Commit type filters
let types = self
.0
.iter()
.filter_map(|filter| match filter {
CommitFilter::Type(commit_type) => Some(commit_type),
_ => None,
})
.collect::<Vec<&CommitType>>();

let filter_type = if types.is_empty() {
true
} else {
types
.iter()
.any(|commit_type| **commit_type == commit.message.commit_type)
};

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

let filter_scopes = if scopes.is_empty() {
true
} else {
scopes
.iter()
.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
}
}

Expand Down Expand Up @@ -198,12 +248,23 @@ impl CocoGitto {
let commits = self.get_commit_range(from, to)?;
let logs = commits
.iter()
// Remove merge commits
.filter(|commit| !commit.message().unwrap_or("").starts_with("Merge"))
.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(err) => true,
Err(_) => false,
})
// Format
.map(|commit| match commit {
Ok(commit) => commit.get_log(),
Err(err) => err.to_string(),
Expand Down

0 comments on commit 88f6f2b

Please sign in to comment.