{"record":{"id":"d9a8e3ac3e8818d8","repo":"xai-org/grok-build","slug":"cannot-read-files-from-bare-repository","errorCode":null,"errorMessage":"cannot read files from bare repository","messagePattern":"cannot read files from bare repository","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-workspace/src/session/git.rs","lineNumber":1535,"sourceCode":"        cli_err.context(format!(\n            \"CLI fallback also failed (libgit2 error: {libgit2_err})\"\n        ))\n    })\n}\npub async fn read_files(\n    git_root: &Path,\n    paths: &[String],\n    version: &str,\n) -> Result<GitReadFilesData> {\n    let start = std::time::Instant::now();\n    let cwd = git_root.to_path_buf();\n    let paths = paths.to_vec();\n    let version = version.to_string();\n    let result = tokio::task::spawn_blocking(move || {\n        let repo = Repository::discover(&cwd)?;\n        let git_root = repo\n            .workdir()\n            .ok_or_else(|| anyhow::anyhow!(\"cannot read files from bare repository\"))?;\n        let paths: Vec<String> = paths\n            .into_iter()\n            .map(|p| {\n                if Path::new(&p).is_absolute() {\n                    Path::new(&p)\n                        .strip_prefix(git_root)\n                        .map(|rel| rel.to_string_lossy().to_string())\n                        .map_err(|_| {\n                            anyhow::anyhow!(\n                                \"path '{}' is not within git repository '{}'\",\n                                p,\n                                git_root.display()\n                            )\n                        })\n                } else {\n                    Ok(p)\n                }\n            })","sourceCodeStart":1517,"sourceCodeEnd":1553,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-workspace/src/session/git.rs#L1517-L1553","documentation":"This error is thrown by a file-reading helper in the workspace git module. After `Repository::discover` opens the repo containing `cwd`, the code calls `repo.workdir()` and errors if it returns None, which happens only when the discovered repository is bare (has no working tree). Since the function's purpose is to map absolute file paths into repo-relative paths under the working directory, a bare repo cannot satisfy the operation.","triggerScenarios":"Calling the read-files API with `cwd` inside (or pointing at) a bare repository, e.g. a `repo.git` server-side clone, a `--bare` clone, or a directory whose discover walk reaches a bare repo before a non-bare one.","commonSituations":"Pointing tooling at a server-side bare clone on a remote host; cloning with `git clone --bare` for CI artifact reads; `GIT_DIR` env pointing at a bare repo so discover resolves to it instead of the intended worktree.","solutions":["Run the API with `cwd` set inside a non-bare checkout of the repository","Clone the repository normally (without --bare) and use that directory","If you must read from a bare repo, use plumbing commands (e.g. `git show HEAD:path`) instead of working-tree file reads","Check `git rev-parse --is-bare-repository` in the target directory before calling"],"exampleFix":"// before\nlet cwd = Path::new(\"/srv/repo.git\"); // bare\n// after\nlet cwd = Path::new(\"/srv/repo-checkout\"); // non-bare clone","handlingStrategy":"validation","validationCode":"let out = std::process::Command::new(\"git\").args([\"rev-parse\",\"--is-bare-repository\"]).current_dir(&cwd).output()?;\nif String::from_utf8_lossy(&out.stdout).trim() == \"true\" {\n    return Err(\"cwd is a bare repository; use a non-bare checkout\".into());\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always point tooling at a normal (non-bare) clone","Verify `git rev-parse --is-bare-repository` returns false before file operations","Avoid --bare/--mirror clones for workflows that read working-tree files"],"tags":["git","bare-repository","filesystem"],"backgroundTag":"bare-repository-no-worktree","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}