Skip to content

Commit fcf8695

Browse files
committed
feat: Add word occurrences anlysis
1 parent d1138bc commit fcf8695

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

Cargo.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ categories = ["command-line-utilities"]
1616
[dependencies]
1717
clap = { version = "4.4.18", features = ["derive"] }
1818
git2 = { version = "0.18.1", features = [] }
19+
regex = "1.10.3"
20+
itertools = "0.12.1"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# git-commit-stats
22

3+
![Crates.io Version](https://img.shields.io/crates/v/git-commit-stats?style=flat)
4+
![Crates.io Total Downloads](https://img.shields.io/crates/d/git-commit-stats)
5+
36
git-commit-stats is a command-line tool designed to provide insightful analysis of Git repositories. It analyzes commit histories, providing users with valuable information about coding habits and patterns. The tool aims to enhance your understanding of code changes over time.
47

58
## Features
@@ -40,3 +43,6 @@ This project is licensed under the MIT License. Feel free to use, modify, and di
4043
### Acknowledgments
4144

4245
- git-commit-stats utilizes Git2 for interacting with Git repositories.
46+
- git-commit-stats utilizes Clap for command-line argument parsing.
47+
- git-commit-stats utilizes Regex for parsing commit messages.
48+
- git-commit-stats utilizes itertools for efficient iteration.

src/analyzer.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use git2::{Commit, DiffStats, Repository};
2+
use itertools::Itertools;
3+
use regex::Regex;
4+
use std::collections::HashMap;
25
use std::error::Error;
36

47
/// Open a Git repository and return it.
@@ -81,8 +84,37 @@ pub(crate) fn show_commit_stats(stats: &[Result<DiffStats, Box<dyn Error>>]) {
8184
}
8285

8386
/// 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+
}
86118
}
87119

88120
/// Get the user name from the Git configuration.

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn main() {
7979

8080
analyzer::show_commit_stats(&stats);
8181
println!();
82-
analyzer::show_coding_habits();
82+
analyzer::show_coding_habits(&repo);
8383
}
8484

8585
/// Check if the specified path is a directory and a Git repository.

0 commit comments

Comments
 (0)