Skip to main content

grit_protocol/
lib.rs

1//! Git smart protocol handlers for HTTP transport.
2//!
3//! Provides a clean Rust API for running git upload-pack and receive-pack
4//! operations. Currently implemented by spawning `grit` as a subprocess
5//! with piped I/O — the same model as `git-http-backend`.
6//!
7//! Future work: replace subprocess calls with in-process protocol handling
8//! once the core protocol logic is libified with generic Read/Write streams.
9
10pub mod receive_pack;
11pub mod upload_pack;
12
13use std::path::{Path, PathBuf};
14
15/// Find the grit executable path.
16///
17/// Checks `GUST_BIN` env var first (for test harness compatibility),
18/// then falls back to `grit` on PATH.
19pub fn grit_executable() -> PathBuf {
20    if let Ok(bin) = std::env::var("GUST_BIN") {
21        if !bin.is_empty() {
22            return PathBuf::from(bin);
23        }
24    }
25    // Try the same binary that's running (if we're inside grit)
26    if let Ok(exe) = std::env::current_exe() {
27        let name = exe.file_name().unwrap_or_default().to_string_lossy();
28        if name.starts_with("grit") {
29            return exe;
30        }
31    }
32    PathBuf::from("grit")
33}
34
35/// Validate that a path looks like a git repository (bare or non-bare).
36pub fn validate_repo_path(path: &Path) -> anyhow::Result<PathBuf> {
37    // Bare repo: has HEAD file directly
38    if path.join("HEAD").is_file() {
39        return Ok(path.to_path_buf());
40    }
41    // Non-bare: has .git/HEAD
42    let dot_git = path.join(".git");
43    if dot_git.join("HEAD").is_file() {
44        return Ok(path.to_path_buf());
45    }
46    anyhow::bail!("not a git repository: {}", path.display())
47}