Skip to content

Commit 6a35d2c

Browse files
MarcoDotIOclaude
andcommitted
fix(server): a branch named like a tag no longer reads as heads/<name>
Git's --short and --abbrev-ref forms print `heads/<name>` when a tag shares the branch's name. Remote status, repository paths and the pull retry check read the current branch that way, so remote status looked up PRs and default-branch state for `heads/release` while local status said `release`, and the review panel labelled the range `heads/release`. Read the full ref and strip `refs/heads/` instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96ffd7a commit 6a35d2c

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,22 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
22382238
});
22392239

22402240
describe("repository status", () => {
2241+
it.effect("reads the current branch by name when a tag shares it", () =>
2242+
Effect.gen(function* () {
2243+
const cwd = yield* makeTmpDir();
2244+
yield* initRepoWithCommit(cwd);
2245+
yield* git(cwd, ["checkout", "-b", "release"]);
2246+
yield* git(cwd, ["tag", "release"]);
2247+
const driver = yield* GitVcsDriver.GitVcsDriver;
2248+
2249+
const remoteStatus = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
2250+
assert.equal(remoteStatus.branch, "release");
2251+
const preview = yield* driver.getReviewDiffPreview({ cwd });
2252+
const branchRange = preview.sources.find((source) => source.kind === "branch-range");
2253+
assert.equal(branchRange?.headRef, "release");
2254+
}),
2255+
);
2256+
22412257
it.effect("does not open a project folder laid out as a bare repository", () =>
22422258
Effect.gen(function* () {
22432259
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,15 @@ function classifyGitFailure(stderr: string): GitCommandFailureReason | null {
519519
return null;
520520
}
521521

522+
// `--short` and `--abbrev-ref` print `heads/<name>` when a tag shares the branch's name, so
523+
// branch reads take the full ref and strip the prefix themselves.
524+
function branchNameFromHeadRef(value: string): string | null {
525+
const ref = value.trim();
526+
return ref.startsWith("refs/heads/") && ref.length > "refs/heads/".length
527+
? ref.slice("refs/heads/".length)
528+
: null;
529+
}
530+
522531
function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): string | null {
523532
const trimmed = value.trim();
524533
const prefix = `refs/remotes/${remoteName}/`;
@@ -1414,7 +1423,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14141423
executeGit(
14151424
"GitVcsDriver.resolveRepositoryPaths.currentBranch",
14161425
cwd,
1417-
["symbolic-ref", "--quiet", "--short", "HEAD"],
1426+
["symbolic-ref", "--quiet", "HEAD"],
14181427
{
14191428
timeoutMs: 5_000,
14201429
allowNonZeroExit: true,
@@ -1445,11 +1454,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14451454
Effect.map((physicalCwd) => path.relative(worktreeRoot, physicalCwd) === ""),
14461455
Effect.orElseSucceed(() => undefined),
14471456
);
1448-
const currentBranchOutput = currentBranchResult.stdout.trim();
14491457
const currentBranch =
1450-
currentBranchResult.exitCode === 0 && currentBranchOutput.length > 0
1451-
? currentBranchOutput
1452-
: null;
1458+
currentBranchResult.exitCode === 0 ? branchNameFromHeadRef(currentBranchResult.stdout) : null;
14531459

14541460
return {
14551461
gitCommonDir,
@@ -1931,7 +1937,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19311937
const branchResult = yield* executeGitWithStableDiagnostics(
19321938
"GitVcsDriver.statusDetailsRemote.branch",
19331939
cwd,
1934-
["rev-parse", "--abbrev-ref", "HEAD"],
1940+
["rev-parse", "--symbolic-full-name", "HEAD"],
19351941
{ allowNonZeroExit: true },
19361942
).pipe(
19371943
Effect.catchTags({
@@ -1953,7 +1959,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19531959
...gitCommandContext({
19541960
operation: "GitVcsDriver.statusDetailsRemote.branch",
19551961
cwd,
1956-
args: ["rev-parse", "--abbrev-ref", "HEAD"],
1962+
args: ["rev-parse", "--symbolic-full-name", "HEAD"],
19571963
}),
19581964
detail: "Git branch lookup failed.",
19591965
exitCode: branchResult.exitCode,
@@ -1965,12 +1971,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19651971
const branchValue = yield* runGitStdout(
19661972
"GitVcsDriver.statusDetailsRemote.unbornBranch",
19671973
cwd,
1968-
["symbolic-ref", "--quiet", "--short", "HEAD"],
1974+
["symbolic-ref", "--quiet", "HEAD"],
19691975
);
1970-
branch = branchValue.trim() || null;
1976+
branch = branchNameFromHeadRef(branchValue);
19711977
} else {
1972-
const branchValue = branchResult.stdout.trim();
1973-
branch = branchValue.length > 0 && branchValue !== "HEAD" ? branchValue : null;
1978+
branch = branchNameFromHeadRef(branchResult.stdout);
19741979
}
19751980
const currentUpstreamRef = yield* readCurrentUpstreamRef(cwd);
19761981
let upstreamRef =
@@ -2760,9 +2765,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27602765
runGitStdout(
27612766
"GitVcsDriver.pullCurrentBranch.currentBranch",
27622767
cwd,
2763-
["symbolic-ref", "--quiet", "--short", "HEAD"],
2768+
["symbolic-ref", "--quiet", "HEAD"],
27642769
true,
2765-
).pipe(Effect.map((stdout) => stdout.trim())),
2770+
).pipe(Effect.map(branchNameFromHeadRef)),
27662771
runGitStdout("GitVcsDriver.pullCurrentBranch.currentSha", cwd, ["rev-parse", "HEAD"]).pipe(
27672772
Effect.map((stdout) => stdout.trim()),
27682773
),

0 commit comments

Comments
 (0)