|
| 1 | +use crate::{GitXError, Result}; |
| 2 | +use console::style; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +pub fn run() -> Result<String> { |
| 7 | + let output = Command::new("git") |
| 8 | + .args(["log", "--all", "--format=%ae|%an|%ad", "--date=short"]) |
| 9 | + .output()?; |
| 10 | + |
| 11 | + if !output.status.success() { |
| 12 | + return Err(GitXError::GitCommand( |
| 13 | + "Failed to retrieve commit history".to_string(), |
| 14 | + )); |
| 15 | + } |
| 16 | + |
| 17 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 18 | + if stdout.trim().is_empty() { |
| 19 | + return Ok("📊 No contributors found in this repository".to_string()); |
| 20 | + } |
| 21 | + |
| 22 | + let contributors = parse_contributors(&stdout)?; |
| 23 | + Ok(format_contributors_output(&contributors)) |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Clone)] |
| 27 | +struct ContributorStats { |
| 28 | + name: String, |
| 29 | + email: String, |
| 30 | + commit_count: usize, |
| 31 | + first_commit: String, |
| 32 | + last_commit: String, |
| 33 | +} |
| 34 | + |
| 35 | +fn parse_contributors(output: &str) -> Result<Vec<ContributorStats>> { |
| 36 | + let mut contributors: HashMap<String, ContributorStats> = HashMap::new(); |
| 37 | + |
| 38 | + for line in output.lines() { |
| 39 | + let parts: Vec<&str> = line.splitn(3, '|').collect(); |
| 40 | + if parts.len() != 3 { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + let email = parts[0].trim().to_string(); |
| 45 | + let name = parts[1].trim().to_string(); |
| 46 | + let date = parts[2].trim().to_string(); |
| 47 | + |
| 48 | + contributors |
| 49 | + .entry(email.clone()) |
| 50 | + .and_modify(|stats| { |
| 51 | + stats.commit_count += 1; |
| 52 | + if date < stats.first_commit { |
| 53 | + stats.first_commit = date.clone(); |
| 54 | + } |
| 55 | + if date > stats.last_commit { |
| 56 | + stats.last_commit = date.clone(); |
| 57 | + } |
| 58 | + }) |
| 59 | + .or_insert(ContributorStats { |
| 60 | + name: name.clone(), |
| 61 | + email: email.clone(), |
| 62 | + commit_count: 1, |
| 63 | + first_commit: date.clone(), |
| 64 | + last_commit: date, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + let mut sorted_contributors: Vec<ContributorStats> = contributors.into_values().collect(); |
| 69 | + sorted_contributors.sort_by(|a, b| b.commit_count.cmp(&a.commit_count)); |
| 70 | + |
| 71 | + Ok(sorted_contributors) |
| 72 | +} |
| 73 | + |
| 74 | +fn format_contributors_output(contributors: &[ContributorStats]) -> String { |
| 75 | + let mut output = Vec::new(); |
| 76 | + let total_commits: usize = contributors.iter().map(|c| c.commit_count).sum(); |
| 77 | + |
| 78 | + output.push(format!( |
| 79 | + "{} Repository Contributors ({} total commits):\n", |
| 80 | + style("📊").bold(), |
| 81 | + style(total_commits).bold() |
| 82 | + )); |
| 83 | + |
| 84 | + for (index, contributor) in contributors.iter().enumerate() { |
| 85 | + let rank_icon = match index { |
| 86 | + 0 => "🥇", |
| 87 | + 1 => "🥈", |
| 88 | + 2 => "🥉", |
| 89 | + _ => "👤", |
| 90 | + }; |
| 91 | + |
| 92 | + let percentage = (contributor.commit_count as f64 / total_commits as f64) * 100.0; |
| 93 | + |
| 94 | + output.push(format!( |
| 95 | + "{} {} {} commits ({:.1}%)", |
| 96 | + rank_icon, |
| 97 | + style(&contributor.name).bold(), |
| 98 | + style(contributor.commit_count).cyan(), |
| 99 | + percentage |
| 100 | + )); |
| 101 | + |
| 102 | + output.push(format!( |
| 103 | + " 📧 {} | 📅 {} to {}", |
| 104 | + style(&contributor.email).dim(), |
| 105 | + style(&contributor.first_commit).dim(), |
| 106 | + style(&contributor.last_commit).dim() |
| 107 | + )); |
| 108 | + |
| 109 | + if index < contributors.len() - 1 { |
| 110 | + output.push(String::new()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + output.join("\n") |
| 115 | +} |
| 116 | + |
| 117 | +#[cfg(test)] |
| 118 | +mod tests { |
| 119 | + use super::*; |
| 120 | + use crate::GitXError; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_parse_contributors_success() { |
| 124 | + let sample_output = r#"alice@example.com|Alice Smith|2025-01-15 |
| 125 | +bob@example.com|Bob Jones|2025-01-10 |
| 126 | +alice@example.com|Alice Smith|2025-01-20 |
| 127 | +charlie@example.com|Charlie Brown|2025-01-12"#; |
| 128 | + |
| 129 | + let result = parse_contributors(sample_output); |
| 130 | + assert!(result.is_ok()); |
| 131 | + |
| 132 | + let contributors = result.unwrap(); |
| 133 | + assert_eq!(contributors.len(), 3); |
| 134 | + |
| 135 | + // Alice should be first with 2 commits |
| 136 | + assert_eq!(contributors[0].name, "Alice Smith"); |
| 137 | + assert_eq!(contributors[0].commit_count, 2); |
| 138 | + assert_eq!(contributors[0].first_commit, "2025-01-15"); |
| 139 | + assert_eq!(contributors[0].last_commit, "2025-01-20"); |
| 140 | + |
| 141 | + // Bob and Charlie should have 1 commit each |
| 142 | + assert!( |
| 143 | + contributors |
| 144 | + .iter() |
| 145 | + .any(|c| c.name == "Bob Jones" && c.commit_count == 1) |
| 146 | + ); |
| 147 | + assert!( |
| 148 | + contributors |
| 149 | + .iter() |
| 150 | + .any(|c| c.name == "Charlie Brown" && c.commit_count == 1) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_parse_contributors_empty_input() { |
| 156 | + let result = parse_contributors(""); |
| 157 | + assert!(result.is_ok()); |
| 158 | + let contributors = result.unwrap(); |
| 159 | + assert!(contributors.is_empty()); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_parse_contributors_malformed_input() { |
| 164 | + let malformed_output = "invalid|line\nincomplete"; |
| 165 | + let result = parse_contributors(malformed_output); |
| 166 | + assert!(result.is_ok()); |
| 167 | + let contributors = result.unwrap(); |
| 168 | + assert!(contributors.is_empty()); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_format_contributors_output() { |
| 173 | + let contributors = vec![ |
| 174 | + ContributorStats { |
| 175 | + name: "Alice Smith".to_string(), |
| 176 | + email: "alice@example.com".to_string(), |
| 177 | + commit_count: 10, |
| 178 | + first_commit: "2025-01-01".to_string(), |
| 179 | + last_commit: "2025-01-15".to_string(), |
| 180 | + }, |
| 181 | + ContributorStats { |
| 182 | + name: "Bob Jones".to_string(), |
| 183 | + email: "bob@example.com".to_string(), |
| 184 | + commit_count: 5, |
| 185 | + first_commit: "2025-01-05".to_string(), |
| 186 | + last_commit: "2025-01-10".to_string(), |
| 187 | + }, |
| 188 | + ]; |
| 189 | + |
| 190 | + let output = format_contributors_output(&contributors); |
| 191 | + |
| 192 | + // Check that output contains expected elements (accounting for styling) |
| 193 | + assert!(output.contains("Repository Contributors")); |
| 194 | + assert!(output.contains("15") && output.contains("total commits")); // Account for styling |
| 195 | + assert!(output.contains("🥇")); |
| 196 | + assert!(output.contains("🥈")); |
| 197 | + assert!(output.contains("Alice Smith")); |
| 198 | + assert!(output.contains("Bob Jones")); |
| 199 | + assert!(output.contains("66.7%")); |
| 200 | + assert!(output.contains("33.3%")); |
| 201 | + assert!(output.contains("10") && output.contains("commits")); // Alice's commit count |
| 202 | + assert!(output.contains("5") && output.contains("commits")); // Bob's commit count |
| 203 | + assert!(output.contains("alice@example.com")); |
| 204 | + assert!(output.contains("bob@example.com")); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_run_no_git_repo() { |
| 209 | + // This test runs in the context where git might fail |
| 210 | + // We test by calling parse_contributors with valid input |
| 211 | + let result = parse_contributors("test@example.com|Test User|2025-01-01"); |
| 212 | + match result { |
| 213 | + Ok(contributors) => { |
| 214 | + assert_eq!(contributors.len(), 1); |
| 215 | + assert_eq!(contributors[0].name, "Test User"); |
| 216 | + } |
| 217 | + Err(_) => panic!("Should parse valid input successfully"), |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + #[test] |
| 222 | + fn test_contributor_stats_fields() { |
| 223 | + let stats = ContributorStats { |
| 224 | + name: "Test User".to_string(), |
| 225 | + email: "test@example.com".to_string(), |
| 226 | + commit_count: 5, |
| 227 | + first_commit: "2025-01-01".to_string(), |
| 228 | + last_commit: "2025-01-15".to_string(), |
| 229 | + }; |
| 230 | + |
| 231 | + assert_eq!(stats.name, "Test User"); |
| 232 | + assert_eq!(stats.email, "test@example.com"); |
| 233 | + assert_eq!(stats.commit_count, 5); |
| 234 | + assert_eq!(stats.first_commit, "2025-01-01"); |
| 235 | + assert_eq!(stats.last_commit, "2025-01-15"); |
| 236 | + } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn test_gitx_error_integration() { |
| 240 | + // Test that our functions work with GitXError types correctly |
| 241 | + let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "git not found"); |
| 242 | + let gitx_error: GitXError = io_error.into(); |
| 243 | + match gitx_error { |
| 244 | + GitXError::Io(_) => {} // Expected |
| 245 | + _ => panic!("Should convert to Io error"), |
| 246 | + } |
| 247 | + |
| 248 | + let git_error = GitXError::GitCommand("test error".to_string()); |
| 249 | + assert_eq!(git_error.to_string(), "Git command failed: test error"); |
| 250 | + } |
| 251 | + |
| 252 | + #[test] |
| 253 | + fn test_sorting_by_commit_count() { |
| 254 | + let sample_output = r#"alice@example.com|Alice Smith|2025-01-15 |
| 255 | +bob@example.com|Bob Jones|2025-01-10 |
| 256 | +alice@example.com|Alice Smith|2025-01-20 |
| 257 | +alice@example.com|Alice Smith|2025-01-25 |
| 258 | +bob@example.com|Bob Jones|2025-01-12 |
| 259 | +charlie@example.com|Charlie Brown|2025-01-12"#; |
| 260 | + |
| 261 | + let result = parse_contributors(sample_output); |
| 262 | + assert!(result.is_ok()); |
| 263 | + |
| 264 | + let contributors = result.unwrap(); |
| 265 | + assert_eq!(contributors.len(), 3); |
| 266 | + |
| 267 | + // Should be sorted by commit count (descending) |
| 268 | + assert_eq!(contributors[0].name, "Alice Smith"); |
| 269 | + assert_eq!(contributors[0].commit_count, 3); |
| 270 | + assert_eq!(contributors[1].name, "Bob Jones"); |
| 271 | + assert_eq!(contributors[1].commit_count, 2); |
| 272 | + assert_eq!(contributors[2].name, "Charlie Brown"); |
| 273 | + assert_eq!(contributors[2].commit_count, 1); |
| 274 | + } |
| 275 | + |
| 276 | + #[test] |
| 277 | + fn test_date_range_tracking() { |
| 278 | + let sample_output = r#"alice@example.com|Alice Smith|2025-01-20 |
| 279 | +alice@example.com|Alice Smith|2025-01-10 |
| 280 | +alice@example.com|Alice Smith|2025-01-15"#; |
| 281 | + |
| 282 | + let result = parse_contributors(sample_output); |
| 283 | + assert!(result.is_ok()); |
| 284 | + |
| 285 | + let contributors = result.unwrap(); |
| 286 | + assert_eq!(contributors.len(), 1); |
| 287 | + |
| 288 | + let alice = &contributors[0]; |
| 289 | + assert_eq!(alice.first_commit, "2025-01-10"); // Earliest date |
| 290 | + assert_eq!(alice.last_commit, "2025-01-20"); // Latest date |
| 291 | + assert_eq!(alice.commit_count, 3); |
| 292 | + } |
| 293 | +} |
0 commit comments