alibaba/open-code-review · error

git show %s:%s: %w

Error message

git show %s:%s: %w

What it means

Wraps a failure from `git show <Ref>:<path>` executed through the injected gitcmd.Runner in readFromGitShow (internal/tool/filereader.go:126). FileReader.Read uses this in ModeRange/ModeCommit to fetch file content at a specific ref. The wrapped error typically comes from git itself: unknown ref, path not present at that ref, or a non-repository working directory. The Runner branch runs when fr.Runner != nil.

Source

Thrown at internal/tool/filereader.go:126

			return fullPath, nil
		}
		return "", fmt.Errorf("resolve file %q: %w", path, err)
	}
	if !pathutil.WithinBase(repoRoot, resolvedPath) {
		return "", fmt.Errorf("file path %q is outside repository", path)
	}
	return resolvedPath, nil
}

func (fr *FileReader) readFromGitShow(parentCtx context.Context, path string) (string, error) {
	ctx, cancel := context.WithTimeout(parentCtx, 30*time.Second)
	defer cancel()

	args := []string{"-c", "core.quotepath=false", "show", "--end-of-options", fr.Ref + ":" + path}
	if fr.Runner != nil {
		output, err := fr.Runner.Output(ctx, fr.RepoDir, args...)
		if err != nil {
			return "", fmt.Errorf("git show %s:%s: %w", fr.Ref, path, err)
		}
		return string(output), nil
	}

	cmd := exec.CommandContext(ctx, "git", args...)
	cmd.Dir = fr.RepoDir
	output, err := cmd.Output()
	if err != nil {
		return "", fmt.Errorf("git show %s:%s: %w", fr.Ref, path, err)
	}
	return string(output), nil
}

// ReadLines returns a window of lines from the file plus the total line count.
// startLine is 1-based; maxLines is the maximum number of lines to collect.
func (fr *FileReader) ReadLines(ctx context.Context, path string, startLine, maxLines int) ([]string, int, error) {
	switch fr.Mode {
	case ModeWorkspace:

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Verify the ref exists: run `git cat-file -t <ref>` in RepoDir; fetch missing refs with `git fetch origin <ref>`.
  2. Confirm the file exists at that ref: `git show <ref>:<path>` manually; if not, use the correct historical path or a different ref.
  3. Ensure FileReader.RepoDir points at the root of a valid git repository.
  4. If the context timed out, check for a hung git (credential prompt, lock file) and increase the timeout or pre-authenticate.
  5. Check Runner configuration (git binary path, env) if the same command works manually.

Example fix

// before: ref not fetched locally
fr := &FileReader{Ref: "origin/feature", Mode: ModeRange, RepoDir: dir, Runner: r}
// after: fetch the ref first
exec.Command("git", "-C", dir, "fetch", "origin", "feature").Run()
Defensive patterns

Strategy: validation

Validate before calling

// validate ref and path before calling Read
if err := exec.Command("git", "-C", repoDir, "cat-file", "-e", ref+":"+path).Run(); err != nil {
    // file does not exist at ref: skip or fix ref/path beforehand
}
if err := exec.Command("git", "-C", repoDir, "rev-parse", "--verify", ref+"^{commit}").Run(); err != nil {
    // ref unknown: fetch it first
}

Type guard

func isGitShowErr(err error) bool {
    return err != nil && strings.HasPrefix(err.Error(), "git show ")
}

Try / catch

content, err := fr.Read(ctx, path)
if err != nil {
    var ee *exec.ExitError
    if isGitShowErr(err) && errors.As(err, &ee) {
        // ref/path missing at ref: degrade gracefully
        return placeholderForMissingFile(path)
    }
    return err
}

Prevention

When it happens

Trigger: FileReader.Read(ctx, path) with ModeRange or ModeCommit and a non-nil Runner, when the git subprocess exits non-zero: bad ref (e.g. wrong --to or --commit value), path that did not exist at that ref, RepoDir not a git repo, context timeout (30s), or git binary missing/unusable through the Runner.

Common situations: Passing a short branch name that does not exist locally after a shallow/partial clone; reading a file added in the working tree but absent at the reviewed commit; reviewing a commit that was garbage-collected or belongs to another remote not fetched; running the tool outside a git repository.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/d3cd24d36ae5531b. Report an issue: GitHub.