Skip to content

Commit 34f80b9

Browse files
feat(utils): 增加提交消息并更新相关功能
- 在 Commit 结构体中添加 commit_msg 字段 - 修改 get_commits 函数以获取并解析提交消息 - 更新 print_commit_stats 函数以显示提交消息 - 在测试用例中添加对提交消息的检查
1 parent 9032906 commit 34f80b9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/utils.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ pub struct Commit {
77
files_changed: usize,
88
added: usize,
99
deleted: usize,
10+
commit_msg: String, // 新增的 commit_msg 字段
1011
}
1112

1213
pub fn get_commits(keyword: &str, exclude_merge: &bool) -> Vec<Commit> {
1314
let k = format!("--grep={}", keyword);
14-
let mut args = vec!["log", &k, "--pretty=format:%H"];
15+
let mut args = vec!["log", &k, "--pretty=format:%H %s"]; // 获取哈希和提交消息
1516

1617
if !exclude_merge {
1718
args.push("--no-merges");
@@ -27,12 +28,16 @@ pub fn get_commits(keyword: &str, exclude_merge: &bool) -> Vec<Commit> {
2728
commits
2829
.lines()
2930
.map(|commit| {
30-
let (files_changed, added, deleted) = calculate_diff_info(commit);
31+
let parts: Vec<&str> = commit.splitn(2, ' ').collect();
32+
let commit_hash = parts[0].to_string();
33+
let commit_msg = parts.get(1).unwrap_or(&"").to_string(); // 获取提交消息
34+
let (files_changed, added, deleted) = calculate_diff_info(&commit_hash);
3135
Commit {
32-
commit_hash: commit.to_string(),
36+
commit_hash,
3337
files_changed,
3438
added,
3539
deleted,
40+
commit_msg,
3641
}
3742
})
3843
.collect()
@@ -84,8 +89,8 @@ pub fn print_commit_stats(commit_stats: Vec<Commit>) {
8489

8590
for commit in commit_stats {
8691
println!(
87-
"Commit {}: {} insertions(+), {} deletions(-),{} files changed",
88-
commit.commit_hash, commit.added, commit.deleted, commit.files_changed,
92+
"Commit {}: {}, {} insertions(+), {} deletions(-),{} files changed",
93+
commit.commit_hash, commit.commit_msg,commit.added, commit.deleted, commit.files_changed,
8994
);
9095

9196
total_added += commit.added;
@@ -107,12 +112,14 @@ mod tests {
107112
let keyword = "README";
108113
let exclude_merge = true;
109114
let expected_commit_id = "cd13ae5251d6aaf12e4497d6285a6aa1b507eb42";
115+
let expected_commit_msg = "feat: README"; // 假设这是提交消息
110116

111117
let commits = get_commits(keyword, &exclude_merge);
112118
assert!(!commits.is_empty());
113119

114120
let expected_commit = Commit {
115121
commit_hash: expected_commit_id.to_string(),
122+
commit_msg: expected_commit_msg.to_string(), // 检查提交消息
116123
files_changed: 1,
117124
added: 99,
118125
deleted: 0,
@@ -145,18 +152,21 @@ mod tests {
145152
files_changed: 2,
146153
added: 10,
147154
deleted: 5,
155+
commit_msg: "commit1".to_string()
148156
},
149157
Commit {
150158
commit_hash: "commit2".to_string(),
151159
files_changed: 3,
152160
added: 50,
153161
deleted: 10,
162+
commit_msg: "commit2".to_string()
154163
},
155164
Commit {
156165
commit_hash: "commit3".to_string(),
157166
files_changed: 1,
158167
added: 30,
159168
deleted: 2,
169+
commit_msg: "commit3".to_string()
160170
},
161171
];
162172

0 commit comments

Comments
 (0)