Skip to main content

git_proc/
tag.rs

1//! Git tag name type with validation, plus `git tag` command builders.
2
3use crate::ref_format::{self, RefFormatError};
4
5crate::cow_str_newtype! {
6    /// A validated git tag name.
7    ///
8    /// Tag names follow git's reference naming rules; see
9    /// [`crate::ref_format`] for the full ruleset.
10    pub struct Tag, TagError(RefFormatError), "invalid tag name"
11}
12
13impl Tag {
14    const fn validate(input: &str) -> Result<(), TagError> {
15        match ref_format::validate(input) {
16            Ok(()) => Ok(()),
17            Err(error) => Err(TagError(error)),
18        }
19    }
20}
21
22/// Create a new `git tag <name>` command builder.
23#[must_use]
24pub fn create(name: &Tag) -> Create<'_> {
25    Create::new(name)
26}
27
28/// Builder for `git tag <name>` (lightweight tag at HEAD).
29///
30/// See `git tag --help` for full documentation.
31#[derive(Debug)]
32pub struct Create<'a> {
33    base: crate::RepoBase<'a>,
34    name: &'a Tag,
35}
36
37crate::impl_repo_base!(Create);
38
39impl<'a> Create<'a> {
40    #[must_use]
41    fn new(name: &'a Tag) -> Self {
42        Self {
43            base: crate::RepoBase::default(),
44            name,
45        }
46    }
47
48    /// Execute the command and return the exit status.
49    pub async fn status(self) -> Result<(), crate::Error> {
50        Ok(crate::Build::build(self)?.status().await?)
51    }
52}
53
54impl crate::Build for Create<'_> {
55    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
56        Ok(self.base.command()?.argument("tag").argument(self.name))
57    }
58}
59
60#[cfg(feature = "test-utils")]
61impl Create<'_> {
62    /// Compare the built command with another command using debug representation.
63    pub fn test_eq(&self, other: &cmd_proc::Command) {
64        let command = crate::Build::build(Self {
65            base: self.base,
66            name: self.name,
67        })
68        .unwrap();
69        command.test_eq(other);
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_valid_tag() {
79        assert!("v1.0.0".parse::<Tag>().is_ok());
80        assert!("release-2024".parse::<Tag>().is_ok());
81        assert!("hotfix/v1.2.3".parse::<Tag>().is_ok());
82    }
83
84    #[test]
85    fn test_invalid_passes_through() {
86        assert!(matches!(
87            "".parse::<Tag>(),
88            Err(TagError(RefFormatError::Empty))
89        ));
90        assert!(matches!(
91            "-tag".parse::<Tag>(),
92            Err(TagError(RefFormatError::StartsWithDash))
93        ));
94        assert!(matches!(
95            "v1.0.0.lock".parse::<Tag>(),
96            Err(TagError(RefFormatError::EndsWithLock))
97        ));
98    }
99
100    #[test]
101    fn test_from_static_or_panic() {
102        let tag = Tag::from_static_or_panic("v1.0.0");
103        assert_eq!(tag.as_str(), "v1.0.0");
104    }
105
106    #[test]
107    fn test_display() {
108        let tag: Tag = "v1.0.0".parse().unwrap();
109        assert_eq!(format!("{tag}"), "v1.0.0");
110    }
111
112    #[test]
113    fn test_as_ref_os_str() {
114        use std::ffi::OsStr;
115        let tag: Tag = "v1.0.0".parse().unwrap();
116        let os_str: &OsStr = tag.as_ref();
117        assert_eq!(os_str, "v1.0.0");
118    }
119
120    #[test]
121    fn test_serialize() {
122        let tag: Tag = "v1.0.0".parse().unwrap();
123        assert_eq!(serde_json::to_string(&tag).unwrap(), "\"v1.0.0\"");
124    }
125
126    #[test]
127    fn test_deserialize() {
128        let tag: Tag = serde_json::from_str("\"v1.0.0\"").unwrap();
129        assert_eq!(tag.as_str(), "v1.0.0");
130    }
131
132    #[test]
133    fn test_deserialize_invalid() {
134        assert!(serde_json::from_str::<Tag>("\"-bad\"").is_err());
135    }
136}