{"record":{"id":"a6091ab4ebc802f8","repo":"affaan-m/ECC","slug":"git-restore-failed-for-stderr","errorCode":null,"errorMessage":"git restore failed for {}: {stderr}","messagePattern":"git restore failed for (.+?): (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"ecc2/src/worktree/mod.rs","lineNumber":353,"sourceCode":"        } else {\n            fs::remove_file(&target)\n                .with_context(|| format!(\"Failed to remove {}\", target.display()))?;\n        }\n        return Ok(());\n    }\n\n    let output = Command::new(\"git\")\n        .arg(\"-C\")\n        .arg(&worktree.path)\n        .args([\"restore\", \"--source=HEAD\", \"--staged\", \"--worktree\", \"--\"])\n        .arg(&entry.path)\n        .output()\n        .with_context(|| format!(\"Failed to reset {}\", entry.path))?;\n    if output.status.success() {\n        Ok(())\n    } else {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"git restore failed for {}: {stderr}\", entry.path);\n    }\n}\n\npub fn git_status_patch_view(\n    worktree: &WorktreeInfo,\n    entry: &GitStatusEntry,\n) -> Result<Option<GitStatusPatchView>> {\n    if entry.untracked {\n        return Ok(None);\n    }\n\n    let staged_patch =\n        git_diff_patch_text_for_paths(&worktree.path, &[\"--cached\"], &[entry.path.clone()])?;\n    let unstaged_patch = git_diff_patch_text_for_paths(&worktree.path, &[], &[entry.path.clone()])?;\n\n    let mut sections = Vec::new();\n    let mut hunks = Vec::new();\n","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/ecc2/src/worktree/mod.rs#L335-L371","documentation":"reset_path runs `git restore --source=HEAD --staged --worktree -- <path>` for tracked entries (untracked entries are deleted from the filesystem instead). The bail! re-throws git's stderr. Failure means git could not restore both the index and working tree to HEAD for that path.","triggerScenarios":"Resetting a path on a repository with no HEAD commit yet (unborn branch); a path that does not exist in HEAD (e.g. it was added in a different branch and never committed to base); `.git/index.lock` held by another process; corrupted loose object referenced by HEAD.","commonSituations":"Reset-all action on a freshly created worktree whose base branch has no commits; HEAD moved out from under the operation by a concurrent rebase/checkout; object database corrupted by disk-full or hardware error.","solutions":["Verify HEAD exists: `git -C <path> rev-parse --verify HEAD^{commit}` before calling reset_path.","Handle the no-HEAD case by falling back to `git rm --cached` plus filesystem delete.","Clear `.git/index.lock` if a previous git process was terminated.","Run `git fsck` in the repo if object corruption is suspected."],"exampleFix":"// before\nreset_path(&worktree, &entry)?;\n\n// after\nlet has_head = Command::new(\"git\")\n    .arg(\"-C\").arg(&worktree.path)\n    .args([\"rev-parse\", \"--verify\", \"HEAD^{commit}\"])\n    .output()?.status.success();\nif !has_head {\n    anyhow::bail!(\"cannot reset: HEAD has no commits yet\");\n}\nreset_path(&worktree, &entry)?;","handlingStrategy":"validation","validationCode":"fn has_head_commit(worktree: &WorktreeInfo) -> anyhow::Result<bool> {\n    let out = std::process::Command::new(\"git\")\n        .arg(\"-C\").arg(&worktree.path)\n        .args([\"rev-parse\", \"--verify\", \"HEAD^{commit}\"])\n        .output()?;\n    Ok(out.status.success())\n}\n\nif !has_head_commit(&worktree)? {\n    anyhow::bail!(\"cannot reset tracked path: HEAD has no commits yet\");\n}\nreset_path(&worktree, &entry)?;","typeGuard":null,"tryCatchPattern":"match reset_path(&worktree, &entry) {\n    Ok(()) => { /* refresh */ }\n    Err(e) => {\n        let m = format!(\"{e:#}\");\n        if m.contains(\"unknown revision\") || m.contains(\"HEAD\") {\n            // fall back: remove from index + delete file\n        } else {\n            return Err(e);\n        }\n    }\n}","preventionTips":["Disable 'reset to HEAD' on worktrees whose base branch has no commits.","Run `git fsck` periodically on long-lived worktrees to catch object corruption early.","Treat any reset_path failure after a concurrent rebase as a 'refresh and retry' signal."],"tags":["git","subprocess","worktree","restore","index"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}