claude_code_status_line/
git.rs1use crate::types::GitInfo;
2use std::process::Command;
3
4pub fn get_git_info(dir: &str) -> Option<GitInfo> {
5 let output = match Command::new("git")
6 .args(["-C", dir, "status", "--porcelain", "-b"])
7 .output()
8 {
9 Ok(o) => o,
10 Err(e) => {
11 if std::env::var("STATUSLINE_DEBUG").is_ok() {
12 eprintln!("statusline warning: git not available: {}", e);
13 }
14 return None;
15 }
16 };
17
18 if !output.status.success() {
19 return None;
20 }
21
22 let stdout = String::from_utf8(output.stdout).ok()?;
23 let lines: Vec<&str> = stdout.lines().collect();
24
25 if lines.is_empty() {
26 return None;
27 }
28
29 let branch_line = lines[0];
30 let branch = if let Some(raw) = branch_line.strip_prefix("## ") {
31 if let Some(idx) = raw.find("...") {
32 raw[..idx].to_string()
33 } else if let Some(stripped) = raw.strip_prefix("No commits yet on ") {
34 stripped.to_string()
35 } else if raw.starts_with("HEAD (no branch)") {
36 "HEAD".to_string()
37 } else {
38 raw.to_string()
39 }
40 } else {
41 return None;
42 };
43
44 let is_dirty = lines.len() > 1;
45
46 Some(GitInfo { branch, is_dirty })
47}