xai-org/grok-build · error
Invalid git repository: {}
Error message
Invalid git repository: {} What it means
find_main_repo_root_from_path discovers the repository and derives the main repo root from the commondir's parent. The error fires when commondir() has no parent path component, which should not happen for a valid repository layout, so it signals the discovered location is not usable as a git repository for worktree operations.
Source
Thrown at crates/codegen/xai-grok-workspace/src/session/git.rs:433
pub fn find_git_root_from_path(path: &Path) -> Result<PathBuf> {
match discover_git_root(path) {
GitDiscoveryResult::Found(root) => Ok(root),
GitDiscoveryResult::NotARepo => {
anyhow::bail!("not a git repository: {}", path.display())
}
GitDiscoveryResult::DiscoveryFailed(e) => Err(e),
}
}
/// Find the main repo root (not the worktree working directory).
/// For regular repos this is the same as find_git_root_from_path.
/// For worktrees, this returns the parent repo's root.
/// Use this for worktree management operations (create/remove/apply).
pub fn find_main_repo_root_from_path(path: &Path) -> Result<PathBuf> {
let repo = Repository::discover(path)?;
repo.commondir()
.parent()
.map(|p| p.to_path_buf())
.ok_or_else(|| anyhow::anyhow!("Invalid git repository: {}", path.display()))
}
pub fn change_type_from_git2_delta(delta: git2::Delta) -> ChangeType {
match delta {
git2::Delta::Added => ChangeType::Create,
git2::Delta::Deleted => ChangeType::Delete,
git2::Delta::Modified => ChangeType::Edit,
git2::Delta::Renamed => ChangeType::Rename,
git2::Delta::Copied => ChangeType::Copy,
git2::Delta::Typechange => ChangeType::Typechange,
git2::Delta::Untracked => ChangeType::Untracked,
other => {
tracing::warn!(?other, "unexpected git delta type, treating as Edit");
ChangeType::Edit
}
}
}
fn change_type_from_git2_status(s: git2::Status, staged: bool) -> ChangeType {
use git2::Status;View on GitHub (pinned to bc7f02eddd)
Solutions
- Verify the repo layout: `git rev-parse --git-common-dir` should return a nested path like /path/to/project/.git
- Re-clone the repository to restore a normal .git structure
- Check for overridden GIT_DIR/GIT_COMMON_DIR environment variables and unset them
- If it persists, run `git fsck` or recreate the repository
Example fix
// before
let root = find_main_repo_root_from_path(Path::new("/"))?; // commondir at root has no parent
// after
let root = find_main_repo_root_from_path(Path::new("/home/me/project"))?; // normal .git layout Defensive patterns
Strategy: validation
Validate before calling
fn valid_common_root(path: &Path) -> Option<std::path::PathBuf> {
let repo = git2::Repository::discover(path).ok()?;
repo.commondir().parent().map(|p| p.to_path_buf())
}
assert!(valid_common_root(path).is_some(), "invalid repo layout"); Type guard
fn repo_common_dir(path: &Path) -> Option<std::path::PathBuf> {
git2::Repository::discover(path).ok().map(|r| r.commondir().to_path_buf())
} Try / catch
match find_main_repo_root_from_path(path) {
Ok(root) => root,
Err(e) if e.to_string().contains("Invalid git repository") => {
anyhow::bail!("repo at {} has unusable .git layout; re-clone", path.display())
}
Err(e) => return Err(e),
} Prevention
- Don't override GIT_DIR/GIT_COMMON_DIR for processes using this library
- Run `git fsck` after disk incidents; re-clone corrupt repos
- Keep .git nested under the project directory as git expects
When it happens
Trigger: Calling any worktree-management entry point (prepare_worktree_creation, create_worktree_streaming, remove_worktree, apply_worktree, worktree_base_dir_for_source) with a path where Repository::discover succeeds but commondir().parent() returns None (commondir resolves to a root-level path).
Common situations: Exotic GIT_DIR/GIT_COMMON_DIR setups, a commondir placed at filesystem root, or a corrupted .git layout where the common directory is not nested under a parent directory.
Related errors
- git reset --hard {} failed: {}
- git clean {} failed: {}
- git checkout {} failed: {}
- git worktree add failed: {}
- git worktree add failed: {}
AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31).
Data as JSON: /api/errors/2944cf9d578fd50d.
Report an issue: GitHub.