Skip to content

Commit bf12e72

Browse files
authored
Properly handle tags with v prefix, and minor versions >9 (#18)
Fixes #17, fixes #9.
1 parent 7fc990a commit bf12e72

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/changelog_generator.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ use std::process::Command;
22

33
pub struct ChangelogGenerator {
44
pub repository_path: String,
5+
6+
// A git revision. Example value: v8.1.0
57
pub from_revision: String,
8+
9+
// A git revision. Example value: v8.1.1
610
pub to_revision: String,
11+
712
pub to_alias: String
813
}
914

src/git_tag.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::cmp::Ordering;
2+
use semver::Version;
3+
4+
#[derive(Clone)]
5+
#[derive(Eq)]
6+
pub struct GitTag {
7+
pub tag: String,
8+
pub version: Version
9+
}
10+
11+
impl Ord for GitTag {
12+
fn cmp(&self, other: &GitTag) -> Ordering {
13+
self.version.cmp(&other.version)
14+
}
15+
}
16+
17+
impl PartialOrd for GitTag {
18+
fn partial_cmp(&self, other: &GitTag) -> Option<Ordering> {
19+
Some(self.version.cmp(&other.version))
20+
}
21+
}
22+
23+
impl PartialEq for GitTag {
24+
fn eq(&self, other: &GitTag) -> bool {
25+
self.version == other.version
26+
}
27+
}

src/git_tag_parser.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use semver::Version;
22
use std::process::Command;
33

4+
use git_tag::GitTag;
5+
46
pub struct GitTagParser {
57
pub repository_path: String
68
}
@@ -10,9 +12,13 @@ impl GitTagParser {
1012
// tag, the "to" revision is the current semver tag.
1113
pub fn get_version_tag_pairs(&self) -> Vec<(String, String)> {
1214
let mut from_version = self.get_root_ancestor();
13-
let mut tag_pairs: Vec<(String, String)> = self.semver_tags().into_iter().rev().map(|tag| {
15+
16+
let mut tags = self.semver_tags();
17+
tags.sort();
18+
19+
let mut tag_pairs: Vec<(String, String)> = tags.into_iter().map(|tag| {
1420
let old_from_version = from_version.clone();
15-
let to_version = tag;
21+
let to_version = tag.tag;
1622
from_version = to_version.clone();
1723

1824
(old_from_version, to_version)
@@ -24,12 +30,16 @@ impl GitTagParser {
2430
tag_pairs
2531
}
2632

27-
fn semver_tags(&self) -> Vec<String> {
33+
fn semver_tags(&self) -> Vec<GitTag> {
2834
let tags = self.get_tags();
29-
tags.into_iter().filter(|e| match Version::parse(e.replace("v", "").as_str()) {
30-
Ok(_) => true,
31-
Err(_) => false
32-
}).collect()
35+
tags
36+
.into_iter()
37+
.filter(|tag| Version::parse(tag.replace("v", "").as_str()).is_ok())
38+
.map(|tag| GitTag {
39+
tag: tag.clone(),
40+
version: Version::parse(tag.replace("v", "").as_str()).ok().unwrap()
41+
})
42+
.collect()
3343
}
3444

3545
// A lot of parameters to this one. 'git tag -l' is much simpler, but the problem is that it produces a list of

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate clap;
66

77
mod changelog_generator;
88
mod git_tag_parser;
9+
mod git_tag;
910

1011
use clap::{App, Arg};
1112
use changelog_generator::ChangelogGenerator;

0 commit comments

Comments
 (0)