Skip to content

Commit

Permalink
feat(changelog): improve changelog title formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 28, 2020
1 parent 9da7321 commit d713886
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use self::WriterMode::*;
use crate::commit::{Commit, CommitType};
use crate::COMMITS_METADATA;
use crate::{OidOf, COMMITS_METADATA};
use anyhow::Result;
use git2::Oid;
use std::fs;
use std::path::PathBuf;

Expand All @@ -13,8 +12,8 @@ pub enum WriterMode {
}

pub(crate) struct Changelog {
pub from: Oid,
pub to: Oid,
pub from: OidOf,
pub to: OidOf,
pub date: String,
pub commits: Vec<Commit>,
pub tag_name: Option<String>,
Expand Down Expand Up @@ -73,8 +72,8 @@ impl Changelog {
pub(crate) fn markdown(&mut self, colored: bool) -> String {
let mut out = String::new();

let short_to = &self.to.to_string()[0..6];
let short_from = &self.from.to_string()[0..6];
let short_to = &self.to;
let short_from = &self.from;
let version_title = self
.tag_name
.as_ref()
Expand Down Expand Up @@ -133,6 +132,7 @@ impl Changelog {
mod test {
use crate::changelog::Changelog;
use crate::commit::{Commit, CommitMessage, CommitType};
use crate::OidOf;
use anyhow::Result;
use chrono::Utc;
use git2::Oid;
Expand All @@ -141,8 +141,8 @@ mod test {
fn should_generate_changelog() -> Result<()> {
// Arrange
let mut ch = Changelog {
from: Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?,
to: Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?,
from: OidOf::Other(Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?),
to: OidOf::Other(Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?),
date: Utc::now().date().naive_local().to_string(),
tag_name: None,
commits: vec![
Expand Down Expand Up @@ -196,8 +196,8 @@ mod test {
fn should_generate_empty_changelog() -> Result<()> {
// Arrange
let mut ch = Changelog {
from: Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?,
to: Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?,
from: OidOf::Other(Oid::from_str("5375e15770ddf8821d0c1ad393d315e243014c15")?),
to: OidOf::Other(Oid::from_str("35085f20c5293fc8830e4e44a9bb487f98734f73")?),
date: Utc::now().date().naive_local().to_string(),
commits: vec![],
tag_name: None,
Expand Down
51 changes: 42 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use colored::*;
use commit::Commit;
use git2::{Oid, RebaseOptions};
use semver::Version;
use serde::export::fmt::Display;
use serde::export::Formatter;
use settings::AuthorSetting;
use std::collections::HashMap;
use std::fs::File;
Expand Down Expand Up @@ -410,13 +412,16 @@ impl CocoGitto {

let mut commits = vec![];

let from_oid = from.get_oid();
let to_oid = to.get_oid();

for commit in self
.repository
.get_commit_range(from, to)
.get_commit_range(from_oid, to_oid)
.map_err(|err| anyhow!("Could not get commit range {}...{} : {}", from, to, err))?
{
// We skip the origin commit (ex: from 0.1.0 to 1.0.0)
if commit.id() == from {
if commit.id() == from_oid {
break;
}

Expand Down Expand Up @@ -448,35 +453,63 @@ impl CocoGitto {
}

// TODO : revparse
fn resolve_to_arg(&self, to: Option<&str>) -> Result<Oid> {
fn resolve_to_arg(&self, to: Option<&str>) -> Result<OidOf> {
if let Some(to) = to {
self.get_raw_oid_or_tag_oid(to)
} else {
self.repository
.get_head_commit_oid()
.or_else(|_err| self.repository.get_first_commit())
.map(OidOf::Head)
.or_else(|_err| self.repository.get_first_commit().map(OidOf::Other))
}
}

// TODO : revparse
fn resolve_from_arg(&self, from: Option<&str>) -> Result<Oid> {
fn resolve_from_arg(&self, from: Option<&str>) -> Result<OidOf> {
if let Some(from) = from {
self.get_raw_oid_or_tag_oid(from)
.map_err(|err| anyhow!("Could not resolve from arg {} : {}", from, err))
} else {
self.repository
.get_latest_tag_oid()
.or_else(|_err| self.repository.get_first_commit())
.get_latest_tag_oidof()
.or_else(|_err| self.repository.get_first_commit().map(OidOf::Other))
}
}

fn get_raw_oid_or_tag_oid(&self, input: &str) -> Result<Oid> {
fn get_raw_oid_or_tag_oid(&self, input: &str) -> Result<OidOf> {
if let Ok(_version) = Version::parse(input) {
self.repository
.resolve_lightweight_tag(input)
.map(|oid| OidOf::Tag(input.to_owned(), oid))
.map_err(|err| anyhow!("tag {} not found : {} ", input, err))
} else {
Oid::from_str(input).map_err(|err| anyhow!("`{}` is not a valid oid : {}", input, err))
Oid::from_str(input)
.map(OidOf::Other)
.map_err(|err| anyhow!("`{}` is not a valid oid : {}", input, err))
}
}
}

enum OidOf {
Tag(String, Oid),
Head(Oid),
Other(Oid),
}

impl OidOf {
fn get_oid(&self) -> Oid {
match self {
OidOf::Tag(_, v) | OidOf::Head(v) | OidOf::Other(v) => v.to_owned(),
}
}
}

impl Display for OidOf {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
OidOf::Tag(tag, _) => write!(f, "{}", tag),
OidOf::Head(_) => write!(f, "HEAD"),
OidOf::Other(oid) => write!(f, "{}", &oid.to_string()[0..6]),
}
}
}
12 changes: 11 additions & 1 deletion src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::ErrorKind::Git;
use crate::OidOf;
use anyhow::Result;
use colored::Colorize;
use git2::{
Expand Down Expand Up @@ -126,7 +127,16 @@ impl Repository {

pub(crate) fn get_latest_tag_oid(&self) -> Result<Oid> {
self.get_latest_tag()
.and_then(|oid| self.resolve_lightweight_tag(&oid))
.and_then(|tag| self.resolve_lightweight_tag(&tag))
.map_err(|err| anyhow!("Could not resolve latest tag : {}", err))
}

pub(crate) fn get_latest_tag_oidof(&self) -> Result<OidOf> {
self.get_latest_tag()
.and_then(|tag| {
self.resolve_lightweight_tag(&tag)
.map(|oid| OidOf::Tag(tag, oid))
})
.map_err(|err| anyhow!("Could not resolve latest tag : {}", err))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cog_verify_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn verify_ok() -> Result<()> {
command.arg("verify").arg(message);

command.assert().success().stdout(format!(
r#"a commitw message (not committed) - now
r#"a commit message (not committed) - now
Author: {username}
Type: chore
Scope: none
Expand Down

0 comments on commit d713886

Please sign in to comment.