alibaba/open-code-review · error

%s failed: %w: %s

Error message

%s failed: %w: %s

What it means

gitFailure's richer variant: when git did write diagnostics to stderr, the error is '<op> failed: <wrapped err>: <diagnostic tail>'. Diagnostics longer than gitDiagLimit are truncated to the tail (git's die() message is last), and invalid UTF-8 at the cut point is trimmed. This is the most informative git diff failure path.

Source

Thrown at internal/diff/git.go:724

func gitFailure(op, stderr string, err error) error {
	diag := strings.TrimSpace(stderr)
	if diag == "" {
		return fmt.Errorf("%s failed: %w", op, err)
	}
	if len(diag) > gitDiagLimit {
		// Keep the tail: die() exits the process, so the fatal git ends on is
		// the last thing it writes, behind any warnings that preceded it.
		diag = diag[len(diag)-gitDiagLimit:]
		// Cutting by bytes can land mid-rune. Git speaks the user's locale,
		// so this is not hypothetical — #972 came from a Japanese-language
		// Windows install. Drop the partial leading rune rather than emit
		// invalid UTF-8.
		for len(diag) > 0 && !utf8.RuneStart(diag[0]) {
			diag = diag[1:]
		}
		diag = "..." + diag
	}
	return fmt.Errorf("%s failed: %w: %s", op, err, diag)
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Read the appended git diagnostic in the error — it states the concrete cause (unknown revision, not a repo, etc.)
  2. Fix the specific git issue the diagnostic names (correct the ref, run inside a repo, repair the external diff tool)
  3. Re-run the equivalent git command manually to confirm the fix
  4. Verify repo integrity (git fsck) if diagnostics suggest corruption

Example fix

// before
p.from = "mainn" // typo -> 'git diff failed: ...: unknown revision mainn'
// after
p.from = "main"
Defensive patterns

Strategy: try-catch

Validate before calling

// verify refs and repo before diffing
for _, ref := range []string{from, to} {
    if err := exec.Command("git", "-C", dir, "rev-parse", "--verify", ref).Run(); err != nil {
        return fmt.Errorf("bad ref %s", ref)
    }
}

Try / catch

d, err := differ.GetDiff(ctx, p)
if err != nil {
    if strings.Contains(err.Error(), "not a git repository") { return errRepoMissing }
    if strings.Contains(err.Error(), "unknown revision") { return errBadRef }
    return err // diagnostic tail already included
}

Prevention

When it happens

Trigger: Any runGitSplit call in GetDiff failing with non-empty stderr — bad revision names, permission errors, unsupported options, corrupt repo, external diff tool failures.

Common situations: Typo'd commit/ref in from/to; diffing in a directory that is not a git repo; core.externalDiff tool broken or missing; file permission errors during diff; locale-dependent messages (stderr is passed raw to preserve them).

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 alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/8c8d5e03a231d7b0. Report an issue: GitHub.