{"record":{"id":"2944cf9d578fd50d","repo":"xai-org/grok-build","slug":"invalid-git-repository","errorCode":null,"errorMessage":"Invalid git repository: {}","messagePattern":"Invalid git repository: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-workspace/src/session/git.rs","lineNumber":433,"sourceCode":"pub fn find_git_root_from_path(path: &Path) -> Result<PathBuf> {\n    match discover_git_root(path) {\n        GitDiscoveryResult::Found(root) => Ok(root),\n        GitDiscoveryResult::NotARepo => {\n            anyhow::bail!(\"not a git repository: {}\", path.display())\n        }\n        GitDiscoveryResult::DiscoveryFailed(e) => Err(e),\n    }\n}\n/// Find the main repo root (not the worktree working directory).\n/// For regular repos this is the same as find_git_root_from_path.\n/// For worktrees, this returns the parent repo's root.\n/// Use this for worktree management operations (create/remove/apply).\npub fn find_main_repo_root_from_path(path: &Path) -> Result<PathBuf> {\n    let repo = Repository::discover(path)?;\n    repo.commondir()\n        .parent()\n        .map(|p| p.to_path_buf())\n        .ok_or_else(|| anyhow::anyhow!(\"Invalid git repository: {}\", path.display()))\n}\npub fn change_type_from_git2_delta(delta: git2::Delta) -> ChangeType {\n    match delta {\n        git2::Delta::Added => ChangeType::Create,\n        git2::Delta::Deleted => ChangeType::Delete,\n        git2::Delta::Modified => ChangeType::Edit,\n        git2::Delta::Renamed => ChangeType::Rename,\n        git2::Delta::Copied => ChangeType::Copy,\n        git2::Delta::Typechange => ChangeType::Typechange,\n        git2::Delta::Untracked => ChangeType::Untracked,\n        other => {\n            tracing::warn!(?other, \"unexpected git delta type, treating as Edit\");\n            ChangeType::Edit\n        }\n    }\n}\nfn change_type_from_git2_status(s: git2::Status, staged: bool) -> ChangeType {\n    use git2::Status;","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-workspace/src/session/git.rs#L415-L451","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nlet root = find_main_repo_root_from_path(Path::new(\"/\"))?; // commondir at root has no parent\n// after\nlet root = find_main_repo_root_from_path(Path::new(\"/home/me/project\"))?; // normal .git layout","handlingStrategy":"validation","validationCode":"fn valid_common_root(path: &Path) -> Option<std::path::PathBuf> {\n    let repo = git2::Repository::discover(path).ok()?;\n    repo.commondir().parent().map(|p| p.to_path_buf())\n}\nassert!(valid_common_root(path).is_some(), \"invalid repo layout\");","typeGuard":"fn repo_common_dir(path: &Path) -> Option<std::path::PathBuf> {\n    git2::Repository::discover(path).ok().map(|r| r.commondir().to_path_buf())\n}","tryCatchPattern":"match find_main_repo_root_from_path(path) {\n    Ok(root) => root,\n    Err(e) if e.to_string().contains(\"Invalid git repository\") => {\n        anyhow::bail!(\"repo at {} has unusable .git layout; re-clone\", path.display())\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["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"],"tags":["git","repository-corruption","worktree"],"backgroundTag":"invalid-git-repository","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}