git_x/
info.rs

1use crate::command::Command;
2use crate::core::git::GitOperations;
3use crate::core::output::Format;
4
5pub fn run() -> crate::Result<()> {
6    let cmd = InfoCommand;
7    cmd.execute(())
8}
9
10/// Command implementation for git info
11pub struct InfoCommand;
12
13impl Command for InfoCommand {
14    type Input = ();
15    type Output = ();
16
17    fn execute(&self, _input: ()) -> crate::Result<()> {
18        let output = run_info()?;
19        println!("{output}");
20        Ok(())
21    }
22
23    fn name(&self) -> &'static str {
24        "info"
25    }
26
27    fn description(&self) -> &'static str {
28        "Show a high-level overview of the current repo"
29    }
30}
31
32fn run_info() -> crate::Result<String> {
33    let repo_name = GitOperations::repo_root()?;
34    let (current_branch, upstream, ahead, behind) = GitOperations::branch_info_optimized()?;
35    let last_commit = GitOperations::run(&["log", "-1", "--pretty=format:%s (%cr)"])?;
36
37    // Format upstream tracking info
38    let tracking = upstream
39        .as_ref()
40        .map(|u| u.to_string())
41        .unwrap_or_else(|| "(no upstream)".to_string());
42
43    let mut lines = Vec::new();
44    lines.push(format!("Repo: {}", Format::bold(&repo_name)));
45    lines.push(format!("Branch: {}", Format::bold(&current_branch)));
46    lines.push(format!("Tracking: {}", Format::bold(&tracking)));
47    lines.push(format!(
48        "Ahead: {} Behind: {}",
49        Format::bold(&ahead.to_string()),
50        Format::bold(&behind.to_string())
51    ));
52    lines.push(format!("Last Commit: \"{}\"", Format::bold(&last_commit)));
53
54    Ok(lines.join("\n"))
55}