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

✨ Stop if there is no tags in repo and release option is absent #15

Merged
merged 6 commits into from
Feb 25, 2019
Merged
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: 2 additions & 0 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions src/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::path::Path;
use std::result::Result;

use git2::Repository;
use handlebars::Handlebars;

use crate::commit::Commit;
use crate::error::Error;
use crate::version::Version;

const TEMPLATE: &str = "{{#each versions as |version|}}
Expand Down Expand Up @@ -89,11 +91,17 @@ impl Changelog {
Changelog { versions }
}

pub fn to_markdown(&self, release: Option<&str>, print_authors: bool) -> String {
pub fn to_markdown(&self, release: Option<&str>, print_authors: bool) -> Result<String, Error> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to expose the std::Error instead of our custom one?

let mut versions = self.versions.clone();

match release {
None => {
if let Some(version) = versions.first() {
if version.name == "HEAD" {
return Err(Error::NoTag);
}
}

if !versions.is_empty() {
versions.remove(0);
}
Expand All @@ -116,6 +124,6 @@ impl Changelog {
},
});

reg.render_template(TEMPLATE, &json).unwrap()
Ok(reg.render_template(TEMPLATE, &json).unwrap())
}
}
27 changes: 27 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::error;
use std::fmt;

#[derive(Debug, Clone)]
pub enum Error {
NoTag,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NoTag => write!(f, "Your repository does not seem to contain any tag. Please use the release option if you wish to generate a changelog either way.")
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::NoTag => "Your repository does not seem to contain any tag. Please use the release option if you wish to generate a changelog either way."
}
}

fn cause(&self) -> Option<&error::Error> {
None
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ extern crate lazy_static;

mod changelog;
mod commit;
mod error;
mod group;
mod version;

pub use crate::changelog::Changelog;
pub use crate::commit::Commit;
pub use crate::error::Error;
pub use crate::group::Group;
pub use crate::version::Version;
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ extern crate clap;

use std::fs::{self, File};
use std::io::prelude::*;
use std::process;

use regex::Regex;

mod changelog;
mod cli;
mod commit;
mod error;
mod group;
mod version;

Expand All @@ -35,10 +37,16 @@ fn main() {
let get_delta = |from| {
let changelog = Changelog::from(repository, from);

changelog.to_markdown(
match changelog.to_markdown(
matches.value_of("release"),
matches.is_present("print-authors"),
)
) {
Err(error) => {
eprintln!("{}", error);
process::exit(1);
}
Ok(delta) => delta,
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
}
};

let format = |header, delta, footer| {
Expand Down