git_x/
info.rs

1use std::path::Path;
2use std::process::Command;
3
4pub fn run() {
5    // Get repo name
6    let output = Command::new("git")
7        .args(["rev-parse", "--show-toplevel"])
8        .output()
9        .expect("Failed to get repo root");
10    let repo_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
11    let repo_name = Path::new(&repo_path)
12        .file_name()
13        .map(|s| s.to_string_lossy().into_owned())
14        .unwrap_or_else(|| "unknown".to_string());
15
16    // Get current branch
17    let output = Command::new("git")
18        .args(["rev-parse", "--abbrev-ref", "HEAD"])
19        .output()
20        .expect("Failed to get current branch");
21    let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
22
23    // Get upstream tracking branch
24    let output = Command::new("git")
25        .args(["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"])
26        .output()
27        .unwrap_or_else(|_| panic!("Failed to get upstream for {branch}"));
28    let tracking_raw = String::from_utf8_lossy(&output.stdout).trim().to_string();
29    let tracking = if tracking_raw.is_empty() {
30        "(no upstream)".to_string()
31    } else {
32        tracking_raw
33    };
34
35    // Get ahead/behind counts
36    let output = Command::new("git")
37        .args(["rev-list", "--left-right", "--count", "HEAD...@{u}"])
38        .output()
39        .expect("Failed to get ahead/behind count");
40    let counts = String::from_utf8_lossy(&output.stdout).trim().to_string();
41    let parts: Vec<&str> = counts.split_whitespace().collect();
42    let ahead = parts.first().unwrap_or(&"0");
43    let behind = parts.get(1).unwrap_or(&"0");
44
45    // Get last commit message and relative date
46    let output = Command::new("git")
47        .args(["log", "-1", "--pretty=format:%s (%cr)"])
48        .output()
49        .expect("Failed to get last commit");
50    let last_commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
51
52    let bold = console::Style::new().bold();
53
54    // Print all the info
55    println!("Repo: {}", bold.apply_to(repo_name));
56    println!("Branch: {}", bold.apply_to(branch));
57    println!("Tracking: {}", bold.apply_to(tracking));
58    println!(
59        "Ahead: {} Behind: {}",
60        bold.apply_to(ahead),
61        bold.apply_to(behind)
62    );
63    println!("Last Commit: \"{}\"", bold.apply_to(last_commit));
64}