{"record":{"id":"02a8c3467feefa9e","repo":"xai-org/grok-build","slug":"invalid-git-file-format","errorCode":null,"errorMessage":"invalid .git file format: {}","messagePattern":"invalid \\.git file format: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/git/discovery.rs","lineNumber":21,"sourceCode":"use std::path::{Path, PathBuf};\n\nuse anyhow::{Context, Result};\n\n/// Find the worktree's git directory from its `.git` file.\n///\n/// Worktrees have a `.git` file (not directory) that points to the actual git dir.\n/// For regular repos, returns the `.git` directory.\npub(crate) fn find_worktree_git_dir(worktree_path: &Path) -> Result<PathBuf> {\n    let git_path = worktree_path.join(\".git\");\n\n    if git_path.is_file() {\n        // Worktree: .git is a file containing \"gitdir: <path>\"\n        let content = std::fs::read_to_string(&git_path)\n            .with_context(|| format!(\"failed to read .git file at {}\", git_path.display()))?;\n\n        let raw = content\n            .strip_prefix(\"gitdir: \")\n            .ok_or_else(|| anyhow::anyhow!(\"invalid .git file format: {}\", content.trim()))?\n            .trim();\n\n        // git may write a RELATIVE pointer (worktrees added with a relative\n        // path). Resolve it against the worktree dir — otherwise downstream\n        // index lookups join it against the CWD and break (mirrors\n        // `read_worktree_gitdir` in api.rs).\n        let raw_path = Path::new(raw);\n        let resolved = if raw_path.is_relative() {\n            worktree_path.join(raw_path)\n        } else {\n            raw_path.to_path_buf()\n        };\n        Ok(dunce::canonicalize(&resolved).unwrap_or(resolved))\n    } else if git_path.is_dir() {\n        // Regular repository\n        Ok(git_path)\n    } else {\n        anyhow::bail!(","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/git/discovery.rs#L3-L39","documentation":"Thrown by find_worktree_git_dir when a worktree's `.git` path is a file but its content does not start with `gitdir: `. A valid worktree `.git` file must be a single line of the form `gitdir: <path>` pointing at the real repository gitdir; anything else means the file is corrupt or not actually a worktree pointer.","triggerScenarios":"Calling find_worktree_git_dir (directly or via copy_git_index / update_index_stats) on a repo where `.git` is a malformed or truncated file, a placeholder file, or a file written by a tool that does not follow the git worktree convention.","commonSituations":"Manual editing or overwriting of `.git` with a path but missing the `gitdir: ` prefix; aborted worktree creation leaving a partial `.git`; syncing tools that flatten the file; Windows/CRLF or stray whitespace edge cases handled only partially by the trim.","solutions":["Inspect the `.git` file and restore the correct `gitdir: <path-to-main-repo>/.git/worktrees/<name>` line","Recreate the worktree with `git worktree add` to regenerate a valid `.git` file","Run `git worktree repair` / `git worktree prune` from the main repository to fix or clean stale worktree pointers","If the path is relative, ensure the resolved path exists relative to the worktree directory"],"exampleFix":"// before (.git file content)\n/main/repo/.git/worktrees/wt1\n// after\ngitdir: /main/repo/.git/worktrees/wt1","handlingStrategy":"validation","validationCode":"fn is_valid_gitfile(path: &Path) -> bool {\n    std::fs::read_to_string(path)\n        .map(|c| c.starts_with(\"gitdir: \"))\n        .unwrap_or(false)\n}\n// call find_worktree_git_dir only if git_path is a file && is_valid_gitfile(...)","typeGuard":"fn valid_gitdir_pointer(s: &str) -> Option<&str> {\n    s.strip_prefix(\"gitdir: \").map(str::trim)\n}","tryCatchPattern":"match find_worktree_git_dir(git_path) {\n    Ok(p) => p,\n    Err(e) if e.to_string().contains(\"invalid .git file format\") => {\n        // regenerate worktree or repair .git pointer\n        fallback_discover_with_gix(git_path)?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Never hand-edit a worktree `.git` file; use `git worktree add`","Validate the `gitdir: ` prefix before parsing in tooling that syncs `.git` files","Add a startup check that every expected worktree pointer resolves to an existing gitdir","Run `git worktree repair` after moving repositories or worktrees"],"tags":["git","worktree","file-format","discovery"],"backgroundTag":"invalid-git-worktree-pointer","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}