Skip to content

Commit c7c915e

Browse files
feat: init
0 parents  commit c7c915e

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 7 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "git_commit_stats"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

src/main.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::env;
2+
use std::process::Command;
3+
use std::str;
4+
5+
fn main() {
6+
// 获取命令行参数中的关键字
7+
let args: Vec<String> = env::args().collect();
8+
if args.len() != 2 {
9+
eprintln!("Usage: git_commit_stats <keyword>");
10+
return;
11+
}
12+
let keyword = &args[1];
13+
14+
// 获取包含关键字的 commit 列表
15+
let output = Command::new("git")
16+
.arg("log")
17+
.arg(format!("--grep={}", keyword))
18+
.arg("--pretty=format:%H")
19+
.output()
20+
.expect("Failed to execute git log");
21+
22+
let commits = String::from_utf8_lossy(&output.stdout);
23+
let commits: Vec<&str> = commits.lines().collect();
24+
25+
println!("commits length {}", commits.len());
26+
27+
let mut total_added = 0;
28+
let mut total_deleted = 0;
29+
30+
for commit in commits {
31+
let output = Command::new("git")
32+
.arg("diff")
33+
.arg("--shortstat")
34+
.arg(commit)
35+
.output()
36+
.expect("Failed to execute git command");
37+
38+
let diff_stats = String::from_utf8_lossy(&output.stdout);
39+
let lines: Vec<&str> = diff_stats.lines().collect();
40+
41+
if !lines.is_empty() {
42+
let last_line = lines.last().unwrap();
43+
let parts: Vec<&str> = last_line.split_whitespace().collect();
44+
45+
if parts.len() >= 6 {
46+
// let added = usize::from_str(parts[3]).unwrap();
47+
let added: usize = parts[3].parse().unwrap();
48+
let deleted: usize = parts[5].parse().unwrap();
49+
// let deleted = usize::from_str(parts[5]).unwrap();
50+
51+
println!(
52+
"Commit {}: Added {} lines, Deleted {} lines",
53+
commit, added, deleted
54+
);
55+
56+
total_added += added;
57+
total_deleted += deleted;
58+
}
59+
}
60+
}
61+
62+
println!("Total Added: {} lines", total_added);
63+
println!("Total Deleted: {} lines", total_deleted);
64+
}

0 commit comments

Comments
 (0)