Skip to content

Commit

Permalink
Feat(bump): allow performing a simple version bump without analysing …
Browse files Browse the repository at this point in the history
…commit messages

Fixes #3, for when you have a version number and a bump type, and just what to know the next version.
  • Loading branch information
albx79 committed Nov 7, 2019
1 parent fa2e04a commit edbe34c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/bumping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use git2::Commit;
use std::convert::TryFrom;
use std::error::Error;
use simple_error::SimpleError;
use std::str::FromStr;

/// Extension methods for `&str`, useful for handling conventional commits
pub trait FirstLine<'a> {
Expand Down Expand Up @@ -91,6 +92,20 @@ pub trait Bump {
fn bump(self, bt: &BumpType) -> Self;
}

impl FromStr for BumpType {
type Err = SimpleError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"none" => Ok(BumpType::None),
"patch" => Ok(BumpType::Patch),
"minor" => Ok(BumpType::Minor),
"major" => Ok(BumpType::Major),
x => Err(SimpleError::new(format!("Not a bump specification: {}", x)))
}
}
}

impl From<&str> for BumpType {
fn from(commit_msg: &str) -> Self {
let first_line = commit_msg.first_line();
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use structopt::StructOpt;

use crate::bumping::{Bump, BumpType};
use crate::changelog::ChangeLog;
use simple_error::SimpleError;

mod bumping;
mod repo;
Expand All @@ -26,6 +27,7 @@ mod changelog;
#[structopt(name = "what-bump")]
struct Config {
/// Analyse commits up to this one (exclusive)
#[structopt(required_unless = "bump", default_value = "")]
up_to_revision: String,

/// Current version of your software
Expand All @@ -39,11 +41,26 @@ struct Config {
/// Also generate a changelog, and write it to this file
#[structopt(long, short)]
changelog: Option<PathBuf>,

/// Perform the specified version bump (you must also specify `--from`)
#[structopt(long, short)]
bump: Option<BumpType>,
}

fn main() -> Result<(), Box<dyn Error>> {
let config: Config = Config::from_args();

match (config.bump, &config.from) {
(Some(ref bump_type), Some(ref version)) => {
println!("{}", version.clone().bump(bump_type));
return Ok(());
},
(Some(_), None) => {
return Err(Box::new(SimpleError::new("If you specify `--bump`, you must also specify `--from`, otherwise I don't know what version to bump.")));
}
_ => (),
}

let max_bump_type = config.path.commits_up_to(&config.up_to_revision)?
.map(|commit| Ok(BumpType::from(commit.message().unwrap_or("<no commit message>"))))
.max()?
Expand Down

0 comments on commit edbe34c

Please sign in to comment.