Skip to content
TypeScript

fix(server): preserve unrelated branch upstreams when opening fork PRs (#18413)

Alex Southwell3cc98ba567bd
diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.ts
index 303bc44f2a9..32337ad4696 100644
--- a/apps/server/src/git/GitManager.test.ts
+++ b/apps/server/src/git/GitManager.test.ts
@@ -6865,7 +6865,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
}),
);
- it.effect("never moves an unrelated local branch that shares the fork head branch name", () =>
+ it.effect("preserves an unrelated local branch and upstream that share the fork head name", () =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
yield* initRepo(repoDir);
@@ -6887,6 +6887,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
);
yield* runGit(repoDir, ["worktree", "add", mainWorktreePath, "main"]);
const localMainBefore = (yield* runGit(repoDir, ["rev-parse", "main"])).stdout.trim();
+ // Leave the fork tracking ref stale so an unnecessary fetch would change it.
+ yield* runGit(repoDir, ["update-ref", "refs/remotes/fork-seed/main", localMainBefore]);
+ const trackingRefsBefore = (yield* runGit(repoDir, [
+ "for-each-ref",
+ "--format=%(refname) %(objectname)",
+ "refs/remotes",
+ ])).stdout;
const setupCalls: ProjectSetupScriptRunner.ProjectSetupScriptRunnerInput[] = [];
const { manager } = yield* makeManager({
@@ -6930,8 +6937,34 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
localMainBefore,
);
expect(NodeFS.existsSync(NodePath.join(mainWorktreePath, "contributor.txt"))).toBe(false);
+ expect(
+ (yield* runGit(mainWorktreePath, [
+ "rev-parse",
+ "--abbrev-ref",
+ "@{upstream}",
+ ])).stdout.trim(),
+ ).toBe("origin/main");
+ expect(result.branch).toBe("t3code/pr-94/main");
+ expect(result.worktreePath && NodeFS.realpathSync.native(result.worktreePath)).toBe(
+ NodeFS.realpathSync.native(mainWorktreePath),
+ );
+ expect(
+ (yield* runGit(repoDir, [
+ "for-each-ref",
+ "--format=%(refname) %(objectname)",
+ "refs/remotes",
+ ])).stdout,
+ ).toBe(trackingRefsBefore);
+ expect((yield* runGit(mainWorktreePath, ["branch", "--show-current"])).stdout.trim()).toBe(
+ "main",
+ );
expect(result.isOnPullRequestHead).toBe(false);
expect(setupCalls).toHaveLength(0);
+
+ yield* (yield* GitVcsDriver.GitVcsDriver).pullCurrentBranch(mainWorktreePath);
+ expect((yield* runGit(mainWorktreePath, ["rev-parse", "HEAD"])).stdout.trim()).toBe(
+ localMainBefore,
+ );
}),
);
@@ -7075,7 +7108,14 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
"..",
`pr-reused-fork-${NodePath.basename(repoDir)}`,
);
- yield* runGit(repoDir, ["worktree", "add", worktreePath, "feature/pr-reused-fork"]);
+ yield* runGit(repoDir, [
+ "worktree",
+ "add",
+ "-b",
+ "t3code/pr-83/feature/pr-reused-fork",
+ worktreePath,
+ "feature/pr-reused-fork",
+ ]);
yield* runGit(worktreePath, ["branch", "--unset-upstream"], true);
const { manager } = yield* makeManager({
@@ -7109,6 +7149,10 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
expect(result.worktreePath && NodeFS.realpathSync.native(result.worktreePath)).toBe(
NodeFS.realpathSync.native(worktreePath),
);
+ expect(result.branch).toBe("t3code/pr-83/feature/pr-reused-fork");
+ expect((yield* runGit(worktreePath, ["branch", "--show-current"])).stdout.trim()).toBe(
+ result.branch,
+ );
expect(
(yield* runGit(worktreePath, ["rev-parse", "--abbrev-ref", "@{upstream}"])).stdout.trim(),
).toBe("fork-seed/feature/pr-reused-fork");
diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts
index 097dd1e0408..f24fc8d43ff 100644
--- a/apps/server/src/git/GitManager.ts
+++ b/apps/server/src/git/GitManager.ts
@@ -2729,7 +2729,6 @@ export const make = Effect.gen(function* () {
// findLocalHeadBranch also accepts a branch that merely shares the head's bare name —
// a fork PR opened from "main" matches the user's own local main. That checkout is
// somebody else's work, so it keeps its tracking config and nothing else.
- yield* ensureExistingWorktreeUpstream(worktreePath);
return {
pullRequest,
branch: localPullRequestBranch,