Skip to content

Commit

Permalink
fix: Find last tag sorted by semantic version in tag names
Browse files Browse the repository at this point in the history
Fix v0.9.0 being selected when v0.9.0, v0.10.0
  • Loading branch information
Sinhyeok committed Mar 18, 2024
1 parent f5c9a75 commit 90c6d02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/commands/version_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn run(args: VersionCommandArgs) {
}

fn version(scope: String, last_tag: String) -> String {
let mut semantic_version = SemanticVersion::from_string(last_tag[1..].to_string())
let mut semantic_version = SemanticVersion::from_string(last_tag.clone())
.unwrap_or_else(|e| panic!("{}: {}", e, last_tag));

semantic_version.increase_by_scope(scope).to_string(true)
Expand Down
27 changes: 21 additions & 6 deletions src/git_service.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::semantic_version::SemanticVersion;
use git2::string_array::StringArray;
use git2::{
Config, Cred, CredentialType, Error, FetchOptions, ObjectType, Oid, PushOptions,
RemoteCallbacks, Repository,
};
use regex::Regex;
use std::env;
use std::ops::Not;
use std::path::Path;

pub(crate) fn tag_names(
Expand All @@ -31,12 +33,25 @@ pub(crate) fn last_tag_by_pattern(
default: &str,
) -> String {
let tag_regex = Regex::new(tag_pattern).unwrap();
tag_names
.iter()
.flatten()
.filter(|t| tag_regex.is_match(t))
.last()
.map_or(default.to_string(), |tag_name| tag_name.to_string())
let mut valid_versions: Vec<SemanticVersion> = vec![];

for tag_name in tag_names.iter().flatten() {
if tag_regex.is_match(tag_name).not() {
continue;
}

match SemanticVersion::from_string(tag_name.to_string()) {
Ok(version) => valid_versions.push(version),
Err(msg) => panic!("{}", msg),
}
}

valid_versions.sort_by(|a, b| b.cmp(a));

match valid_versions.first() {
Some(version) => version.to_string(true),
None => default.to_string(),
}
}

pub(crate) fn branch_name(repo_path: &str) -> Result<String, Error> {
Expand Down
25 changes: 24 additions & 1 deletion src/semantic_version.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use std::cmp::Ordering;

#[derive(Eq, PartialEq, Debug)]
pub struct SemanticVersion {
pub major: u64,
pub minor: u64,
pub patch: u64,
}

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

impl Ord for SemanticVersion {
fn cmp(&self, other: &Self) -> Ordering {
self.major
.cmp(&other.major)
.then_with(|| self.minor.cmp(&other.minor))
.then_with(|| self.patch.cmp(&other.patch))
}
}

impl SemanticVersion {
pub fn increase_major(&mut self) {
self.major += 1;
Expand Down Expand Up @@ -34,7 +52,12 @@ impl SemanticVersion {
}

pub fn from_string(version_string: String) -> Result<Self, String> {
let parts: Vec<&str> = version_string.split('.').collect();
let prefix_stripped = match version_string.strip_prefix('v') {
Some(stripped) => stripped.to_string(),
None => version_string,
};

let parts: Vec<&str> = prefix_stripped.split('.').collect();

if parts.len() != 3 {
return Err("Invalid version string format".to_string());
Expand Down

0 comments on commit 90c6d02

Please sign in to comment.