1use anyhow::{Context, Result};
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10use std::path::Path;
11use std::process::Command;
12
13#[derive(Debug, Clone, Serialize, Deserialize, Default)]
15pub struct GitInfo {
16 pub remotes: BTreeMap<String, String>,
18 pub head_commit: Option<String>,
20 pub repo_root: Option<String>,
22}
23
24pub fn get_git_remote_urls(cwd: &Path) -> Result<BTreeMap<String, String>> {
34 let output = Command::new("git")
35 .args(["remote", "-v"])
36 .current_dir(cwd)
37 .output()
38 .with_context(|| format!("Failed to run git remote -v in {}", cwd.display()))?;
39
40 if !output.status.success() {
41 return Ok(BTreeMap::new());
43 }
44
45 let stdout = String::from_utf8_lossy(&output.stdout);
46 let mut remotes = BTreeMap::new();
47
48 for line in stdout.lines() {
52 let parts: Vec<&str> = line.split_whitespace().collect();
53 if parts.len() >= 3 {
54 let name = parts[0].to_string();
55 let url = parts[1].to_string();
56 let purpose = parts[2].trim_matches(|c| c == '(' || c == ')');
57
58 if purpose == "fetch" {
60 remotes.insert(name, url);
61 }
62 }
63 }
64
65 Ok(remotes)
66}
67
68pub fn get_head_commit_hash(cwd: &Path) -> Result<Option<String>> {
76 let output = Command::new("git")
77 .args(["rev-parse", "--short", "HEAD"])
78 .current_dir(cwd)
79 .output()
80 .with_context(|| format!("Failed to run git rev-parse in {}", cwd.display()))?;
81
82 if !output.status.success() {
83 return Ok(None);
84 }
85
86 let hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
87
88 if hash.is_empty() {
89 Ok(None)
90 } else {
91 Ok(Some(hash))
92 }
93}
94
95pub fn get_git_repo_root(cwd: &Path) -> Result<Option<String>> {
103 let output = Command::new("git")
104 .args(["rev-parse", "--show-toplevel"])
105 .current_dir(cwd)
106 .output()
107 .with_context(|| {
108 format!(
109 "Failed to run git rev-parse --show-toplevel in {}",
110 cwd.display()
111 )
112 })?;
113
114 if !output.status.success() {
115 return Ok(None);
116 }
117
118 let root = String::from_utf8_lossy(&output.stdout).trim().to_string();
119
120 if root.is_empty() {
121 Ok(None)
122 } else {
123 Ok(Some(root))
124 }
125}
126
127pub fn collect_git_info(cwd: &Path) -> Result<GitInfo> {
136 let remotes = get_git_remote_urls(cwd)?;
137 let head_commit = get_head_commit_hash(cwd)?;
138 let repo_root = get_git_repo_root(cwd)?;
139
140 Ok(GitInfo {
141 remotes,
142 head_commit,
143 repo_root,
144 })
145}
146
147#[must_use]
155pub fn is_git_repo(cwd: &Path) -> bool {
156 Command::new("git")
157 .args(["rev-parse", "--git-dir"])
158 .current_dir(cwd)
159 .stdout(std::process::Stdio::null())
160 .stderr(std::process::Stdio::null())
161 .status()
162 .map(|status| status.success())
163 .unwrap_or(false)
164}
165
166pub async fn get_git_remote_urls_async(
169 cwd: std::path::PathBuf,
170) -> Result<BTreeMap<String, String>> {
171 tokio::task::spawn_blocking(move || get_git_remote_urls(&cwd))
172 .await
173 .context("Git remote URLs task panicked")?
174}
175
176pub async fn get_head_commit_hash_async(cwd: std::path::PathBuf) -> Result<Option<String>> {
179 tokio::task::spawn_blocking(move || get_head_commit_hash(&cwd))
180 .await
181 .context("Git HEAD hash task panicked")?
182}
183
184#[cfg(test)]
185mod tests {
186 use super::*;
187 use std::path::PathBuf;
188
189 #[test]
190 fn test_is_git_repo() {
191 let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
193 assert!(is_git_repo(&repo_root));
194 }
195
196 #[test]
197 fn test_get_git_repo_root() {
198 let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
199 let root = get_git_repo_root(&repo_root).unwrap();
200 assert!(root.is_some());
201 assert!(root.unwrap().contains("vtcode"));
203 }
204
205 #[test]
206 fn test_get_head_commit_hash() {
207 let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
208 let hash = get_head_commit_hash(&repo_root).unwrap();
209 assert!(hash.is_some());
210 let hash_str = hash.unwrap();
212 assert!(hash_str.len() >= 7 && hash_str.len() <= 12);
213 assert!(hash_str.chars().all(|c| c.is_ascii_hexdigit()));
215 }
216
217 #[test]
218 fn test_collect_git_info() {
219 let repo_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
220 let info = collect_git_info(&repo_root).unwrap();
221
222 assert!(info.head_commit.is_some());
224
225 assert!(info.repo_root.is_some());
227
228 }
231
232 #[test]
233 fn test_non_git_directory() {
234 use std::fs;
235 use tempfile::TempDir;
236
237 let temp_dir = TempDir::new().unwrap();
238 let non_git_path = temp_dir.path().join("not_a_repo");
239 fs::create_dir(&non_git_path).unwrap();
240
241 assert!(!is_git_repo(&non_git_path));
243 assert!(get_git_remote_urls(&non_git_path).unwrap().is_empty());
244 assert!(get_head_commit_hash(&non_git_path).unwrap().is_none());
245 assert!(get_git_repo_root(&non_git_path).unwrap().is_none());
246 }
247}