{"record":{"id":"8894215ddba2c71c","repo":"xai-org/grok-build","slug":"bare-repository-has-no-working-directory","errorCode":null,"errorMessage":"bare repository has no working directory","messagePattern":"bare repository has no working directory","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/git/discovery.rs","lineNumber":59,"sourceCode":"            worktree_path.display()\n        )\n    }\n}\n\n/// Find the worktree root (working directory root) for a path.\n///\n/// This handles both regular repositories and worktrees correctly.\n/// For a regular repo at `/repo`, returns `/repo`.\n/// For a worktree at `/worktrees/wt1`, returns `/worktrees/wt1`.\n/// For a subdirectory `/repo/subdir`, returns `/repo`.\npub(crate) fn find_worktree_root(path: &Path) -> Result<PathBuf> {\n    let repo = gix::discover(path)\n        .with_context(|| format!(\"failed to discover git repo at {}\", path.display()))?;\n\n    // workdir() returns the working directory root for both repos and worktrees\n    let work_dir = repo\n        .workdir()\n        .ok_or_else(|| anyhow::anyhow!(\"bare repository has no working directory\"))?;\n\n    Ok(work_dir.to_path_buf())\n}\n\n/// Get the HEAD commit hash using gix.\npub(crate) fn get_head_commit(path: &Path) -> Result<String> {\n    let repo = gix::discover(path)\n        .with_context(|| format!(\"failed to discover git repo at {}\", path.display()))?;\n\n    let head = repo\n        .head()\n        .context(\"failed to get HEAD\")?\n        .peel_to_commit()\n        .context(\"failed to peel HEAD to commit\")?;\n\n    Ok(head.id().to_string())\n}\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/git/discovery.rs#L41-L77","documentation":"find_worktree_root discovers a git repository at `path` with gix and then requires a working directory. Bare repositories (created with `git init --bare` or clones ending in `.git` with no checkout) have `workdir() == None`, so the function cannot return a worktree root and throws this error.","triggerScenarios":"Calling find_worktree_root (or any API built on it) with a path pointing to a bare repository — e.g. a central server-side repo, a `.git` directory passed directly, or `--bare` clones.","commonSituations":"Pointing tooling at a remote/origin bare repo on a server or NAS; passing a repo's internal `.git` directory instead of the checkout root; CI setups that use bare clones for caching.","solutions":["Pass the path of a non-bare checkout (the directory containing a working tree) instead of the bare repo","Clone the repository non-bare (`git clone <url>`) and operate on the clone","If you intentionally need a working area from a bare repo, use `git worktree add` against it and pass the new worktree path"],"exampleFix":"// before\nlet root = find_worktree_root(Path::new(\"/srv/repo.git\"))?;\n// after\nlet root = find_worktree_root(Path::new(\"/srv/checkout\"))?; // non-bare clone","handlingStrategy":"validation","validationCode":"fn has_workdir(path: &Path) -> bool {\n    gix::discover(path).map(|r| r.workdir().is_some()).unwrap_or(false)\n}\n// before: assert repo_path is a non-bare checkout\ngit::discover_guard(&repo_path)?;","typeGuard":"fn non_bare_repo(path: &Path) -> Option<std::path::PathBuf> {\n    gix::discover(path).ok()?.workdir().map(|p| p.to_path_buf())\n}","tryCatchPattern":"match find_worktree_root(path) {\n    Ok(root) => root,\n    Err(e) if e.to_string().contains(\"bare repository\") => {\n        return Err(anyhow!(\"{} is a bare repo; pass a checkout instead\", path.display()))\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Point tools at a working-tree checkout, never at `repo.git` or a server-side bare repo","Check `git rev-parse --is-bare-repository` before invoking the API","Avoid passing a `.git` directory as the repo path","In CI, clone non-bare (or add a worktree) when a working directory is required"],"tags":["git","bare-repository","discovery"],"backgroundTag":"bare-repository-no-workdir","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}