Skip to main content

governor_git/
commit.rs

1//! Git commit wrapper
2
3use governor_core::domain::commit::Commit;
4
5/// Wrapper for Git commits
6#[derive(Debug, Clone)]
7pub struct GitCommit {
8    inner: Commit,
9}
10
11impl GitCommit {
12    /// Create a new Git commit wrapper
13    #[must_use]
14    pub const fn new(commit: Commit) -> Self {
15        Self { inner: commit }
16    }
17
18    /// Get the inner commit
19    #[must_use]
20    pub const fn inner(&self) -> &Commit {
21        &self.inner
22    }
23
24    /// Get the commit hash
25    #[must_use]
26    pub fn hash(&self) -> &str {
27        &self.inner.hash
28    }
29
30    /// Get the short hash
31    #[must_use]
32    pub fn short_hash(&self) -> &str {
33        &self.inner.short_hash
34    }
35
36    /// Get the commit message
37    #[must_use]
38    pub fn message(&self) -> &str {
39        &self.inner.message
40    }
41
42    /// Get the author
43    #[must_use]
44    pub fn author(&self) -> &str {
45        &self.inner.author
46    }
47
48    /// Check if this is a breaking change
49    #[must_use]
50    pub const fn is_breaking(&self) -> bool {
51        self.inner.breaking
52    }
53
54    /// Get the commit type
55    #[must_use]
56    pub const fn commit_type(&self) -> Option<governor_core::domain::commit::CommitType> {
57        self.inner.commit_type
58    }
59
60    /// Get the scope
61    #[must_use]
62    pub fn scope(&self) -> Option<&str> {
63        self.inner.scope.as_deref()
64    }
65}
66
67impl From<Commit> for GitCommit {
68    fn from(commit: Commit) -> Self {
69        Self::new(commit)
70    }
71}
72
73impl From<GitCommit> for Commit {
74    fn from(git_commit: GitCommit) -> Self {
75        git_commit.inner
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use chrono::Utc;
83    use governor_core::domain::commit::CommitType;
84
85    #[test]
86    fn test_new() {
87        let commit = Commit::new(
88            "abc123".to_string(),
89            "feat: test".to_string(),
90            "Author".to_string(),
91            "author@test.com".to_string(),
92            Utc::now(),
93        );
94        let git_commit = GitCommit::new(commit);
95        assert_eq!(git_commit.hash(), "abc123");
96    }
97
98    #[test]
99    fn test_inner() {
100        let commit = Commit::new(
101            "xyz789".to_string(),
102            "fix: test".to_string(),
103            "Author".to_string(),
104            "author@test.com".to_string(),
105            Utc::now(),
106        );
107        let git_commit = GitCommit::new(commit);
108        assert_eq!(git_commit.inner().hash, "xyz789");
109    }
110
111    #[test]
112    fn test_hash() {
113        let commit = Commit::new(
114            "hash123".to_string(),
115            "test".to_string(),
116            "Author".to_string(),
117            "author@test.com".to_string(),
118            Utc::now(),
119        );
120        let git_commit = GitCommit::new(commit);
121        assert_eq!(git_commit.hash(), "hash123");
122    }
123
124    #[test]
125    fn test_short_hash() {
126        let commit = Commit::new(
127            "12345678".to_string(),
128            "test".to_string(),
129            "Author".to_string(),
130            "author@test.com".to_string(),
131            Utc::now(),
132        );
133        let git_commit = GitCommit::new(commit);
134        assert_eq!(git_commit.short_hash(), "1234567");
135    }
136
137    #[test]
138    fn test_message() {
139        let commit = Commit::new(
140            "hash".to_string(),
141            "feat: new feature".to_string(),
142            "Author".to_string(),
143            "author@test.com".to_string(),
144            Utc::now(),
145        );
146        let git_commit = GitCommit::new(commit);
147        assert_eq!(git_commit.message(), "feat: new feature");
148    }
149
150    #[test]
151    fn test_author() {
152        let commit = Commit::new(
153            "hash".to_string(),
154            "test".to_string(),
155            "Jane Doe".to_string(),
156            "jane@test.com".to_string(),
157            Utc::now(),
158        );
159        let git_commit = GitCommit::new(commit);
160        assert_eq!(git_commit.author(), "Jane Doe");
161    }
162
163    #[test]
164    fn test_is_breaking_true() {
165        let commit = Commit::new(
166            "hash".to_string(),
167            "feat!: breaking".to_string(),
168            "Author".to_string(),
169            "author@test.com".to_string(),
170            Utc::now(),
171        );
172        let git_commit = GitCommit::new(commit);
173        assert!(git_commit.is_breaking());
174    }
175
176    #[test]
177    fn test_is_breaking_false() {
178        let commit = Commit::new(
179            "hash".to_string(),
180            "feat: normal".to_string(),
181            "Author".to_string(),
182            "author@test.com".to_string(),
183            Utc::now(),
184        );
185        let git_commit = GitCommit::new(commit);
186        assert!(!git_commit.is_breaking());
187    }
188
189    #[test]
190    fn test_commit_type() {
191        let commit = Commit::new(
192            "hash".to_string(),
193            "feat: feature".to_string(),
194            "Author".to_string(),
195            "author@test.com".to_string(),
196            Utc::now(),
197        );
198        let git_commit = GitCommit::new(commit);
199        assert_eq!(git_commit.commit_type(), Some(CommitType::Feat));
200    }
201
202    #[test]
203    fn test_scope() {
204        let commit = Commit::new(
205            "hash".to_string(),
206            "feat(api): endpoint".to_string(),
207            "Author".to_string(),
208            "author@test.com".to_string(),
209            Utc::now(),
210        );
211        let git_commit = GitCommit::new(commit);
212        assert_eq!(git_commit.scope(), Some("api"));
213    }
214
215    #[test]
216    fn test_from_commit() {
217        let commit = Commit::new(
218            "hash".to_string(),
219            "fix: bug".to_string(),
220            "Author".to_string(),
221            "author@test.com".to_string(),
222            Utc::now(),
223        );
224        let git_commit = GitCommit::from(commit.clone());
225        assert_eq!(git_commit.hash(), commit.hash);
226    }
227
228    #[test]
229    fn test_into_commit() {
230        let commit = Commit::new(
231            "hash".to_string(),
232            "fix: bug".to_string(),
233            "Author".to_string(),
234            "author@test.com".to_string(),
235            Utc::now(),
236        );
237        let git_commit = GitCommit::new(commit.clone());
238        let converted: Commit = git_commit.into();
239        assert_eq!(converted.hash, commit.hash);
240    }
241}