Skip to content

Commit d037337

Browse files
refactor(src): 优化代码结构并添加提交消息字段
- 在 Commit 结构体中添加 commit_msg 字段 - 更新 get_commits 函数以获取并存储提交消息 - 修改 print_commit_stats 函数以按新格式输出提交信息 - 更新单元测试以检查提交消息
1 parent 34f80b9 commit d037337

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/utils.rs

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

1313
pub fn get_commits(keyword: &str, exclude_merge: &bool) -> Vec<Commit> {
@@ -30,7 +30,7 @@ pub fn get_commits(keyword: &str, exclude_merge: &bool) -> Vec<Commit> {
3030
.map(|commit| {
3131
let parts: Vec<&str> = commit.splitn(2, ' ').collect();
3232
let commit_hash = parts[0].to_string();
33-
let commit_msg = parts.get(1).unwrap_or(&"").to_string(); // 获取提交消息
33+
let commit_msg = parts.get(1).unwrap_or(&"").to_string(); // 获取提交消息
3434
let (files_changed, added, deleted) = calculate_diff_info(&commit_hash);
3535
Commit {
3636
commit_hash,
@@ -90,7 +90,11 @@ pub fn print_commit_stats(commit_stats: Vec<Commit>) {
9090
for commit in commit_stats {
9191
println!(
9292
"Commit {}: {}, {} insertions(+), {} deletions(-),{} files changed",
93-
commit.commit_hash, commit.commit_msg,commit.added, commit.deleted, commit.files_changed,
93+
commit.commit_msg,
94+
commit.commit_hash,
95+
commit.added,
96+
commit.deleted,
97+
commit.files_changed,
9498
);
9599

96100
total_added += commit.added;
@@ -112,14 +116,14 @@ mod tests {
112116
let keyword = "README";
113117
let exclude_merge = true;
114118
let expected_commit_id = "cd13ae5251d6aaf12e4497d6285a6aa1b507eb42";
115-
let expected_commit_msg = "feat: README"; // 假设这是提交消息
119+
let expected_commit_msg = "feat: README"; // 假设这是提交消息
116120

117121
let commits = get_commits(keyword, &exclude_merge);
118122
assert!(!commits.is_empty());
119123

120124
let expected_commit = Commit {
121125
commit_hash: expected_commit_id.to_string(),
122-
commit_msg: expected_commit_msg.to_string(), // 检查提交消息
126+
commit_msg: expected_commit_msg.to_string(), // 检查提交消息
123127
files_changed: 1,
124128
added: 99,
125129
deleted: 0,
@@ -152,21 +156,21 @@ mod tests {
152156
files_changed: 2,
153157
added: 10,
154158
deleted: 5,
155-
commit_msg: "commit1".to_string()
159+
commit_msg: "commit1".to_string(),
156160
},
157161
Commit {
158162
commit_hash: "commit2".to_string(),
159163
files_changed: 3,
160164
added: 50,
161165
deleted: 10,
162-
commit_msg: "commit2".to_string()
166+
commit_msg: "commit2".to_string(),
163167
},
164168
Commit {
165169
commit_hash: "commit3".to_string(),
166170
files_changed: 1,
167171
added: 30,
168172
deleted: 2,
169-
commit_msg: "commit3".to_string()
173+
commit_msg: "commit3".to_string(),
170174
},
171175
];
172176

0 commit comments

Comments
 (0)