1use std::process::Command;
2
3pub fn run() {
4 let output = Command::new("git")
5 .args(get_color_git_log_args())
6 .output()
7 .expect("Failed to run git log");
8
9 if is_command_successful(&output) {
10 print_git_output(&output.stdout);
11 } else {
12 print_git_error(&output.stderr);
13 }
14}
15
16pub fn is_command_successful(output: &std::process::Output) -> bool {
18 output.status.success()
19}
20
21pub fn print_git_output(stdout: &[u8]) {
23 let result = convert_output_to_string(stdout);
24 println!("{result}");
25}
26
27pub fn print_git_error(stderr: &[u8]) {
29 let err = convert_output_to_string(stderr);
30 eprintln!("{}", format_color_git_error(&err));
31}
32
33pub fn convert_output_to_string(output: &[u8]) -> String {
35 String::from_utf8_lossy(output).to_string()
36}
37
38pub fn get_color_git_log_args() -> [&'static str; 7] {
40 [
41 "log",
42 "--oneline",
43 "--graph",
44 "--decorate",
45 "--all",
46 "--color=always",
47 "--pretty=format:%C(auto)%h%d %s %C(dim)(%an, %ar)%C(reset)",
48 ]
49}
50
51pub fn format_color_git_error(stderr: &str) -> String {
53 format!("❌ git log failed:\n{stderr}")
54}