Skip to content

Commit d2178d0

Browse files
committed
feat(utils): implement get_repo_logs for cross-platform git log retrieval
1 parent f3a7379 commit d2178d0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/i18n/messages/en.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ pub static MESSAGES: phf::Map<&'static str, &'static str> = phf_map! {
3232
"commit_category_refactor" => "Refactored",
3333
"commit_category_test" => "Test Cases",
3434
"commit_category_chores" => "Chores",
35+
36+
// Git repository error messages
37+
"err_repo_not_found" => "Repo path does not exist or is not a directory: {}",
38+
"err_git_log_failed" => "git log failed in directory: {}\nstderr: {}",
3539
};

src/i18n/messages/zh.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ pub static MESSAGES: phf::Map<&'static str, &'static str> = phf_map! {
3232
"commit_category_refactor" => "代码重构",
3333
"commit_category_test" => "测试用例",
3434
"commit_category_chores" => "其他优化",
35+
36+
// Git repository error messages
37+
"err_repo_not_found" => "仓库路径不存在或不是目录:{}",
38+
"err_git_log_failed" => "git log 执行失败,目录:{}\n错误信息:{}",
3539
};

src/utils/get_repo_logs.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::i18n::t;
2+
use std::path::Path;
3+
use std::process::Command;
4+
5+
/// Get repository logs
6+
pub fn get_repo_logs(repo_dir: &str) -> anyhow::Result<Vec<String>> {
7+
let path = Path::new(repo_dir);
8+
if !path.exists() || !path.is_dir() {
9+
anyhow::bail!(t("err_repo_not_found").replace("{}", &repo_dir));
10+
}
11+
12+
// Git log format reference
13+
// https://git-scm.com/docs/pretty-formats
14+
let output = Command::new("git")
15+
.arg("log")
16+
.arg("--pretty=format:%an|||%ae|||%s|||'%h|||%ad")
17+
.current_dir(path)
18+
.output()?;
19+
20+
if !output.status.success() {
21+
anyhow::bail!(
22+
"{} {}",
23+
t("err_git_log_failed").replace("{}", &repo_dir),
24+
String::from_utf8_lossy(&output.stderr)
25+
);
26+
}
27+
28+
let stdout = String::from_utf8_lossy(&output.stdout);
29+
let lines: Vec<String> = stdout
30+
.lines()
31+
.map(|line| line.trim().to_string())
32+
.filter(|line| !line.is_empty())
33+
.collect();
34+
35+
Ok(lines)
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
#[ignore]
44+
fn test_git_log() {
45+
let repo_dir = "/Users/xxx/projects/my-repo";
46+
let logs = get_repo_logs(repo_dir).unwrap();
47+
assert!(!logs.is_empty());
48+
}
49+
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod format_commit;
22
pub mod format_log;
3+
pub mod get_repo_logs;
34
pub mod get_repo_name;
45
pub mod keypress;

0 commit comments

Comments
 (0)