Hmbown/CodeWhale · error · anyhow::Error

git apply failed

Error message

git apply failed: {}

What it means

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.

Solutions

  1. Re-read the embedded git stderr — it names the failing file/hunk and reason (e.g. 'patch does not apply', 'corrupt patch')
  2. Regenerate the patch against the current tree: `git diff HEAD > p.diff` on the source repo at the right base
  3. Try tolerant application: `git apply --3way p.diff` manually, or fix line endings (`dos2unix p.diff`)

Example fix

// before
codewhale apply --patch p.diff
error: git apply failed: error: patch failed: src/lib.rs:100
// after
git apply --3way p.diff   # resolve manually, or regenerate patch on current base
Defensive patterns

Strategy: try-catch

Validate before calling

// dry-run the patch first
git apply --check p.diff   # non-zero exit predicts this error

Try / catch

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(_) => {} }

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/309efc2cfc0e9c73. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/lib.rs:9925

    if patch.trim().is_empty() {
        bail!("Patch is empty.");
    }

    let mut tmp = NamedTempFile::new()?;
    tmp.write_all(patch.as_bytes())?;
    let tmp_path = tmp.path().to_path_buf();

    let output = crate::dependencies::Git::command()
        .ok_or_else(|| anyhow::anyhow!("git not found on PATH"))?
        .arg("apply")
        .arg("--whitespace=nowarn")
        .arg(&tmp_path)
        .output()
        .map_err(|e| anyhow::anyhow!("Failed to run git apply: {e}"))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("git apply failed: {}", stderr.trim());
    }
    println!("Applied patch successfully.");
    Ok(())
}

/// Maximum bytes read for a patch on stdin. Generous for large diffs;
/// anything larger is not a patch.
const MAX_STDIN_PATCH_BYTES: u64 = 16 * 1024 * 1024;

fn read_patch_from_stdin() -> Result<String> {
    let stdin = io::stdin();
    if stdin.is_terminal() {
        bail!("No patch file provided and stdin is empty.");
    }
    let mut buffer = String::new();
    stdin
        .take(MAX_STDIN_PATCH_BYTES + 1)
        .read_to_string(&mut buffer)?;

View on GitHub (pinned to 73e0f67d83)