{"record":{"id":"309efc2cfc0e9c73","repo":"Hmbown/CodeWhale","slug":"git-apply-failed","errorCode":null,"errorMessage":"git apply failed: {}","messagePattern":"git apply failed: (.+?)","errorType":"error_code","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/lib.rs","lineNumber":9925,"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\n/// Maximum bytes read for a patch on stdin. Generous for large diffs;\n/// anything larger is not a patch.\nconst MAX_STDIN_PATCH_BYTES: u64 = 16 * 1024 * 1024;\n\nfn read_patch_from_stdin() -> Result<String> {\n    let 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\n        .take(MAX_STDIN_PATCH_BYTES + 1)\n        .read_to_string(&mut buffer)?;","sourceCodeStart":9907,"sourceCodeEnd":9943,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/lib.rs#L9907-L9943","documentation":"After writing the patch to a temp file, the code runs `git apply <tmp>` and, if git exits non-zero, wraps git's trimmed stderr in this error. The patch was syntactically rejected by git and nothing (or partially, depending on git's checks) was applied.","triggerScenarios":"`git apply` exits non-zero because the patch does not apply: wrong base commit, context lines mismatch, patch already applied, wrong line endings (CRLF), or paths in the patch outside the current repo.","commonSituations":"Applying a patch generated against a different branch state; copy-pasted patches whose whitespace was mangled; patches saved on Windows with CRLF endings; applying from the wrong working directory.","solutions":["Re-read the embedded git stderr — it names the failing file/hunk and reason (e.g. 'patch does not apply', 'corrupt patch')","Regenerate the patch against the current tree: `git diff HEAD > p.diff` on the source repo at the right base","Try tolerant application: `git apply --3way p.diff` manually, or fix line endings (`dos2unix p.diff`)"],"exampleFix":"// before\ncodewhale apply --patch p.diff\nerror: git apply failed: error: patch failed: src/lib.rs:100\n// after\ngit apply --3way p.diff   # resolve manually, or regenerate patch on current base","handlingStrategy":"try-catch","validationCode":"// dry-run the patch first\ngit apply --check p.diff   # non-zero exit predicts this error","typeGuard":null,"tryCatchPattern":"match apply_result { Err(e) if e.to_string().contains(\"git apply failed\") => { // fall back to `git apply --3way`, or regenerate patch on current base }, Ok(_) => {} }","preventionTips":["Dry-run with `git apply --check` before applying","Generate patches against the same base commit you apply on","Normalize line endings (dos2unix) on patches crossing platforms"],"tags":["cli","git","patch","subprocess"],"backgroundTag":"git-command-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}