alibaba/open-code-review · error

%w: %s

Error message

%w: %s

What it means

Stream (internal/gitcmd/runner.go:125) runs a git command, streams its stdout to a consume callback, and captures stderr in parallel. When the git process exits non-zero (waitErr) and something was written to stderr, it wraps the exit error with the stderr text via fmt.Errorf("%w: %s", ...). The wrap preserves the original *exec.ExitError for errors.Is/As inspection while surfacing git's own diagnostic message.

Source

Thrown at internal/gitcmd/runner.go:125

		return err
	}

	if err := cmd.Start(); err != nil {
		return err
	}

	consumeErr := consume(stdoutPipe)
	if consumeErr != nil {
		cmd.Process.Kill()
	}
	waitErr := cmd.Wait()

	if consumeErr != nil {
		return consumeErr
	}
	if waitErr != nil {
		if stderrBuf.Len() > 0 {
			return fmt.Errorf("%w: %s", waitErr, stderrBuf.String())
		}
		return waitErr
	}
	return nil
}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Read the appended stderr text — it is git's own message and names the failing revision or cause
  2. Verify the commit/blob identifier exists: git cat-file -e <rev> before calling Stream
  3. Run git fetch (or unshallow) if the revision may exist only on the remote
  4. Confirm repoDir passed to Stream is a valid git work tree

Example fix

// before
count, err := readLinesFromGitShow(ctx, repo, unknownHash)
// after
if err := repo.VerifyCommit(ctx, unknownHash); err != nil {
    return fmt.Errorf("commit %s not present locally, run git fetch: %w", unknownHash, err)
}
count, err := readLinesFromGitShow(ctx, repo, unknownHash)
Defensive patterns

Strategy: try-catch

Validate before calling

if err := exec.CommandContext(ctx, "git", "-C", repoDir, "cat-file", "-e", rev+"^{commit}").Run(); err != nil {
    return fmt.Errorf("revision %s not present in %s: run git fetch", rev, repoDir)
}

Type guard

var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
    // git-specific failure; exitErr.ExitCode() tells which
}

Try / catch

if err := runner.Stream(ctx, repoDir, consume, "show", rev); err != nil {
    var exitErr *exec.ExitError
    if errors.As(err, &exitErr) {
        log.Printf("git exited %d for %s: %v", exitErr.ExitCode(), rev, err)
    }
    return err
}

Prevention

When it happens

Trigger: Any Stream call (e.g. readLinesFromGitShow passing `git show <rev>`) where the git process exits non-zero after writing to stderr: bad revision, corrupt object, missing file at that rev, or the context being cancelled mid-run leaving partial stderr output.

Common situations: Requesting a commit hash that does not exist locally (shallow clone or after a force-push rewrote history); reading `git show` output in a repo whose objects were pruned; running in a directory that is not a git work tree (git prints 'fatal: not a git repository' to stderr).

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/65ab922dd1cda2bb. Report an issue: GitHub.