alibaba/open-code-review · error

%s value %q is not a valid commit ref

Error message

%s value %q is not a valid commit ref

What it means

Same check as the message-bearing variant: when `git rev-parse --verify --end-of-options <ref>^{commit}` fails but git produced no output, validateReviewRefs returns this shorter error. It indicates an unresolvable ref with no diagnostic from git (e.g. command-level failure, empty output).

Source

Thrown at cmd/opencodereview/review_cmd.go:482

		ref  string
	}{
		{"--from", opts.from},
		{"--to", opts.to},
		{"--commit", opts.commit},
	}
	for _, item := range refs {
		if item.ref == "" {
			continue
		}
		if strings.HasPrefix(item.ref, "-") {
			return fmt.Errorf("%s value %q is not a valid git ref: refs must not start with '-'", item.flag, item.ref)
		}
		if out, err := runGitCmd(repoDir, "rev-parse", "--verify", "--end-of-options", item.ref+"^{commit}"); err != nil {
			msg := strings.TrimSpace(string(out))
			if msg != "" {
				return fmt.Errorf("%s value %q is not a valid commit ref: %s", item.flag, item.ref, msg)
			}
			return fmt.Errorf("%s value %q is not a valid commit ref", item.flag, item.ref)
		}
	}
	return nil
}

func runPreviewContext(ctx context.Context, cc *commonContext, opts reviewOptions, out io.Writer) error {
	preview, err := agent.Preview(ctx, agent.Args{
		RepoDir:    cc.RepoDir,
		From:       opts.from,
		To:         opts.to,
		Commit:     opts.commit,
		FileFilter: cc.FileFilter,
		GitRunner:  cc.GitRunner,
	})
	if err != nil {
		return fmt.Errorf("preview failed: %w", err)
	}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Run `git rev-parse --verify <value>^{commit}` manually to see git's own diagnosis.
  2. Replace the ref with a plain commit SHA, which avoids ref-expression parsing entirely.
  3. Inspect the repo's refs (`git show-ref`) for corruption and run `git fsck` if suspect.

Example fix

// before
ocr review --commit HEAD^3^{tree}
// after
ocr review --commit $(git rev-parse HEAD~3)
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("git", "rev-parse", "--verify", ref+"^{commit}").CombinedOutput()
if err != nil || len(bytes.TrimSpace(out)) == 0 {
    return fmt.Errorf("ref %q unusable, use a full commit SHA instead", ref)
}

Try / catch

if err := validateReviewRefs(repoDir, opts); err != nil {
    // no git message available: fall back to a plain SHA
    return fmt.Errorf("use `git rev-parse <ref>` to obtain a SHA and retry: %w", err)
}

Prevention

When it happens

Trigger: --from/--to/--commit value that fails rev-parse with empty stderr/stdout — unusual git failures, corrupted refs, or a git runner that swallows output.

Common situations: Corrupted or pruned .git/refs; custom GitRunner in tests not returning output; exotic ref expressions git silently rejects.

Related errors


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