Hmbown/CodeWhale · error · anyhow::Error

git apply failed: {}

Error message

git apply failed: {}

What it means

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.

Source

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

    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(())
}

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

async fn run_mcp_command(
    config: &Config,
    workspace: &Path,

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Preview per-hunk detail: `git apply --check -v patch.diff`
  2. Confirm HEAD matches the patch's base; regenerate the diff if stale
  3. Attempt a 3-way fallback manually: `git apply -3 patch.diff`
  4. Apply from the repo root so diff paths resolve
  5. Strip copy/paste artifacts and unwrap soft-wrapped lines

Example fix

# before
codewhale apply --patch-file fix.diff   # git apply failed: error: patch failed: src/main.rs:42

# after
git apply --check -v fix.diff || git apply -3 fix.diff   # diagnose, then 3-way attempt
Defensive patterns

Strategy: validation

Validate before calling

cd "$(git rev-parse --show-toplevel)"
git apply --check --whitespace=nowarn "$PATCH" || { echo 'patch will not apply cleanly to HEAD'; exit 1; }

Try / catch

match run_apply(args) {
    Err(e) if e.to_string().contains("git apply failed") => { eprintln!("{e:#}"); /* regenerate patch against current HEAD, or `git apply -3` manually */ }
    other => other?,
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/309efc2cfc0e9c73. Report an issue: GitHub.