{"record":{"id":"e751d3ead8ff1d63","repo":"can1357/oh-my-pi","slug":"patch-does-not-apply-message","errorCode":null,"errorMessage":"patch does not apply: {message}","messagePattern":"patch does not apply: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/pi-vcs/src/error.rs","lineNumber":56,"sourceCode":"\t/// auto-resolved to HEAD). Callers should skip and continue the range —\n\t/// replaces the historical `/the previous cherry-pick is now empty/` stderr\n\t/// regex.\n\t#[error(\"cherry-pick of {sha} is empty\")]\n\tEmptyCherryPick {\n\t\t/// The commit that collapsed to a no-op.\n\t\tsha: String,\n\t},\n\n\t/// A merge-style operation (cherry-pick, stash pop, 3-way apply) hit\n\t/// conflicting changes.\n\t#[error(\"merge conflict in {} file(s)\", paths.len())]\n\tConflict {\n\t\t/// Worktree-relative paths left in a conflicted state.\n\t\tpaths: Vec<String>,\n\t},\n\n\t/// A patch did not apply (context mismatch, missing file, malformed input).\n\t#[error(\"patch does not apply: {message}\")]\n\tPatchFailed {\n\t\t/// Human-readable reason, including the offending path when known.\n\t\tmessage: String,\n\t},\n\n\t/// A CLI-backed operation (push/fetch/clone, reftable fallback) exited\n\t/// non-zero. Carries the captured streams for user-facing error surfaces.\n\t#[error(\"{}\", crate::error::cli_message(command, *exit_code, stdout, stderr))]\n\tCli {\n\t\t/// Rendered command line (`git push --no-follow-tags …`).\n\t\tcommand:   String,\n\t\t/// Process exit code.\n\t\texit_code: i32,\n\t\t/// Captured stdout (may be truncated).\n\t\tstdout:    String,\n\t\t/// Captured stderr (may be truncated).\n\t\tstderr:    String,\n\t},","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/crates/pi-vcs/src/error.rs#L38-L74","documentation":"Error::PatchFailed is raised when a patch did not apply — context mismatch, missing target file, or malformed patch input. The message includes the human-readable reason and, when known, the offending path.","triggerScenarios":"Applying a diff whose context lines no longer match the worktree/HEAD content (file edited since the patch was made), applying a diff to a repo where a target file does not exist, or feeding truncated/corrupted diff text into the apply API.","commonSituations":"Patches emailed or copy-pasted and mangled (whitespace/line-ending drift, CRLF vs LF), diffs against a much older commit, applying AI-generated patches after the file changed, patch line numbers offset beyond fuzz tolerance.","solutions":["Regenerate the patch against the current HEAD (`git diff` from an up-to-date base) and retry.","Apply with more fuzz/context tolerance if the API allows it (equivalent of `git apply -C1 --3way`).","Check the file in `message` — restore/rename it if the patch expects a path that moved, or rewrite the patch paths.","If the patch text was mangled in transit, transfer it as a file/bytes (preserving LF) instead of pasting."],"exampleFix":"// before\nrepo.apply_patch(text)?; // context mismatch, hard error\n// after\nmatch repo.apply_patch(text) {\n    Err(Error::PatchFailed { message, .. }) if message.contains(\"context\") => {\n        repo.apply_patch_3way(text)?; // retry with 3-way merge\n    }\n    Err(e) => return Err(e),\n    Ok(()) => {}\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn is_patch_failed(err: &pi_vcs::Error) -> bool {\n    matches!(err, pi_vcs::Error::PatchFailed { .. })\n}","tryCatchPattern":"match repo.apply_patch(patch_text) {\n    Err(pi_vcs::Error::PatchFailed { message, .. }) => {\n        log::warn!(\"patch rejected: {message}; retrying with 3-way merge\");\n        repo.apply_patch_3way(patch_text)?;\n    }\n    other => other?,\n}","preventionTips":["Generate patches from the same commit the target worktree is based on; regenerate rather than reuse stale diffs.","Transfer patch text as files/bytes to avoid whitespace, line-ending, and truncation mangling.","Normalize line endings (LF) in both patch source and target files before applying.","Prefer 3-way apply (`--3way`) when the target may have drifted from the patch base."],"tags":["vcs","git","patch","apply"],"backgroundTag":"patch-apply-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}