|
1 | 1 | use git2::{Commit, DiffStats, Repository}; |
| 2 | +use itertools::Itertools; |
| 3 | +use regex::Regex; |
| 4 | +use std::collections::HashMap; |
2 | 5 | use std::error::Error; |
3 | 6 |
|
4 | 7 | /// Open a Git repository and return it. |
@@ -81,8 +84,37 @@ pub(crate) fn show_commit_stats(stats: &[Result<DiffStats, Box<dyn Error>>]) { |
81 | 84 | } |
82 | 85 |
|
83 | 86 | /// Display a message about coding habits. |
84 | | -pub(crate) fn show_coding_habits() { |
85 | | - println!("Coding habits: "); |
| 87 | +pub(crate) fn show_coding_habits(repo: &Repository) { |
| 88 | + let mut commit_messages = Vec::new(); |
| 89 | + let mut revwalk = repo.revwalk().unwrap(); |
| 90 | + revwalk.push_head().unwrap(); |
| 91 | + |
| 92 | + for oid in revwalk { |
| 93 | + let oid = oid.unwrap(); |
| 94 | + let commit = repo.find_commit(oid).unwrap(); |
| 95 | + commit_messages.push(commit.message().unwrap_or("").to_string()); |
| 96 | + } |
| 97 | + |
| 98 | + if commit_messages.is_empty() { |
| 99 | + println!("No commit data available for analysis."); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // Simple analysis: Counting word occurrences in commit messages |
| 104 | + let mut word_counts: HashMap<String, usize> = HashMap::new(); |
| 105 | + let word_regex = Regex::new(r"\b\w+\b").unwrap(); |
| 106 | + |
| 107 | + for message in &commit_messages { |
| 108 | + for word in word_regex.find_iter(message.to_lowercase().as_str()) { |
| 109 | + let word_entry = word_counts.entry(word.as_str().to_owned()).or_insert(0); |
| 110 | + *word_entry += 1; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + println!("Commit message word occurrences:"); |
| 115 | + for (word, count) in word_counts.iter().sorted_by(|a, b| b.1.cmp(a.1)).take(10) { |
| 116 | + println!("{}: {}", word, count); |
| 117 | + } |
86 | 118 | } |
87 | 119 |
|
88 | 120 | /// Get the user name from the Git configuration. |
|
0 commit comments