{"record":{"id":"cc0cc850ac26d2ed","repo":"xai-org/grok-build","slug":"failed-to-load-git-index-e","errorCode":null,"errorMessage":"failed to load git index: {e}","messagePattern":"failed to load git index: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/lib.rs","lineNumber":113,"sourceCode":"}\npub fn local_clean_artifacts(_dest: &std::path::Path) -> anyhow::Result<CleanArtifactsReply> {\n    anyhow::bail!(\"not available in this build\")\n}\npub use sync::{SourceDirtyState, SyncReport, WorktreeSync, collect_source_dirty_state};\n#[cfg(target_os = \"linux\")]\npub use worktree::execute::cleanup_snapshot_git_state;\npub use worktree::{STRATEGY_GROVE_FUSE, STRATEGY_GROVE_NFS, STRATEGY_NFS, is_grove_strategy};\n/// Count the number of tracked files in a git repository's index.\n///\n/// Reads the index header via `gix`, which contains the entry count — this\n/// is an O(1) read (no directory walk). Useful for deciding whether a repo\n/// is large enough to benefit from worktree pooling.\npub fn count_tracked_files(repo_path: &std::path::Path) -> anyhow::Result<usize> {\n    let repo = gix::discover(repo_path)\n        .map_err(|e| anyhow::anyhow!(\"failed to discover git repo: {e}\"))?;\n    let index = repo\n        .index_or_load_from_head()\n        .map_err(|e| anyhow::anyhow!(\"failed to load git index: {e}\"))?;\n    Ok(index.entries().len())\n}\n","sourceCodeStart":95,"sourceCodeEnd":116,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/lib.rs#L95-L116","documentation":"count_tracked_files loads the git index via `index_or_load_from_head()` after discovery succeeds; failure to load or parse the index is mapped to this error. Since it falls back to the HEAD tree's index, this usually indicates a corrupt or unreadable index.","triggerScenarios":"Calling count_tracked_files on a repo whose `.git/index` is corrupt, truncated, or has an unsupported version, or where HEAD cannot be resolved to build the fallback index (e.g. unborn HEAD with no index file).","commonSituations":"Crash or power loss mid `git add` leaving a truncated index; concurrent index writers; empty freshly-`init`-ed repos with no commits and no index; index version newer than the reader supports.","solutions":["Run `git status` or `git read-tree HEAD` to verify/repair the index","Delete `.git/index` and restore it with `git reset --mixed HEAD` (regenerates from HEAD)","Ensure the repository has at least one commit or a valid index file","Check for concurrent writers locking the index (remove stale `.git/index.lock` if no git process is running)"],"exampleFix":"// shell repair\nrm .git/index && git reset --mixed HEAD\n// then retry\ncount_tracked_files(&repo_path)?;","handlingStrategy":"retry","validationCode":"fn index_loadable(path: &Path) -> bool {\n    std::process::Command::new(\"git\").arg(\"-C\").arg(path)\n        .args([\"rev-parse\", \"--verify\", \"HEAD\"])\n        .output().map(|o| o.status.success()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"let count = loop {\n    match count_tracked_files(repo_path) {\n        Ok(n) => break n,\n        Err(e) if e.to_string().contains(\"failed to load git index\") && retries < 2 => {\n            retries += 1;\n            std::thread::sleep(Duration::from_millis(100)); // wait out concurrent index writer\n        }\n        Err(e) => return Err(e),\n    }\n};","preventionTips":["Avoid running concurrent index-mutating git operations while counting","After crashes, repair with `rm .git/index && git reset --mixed HEAD`","Remove stale `.git/index.lock` only when no git process is running","Ensure the repo has commits or a valid index before querying"],"tags":["git","index","corruption"],"backgroundTag":"git-index-load-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}