Skip to main content

vtcode_core/
git_info.rs

1//! Git repository information collection
2//!
3//! This module provides utilities for collecting git metadata from the workspace,
4//! similar to OpenAI Codex PR #10145. It collects remote URLs, HEAD commit hash,
5//! and repository root path for inclusion in LLM request headers.
6
7use anyhow::{Context, Result};
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10use std::path::Path;
11use std::process::Command;
12
13/// Git repository information for a workspace
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
15pub struct GitInfo {
16    /// Remote URLs keyed by remote name (e.g., "origin")
17    pub remotes: BTreeMap<String, String>,
18    /// HEAD commit hash (short form)
19    pub head_commit: Option<String>,
20    /// Repository root path
21    pub repo_root: Option<String>,
22}
23
24/// Get git remote URLs for fetch remotes in the repository at the given path.
25/// Returns a BTreeMap mapping remote names to their fetch URLs.
26///
27/// # Arguments
28/// * `cwd` - The working directory to run git commands in
29///
30/// # Returns
31/// A BTreeMap where keys are remote names (e.g., "origin") and values are fetch URLs.
32/// Returns an empty map if not in a git repository or if no remotes are configured.
33pub 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        // Not a git repository or git not available
42        return Ok(BTreeMap::new());
43    }
44
45    let stdout = String::from_utf8_lossy(&output.stdout);
46    let mut remotes = BTreeMap::new();
47
48    // Parse output like:
49    // origin  https://github.com/user/repo.git (fetch)
50    // origin  https://github.com/user/repo.git (push)
51    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            // Only collect fetch remotes to avoid duplicates
59            if purpose == "fetch" {
60                remotes.insert(name, url);
61            }
62        }
63    }
64
65    Ok(remotes)
66}
67
68/// Get the HEAD commit hash (short form) for the repository at the given path.
69///
70/// # Arguments
71/// * `cwd` - The working directory to run git commands in
72///
73/// # Returns
74/// The short commit hash (7 characters) of HEAD, or None if not in a git repository.
75pub 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
95/// Get the repository root path for the given working directory.
96///
97/// # Arguments
98/// * `cwd` - The working directory to run git commands in
99///
100/// # Returns
101/// The absolute path to the repository root, or None if not in a git repository.
102pub 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
127/// Collect all git information for a workspace.
128///
129/// # Arguments
130/// * `cwd` - The working directory to collect git info from
131///
132/// # Returns
133/// A GitInfo struct containing remote URLs, HEAD commit hash, and repo root.
134/// Returns default GitInfo if not in a git repository.
135pub 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/// Check if the given path is inside a git repository.
148///
149/// # Arguments
150/// * `cwd` - The working directory to check
151///
152/// # Returns
153/// true if inside a git repository, false otherwise.
154#[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
166/// Async variant of [`get_git_remote_urls`]. Runs the git subprocess on the
167/// blocking thread pool.
168pub 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
176/// Async variant of [`get_head_commit_hash`]. Runs the git subprocess on the
177/// blocking thread pool.
178pub 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        // The vtcode repo itself should be a git repo
192        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        // The root should contain the path
202        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        // Short hash should be 7-12 characters
211        let hash_str = hash.unwrap();
212        assert!(hash_str.len() >= 7 && hash_str.len() <= 12);
213        // Should only contain hex characters
214        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        // Should have a HEAD commit
223        assert!(info.head_commit.is_some());
224
225        // Should have a repo root
226        assert!(info.repo_root.is_some());
227
228        // Remotes may or may not be present depending on git config
229        // but the function should not error
230    }
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        // Should return empty results without error
242        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}