xai-org/grok-build · error

bare git repository: {}

Error message

bare git repository: {}

What it means

discover_git_root wraps git2::Repository::discover and returns the working-directory root when found. If discovery succeeds but the repository is bare (no workdir), it produces this error because there is no working-tree root to return. It prevents callers from treating a bare repo as a valid project root.

Source

Thrown at crates/codegen/xai-grok-workspace/src/session/git.rs:215

    /// The path is definitively not inside a git repository.
    NotARepo,
    /// libgit2 failed for a reason other than "not found" (e.g. permissions,
    /// unsupported extensions, corrupt repo). The user may or may not be in a
    /// git repo — we can't tell.
    DiscoveryFailed(anyhow::Error),
}
/// Discover whether `path` is inside a git repository.
///
/// Returns [`GitDiscoveryResult::Found`] with the worktree root on success,
/// [`GitDiscoveryResult::NotARepo`] when the path is definitively outside any
/// repo, or [`GitDiscoveryResult::DiscoveryFailed`] when libgit2 errors for
/// an unexpected reason (so callers can avoid false-positive "not a repo"
/// decisions).
pub fn discover_git_root(path: &Path) -> GitDiscoveryResult {
    match Repository::discover(path) {
        Ok(repo) => match repo.workdir() {
            Some(root) => GitDiscoveryResult::Found(root.to_path_buf()),
            None => GitDiscoveryResult::DiscoveryFailed(anyhow::anyhow!(
                "bare git repository: {}",
                path.display()
            )),
        },
        Err(e) => {
            let is_not_found =
                e.code() == git2::ErrorCode::NotFound && e.class() == git2::ErrorClass::Repository;
            if is_not_found {
                GitDiscoveryResult::NotARepo
            } else {
                GitDiscoveryResult::DiscoveryFailed(e.into())
            }
        }
    }
}
#[allow(
    dead_code,
    reason = "Phase 1 internal git helper; will be used by WorkspaceChannel git operations"

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Point the API at a non-bare clone that has a working directory
  2. Run `git config --bool core.bare` on the repo; if true, re-clone normally or set bare=false and restore the worktree
  3. Re-clone the repository: `git clone <url>` instead of using the bare copy
  4. If you must handle bare repos, match on GitDiscoveryResult::DiscoveryFailed and skip/notify instead of treating it as a project root

Example fix

// before
let root = discover_git_root(Path::new("/srv/git/project.git"))?; // bare -> error
// after
let path = Path::new("/home/me/project"); // non-bare clone with workdir
let root = match discover_git_root(path) {
    GitDiscoveryResult::Found(p) => p,
    GitDiscoveryResult::DiscoveryFailed(e) => return Err(e),
};
Defensive patterns

Strategy: validation

Validate before calling

fn is_bare(path: &Path) -> bool {
    git2::Repository::open(path)
        .map(|r| r.is_bare())
        .unwrap_or(false)
}
if is_bare(path) { eprintln!("{} is a bare repository", path.display()); return; }

Type guard

fn has_workdir(path: &Path) -> Option<std::path::PathBuf> {
    git2::Repository::discover(path).ok()?.workdir().map(|p| p.to_path_buf())
}

Try / catch

match discover_git_root(path) {
    GitDiscoveryResult::Found(root) => root,
    GitDiscoveryResult::DiscoveryFailed(e) if e.to_string().contains("bare git repository") => {
        // skip bare repos; don't treat as 'not a repo' false-positive
        return Ok(None);
    }
    GitDiscoveryResult::DiscoveryFailed(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling discover_git_root on a path that resolves to a bare repository (e.g. repo.git directory) or a repo with core.bare=true, so Repository::discover succeeds but repo.workdir() returns None.

Common situations: Pointing the workspace at a server-side bare clone (e.g. /srv/git/project.git), cloning with --bare for CI, or a misconfigured GIT_DIR where the checkout directory was moved/deleted leaving only the bare store.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/4dddfdb7d92e4fd3. Report an issue: GitHub.