{"record":{"id":"3ccb7686a55b59ba","repo":"affaan-m/ECC","slug":"git-add-failed-for-path-stderr","errorCode":null,"errorMessage":"git add failed for {path}: {stderr}","messagePattern":"git add failed for (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ecc2/src/worktree/mod.rs","lineNumber":304,"sourceCode":"    Ok(String::from_utf8_lossy(&output.stdout)\n        .lines()\n        .filter_map(parse_git_status_entry)\n        .collect())\n}\n\npub fn stage_path(worktree: &WorktreeInfo, path: &str) -> Result<()> {\n    let output = Command::new(\"git\")\n        .arg(\"-C\")\n        .arg(&worktree.path)\n        .args([\"add\", \"--\"])\n        .arg(path)\n        .output()\n        .with_context(|| format!(\"Failed to stage {}\", path))?;\n    if output.status.success() {\n        Ok(())\n    } else {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"git add failed for {path}: {stderr}\");\n    }\n}\n\npub fn unstage_path(worktree: &WorktreeInfo, path: &str) -> Result<()> {\n    let output = Command::new(\"git\")\n        .arg(\"-C\")\n        .arg(&worktree.path)\n        .args([\"reset\", \"HEAD\", \"--\"])\n        .arg(path)\n        .output()\n        .with_context(|| format!(\"Failed to unstage {}\", path))?;\n    if output.status.success() {\n        Ok(())\n    } else {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"git reset failed for {path}: {stderr}\");\n    }\n}","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/ecc2/src/worktree/mod.rs#L286-L322","documentation":"stage_path runs `git add -- <path>` inside the worktree and, on non-zero exit, includes both the offending path and git's stderr in the error. Untracked-but-ignored paths, pathspec errors, and index lock conflicts all land here.","triggerScenarios":"Calling stage_path with a path that does not exist in the worktree, is excluded by .gitignore, matches no pathspec, or while a git index.lock is held by another process.","commonSituations":"Staging a file that was deleted before the call; staging a file listed in .gitignore (git add refuses without -f); two concurrent session operations racing on the same worktree index; a path with characters that confuse the pathspec (e.g. leading '-').","solutions":["Check the file exists and is not gitignored before staging (`git -C <path> check-ignore <file>`; use force-add intentionally if needed)","Retry after the concurrent git operation completes if stderr mentions index.lock","Ensure the path is relative to the worktree root and points inside it","Quote/normalize the path so it cannot be misread as a git option"],"exampleFix":"// before\nstage_path(&worktree, \"build/output.log\")?; // ignored file\n// after\nif !is_ignored(&worktree, \"build/output.log\")? {\n    stage_path(&worktree, \"build/output.log\")?;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check the path before staging\nfn stageable(worktree: &WorktreeInfo, path: &str) -> bool {\n    worktree.path.join(path).exists()\n        && !Command::new(\"git\").arg(\"-C\").arg(&worktree.path)\n            .args([\"check-ignore\", path]).output()\n            .map(|o| o.status.success()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match stage_path(&worktree, path) {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"index.lock\") => {\n        wait_and_retry_stage(&worktree, path)?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Confirm the file exists and is not gitignored before staging; use git add -f deliberately when needed","Ensure paths are relative to the worktree root and normalized (no leading '-')","Serialize git index operations per worktree to avoid index.lock collisions","Re-read status after external changes instead of staging a cached path list"],"tags":["git","staging","rust","process"],"backgroundTag":"git-command-failed","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}