Skip to content

Commit

Permalink
feat(check): filter commits made by git revert
Browse files Browse the repository at this point in the history
Adds the option: --ignore-reverts
If turned on, git commit messages matches on
regex `(?i)revert` are ignored.
  • Loading branch information
liufuyang authored and hdevalke committed Sep 25, 2022
1 parent cb508c6 commit 57837e8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct CheckCommand {
/// Follow only the first parent
#[clap(long)]
pub first_parent: bool,
/// Ignore commits created by `git revert` commands
#[clap(long)]
pub ignore_reverts: bool,
}

#[derive(Debug, Parser)]
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
cli::CheckCommand,
cmd::Command,
conventional::{self, Type},
git::filter_merge_commits,
git::{filter_merge_commits, filter_revert_commits},
Error,
};

Expand Down Expand Up @@ -75,6 +75,7 @@ impl Command for CheckCommand {
.flatten()
.flat_map(|oid| repo.find_commit(oid).ok())
.filter(|commit| filter_merge_commits(commit, merges))
.filter(|commit| filter_revert_commits(commit, self.ignore_reverts))
.take(self.number.unwrap_or(std::usize::MAX))
{
total += 1;
Expand Down
7 changes: 7 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ pub(crate) fn filter_merge_commits(commit: &git2::Commit, merges: bool) -> bool
merges || commit.parent_count() <= 1
}

pub(crate) fn filter_revert_commits(commit: &git2::Commit, ignore_reverts: bool) -> bool {
if ignore_reverts {
return commit.message().map(|m| !m.starts_with("Revert \"")).unwrap_or(true)
}
true
}

/// Build a hashmap that contains Commit `Oid` as key and `Version` as value.
/// Can be used to easily walk a graph and check if it is a version.
fn make_oid_version_map(repo: &Repository, prefix: &str) -> HashMap<Oid, VersionAndTag> {
Expand Down

0 comments on commit 57837e8

Please sign in to comment.