git_x/
color_graph.rs

1use crate::command::Command;
2use crate::{GitXError, Result};
3use std::process::Command as StdCommand;
4
5pub fn run() -> Result<()> {
6    let cmd = ColorGraphCommand;
7    cmd.execute(())
8}
9
10/// Command implementation for git color-graph
11pub struct ColorGraphCommand;
12
13impl Command for ColorGraphCommand {
14    type Input = ();
15    type Output = ();
16
17    fn execute(&self, _input: ()) -> Result<()> {
18        let output = run_color_graph()?;
19        print!("{output}");
20        Ok(())
21    }
22
23    fn name(&self) -> &'static str {
24        "color-graph"
25    }
26
27    fn description(&self) -> &'static str {
28        "Show a colorized git log graph"
29    }
30}
31
32fn run_color_graph() -> Result<String> {
33    let output = StdCommand::new("git")
34        .args(get_color_git_log_args())
35        .output()
36        .map_err(GitXError::Io)?;
37
38    if output.status.success() {
39        Ok(String::from_utf8_lossy(&output.stdout).to_string())
40    } else {
41        let stderr = String::from_utf8_lossy(&output.stderr);
42        Err(GitXError::GitCommand(format!(
43            "git log failed: {}",
44            stderr.trim()
45        )))
46    }
47}
48
49fn get_color_git_log_args() -> [&'static str; 7] {
50    [
51        "log",
52        "--oneline",
53        "--graph",
54        "--decorate",
55        "--all",
56        "--color=always",
57        "--pretty=format:%C(auto)%h%d %s %C(dim)(%an, %ar)%C(reset)",
58    ]
59}