Skip to content

Commit 849d2b5

Browse files
MarcoDotIOclaudemaria-rcks
authored
fix(server): a branch named like a tag no longer reads as heads/<name> (pingdotgg#24258)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent a8e4188 commit 849d2b5

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ describe("pullCurrentBranch", () => {
404404
const driver = yield* GitVcsDriver.GitVcsDriver;
405405
const fs = yield* FileSystem.FileSystem;
406406
const { cwd, updater, initialBranch, remoteHead } = yield* cloneBehindRemote;
407+
yield* git(cwd, ["tag", initialBranch]);
407408
yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
408409
const remoteName = customMapping ? "team/upstream" : "origin";
409410
const refPrefix = customMapping
@@ -2279,6 +2280,22 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
22792280
});
22802281

22812282
describe("repository status", () => {
2283+
it.effect("reads the current branch by name when a tag shares it", () =>
2284+
Effect.gen(function* () {
2285+
const cwd = yield* makeTmpDir();
2286+
yield* initRepoWithCommit(cwd);
2287+
yield* git(cwd, ["checkout", "-b", "release"]);
2288+
yield* git(cwd, ["tag", "release"]);
2289+
const driver = yield* GitVcsDriver.GitVcsDriver;
2290+
2291+
const remoteStatus = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
2292+
assert.equal(remoteStatus.branch, "release");
2293+
const preview = yield* driver.getReviewDiffPreview({ cwd });
2294+
const branchRange = preview.sources.find((source) => source.kind === "branch-range");
2295+
assert.equal(branchRange?.headRef, "release");
2296+
}),
2297+
);
2298+
22822299
it.effect("does not open a project folder laid out as a bare repository", () =>
22832300
Effect.gen(function* () {
22842301
const cwd = yield* makeTmpDir();

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

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

517+
// `--short` and `--abbrev-ref` print `heads/<name>` when a tag shares the branch's name, so
518+
// branch reads take the full ref and strip the prefix themselves.
519+
function branchNameFromHeadRef(value: string): string | null {
520+
const ref = value.trim();
521+
return ref.startsWith("refs/heads/") && ref.length > "refs/heads/".length
522+
? ref.slice("refs/heads/".length)
523+
: null;
524+
}
525+
517526
function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): string | null {
518527
const trimmed = value.trim();
519528
const prefix = `refs/remotes/${remoteName}/`;
@@ -1412,7 +1421,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14121421
executeGit(
14131422
"GitVcsDriver.resolveRepositoryPaths.currentBranch",
14141423
cwd,
1415-
["symbolic-ref", "--quiet", "--short", "HEAD"],
1424+
["symbolic-ref", "--quiet", "HEAD"],
14161425
{
14171426
timeoutMs: 5_000,
14181427
allowNonZeroExit: true,
@@ -1443,11 +1452,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14431452
Effect.map((physicalCwd) => path.relative(worktreeRoot, physicalCwd) === ""),
14441453
Effect.orElseSucceed(() => undefined),
14451454
);
1446-
const currentBranchOutput = currentBranchResult.stdout.trim();
14471455
const currentBranch =
1448-
currentBranchResult.exitCode === 0 && currentBranchOutput.length > 0
1449-
? currentBranchOutput
1450-
: null;
1456+
currentBranchResult.exitCode === 0 ? branchNameFromHeadRef(currentBranchResult.stdout) : null;
14511457

14521458
return {
14531459
gitCommonDir,
@@ -1938,7 +1944,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19381944
const branchResult = yield* executeGitWithStableDiagnostics(
19391945
"GitVcsDriver.statusDetailsRemote.branch",
19401946
cwd,
1941-
["rev-parse", "--abbrev-ref", "HEAD"],
1947+
["rev-parse", "--symbolic-full-name", "HEAD"],
19421948
{ allowNonZeroExit: true },
19431949
).pipe(
19441950
Effect.catchTags({
@@ -1960,7 +1966,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19601966
...gitCommandContext({
19611967
operation: "GitVcsDriver.statusDetailsRemote.branch",
19621968
cwd,
1963-
args: ["rev-parse", "--abbrev-ref", "HEAD"],
1969+
args: ["rev-parse", "--symbolic-full-name", "HEAD"],
19641970
}),
19651971
detail: "Git branch lookup failed.",
19661972
exitCode: branchResult.exitCode,
@@ -1972,12 +1978,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19721978
const branchValue = yield* runGitStdout(
19731979
"GitVcsDriver.statusDetailsRemote.unbornBranch",
19741980
cwd,
1975-
["symbolic-ref", "--quiet", "--short", "HEAD"],
1981+
["symbolic-ref", "--quiet", "HEAD"],
19761982
);
1977-
branch = branchValue.trim() || null;
1983+
branch = branchNameFromHeadRef(branchValue);
19781984
} else {
1979-
const branchValue = branchResult.stdout.trim();
1980-
branch = branchValue.length > 0 && branchValue !== "HEAD" ? branchValue : null;
1985+
branch = branchNameFromHeadRef(branchResult.stdout);
19811986
}
19821987
const currentUpstreamRef = yield* readCurrentUpstreamRef(cwd);
19831988
let upstreamRef =
@@ -2781,9 +2786,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27812786
runGitStdout(
27822787
"GitVcsDriver.pullCurrentBranch.currentBranch",
27832788
cwd,
2784-
["symbolic-ref", "--quiet", "--short", "HEAD"],
2789+
["symbolic-ref", "--quiet", "HEAD"],
27852790
true,
2786-
).pipe(Effect.map((stdout) => stdout.trim())),
2791+
).pipe(Effect.map(branchNameFromHeadRef)),
27872792
runGitStdout("GitVcsDriver.pullCurrentBranch.currentSha", cwd, ["rev-parse", "HEAD"]).pipe(
27882793
Effect.map((stdout) => stdout.trim()),
27892794
),

0 commit comments

Comments
 (0)