Skip to content

Commit

Permalink
Properly handle tags with v prefix, and minor versions >9 (#18)
Browse files Browse the repository at this point in the history
Fixes #17, fixes #9.
  • Loading branch information
perlun authored Feb 8, 2018
1 parent 7fc990a commit bf12e72
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/changelog_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use std::process::Command;

pub struct ChangelogGenerator {
pub repository_path: String,

// A git revision. Example value: v8.1.0
pub from_revision: String,

// A git revision. Example value: v8.1.1
pub to_revision: String,

pub to_alias: String
}

Expand Down
27 changes: 27 additions & 0 deletions src/git_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::cmp::Ordering;
use semver::Version;

#[derive(Clone)]
#[derive(Eq)]
pub struct GitTag {
pub tag: String,
pub version: Version
}

impl Ord for GitTag {
fn cmp(&self, other: &GitTag) -> Ordering {
self.version.cmp(&other.version)
}
}

impl PartialOrd for GitTag {
fn partial_cmp(&self, other: &GitTag) -> Option<Ordering> {
Some(self.version.cmp(&other.version))
}
}

impl PartialEq for GitTag {
fn eq(&self, other: &GitTag) -> bool {
self.version == other.version
}
}
24 changes: 17 additions & 7 deletions src/git_tag_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use semver::Version;
use std::process::Command;

use git_tag::GitTag;

pub struct GitTagParser {
pub repository_path: String
}
Expand All @@ -10,9 +12,13 @@ impl GitTagParser {
// tag, the "to" revision is the current semver tag.
pub fn get_version_tag_pairs(&self) -> Vec<(String, String)> {
let mut from_version = self.get_root_ancestor();
let mut tag_pairs: Vec<(String, String)> = self.semver_tags().into_iter().rev().map(|tag| {

let mut tags = self.semver_tags();
tags.sort();

let mut tag_pairs: Vec<(String, String)> = tags.into_iter().map(|tag| {
let old_from_version = from_version.clone();
let to_version = tag;
let to_version = tag.tag;
from_version = to_version.clone();

(old_from_version, to_version)
Expand All @@ -24,12 +30,16 @@ impl GitTagParser {
tag_pairs
}

fn semver_tags(&self) -> Vec<String> {
fn semver_tags(&self) -> Vec<GitTag> {
let tags = self.get_tags();
tags.into_iter().filter(|e| match Version::parse(e.replace("v", "").as_str()) {
Ok(_) => true,
Err(_) => false
}).collect()
tags
.into_iter()
.filter(|tag| Version::parse(tag.replace("v", "").as_str()).is_ok())
.map(|tag| GitTag {
tag: tag.clone(),
version: Version::parse(tag.replace("v", "").as_str()).ok().unwrap()
})
.collect()
}

// A lot of parameters to this one. 'git tag -l' is much simpler, but the problem is that it produces a list of
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate clap;

mod changelog_generator;
mod git_tag_parser;
mod git_tag;

use clap::{App, Arg};
use changelog_generator::ChangelogGenerator;
Expand Down

0 comments on commit bf12e72

Please sign in to comment.