{"record":{"id":"309efc2cfc0e9c73","repo":"Hmbown/CodeWhale","slug":"git-apply-failed","errorCode":null,"errorMessage":"git apply failed: {}","messagePattern":"git apply failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/lib.rs","lineNumber":8436,"sourceCode":"    if patch.trim().is_empty() {\n        bail!(\"Patch is empty.\");\n    }\n\n    let mut tmp = NamedTempFile::new()?;\n    tmp.write_all(patch.as_bytes())?;\n    let tmp_path = tmp.path().to_path_buf();\n\n    let output = crate::dependencies::Git::command()\n        .ok_or_else(|| anyhow::anyhow!(\"git not found on PATH\"))?\n        .arg(\"apply\")\n        .arg(\"--whitespace=nowarn\")\n        .arg(&tmp_path)\n        .output()\n        .map_err(|e| anyhow::anyhow!(\"Failed to run git apply: {e}\"))?;\n\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        bail!(\"git apply failed: {}\", stderr.trim());\n    }\n    println!(\"Applied patch successfully.\");\n    Ok(())\n}\n\nfn read_patch_from_stdin() -> Result<String> {\n    let mut stdin = io::stdin();\n    if stdin.is_terminal() {\n        bail!(\"No patch file provided and stdin is empty.\");\n    }\n    let mut buffer = String::new();\n    stdin.read_to_string(&mut buffer)?;\n    Ok(buffer)\n}\n\nasync fn run_mcp_command(\n    config: &Config,\n    workspace: &Path,","sourceCodeStart":8418,"sourceCodeEnd":8454,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/lib.rs#L8418-L8454","documentation":"run_apply writes the patch to a temp file and runs `git apply --whitespace=nowarn <tmpfile>`; on non-zero exit it bails with git's stderr. git apply is strict and atomic: the diff's context lines, line numbers, and file paths must exactly match the current work tree, and it performs no merge or commit.","triggerScenarios":"Patch generated against a different HEAD than the current one; context lines drifted since the diff was made; the patch is already applied; running from a subdirectory so a/ b/ prefixes do not resolve; diff mangled by copy/paste or an LLM (wrapped lines, smart quotes, broken hunk headers); binary patch without its literal payload.","commonSituations":"Applying model-generated diffs whose line numbers drifted; applying the same patch twice; Windows CRLF conversion rewriting files under the patch; wrong branch checked out.","solutions":["Preview per-hunk detail: `git apply --check -v patch.diff`","Confirm HEAD matches the patch's base; regenerate the diff if stale","Attempt a 3-way fallback manually: `git apply -3 patch.diff`","Apply from the repo root so diff paths resolve","Strip copy/paste artifacts and unwrap soft-wrapped lines"],"exampleFix":"# before\ncodewhale apply --patch-file fix.diff   # git apply failed: error: patch failed: src/main.rs:42\n\n# after\ngit apply --check -v fix.diff || git apply -3 fix.diff   # diagnose, then 3-way attempt","handlingStrategy":"validation","validationCode":"cd \"$(git rev-parse --show-toplevel)\"\ngit apply --check --whitespace=nowarn \"$PATCH\" || { echo 'patch will not apply cleanly to HEAD'; exit 1; }","typeGuard":null,"tryCatchPattern":"match run_apply(args) {\n    Err(e) if e.to_string().contains(\"git apply failed\") => { eprintln!(\"{e:#}\"); /* regenerate patch against current HEAD, or `git apply -3` manually */ }\n    other => other?,\n}","preventionTips":["Always run `git apply --check` as a dry run before the real apply","Generate patches against the exact HEAD you will apply them to","Apply from the repo root; avoid applying the same patch twice"],"tags":["git","patch","git-apply","merge-conflict"],"backgroundTag":"git-apply-conflict","analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}