MCPcopy
hub / github.com/openai/codex-plugin-cc / detectDefaultBranch

Function detectDefaultBranch

plugins/codex/scripts/lib/git.mjs:93–115  ·  view source on GitHub ↗
(cwd)

Source from the content-addressed store, hash-verified

91}
92
93export function detectDefaultBranch(cwd) {
94 const symbolic = git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"]);
95 if (symbolic.status === 0) {
96 const remoteHead = symbolic.stdout.trim();
97 if (remoteHead.startsWith("refs/remotes/origin/")) {
98 return remoteHead.replace("refs/remotes/origin/", "");
99 }
100 }
101
102 const candidates = ["main", "master", "trunk"];
103 for (const candidate of candidates) {
104 const local = git(cwd, ["show-ref", "--verify", "--quiet", `refs/heads/${candidate}`]);
105 if (local.status === 0) {
106 return candidate;
107 }
108 const remote = git(cwd, ["show-ref", "--verify", "--quiet", `refs/remotes/origin/${candidate}`]);
109 if (remote.status === 0) {
110 return `origin/${candidate}`;
111 }
112 }
113
114 throw new Error("Unable to detect the repository default branch. Pass --base <ref> or use --scope working-tree.");
115}
116
117export function getCurrentBranch(cwd) {
118 return gitChecked(cwd, ["branch", "--show-current"]).stdout.trim() || "HEAD";

Callers 1

resolveReviewTargetFunction · 0.85

Calls 1

gitFunction · 0.85

Tested by

no test coverage detected