xai-org/grok-build · error · anyhow::Error
failed to discover git repo: {e}
Error message
failed to discover git repo: {e} What it means
count_tracked_files discovers the repository with gix before reading the index; if `gix::discover` fails (no repo found at or above repo_path, or the repo metadata is unreadable) it is mapped to this error. It is a public API used to cheaply count index entries.
Source
Thrown at crates/codegen/xai-fast-worktree/src/lib.rs:110
_out: &std::path::Path,
) -> anyhow::Result<SalvageReply> {
anyhow::bail!("not available in this build")
}
pub fn local_clean_artifacts(_dest: &std::path::Path) -> anyhow::Result<CleanArtifactsReply> {
anyhow::bail!("not available in this build")
}
pub use sync::{SourceDirtyState, SyncReport, WorktreeSync, collect_source_dirty_state};
#[cfg(target_os = "linux")]
pub use worktree::execute::cleanup_snapshot_git_state;
pub use worktree::{STRATEGY_GROVE_FUSE, STRATEGY_GROVE_NFS, STRATEGY_NFS, is_grove_strategy};
/// Count the number of tracked files in a git repository's index.
///
/// Reads the index header via `gix`, which contains the entry count — this
/// is an O(1) read (no directory walk). Useful for deciding whether a repo
/// is large enough to benefit from worktree pooling.
pub fn count_tracked_files(repo_path: &std::path::Path) -> anyhow::Result<usize> {
let repo = gix::discover(repo_path)
.map_err(|e| anyhow::anyhow!("failed to discover git repo: {e}"))?;
let index = repo
.index_or_load_from_head()
.map_err(|e| anyhow::anyhow!("failed to load git index: {e}"))?;
Ok(index.entries().len())
}
View on GitHub (pinned to bc7f02eddd)
Solutions
- Verify repo_path exists and is inside a valid git working tree (`git -C <path> rev-parse --show-toplevel`)
- Correct the path passed to count_tracked_files
- Check filesystem permissions on the path and its `.git` directory
- Repair repository corruption (`git status` to diagnose, re-clone if needed)
Example fix
// before
count_tracked_files(Path::new("/tmp/not-a-repo"))?;
// after
count_tracked_files(Path::new("/home/me/project"))?; // dir containing .git Defensive patterns
Strategy: validation
Validate before calling
fn inside_git_repo(path: &Path) -> bool {
path.join(".git").exists()
|| std::process::Command::new("git")
.arg("-C").arg(path).arg("rev-parse").arg("--git-dir")
.output().map(|o| o.status.success()).unwrap_or(false)
} Try / catch
let count = count_tracked_files(repo_path)
.map_err(|e| e.context(format!("cannot count tracked files in {}", repo_path.display())))?; Prevention
- Verify repo_path exists and is inside a checkout before calling
- Use the checkout root rather than a subdirectory of unknown provenance
- Check permissions on the path and `.git` in containerized environments
- Create/clone the repo before any counting logic runs
When it happens
Trigger: Calling count_tracked_files with a path that is not inside a git repository, a path that does not exist, or a repository whose `.git` discovery fails due to permissions or corrupt config.
Common situations: Typos in the repo path; running outside any checkout; passing a bare repo with an unreadable layout; running before the workspace checkout exists.
Related errors
- invalid .git file format: {}
- bare repository has no working directory
- git reset --hard {} failed: {}
- git clean {} failed: {}
- git checkout {} failed: {}
AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31).
Data as JSON: /api/errors/bb6b6498068a15a8.
Report an issue: GitHub.