Skip to content

Commit

Permalink
feat: improve cli message format and fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent dd26c84 commit d0bb0d4
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 112 deletions.
84 changes: 42 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ which = "^4"
lazy_static = "^1"
toml = "^0"
structopt = { version = "^0", optional = true }
conventional_commit_parser = "^0"
conventional_commit_parser = "0.5.2"
pest = "2.1.3"
pest_derive = "2.1.0"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Let us now create a version:
```
❯ cog bump --auto
Found feature commit caef0f, bumping to 0.1.0
Skipping irrelevant commit 025cc0 with type:docs
Skipping irrelevant commit 025cc0 with type: docs
Found bug fix commit e2af66, bumping to 0.1.1
Found feature commit 1b87aa, bumping to 0.2.0
Bumped version:0.0.0 -> 0.2.0
Expand Down
3 changes: 1 addition & 2 deletions src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Commit {
pub(crate) date: NaiveDateTime,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct CommitConfig {
pub changelog_title: String,
}
Expand All @@ -41,7 +41,6 @@ impl Commit {
let git2_message = message.unwrap().to_owned();
let author = commit.author().name().unwrap_or("").to_string();

// FIXME:Why suddenly commit message start and finish with '\n'
let message = git2_message.trim_end().trim_start();
let conventional_commit = conventional_commit_parser::parse(message);

Expand Down
91 changes: 62 additions & 29 deletions src/conventional/version.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::conventional::commit::Commit;
use crate::git::repository::Repository;
use anyhow::Result;
use colored::*;
use conventional_commit_parser::commit::CommitType;
use git2::Commit as Git2Commit;
use itertools::Itertools;
use semver::Version;

use crate::conventional::commit::Commit;
use crate::git::repository::Repository;

pub enum VersionIncrement {
Major,
Minor,
Expand Down Expand Up @@ -100,50 +102,81 @@ impl VersionIncrement {
}

fn display_history(commits: &[&Git2Commit]) {
for commit in commits {
let commit = Commit::from_git_commit(commit);
// TODO: prompt for continue on err
if let Err(err) = commit {
eprintln!("{}", err);
} else {
let commit = commit.unwrap();
match (
&commit.message.commit_type,
commit.message.is_breaking_change,
) {
(CommitType::Feature, false) => {
println!("Found feature commit {}", commit.shorthand().blue(),)
}
(CommitType::BugFix, false) => {
println!("Found bug fix commit {}", commit.shorthand().blue(),)
}
(commit_type, true) => println!(
"Found {} commit {} with type:{}",
let conventional_commits: Vec<Result<Commit, anyhow::Error>> = commits
.iter()
.map(|commit| Commit::from_git_commit(commit))
.collect();

// Commits which type are neither feat, fix nor breaking changes
// won't affect the version number.
let mut non_bump_commits: Vec<&CommitType> = conventional_commits
.iter()
.filter_map(|commit| match commit {
Ok(commit) => match commit.message.commit_type {
CommitType::Feature | CommitType::BugFix => None,
_ => Some(&commit.message.commit_type),
},
Err(_) => None,
})
.collect();

non_bump_commits.sort();

let non_bump_commits: Vec<(usize, &CommitType)> = non_bump_commits
.into_iter()
.dedup_by_with_count(|c1, c2| c1 == c2)
.collect();

let mut skip_message = "Skipping irrelevant commits:\n".to_string();
for (count, commit_type) in non_bump_commits {
skip_message.push_str(&format!("\t- {}: {}\n", commit_type.as_ref(), count))
}

println!("{}", skip_message);

let bump_commits = conventional_commits
.iter()
.filter_map(|commit| match commit {
Ok(commit) => match commit.message.commit_type {
CommitType::Feature | CommitType::BugFix => Some(Ok(commit)),
_ => None,
},
Err(err) => Some(Err(err)),
});

for commit in bump_commits {
match commit {
Ok(commit) if commit.message.is_breaking_change => {
println!(
"Found {} commit {} with type: {}",
"BREAKING CHANGE".red(),
commit.shorthand().blue(),
commit_type.as_ref().yellow()
),
(_, false) => println!(
"Skipping irrelevant commit {} with type:{}",
commit.shorthand().blue(),
commit.message.commit_type.as_ref().yellow()
),
)
}
Ok(commit) if commit.message.commit_type == CommitType::BugFix => {
println!("Found bug fix commit {}", commit.shorthand().blue())
}
Ok(commit) if commit.message.commit_type == CommitType::Feature => {
println!("Found feature commit {}", commit.shorthand().blue())
}
_ => (),
}
}
}
}

#[cfg(test)]
mod test {
use crate::conventional::commit::Commit;
use crate::conventional::version::VersionIncrement;
use anyhow::Result;
use chrono::Utc;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use semver::Version;
use speculoos::prelude::*;

use crate::conventional::commit::Commit;
use crate::conventional::version::VersionIncrement;

// Auto version tests resides in test/ dir since it rely on git log
// To generate the version

Expand Down
Loading

0 comments on commit d0bb0d4

Please sign in to comment.