Skip to content

Commit c8418b1

Browse files
committed
fix(server): disambiguate local diff base refs
1 parent bdbd7c6 commit c8418b1

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,70 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21932193
}),
21942194
);
21952195

2196+
it.effect("keeps local branch bases consistent when tags share their names", () =>
2197+
Effect.gen(function* () {
2198+
const cwd = yield* makeTmpDir();
2199+
yield* initRepoWithCommit(cwd);
2200+
const driver = yield* GitVcsDriver.GitVcsDriver;
2201+
yield* git(cwd, ["branch", "-M", "main"]);
2202+
const initialCommit = yield* git(cwd, ["rev-parse", "HEAD"]);
2203+
yield* git(cwd, ["update-ref", "refs/tags/main", initialCommit]);
2204+
yield* git(cwd, ["update-ref", "refs/tags/release/v2", initialCommit]);
2205+
yield* writeTextFile(cwd, "README.md", "# test\nbase work\n");
2206+
yield* git(cwd, ["commit", "-am", "base work"]);
2207+
yield* git(cwd, ["branch", "release/v2", "HEAD"]);
2208+
yield* git(cwd, ["checkout", "-b", "feature/local-base", "HEAD"]);
2209+
yield* writeTextFile(cwd, "README.md", "# test\nbase work\nfeature work\n");
2210+
yield* git(cwd, ["commit", "-am", "feature work"]);
2211+
2212+
for (const configuredBase of [undefined, "main", "release/v2"]) {
2213+
if (configuredBase !== undefined) {
2214+
yield* git(cwd, ["config", "branch.feature/local-base.gh-merge-base", configuredBase]);
2215+
}
2216+
const baseRef = configuredBase ?? "main";
2217+
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2218+
assert.deepStrictEqual(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
2219+
assert.strictEqual(status.aheadCount, 1);
2220+
const preview = yield* driver.getReviewDiffPreview({ cwd });
2221+
const changes = preview.sources.find((source) => source.kind === "branch-range")!;
2222+
assert.strictEqual(changes.baseRef, baseRef);
2223+
assert.deepStrictEqual(changes.files, [
2224+
{ path: "README.md", previousPath: null, additions: 1, deletions: 0 },
2225+
]);
2226+
const scoped = yield* driver.getReviewDiffPreview({
2227+
cwd,
2228+
baseRef,
2229+
file: { path: "README.md", previousPath: null, sourceKind: "branch-range" },
2230+
});
2231+
const scopedChanges = scoped.sources.find((source) => source.kind === "branch-range")!;
2232+
assert.deepStrictEqual(scopedChanges.files, changes.files);
2233+
assert.strictEqual(scopedChanges.diff, changes.diff);
2234+
const contents = yield* driver.getReviewDiffFileContents(
2235+
makeReviewDiffFileContentsInput(cwd, {
2236+
sourceKind: "branch-range",
2237+
baseRef: changes.baseRef,
2238+
headRef: "feature/local-base",
2239+
}),
2240+
);
2241+
assert.deepStrictEqual(contents, {
2242+
oldContents: "# test\nbase work\n",
2243+
newContents: "# test\nbase work\nfeature work\n",
2244+
});
2245+
}
2246+
2247+
for (const baseRef of ["refs/tags/main", initialCommit, "HEAD~2"]) {
2248+
const contents = yield* driver.getReviewDiffFileContents(
2249+
makeReviewDiffFileContentsInput(cwd, {
2250+
sourceKind: "branch-range",
2251+
baseRef,
2252+
headRef: "feature/local-base",
2253+
}),
2254+
);
2255+
assert.strictEqual(contents.oldContents, "# test\n");
2256+
}
2257+
}),
2258+
);
2259+
21962260
it.effect("Changes accepts an explicit base on a detached HEAD and rejects a bad one", () =>
21972261
Effect.gen(function* () {
21982262
const cwd = yield* makeTmpDir();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2704,7 +2704,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27042704
explicitBaseRef !== undefined
27052705
? ((yield* resolveRemoteBaseRef(cwd, explicitBaseRef)) ?? {
27062706
baseRef: explicitBaseRef,
2707-
comparisonRef: explicitBaseRef,
2707+
comparisonRef:
2708+
!explicitBaseRef.startsWith("refs/") && (yield* branchExists(cwd, explicitBaseRef))
2709+
? `refs/heads/${explicitBaseRef}`
2710+
: explicitBaseRef,
27082711
})
27092712
: branch
27102713
? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true }).pipe(

0 commit comments

Comments
 (0)