openai/codex-plugin-cc · error · Error

This command must run inside a Git repository.

Error message

This command must run inside a Git repository.

What it means

Thrown by ensureGitRepository when the 'git' binary runs successfully (no ENOENT) but 'git rev-parse --show-toplevel' exits with a non-zero status. A non-zero exit from rev-parse means the cwd is not inside a Git working tree, so repository-relative operations (review, diff, branch context) cannot proceed.

Source

Thrown at plugins/codex/scripts/lib/git.mjs:85

}

function buildBranchComparison(cwd, baseRef) {
  const mergeBase = gitChecked(cwd, ["merge-base", "HEAD", baseRef]).stdout.trim();
  return {
    mergeBase,
    commitRange: `${mergeBase}..HEAD`,
    reviewRange: `${baseRef}...HEAD`
  };
}

export function ensureGitRepository(cwd) {
  const result = git(cwd, ["rev-parse", "--show-toplevel"]);
  const errorCode = result.error && "code" in result.error ? result.error.code : null;
  if (errorCode === "ENOENT") {
    throw new Error("git is not installed. Install Git and retry.");
  }
  if (result.status !== 0) {
    throw new Error("This command must run inside a Git repository.");
  }
  return result.stdout.trim();
}

export function getRepoRoot(cwd) {
  return gitChecked(cwd, ["rev-parse", "--show-toplevel"]).stdout.trim();
}

export function detectDefaultBranch(cwd) {
  const symbolic = git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"]);
  if (symbolic.status === 0) {
    const remoteHead = symbolic.stdout.trim();
    if (remoteHead.startsWith("refs/remotes/origin/")) {
      return remoteHead.replace("refs/remotes/origin/", "");
    }
  }

  const candidates = ["main", "master", "trunk"];

View on GitHub (pinned to db52e28f4d)

Solutions

  1. Run the command from inside a Git working tree (cd into the repo root).
  2. Initialize a repository if appropriate: git init.
  3. Verify with 'git rev-parse --show-toplevel' in the same cwd before retrying.
  4. If using --scope working-tree or branch review, ensure the target directory is the repo or a subdirectory of it.
  5. Repair or re-clone the repository if .git is corrupted.

Example fix

// before
ensureGitRepository('/tmp/scratch') // throws: not inside a repo

// after
//   cd /path/to/repo   (or pass the repo cwd)
ensureGitRepository('/path/to/repo')
// or initialize:
//   git -C /tmp/scratch init
ensureGitRepository('/tmp/scratch')
Defensive patterns

Strategy: validation

Validate before calling

import { execFileSync } from 'node:child_process';

function insideGitRepo(cwd) {
  try {
    execFileSync('git', ['rev-parse', '--show-toplevel'], { cwd, stdio: 'ignore', shell: false });
    return true;
  } catch {
    return false;
  }
}
if (!insideGitRepo(cwd)) {
  throw new Error(`Not inside a git repository: ${cwd}`);
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling ensureGitRepository(cwd) (or resolveReviewTarget) with cwd pointing at a directory that is not part of any Git repository. rev-parse --show-toplevel returns non-zero because there is no .git ancestor.

Common situations: Running a review/task command from a scratch directory, /tmp, or a fresh project that was never 'git init'-ed. cwd was changed to outside the repo by a wrapper. The repo's .git was deleted or is corrupted. Bare repo checked out oddly so no working tree is detected.

Related errors


AI-assisted analysis of openai/codex-plugin-cc@db52e28f4d (2026-08-13). Data as JSON: /api/errors/231cef4237d7ae44. Report an issue: GitHub.