{"record":{"id":"231cef4237d7ae44","repo":"openai/codex-plugin-cc","slug":"this-command-must-run-inside-a-git-repository","errorCode":null,"errorMessage":"This command must run inside a Git repository.","messagePattern":"This command must run inside a Git repository\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/codex/scripts/lib/git.mjs","lineNumber":85,"sourceCode":"}\n\nfunction buildBranchComparison(cwd, baseRef) {\n  const mergeBase = gitChecked(cwd, [\"merge-base\", \"HEAD\", baseRef]).stdout.trim();\n  return {\n    mergeBase,\n    commitRange: `${mergeBase}..HEAD`,\n    reviewRange: `${baseRef}...HEAD`\n  };\n}\n\nexport function ensureGitRepository(cwd) {\n  const result = git(cwd, [\"rev-parse\", \"--show-toplevel\"]);\n  const errorCode = result.error && \"code\" in result.error ? result.error.code : null;\n  if (errorCode === \"ENOENT\") {\n    throw new Error(\"git is not installed. Install Git and retry.\");\n  }\n  if (result.status !== 0) {\n    throw new Error(\"This command must run inside a Git repository.\");\n  }\n  return result.stdout.trim();\n}\n\nexport function getRepoRoot(cwd) {\n  return gitChecked(cwd, [\"rev-parse\", \"--show-toplevel\"]).stdout.trim();\n}\n\nexport function detectDefaultBranch(cwd) {\n  const symbolic = git(cwd, [\"symbolic-ref\", \"refs/remotes/origin/HEAD\"]);\n  if (symbolic.status === 0) {\n    const remoteHead = symbolic.stdout.trim();\n    if (remoteHead.startsWith(\"refs/remotes/origin/\")) {\n      return remoteHead.replace(\"refs/remotes/origin/\", \"\");\n    }\n  }\n\n  const candidates = [\"main\", \"master\", \"trunk\"];","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/openai/codex-plugin-cc/blob/db52e28f4d9ded852ab3942cea316258ae4ef346/plugins/codex/scripts/lib/git.mjs#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the command from inside a Git working tree (cd into the repo root).","Initialize a repository if appropriate: git init.","Verify with 'git rev-parse --show-toplevel' in the same cwd before retrying.","If using --scope working-tree or branch review, ensure the target directory is the repo or a subdirectory of it.","Repair or re-clone the repository if .git is corrupted."],"exampleFix":"// before\nensureGitRepository('/tmp/scratch') // throws: not inside a repo\n\n// after\n//   cd /path/to/repo   (or pass the repo cwd)\nensureGitRepository('/path/to/repo')\n// or initialize:\n//   git -C /tmp/scratch init\nensureGitRepository('/tmp/scratch')","handlingStrategy":"validation","validationCode":"import { execFileSync } from 'node:child_process';\n\nfunction insideGitRepo(cwd) {\n  try {\n    execFileSync('git', ['rev-parse', '--show-toplevel'], { cwd, stdio: 'ignore', shell: false });\n    return true;\n  } catch {\n    return false;\n  }\n}\nif (!insideGitRepo(cwd)) {\n  throw new Error(`Not inside a git repository: ${cwd}`);\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Run repository-relative commands from within a checked-out working tree.","git init new scratch projects before invoking review/diff commands.","Pre-check 'git rev-parse --show-toplevel' in the command layer.","Guard the UI so repo-dependent commands are disabled outside a repo."],"tags":["git","environment","repository","validation"],"backgroundTag":null,"analyzedSha":"db52e28f4d9ded852ab3942cea316258ae4ef346","analyzedAt":"2026-08-13T05:13:20.855Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}