Skip to content

Commit

Permalink
refactor: replace custom semver struct with semver crate
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 11, 2020
1 parent fecbcf4 commit f780561
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 97 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ config = "^0"
serde_derive = "^1"
serde = "^1"
tempdir = "^0"
semver = "0.10.0"
moins = "0.1.2"
clap = { version = "^2", optional = true }

Expand Down
16 changes: 7 additions & 9 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl Commit {
let date = NaiveDateTime::from_timestamp(commit.time().seconds(), 0);
let message = commit.message();
let git2_message = message.unwrap().to_owned();
let author = commit.author().name().unwrap_or_else(|| "").to_string();
let author = commit.author().name().unwrap_or("").to_string();
let message = Commit::parse_commit_message(&git2_message);

let result = match message {
match message {
Ok(message) => Ok(Commit {
shorthand,
message,
Expand All @@ -76,9 +76,7 @@ impl Commit {
cause
}))
}
};

result
}
}

// Todo extract to ParseError
Expand All @@ -94,21 +92,21 @@ impl Commit {
let idx = type_separator.unwrap();

let mut type_and_scope = &message[0..idx];
let mut is_breaking_change = type_and_scope.chars().last() == Some('!');
let mut is_breaking_change = type_and_scope.ends_with('!');

if is_breaking_change {
type_and_scope = &type_and_scope[0..type_and_scope.len() - 1];
}

let commit_type;

let scope: Option<String> = if let Some(left_par_idx) = type_and_scope.find("(") {
let scope: Option<String> = if let Some(left_par_idx) = type_and_scope.find('(') {
commit_type = CommitType::from(&type_and_scope[0..left_par_idx]);

Some(
type_and_scope
.find(")")
.ok_or(anyhow!("missing closing parenthesis"))
.find(')')
.ok_or_else(|| anyhow!("missing closing parenthesis"))
.map(|right_par_idx| {
type_and_scope[left_par_idx + 1..right_par_idx].to_string()
})?,
Expand Down
1 change: 1 addition & 0 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Hook {

impl Hook {
pub(crate) fn run(&self) -> Result<()> {
todo!();
let command_display = format!("`{}`", &self.command.green());
println!("Running pre-version hook : {}", command_display);

Expand Down
55 changes: 30 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate serde_derive;
mod changelog;
pub mod error;
mod hook;
mod semver;

pub mod commit;
pub mod repository;
Expand All @@ -16,13 +15,13 @@ pub mod settings;
use crate::changelog::Changelog;
use crate::commit::CommitType;
use crate::repository::Repository;
use crate::semver::SemVer;
use crate::settings::Settings;
use anyhow::Result;
use chrono::Utc;
use colored::*;
use commit::Commit;
use git2::{Commit as Git2Commit, Oid, RebaseOptions, Repository as Git2Repository};
use semver::Version;
use std::fs::File;
use std::io::Write;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -216,7 +215,7 @@ impl CocoGitto {

pub fn create_version(&self, increment: VersionIncrement) -> Result<()> {
let next_version = match increment {
VersionIncrement::Manual(version) => SemVer::from_tag(&version)?,
VersionIncrement::Manual(version) => Version::parse(&version)?,
VersionIncrement::Auto => self.get_auto_version()?,
VersionIncrement::Major => self.get_next_major()?,
VersionIncrement::Patch => self.get_next_patch()?,
Expand Down Expand Up @@ -270,13 +269,13 @@ impl CocoGitto {
Ok(changelog.tag_diff_to_markdown())
}

fn get_auto_version(&self) -> Result<SemVer> {
fn get_auto_version(&self) -> Result<Version> {
let tag = self
.repository
.get_latest_tag()
.unwrap_or_else(|_| SemVer::default().to_string());
.unwrap_or_else(|_| Version::new(0, 0, 0).to_string());

let mut version = SemVer::from_tag(&tag)?;
let mut version = Version::parse(&tag)?;

let latest_tag = self
.repository
Expand All @@ -293,23 +292,23 @@ impl CocoGitto {
commit.message.is_breaking_change,
) {
(CommitType::Feature, false) => {
version = version.inc_patch();
version.increment_patch();
println!(
"Found feature commit {}, bumping to {}",
commit.shorthand.blue(),
version.to_string().green()
)
}
(CommitType::BugFix, false) => {
version = version.inc_minor();
version.increment_minor();
println!(
"Found bug fix commit {}, bumping to {}",
commit.shorthand.blue(),
version.to_string().green()
)
}
(commit_type, true) => {
version = version.inc_major();
version.increment_major();
println!(
"Found {} commit {} with type : {}",
"BREAKING CHANGE".red(),
Expand All @@ -327,29 +326,31 @@ impl CocoGitto {
Err(anyhow!(""))
}

fn get_next_major(&self) -> Result<SemVer> {
fn get_next_major(&self) -> Result<Version> {
let tag = self.repository.get_latest_tag()?;
Ok(SemVer::from_tag(&tag)?.inc_major())
let mut version = Version::parse(&tag)?;
version.increment_major();
Ok(version)
}

fn get_next_patch(&self) -> Result<SemVer> {
fn get_next_patch(&self) -> Result<Version> {
let tag = self.repository.get_latest_tag()?;
Ok(SemVer::from_tag(&tag)?.inc_patch())
let mut version = Version::parse(&tag)?;
version.increment_patch();
Ok(version)
}

fn get_next_minor(&self) -> Result<SemVer> {
fn get_next_minor(&self) -> Result<Version> {
let tag = self.repository.get_latest_tag()?;
Ok(SemVer::from_tag(&tag)?.inc_minor())
let mut version = Version::parse(&tag)?;
version.increment_minor();
Ok(version)
}

// TODO : revparse
fn resolve_to_arg(&self, to: Option<&str>) -> Result<Oid> {
if let Some(to) = to {
if to.contains(".") {
self.repository.resolve_lightweight_tag(to)
} else {
Oid::from_str(to).map_err(|err| anyhow!(err))
}
self.get_raw_oid_or_tag_oid(to)
} else {
self.repository
.get_head_commit_oid()
Expand All @@ -360,18 +361,22 @@ impl CocoGitto {
// TODO : revparse
fn resolve_from_arg(&self, from: Option<&str>) -> Result<Oid> {
if let Some(from) = from {
if from.contains(".") {
self.repository.resolve_lightweight_tag(from)
} else {
Oid::from_str(from).map_err(|err| anyhow!(err))
}
self.get_raw_oid_or_tag_oid(from)
} else {
self.repository
.get_latest_tag_oid()
.or_else(|_err| self.repository.get_first_commit())
}
}

fn get_raw_oid_or_tag_oid(&self, input: &str) -> Result<Oid> {
if let Ok(_version) = Version::parse(input) {
self.repository.resolve_lightweight_tag(input)
} else {
Oid::from_str(input).map_err(|err| anyhow!(err))
}
}

fn get_commit_range(&self, from: Oid, to: Oid) -> Result<Vec<Git2Commit>> {
// Ensure commit exists
let repository = self.get_repository();
Expand Down
2 changes: 1 addition & 1 deletion src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Repository {
revwalk.push_head()?;
revwalk
.last()
.ok_or(anyhow!("Could not find commit"))?
.ok_or_else(|| anyhow!("Could not find commit"))?
.map_err(|err| anyhow!(err))
}

Expand Down
62 changes: 0 additions & 62 deletions src/semver.rs

This file was deleted.

0 comments on commit f780561

Please sign in to comment.