{"record":{"id":"753960e2599c6a8e","repo":"openai/codex-plugin-cc","slug":"unable-to-detect-the-repository-default-branch-pa","errorCode":null,"errorMessage":"Unable to detect the repository default branch. Pass --base <ref> or use --scope working-tree.","messagePattern":"Unable to detect the repository default branch\\. Pass --base <ref> or use --scope working-tree\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/codex/scripts/lib/git.mjs","lineNumber":115,"sourceCode":"    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\"];\n  for (const candidate of candidates) {\n    const local = git(cwd, [\"show-ref\", \"--verify\", \"--quiet\", `refs/heads/${candidate}`]);\n    if (local.status === 0) {\n      return candidate;\n    }\n    const remote = git(cwd, [\"show-ref\", \"--verify\", \"--quiet\", `refs/remotes/origin/${candidate}`]);\n    if (remote.status === 0) {\n      return `origin/${candidate}`;\n    }\n  }\n\n  throw new Error(\"Unable to detect the repository default branch. Pass --base <ref> or use --scope working-tree.\");\n}\n\nexport function getCurrentBranch(cwd) {\n  return gitChecked(cwd, [\"branch\", \"--show-current\"]).stdout.trim() || \"HEAD\";\n}\n\nexport function getWorkingTreeState(cwd) {\n  const staged = gitChecked(cwd, [\"diff\", \"--cached\", \"--name-only\"]).stdout.trim().split(\"\\n\").filter(Boolean);\n  const unstaged = gitChecked(cwd, [\"diff\", \"--name-only\"]).stdout.trim().split(\"\\n\").filter(Boolean);\n  const untracked = gitChecked(cwd, [\"ls-files\", \"--others\", \"--exclude-standard\"]).stdout.trim().split(\"\\n\").filter(Boolean);\n\n  return {\n    staged,\n    unstaged,\n    untracked,\n    isDirty: staged.length > 0 || unstaged.length > 0 || untracked.length > 0\n  };\n}","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/openai/codex-plugin-cc/blob/db52e28f4d9ded852ab3942cea316258ae4ef346/plugins/codex/scripts/lib/git.mjs#L97-L133","documentation":"Thrown by detectDefaultBranch() when it cannot determine the repository's default branch. It first tries `git symbolic-ref refs/remotes/origin/HEAD`, then probes a fixed list of candidate branch names (main, master, trunk) against both local refs and origin remotes; if none resolve, it gives up. This is a heuristic guess, not a guarantee — repos with non-standard branch names (develop, default, release) defeat it.","triggerScenarios":"Calling resolveReviewTarget(cwd, {scope:'branch'}) (or scope 'auto' with a clean tree) on a repo where origin/HEAD is unset AND no local or remote branch named main/master/trunk exists. Triggered by the codex review command in branch/auto mode when --base is not supplied.","commonSituations":"Freshly cloned repo where `git remote set-head` was never run; a repo whose default branch is named `develop` or `release/*`; a bare or detached-HEAD checkout; CI environments that shallow-clone without origin/HEAD metadata; a local-only repo with no remote configured.","solutions":["Pass an explicit base ref: invoke the review with --base <ref> (e.g. --base origin/develop).","Run the review in working-tree scope: use --scope working-tree to skip branch detection entirely.","Make a candidate branch detectable: `git remote set-head origin -a` so symbolic-ref resolves, or rename/create a main|master|trunk branch.","If your default branch is non-standard, set origin/HEAD: `git remote set-head origin <your-default-branch>`."],"exampleFix":"// before\nresolveReviewTarget(cwd, { scope: \"branch\" });\n\n// after\nresolveReviewTarget(cwd, { scope: \"branch\", base: \"origin/develop\" });","handlingStrategy":"validation","validationCode":"import { execFileSync } from \"node:child_process\";\n\nfunction hasDetectableDefaultBranch(cwd) {\n  // origin/HEAD set?\n  try {\n    execFileSync(\"git\", [\"-C\", cwd, \"symbolic-ref\", \"refs/remotes/origin/HEAD\"], { stdio: \"ignore\" });\n    return true;\n  } catch {}\n  for (const c of [\"main\", \"master\", \"trunk\"]) {\n    for (const ref of [`refs/heads/${c}`, `refs/remotes/origin/${c}`]) {\n      try {\n        execFileSync(\"git\", [\"-C\", cwd, \"show-ref\", \"--verify\", \"--quiet\", ref], { stdio: \"ignore\" });\n        return true;\n      } catch {}\n    }\n  }\n  return false;\n}\n\n// before calling resolveReviewTarget in branch/auto mode:\nconst opts = hasDetectableDefaultBranch(cwd)\n  ? { scope: \"branch\" }\n  : { scope: \"working-tree\" }; // or { base: \"origin/develop\" }","typeGuard":null,"tryCatchPattern":"try {\n  resolveReviewTarget(cwd, { scope: \"branch\" });\n} catch (err) {\n  if (/Unable to detect the repository default branch/.test(err.message)) {\n    // fall back to an explicit base or working-tree scope\n    resolveReviewTarget(cwd, { scope: \"working-tree\" });\n  } else {\n    throw err;\n  }\n}","preventionTips":["Run `git remote set-head origin -a` after cloning so origin/HEAD is resolvable.","Prefer passing --base <ref> explicitly in CI where branch layout is unknown.","Default to --scope working-tree when you only care about uncommitted changes."],"tags":["git","branch-detection","codex-review","config"],"backgroundTag":null,"analyzedSha":"db52e28f4d9ded852ab3942cea316258ae4ef346","analyzedAt":"2026-08-13T05:13:20.855Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}