Hmbown/CodeWhale · error · anyhow::Error

git diff failed

Error message

git diff failed: {}

What it means

The local code-review path runs `git diff` as a subprocess to collect the diff for review. If git exits non-zero, its trimmed stderr is wrapped in this error. It indicates the diff could not be produced in the current working tree.

Solutions

  1. Run `git diff` manually in the target directory to see the underlying git error
  2. Confirm the working directory is inside a git repository (`git rev-parse --is-inside-work-tree`)
  3. Remove a stale `.git/index.lock` if present, then retry

Example fix

// before
$ cd ~/not-a-repo && codewhale review --path .
error: git diff failed: fatal: not a git repository
// after
$ cd ~/my-repo && codewhale review --path .
Defensive patterns

Strategy: validation

Validate before calling

if !std::path::Path::new(".git").exists() && std::process::Command::new("git").args(["rev-parse","--is-inside-work-tree"]).output().map(|o| !o.status.success()).unwrap_or(true) { /* not a repo — bail early */ }

Prevention

When it happens

Trigger: `git diff` exits non-zero: not inside a git repository, corrupted index/lock file, invalid path arguments passed via --path, or git object store issues.

Common situations: Running the review command outside a repo (or in a subdirectory of a non-repo); leftover `.git/index.lock` from a crashed git process; passing a --path that git's pathspec parser rejects.

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/1e92d94fc154cef3. Report an issue: GitHub.

Appendix: source

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

        // Review repository content without executing its diff drivers.
        cmd.current_dir(workspace)
            .args(["diff", "--no-ext-diff", "--no-textconv"]);
        if args.staged {
            cmd.arg("--cached");
        }
        if let Some(base) = &args.base {
            cmd.arg(format!("{base}...HEAD"));
        }
        if let Some(path) = &args.path {
            cmd.arg("--").arg(path);
        }

        let output = cmd
            .output()
            .map_err(|e| anyhow::anyhow!("Failed to run git diff. Is git installed? ({e})"))?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git diff failed: {}", stderr.trim());
        }
        String::from_utf8_lossy(&output.stdout).to_string()
    };
    if args.pr.is_none() {
        ensure_local_review_diff_fits(&diff, args.max_chars)?;
    }
    Ok(diff)
}

fn ensure_local_review_diff_fits(diff: &str, max_chars: usize) -> Result<()> {
    let chars = diff.chars().count();
    if chars > max_chars {
        bail!(
            "Complete local diff requires {chars} characters, exceeding the review limit of {max_chars}. No review was run and no receipt was written or accepted. Select an explicit --path scope, or increase --max-chars only if the selected model can accept the complete input."
        );
    }
    Ok(())
}

View on GitHub (pinned to 73e0f67d83)