git_editor/utils/
commit_history.rs

1use crate::utils::types::Result;
2use crate::{args::Args, utils::types::CommitInfo};
3use colored::Colorize;
4use git2::{Repository, Sort};
5
6pub fn get_commit_history(args: &Args, print: bool) -> Result<Vec<CommitInfo>> {
7    let repo = Repository::open(args.repo_path.as_ref().unwrap())?;
8
9    let mut revwalk = repo.revwalk()?;
10    revwalk.push_head()?;
11    revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?;
12
13    // Collect all commits first
14    let mut commits = Vec::new();
15    let mut commit_infos = Vec::new();
16
17    for oid_result in revwalk {
18        let oid = oid_result?;
19        let commit = repo.find_commit(oid)?;
20        let timestamp = commit.time();
21        let datetime = chrono::DateTime::from_timestamp(timestamp.seconds(), 0)
22            .unwrap_or_default()
23            .naive_utc();
24
25        let commit_info = CommitInfo {
26            oid,
27            short_hash: oid.to_string()[..8].to_string(),
28            timestamp: datetime,
29            author_name: commit.author().name().unwrap_or("Unknown").to_string(),
30            author_email: commit
31                .author()
32                .email()
33                .unwrap_or("unknown@email.com")
34                .to_string(),
35            message: commit.message().unwrap_or("(no message)").to_string(),
36            parent_count: commit.parent_count(),
37        };
38
39        if print {
40            commits.push((oid, commit));
41        }
42        commit_infos.push(commit_info);
43    }
44
45    // If print is true, calculate and display statistics
46    if print {
47        let total_commits = commit_infos.len();
48
49        if total_commits > 0 {
50            let timestamps: Vec<_> = commit_infos.iter().map(|c| c.timestamp).collect();
51            let mut sorted_timestamps = timestamps.clone();
52            sorted_timestamps.sort();
53
54            let earliest_date = sorted_timestamps[0];
55            let latest_date = sorted_timestamps[sorted_timestamps.len() - 1];
56            let date_span = latest_date.signed_duration_since(earliest_date).num_days();
57
58            let unique_authors: std::collections::HashSet<String> =
59                commit_infos.iter().map(|c| c.author_name.clone()).collect();
60
61            // Print summary
62            println!("\n{}", "Updated Commit History Summary:".bold().green());
63            println!("{}", "-".repeat(60).cyan());
64            println!(
65                "{}: {}",
66                "Total Commits".bold(),
67                total_commits.to_string().yellow()
68            );
69            println!(
70                "{}: {} days",
71                "Date Span".bold(),
72                date_span.to_string().yellow()
73            );
74            println!(
75                "{}: {} to {}",
76                "Date Range".bold(),
77                earliest_date.format("%Y-%m-%d %H:%M:%S").to_string().blue(),
78                latest_date.format("%Y-%m-%d %H:%M:%S").to_string().blue()
79            );
80            println!(
81                "{}: {}",
82                "Unique Authors".bold(),
83                unique_authors.len().to_string().yellow()
84            );
85            if unique_authors.len() <= 5 {
86                println!(
87                    "{}: {}",
88                    "Authors".bold(),
89                    unique_authors
90                        .iter()
91                        .cloned()
92                        .collect::<Vec<_>>()
93                        .join(", ")
94                        .magenta()
95                );
96            }
97            println!("{}", "=".repeat(60).cyan());
98
99            // Print detailed commit history
100            println!("\n{}", "Detailed Commit History:".bold().green());
101            println!("{}", "-".repeat(60).cyan());
102
103            for commit_info in &commit_infos {
104                println!(
105                    "{} {} {} {}",
106                    commit_info.short_hash.yellow().bold(),
107                    commit_info
108                        .timestamp
109                        .format("%Y-%m-%d %H:%M:%S")
110                        .to_string()
111                        .blue(),
112                    commit_info.author_name.magenta(),
113                    commit_info.message.lines().next().unwrap_or("").white()
114                );
115            }
116
117            println!("{}", "=".repeat(60).cyan());
118        }
119    }
120
121    Ok(commit_infos)
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use std::fs;
128    use tempfile::TempDir;
129
130    fn create_test_repo_with_commits() -> (TempDir, String) {
131        let temp_dir = TempDir::new().unwrap();
132        let repo_path = temp_dir.path().to_str().unwrap().to_string();
133
134        // Initialize git repo
135        let repo = git2::Repository::init(&repo_path).unwrap();
136
137        // Create multiple commits
138        for i in 1..=3 {
139            let file_path = temp_dir.path().join(format!("test{i}.txt"));
140            fs::write(&file_path, format!("test content {i}")).unwrap();
141
142            let mut index = repo.index().unwrap();
143            index
144                .add_path(std::path::Path::new(&format!("test{i}.txt")))
145                .unwrap();
146            index.write().unwrap();
147
148            let tree_id = index.write_tree().unwrap();
149            let tree = repo.find_tree(tree_id).unwrap();
150
151            let sig = git2::Signature::new(
152                "Test User",
153                "test@example.com",
154                &git2::Time::new(1234567890 + i as i64 * 3600, 0),
155            )
156            .unwrap();
157
158            let parents = if i == 1 {
159                vec![]
160            } else {
161                let head = repo.head().unwrap();
162                let parent_commit = head.peel_to_commit().unwrap();
163                vec![parent_commit]
164            };
165
166            repo.commit(
167                Some("HEAD"),
168                &sig,
169                &sig,
170                &format!("Commit {i}"),
171                &tree,
172                &parents.iter().collect::<Vec<_>>(),
173            )
174            .unwrap();
175        }
176
177        (temp_dir, repo_path)
178    }
179
180    #[test]
181    fn test_get_commit_history_without_print() {
182        let (_temp_dir, repo_path) = create_test_repo_with_commits();
183        let args = Args {
184            repo_path: Some(repo_path),
185            email: None,
186            name: None,
187            start: None,
188            end: None,
189            show_history: false,
190            pic_specific_commits: false,
191        };
192
193        let result = get_commit_history(&args, false);
194        assert!(result.is_ok());
195
196        let commit_infos = result.unwrap();
197        assert_eq!(commit_infos.len(), 3);
198
199        // Check that commits are in reverse chronological order (newest first)
200        assert_eq!(commit_infos[0].message, "Commit 3");
201        assert_eq!(commit_infos[1].message, "Commit 2");
202        assert_eq!(commit_infos[2].message, "Commit 1");
203    }
204
205    #[test]
206    fn test_get_commit_history_with_print() {
207        let (_temp_dir, repo_path) = create_test_repo_with_commits();
208        let args = Args {
209            repo_path: Some(repo_path),
210            email: None,
211            name: None,
212            start: None,
213            end: None,
214            show_history: true,
215            pic_specific_commits: false,
216        };
217
218        let result = get_commit_history(&args, true);
219        assert!(result.is_ok());
220
221        let commit_infos = result.unwrap();
222        assert_eq!(commit_infos.len(), 3);
223    }
224
225    #[test]
226    fn test_commit_info_fields() {
227        let (_temp_dir, repo_path) = create_test_repo_with_commits();
228        let args = Args {
229            repo_path: Some(repo_path),
230            email: None,
231            name: None,
232            start: None,
233            end: None,
234            show_history: false,
235            pic_specific_commits: false,
236        };
237
238        let result = get_commit_history(&args, false);
239        assert!(result.is_ok());
240
241        let commit_infos = result.unwrap();
242        let first_commit = &commit_infos[0];
243
244        // Check that all fields are populated correctly
245        assert!(!first_commit.short_hash.is_empty());
246        assert_eq!(first_commit.short_hash.len(), 8);
247        assert_eq!(first_commit.author_name, "Test User");
248        assert_eq!(first_commit.author_email, "test@example.com");
249        assert!(!first_commit.message.is_empty());
250        assert_eq!(first_commit.parent_count, 1); // Second and third commits have parents
251    }
252
253    #[test]
254    fn test_get_commit_history_empty_repo() {
255        let temp_dir = TempDir::new().unwrap();
256        let repo_path = temp_dir.path().to_str().unwrap().to_string();
257
258        // Initialize empty git repo
259        git2::Repository::init(&repo_path).unwrap();
260
261        let args = Args {
262            repo_path: Some(repo_path),
263            email: None,
264            name: None,
265            start: None,
266            end: None,
267            show_history: false,
268            pic_specific_commits: false,
269        };
270
271        let result = get_commit_history(&args, false);
272        // Empty repo should return error because there's no HEAD
273        assert!(result.is_err());
274    }
275
276    #[test]
277    fn test_get_commit_history_invalid_repo() {
278        let args = Args {
279            repo_path: Some("/nonexistent/path".to_string()),
280            email: None,
281            name: None,
282            start: None,
283            end: None,
284            show_history: false,
285            pic_specific_commits: false,
286        };
287
288        let result = get_commit_history(&args, false);
289        assert!(result.is_err());
290    }
291
292    #[test]
293    fn test_commit_info_parent_count() {
294        let (_temp_dir, repo_path) = create_test_repo_with_commits();
295        let args = Args {
296            repo_path: Some(repo_path),
297            email: None,
298            name: None,
299            start: None,
300            end: None,
301            show_history: false,
302            pic_specific_commits: false,
303        };
304
305        let result = get_commit_history(&args, false);
306        assert!(result.is_ok());
307
308        let commit_infos = result.unwrap();
309
310        // First commit (chronologically) should have 0 parents
311        assert_eq!(commit_infos[2].parent_count, 0);
312
313        // Second and third commits should have 1 parent each
314        assert_eq!(commit_infos[1].parent_count, 1);
315        assert_eq!(commit_infos[0].parent_count, 1);
316    }
317}