Skip to main content

git_semantic/git/
mod.rs

1mod diff;
2mod error;
3mod parser;
4
5pub use error::GitError;
6pub use parser::RepositoryParser;
7
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct CommitInfo {
13    pub hash: String,
14    pub author: String,
15    pub date: DateTime<Utc>,
16    pub message: String,
17    pub diff_summary: String,
18}
19
20impl CommitInfo {
21    pub fn to_text(&self, include_diff: bool) -> String {
22        let mut text = format!("{}\n{}", self.message, self.author);
23
24        if include_diff && !self.diff_summary.is_empty() {
25            text.push('\n');
26            text.push_str(&self.diff_summary);
27        }
28
29        text
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    fn sample_commit() -> CommitInfo {
38        CommitInfo {
39            hash: "abc1234def5678".to_string(),
40            author: "Alice Chen".to_string(),
41            date: chrono::DateTime::parse_from_rfc3339("2024-06-15T12:00:00Z")
42                .unwrap()
43                .with_timezone(&Utc),
44            message: "fix: resolve race condition in auth".to_string(),
45            diff_summary: "+mutex.lock()\n-unsafe_access()".to_string(),
46        }
47    }
48
49    #[test]
50    fn test_to_text_without_diff() {
51        let commit = sample_commit();
52        let text = commit.to_text(false);
53        assert_eq!(text, "fix: resolve race condition in auth\nAlice Chen");
54        assert!(!text.contains("mutex"));
55    }
56
57    #[test]
58    fn test_to_text_with_diff() {
59        let commit = sample_commit();
60        let text = commit.to_text(true);
61        assert!(text.contains("fix: resolve race condition in auth"));
62        assert!(text.contains("Alice Chen"));
63        assert!(text.contains("+mutex.lock()"));
64        assert!(text.contains("-unsafe_access()"));
65    }
66
67    #[test]
68    fn test_to_text_with_diff_flag_but_empty_diff() {
69        let mut commit = sample_commit();
70        commit.diff_summary = String::new();
71        let text = commit.to_text(true);
72        // Should not have trailing newline when diff is empty
73        assert_eq!(text, "fix: resolve race condition in auth\nAlice Chen");
74    }
75
76    #[test]
77    fn test_to_text_includes_message_and_author() {
78        let commit = sample_commit();
79        let text = commit.to_text(false);
80        assert!(text.starts_with(&commit.message));
81        assert!(text.ends_with(&commit.author));
82    }
83}