Skip to content
TypeScript

fix(server): an upstream shadowed by a local origin/<branch> branch keeps its real remote (#29180)

Alex Southwell064cf8f7ac01
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 3e4881419d7..26807326715 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -3169,16 +3169,24 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
});
describe("repository status", () => {
- it.effect("reads the current branch by name when a tag shares it", () =>
+ it.effect("reads the current branch and upstream by name when other refs share them", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
+ const remote = yield* makeTmpDir("git-vcs-driver-remote-");
yield* initRepoWithCommit(cwd);
+ yield* git(remote, ["init", "--bare"]);
+ yield* git(cwd, ["remote", "add", "origin", remote]);
yield* git(cwd, ["checkout", "-b", "release"]);
+ yield* git(cwd, ["push", "-u", "origin", "release"]);
+ yield* git(cwd, ["commit", "--allow-empty", "-m", "unpushed"]);
+ yield* git(cwd, ["branch", "origin/release"]);
yield* git(cwd, ["tag", "release"]);
const driver = yield* GitVcsDriver.GitVcsDriver;
const remoteStatus = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
assert.equal(remoteStatus.branch, "release");
+ assert.equal(remoteStatus.upstreamRef, "origin/release");
+ assert.equal(remoteStatus.aheadCount, 1);
const preview = yield* driver.getReviewDiffPreview({ cwd });
const branchRange = preview.sources.find((source) => source.kind === "branch-range");
assert.equal(branchRange?.headRef, "release");
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 20072bd4bc2..d150ed97791 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -1293,13 +1293,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
});
});
+ // `--abbrev-ref` prints `remotes/origin/<name>` when a local `origin/<name>` branch exists.
const readCurrentUpstreamRef = Effect.fn("readCurrentUpstreamRef")(function* (cwd: string) {
const upstreamRef = yield* runGitStdout(
"GitVcsDriver.resolveCurrentUpstream",
cwd,
- ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"],
+ ["rev-parse", "--symbolic-full-name", "@{upstream}"],
true,
- ).pipe(Effect.map((stdout) => stdout.trim()));
+ ).pipe(Effect.map((stdout) => stdout.trim().replace(/^refs\/(?:remotes|heads)\//, "")));
if (upstreamRef.length === 0 || upstreamRef === "@{upstream}") {
return null;
@@ -2032,7 +2033,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const divergence = yield* executeGit(
"GitVcsDriver.statusDetailsRemote.divergence",
cwd,
- ["rev-list", "--left-right", "--count", `HEAD...${upstreamRef}`],
+ ["rev-list", "--left-right", "--count", "HEAD...@{upstream}"],
{ allowNonZeroExit: true },
);
if (divergence.exitCode === 0) {