Skip to content

Commit

Permalink
feat(check): allow running check on commit range
Browse files Browse the repository at this point in the history
  • Loading branch information
SanchithHegde authored and oknozor committed May 20, 2023
1 parent 4a6a8b3 commit 5168f75
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/bin/cog/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,16 @@ enum Command {
/// Verify all commit messages against the conventional commit specification
Check {
/// Check commit history, starting from the latest tag to HEAD
#[arg(short = 'l', long)]
#[arg(short = 'l', long, group = "commit_range")]
from_latest_tag: bool,

/// Ignore merge commits messages
#[arg(short, long)]
ignore_merge_commits: bool,

/// Check commits in the specified range
#[arg(group = "commit_range")]
range: Option<String>,
},

/// Create a new conventional commit
Expand Down Expand Up @@ -423,11 +427,12 @@ fn main() -> Result<()> {
Command::Check {
from_latest_tag,
ignore_merge_commits,
range,
} => {
let cocogitto = CocoGitto::get()?;
let from_latest_tag = from_latest_tag || SETTINGS.from_latest_tag;
let ignore_merge_commits = ignore_merge_commits || SETTINGS.ignore_merge_commits;
cocogitto.check(from_latest_tag, ignore_merge_commits)?;
cocogitto.check(from_latest_tag, ignore_merge_commits, range)?;
}
Command::Edit { from_latest_tag } => {
let cocogitto = CocoGitto::get()?;
Expand Down
12 changes: 10 additions & 2 deletions src/command/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ use colored::*;
use log::info;

impl CocoGitto {
pub fn check(&self, check_from_latest_tag: bool, ignore_merge_commits: bool) -> Result<()> {
let commit_range = if check_from_latest_tag {
pub fn check(
&self,
check_from_latest_tag: bool,
ignore_merge_commits: bool,
range: Option<String>,
) -> Result<()> {
let commit_range = if let Some(range) = range {
self.repository
.get_commit_range(&RevspecPattern::from(range.as_str()))?
} else if check_from_latest_tag {
self.repository
.get_commit_range(&RevspecPattern::default())?
} else {
Expand Down
14 changes: 7 additions & 7 deletions tests/lib_tests/cocogitto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn check_commit_history_ok() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(false, false);
let check = cocogitto.check(false, false, None);

// Assert
assert_that!(check).is_ok();
Expand All @@ -58,7 +58,7 @@ fn check_commit_history_err_with_merge_commit() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(false, false);
let check = cocogitto.check(false, false, None);

// Assert
assert_that!(check).is_err();
Expand All @@ -81,7 +81,7 @@ fn check_commit_history_ok_with_merge_commit_ignored() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(false, true);
let check = cocogitto.check(false, true, None);

// Assert
assert_that!(check).is_ok();
Expand All @@ -98,7 +98,7 @@ fn check_commit_history_err() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(false, false);
let check = cocogitto.check(false, false, None);

// Assert
assert_that!(check).is_err();
Expand All @@ -117,7 +117,7 @@ fn check_commit_ok_from_latest_tag() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(true, false);
let check = cocogitto.check(true, false, None);

// Assert
assert_that!(check).is_ok();
Expand All @@ -135,7 +135,7 @@ fn check_commit_err_from_latest_tag() -> Result<()> {
let cocogitto = CocoGitto::get()?;

// Act
let check = cocogitto.check(true, false);
let check = cocogitto.check(true, false, None);

// Assert
assert_that!(check).is_err();
Expand All @@ -152,7 +152,7 @@ fn long_commit_summary_does_not_panic() -> Result<()> {
git_add("Hello", "file")?;
cocogitto.conventional_commit("feat", None, message, None, None, false, false)?;

let check = cocogitto.check(false, false);
let check = cocogitto.check(false, false, None);

assert_that!(check.is_ok());
Ok(())
Expand Down

0 comments on commit 5168f75

Please sign in to comment.