-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tag): add configurable tag prefix as described in #122
- Loading branch information
Showing
7 changed files
with
327 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod hook; | ||
pub mod repository; | ||
pub mod status; | ||
pub mod tag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::SETTINGS; | ||
use anyhow::anyhow; | ||
use anyhow::Result; | ||
use git2::Oid; | ||
use semver::Version; | ||
use std::cmp::Ordering; | ||
use std::fmt; | ||
use std::fmt::Formatter; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub(crate) struct Tag { | ||
tag: String, | ||
oid: Oid, | ||
} | ||
|
||
impl Tag { | ||
pub(crate) fn oid(&self) -> &Oid { | ||
&self.oid | ||
} | ||
|
||
pub(crate) fn new(name: &str, oid: Oid) -> Result<Tag> { | ||
let tag = match SETTINGS.tag_prefix.as_ref() { | ||
None => Ok(name), | ||
Some(prefix) => name | ||
.strip_prefix(prefix) | ||
.ok_or_else(|| anyhow!("Expected a tag with prefix {}, got {}", prefix, name)), | ||
}? | ||
.to_string(); | ||
|
||
Ok(Tag { tag, oid }) | ||
} | ||
|
||
pub(crate) fn to_version(&self) -> Result<Version> { | ||
Version::parse(&self.tag).map_err(|err| anyhow!("{}", err)) | ||
} | ||
|
||
pub(crate) fn to_string_with_prefix(&self) -> String { | ||
match SETTINGS.tag_prefix.as_ref() { | ||
None => self.tag.to_string(), | ||
Some(prefix) => format!("{}{}", prefix, self.tag), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Tag { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.to_string_with_prefix()) | ||
} | ||
} | ||
|
||
impl Ord for Tag { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.partial_cmp(other).unwrap() | ||
} | ||
} | ||
|
||
impl PartialOrd<Tag> for Tag { | ||
fn partial_cmp(&self, other: &Tag) -> Option<Ordering> { | ||
Some(self.to_version().ok().cmp(&other.to_version().ok())) | ||
} | ||
} |
Oops, something went wrong.