Skip to content

Commit fe7ba57

Browse files
authored
fix: program panics when the user isn't a contributor of the repo (#53)
2 parents 154fcb1 + cf96ed9 commit fe7ba57

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/analyzer.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ fn get_commit_stats_for_commit<'repo>(
6767
}
6868

6969
/// Display commit statistics.
70-
pub(crate) fn show_commit_stats(stats: &[Result<DiffStats, Box<dyn Error>>]) {
70+
pub(crate) fn show_commit_stats(stats: &[Result<DiffStats, Box<dyn Error>>], user_name: &String) {
71+
if stats.is_empty() {
72+
println!(
73+
"Warning: The user \"{}\" did not contribute to this repository.",
74+
user_name
75+
);
76+
return;
77+
}
78+
7179
let (total_files_changed, total_insertions, total_deletions) =
7280
stats.iter().fold((0, 0, 0), |acc, commit_stats| {
7381
if let Ok(stats) = commit_stats {
@@ -81,7 +89,7 @@ pub(crate) fn show_commit_stats(stats: &[Result<DiffStats, Box<dyn Error>>]) {
8189
}
8290
});
8391

84-
println!("Commit statistics:");
92+
println!("Commit statistics for user \"{}\":", user_name);
8593
println!("Files changed: {}", total_files_changed);
8694
println!("Insertions: {}", total_insertions);
8795
println!("Deletions: {}", total_deletions);
@@ -183,7 +191,7 @@ pub(crate) fn get_user_name() -> String {
183191

184192
#[cfg(test)]
185193
mod tests {
186-
use crate::analyzer::{get_commit_stats, get_commits, get_repo, get_user_name};
194+
use crate::analyzer::{get_commits, get_repo, get_user_name};
187195

188196
#[test]
189197
fn show_commit_stats() {
@@ -198,9 +206,5 @@ mod tests {
198206
let user = get_user_name();
199207
println!("User: {}", user);
200208
assert!(!user.is_empty(), "Failed to get user name");
201-
202-
let stats = get_commit_stats(&repo, &commits.unwrap(), &user);
203-
println!("Commit Stats: {:?}", stats);
204-
assert!(!stats.is_empty(), "Failed to get commit stats");
205209
}
206210
}

src/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ fn main() {
7979
let commits_vec = commits.unwrap();
8080
let stats = analyzer::get_commit_stats(&repo, &commits_vec, &user_name);
8181

82-
if stats.is_empty() {
83-
eprintln!("Error: Failed to get commit stats.");
84-
process::exit(1);
85-
}
86-
87-
analyzer::show_commit_stats(&stats);
82+
analyzer::show_commit_stats(&stats, &user_name);
8883
println!();
8984
analyzer::show_coding_habits(&commits_vec);
9085
println!();

0 commit comments

Comments
 (0)