Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable version increments per Type #164

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cmd/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a> ChangeLogTransformer<'a> {
short_hash,
references,
};
if let Some(section) = self.group_types.get(conv_commit.r#type.as_ref()) {
if let Some(section) = self.group_types.get(conv_commit.r#type.as_str()) {
commits.entry(section).or_default().push(commit_context)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use git2::Repository;
use crate::{
cli::CheckCommand,
cmd::Command,
conventional::{self, Type},
conventional,
git::{filter_merge_commits, filter_revert_commits},
strip::Strip,
Error,
Expand All @@ -23,7 +23,7 @@ fn print_fail(msg: &str, short_id: &str, e: Error) -> bool {
false
}

fn print_wrong_type(msg: &str, short_id: &str, commit_type: Type) -> bool {
fn print_wrong_type(msg: &str, short_id: &str, commit_type: String) -> bool {
print_fail(
msg,
short_id,
Expand All @@ -37,7 +37,7 @@ fn print_check(
msg: &str,
short_id: &str,
parser: &conventional::CommitParser,
types: &[Type],
types: &[String],
) -> bool {
let msg_parsed = parser.parse(msg);

Expand Down Expand Up @@ -66,11 +66,11 @@ impl Command for CheckCommand {
.scope_regex(config.scope_regex)
.strip_regex(config.strip_regex)
.build();
let types: Vec<Type> = config
let types: Vec<String> = config
.types
.iter()
.map(|ty| ty.r#type.as_str())
.map(Type::from)
.map(String::from)
.collect();

let Config { merges, .. } = config;
Expand Down
10 changes: 3 additions & 7 deletions src/cmd/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ fn edit_message(msg: &str) -> Result<String, Error> {
.strip())
}

fn edit_loop(
msg: &str,
parser: &CommitParser,
types: &[crate::conventional::Type],
) -> Result<String, Error> {
fn edit_loop(msg: &str, parser: &CommitParser, types: &[String]) -> Result<String, Error> {
let mut edit_msg = msg.to_owned();
loop {
edit_msg = edit_message(&edit_msg)?;
Expand Down Expand Up @@ -374,11 +370,11 @@ impl Command for CommitCommand {
}
}

fn config_types_to_conventional(types: &[Type]) -> Vec<crate::conventional::Type> {
fn config_types_to_conventional(types: &[Type]) -> Vec<String> {
types
.iter()
.map(|ty| ty.r#type.as_str())
.map(crate::conventional::Type::from)
.map(String::from)
.collect()
}

Expand Down
27 changes: 19 additions & 8 deletions src/cmd/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use semver::Version;
use crate::{
cli::VersionCommand,
cmd::Command,
conventional::{CommitParser, Config, Type},
conventional::{
config::{Config, Increment, Type},
CommitParser,
},
git::{GitHelper, VersionAndTag},
semver::SemVer,
Error,
Expand Down Expand Up @@ -59,6 +62,7 @@ impl VersionCommand {
last_v_tag: &str,
mut last_version: SemVer,
parser: &CommitParser,
types: Vec<Type>,
) -> Result<(Version, Label, String), Error> {
let prefix = self.prefix.as_str();
let git = GitHelper::new(prefix)?;
Expand Down Expand Up @@ -94,11 +98,17 @@ impl VersionCommand {
}
break;
}
match (commit.r#type, major_version_zero) {
(Type::Feat, true) => patch = true,
(Type::Feat, false) => minor = true,
(Type::Fix, _) => patch = true,
_ => (),

let option_commit_type = types.iter().find(|x| x.to_string() == commit.r#type);

if let Some(some_commit_type) = option_commit_type {
match (&some_commit_type.increment, major_version_zero) {
(&Increment::Major, _) => major = true,
(&Increment::Minor, true) => patch = true,
(&Increment::Minor, false) => minor = true,
(&Increment::Patch, _) => patch = true,
(&Increment::None, _) => {}
}
}
}
let label = match (major, minor, patch) {
Expand Down Expand Up @@ -127,6 +137,7 @@ impl VersionCommand {
&self,
scope_regex: String,
strip_regex: String,
types: Vec<Type>,
) -> Result<(Version, Label, String), Error> {
if let Some(VersionAndTag {
tag,
Expand Down Expand Up @@ -158,7 +169,7 @@ impl VersionCommand {
.scope_regex(scope_regex)
.strip_regex(strip_regex)
.build();
self.find_bump_version(tag.as_str(), version, &parser)?
self.find_bump_version(tag.as_str(), version, &parser, types)?
}
} else {
(version.0, Label::Release, commit_sha)
Expand All @@ -185,7 +196,7 @@ impl VersionCommand {
impl Command for VersionCommand {
fn exec(&self, config: Config) -> anyhow::Result<()> {
let (version, label, commit_sha) =
self.get_version(config.scope_regex, config.strip_regex)?;
self.get_version(config.scope_regex, config.strip_regex, config.types)?;
if self.label {
println!("{label}");
} else if self.commit_sha {
Expand Down
2 changes: 1 addition & 1 deletion src/conventional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub(crate) mod changelog;
mod commits;
pub(crate) mod config;

pub(crate) use commits::{CommitParser, Footer, ParseError, Type};
pub(crate) use commits::{CommitParser, Footer, ParseError};
pub(crate) use config::Config;
80 changes: 10 additions & 70 deletions src/conventional/commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,6 @@ use regex::Regex;
use serde::Serialize;
use thiserror::Error;

#[derive(Debug, PartialEq)]
pub(crate) enum Type {
Build,
Chore,
Ci,
Docs,
Feat,
Fix,
Perf,
Refactor,
Revert,
Style,
Test,
Custom(String),
}

impl AsRef<str> for Type {
fn as_ref(&self) -> &str {
match self {
Self::Build => "build",
Self::Chore => "chore",
Self::Ci => "ci",
Self::Docs => "docs",
Self::Feat => "feat",
Self::Fix => "fix",
Self::Perf => "perf",
Self::Refactor => "refactor",
Self::Revert => "revert",
Self::Style => "style",
Self::Test => "test",
Self::Custom(c) => c.as_str(),
}
}
}

impl From<&str> for Type {
fn from(s: &str) -> Type {
match s.to_ascii_lowercase().as_str() {
"build" => Self::Build,
"chore" => Self::Chore,
"ci" => Self::Ci,
"docs" => Self::Docs,
"feat" => Self::Feat,
"fix" => Self::Fix,
"perf" => Self::Perf,
"refactor" => Self::Refactor,
"revert" => Self::Revert,
"style" => Self::Style,
"test" => Self::Test,
custom => Self::Custom(custom.to_owned()),
}
}
}

impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}

#[derive(Debug, PartialEq)]
pub(crate) struct Footer {
pub(crate) key: String,
Expand All @@ -79,7 +19,7 @@ pub(crate) struct Reference {

#[derive(Debug, PartialEq)]
pub struct Commit {
pub(crate) r#type: Type,
pub(crate) r#type: String,
pub(crate) scope: Option<String>,
pub(crate) breaking: bool,
pub(crate) description: String,
Expand Down Expand Up @@ -136,7 +76,7 @@ impl CommitParser {
let mut lines = s.lines();
if let Some(first) = lines.next() {
if let Some(capts) = self.regex_first_line.captures(first) {
let r#type: Option<Type> = capts.name("type").map(|t| t.as_str().into());
let r#type = capts.name("type").map(|t| t.as_str().to_owned());
let scope = capts.name("scope").map(|s| s.as_str().to_owned());
if let Some(ref scope) = scope {
if !self.regex_scope.is_match(scope.as_str()) {
Expand Down Expand Up @@ -318,7 +258,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Docs,
r#type: "docs".into(),
scope: None,
breaking: false,
description: "correct spelling of CHANGELOG".into(),
Expand All @@ -337,7 +277,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Feat,
r#type: "feat".into(),
scope: Some("lang".into()),
breaking: false,
description: "add polish language".into(),
Expand All @@ -356,7 +296,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Feat,
r#type: "feat".into(),
scope: Some("bar2/a_b-C4".into()),
breaking: false,
description: "add a foo to new bar".into(),
Expand Down Expand Up @@ -385,7 +325,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Refactor,
r#type: "refactor".into(),
scope: None,
breaking: true,
description: "drop support for Node 6".into(),
Expand All @@ -406,7 +346,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Feat,
r#type: "feat".into(),
scope: None,
breaking: false,
description: "allow provided config object to extend other configs".into(),
Expand Down Expand Up @@ -446,7 +386,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Fix,
r#type: "fix".into(),
scope: None,
breaking: false,
description: "correct minor typos in code".into(),
Expand Down Expand Up @@ -480,7 +420,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Revert,
r#type: "revert".into(),
scope: None,
breaking: false,
description: "let us never again speak of the noodle incident #1".into(),
Expand Down Expand Up @@ -522,7 +462,7 @@ mod tests {
assert_eq!(
commit,
Commit {
r#type: Type::Docs,
r#type: "docs".into(),
scope: None,
breaking: false,
description: "correct spelling of CHANGELOG".into(),
Expand Down
Loading
Loading