alibaba/open-code-review · error

%s is not a git repository, code review requires a valid git

Error message

%s is not a git repository, code review requires a valid git repository

What it means

requireGitRepo runs `git rev-parse --git-dir` in the given directory; if the command fails or returns empty output, the directory is not inside a git repository and this error is thrown. Code review (diffing) fundamentally requires a git repo, so the tool refuses to proceed.

Source

Thrown at cmd/opencodereview/review_cmd.go:454

// resolveRepoDir resolves the repo dir for `ocr rules check`. It delegates to
// resolveWorkingDir(requireGit=true) so it anchors at the git top-level just
// like the review path — keeping rule resolution consistent when run from a
// monorepo subdirectory (#287).
func resolveRepoDir(input string) (string, error) {
	absPath, _, err := resolveWorkingDir(input, true)
	return absPath, err
}

// requireGitRepo validates that the given directory is part of a git repository.
func requireGitRepo(dir string) error {
	repoDir, err := filepath.Abs(dir)
	if err != nil {
		return fmt.Errorf("resolve path: %w", err)
	}
	out, err := runGitCmd(repoDir, "rev-parse", "--git-dir")
	if err != nil || len(out) == 0 {
		return fmt.Errorf("%s is not a git repository, code review requires a valid git repository", repoDir)
	}
	return nil
}

// validateReviewRefs rejects ref-option injection (#112): any --from/--to/
// --commit value must be a real commit ref and must not start with '-'.
func validateReviewRefs(repoDir string, opts reviewOptions) error {
	refs := []struct {
		flag string
		ref  string
	}{
		{"--from", opts.from},
		{"--to", opts.to},
		{"--commit", opts.commit},
	}
	for _, item := range refs {
		if item.ref == "" {
			continue

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Run `git init` in the directory if it should be a repository.
  2. Point --repo (or cd) at a directory that is actually inside a git work tree.
  3. Confirm git is installed and on PATH (`git --version`) — a missing binary also makes the probe fail.
  4. If the code came from an archive, clone it with git instead of downloading a snapshot.

Example fix

// before
cd /tmp/source-snapshot && ocr review --commit HEAD
// after
git clone https://example.com/repo && cd repo && ocr review --commit HEAD
Defensive patterns

Strategy: validation

Validate before calling

func inGitRepo(dir string) bool {
    out, err := exec.Command("git", "-C", dir, "rev-parse", "--git-dir").Output()
    return err == nil && len(bytes.TrimSpace(out)) > 0
}

Try / catch

if err := requireGitRepo(dir); err != nil {
    fmt.Fprintf(os.Stderr, "hint: run inside a git work tree or pass --repo <path>\n")
    os.Exit(1)
}

Prevention

When it happens

Trigger: Executing any review/rules command whose --repo or working directory is not inside a git work tree (no .git, bare parent, or git not installed so the probe fails).

Common situations: Running `ocr review` in a plain folder downloaded as a zip/tarball; pointing --repo at a non-repo subdirectory; missing git binary in a CI container.

Related errors


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