Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,49 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
assert.equal(yield* fileSystem.exists(worktreePath), false);
}),
);

it.effect("creates a new worktree when the base branch name is also a remote ref", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
const { initialBranch } = yield* initRepoWithCommit(cwd);
const pathService = yield* Path.Path;
const worktreePath = pathService.join(
yield* makeTmpDir("git-worktrees-"),
"feature-ambiguous-worktree",
);
const driver = yield* GitVcsDriver.GitVcsDriver;

yield* git(remote, ["init", "--bare"]);
yield* git(cwd, ["remote", "add", initialBranch, remote]);
yield* git(cwd, ["push", initialBranch, `${initialBranch}:${initialBranch}`]);
yield* git(cwd, [
"fetch",
initialBranch,
`${initialBranch}:refs/remotes/${initialBranch}/${initialBranch}`,
]);

const refs = yield* git(cwd, ["show-ref", initialBranch]);
assert.include(refs, `refs/heads/${initialBranch}`);
assert.include(refs, `refs/remotes/${initialBranch}/${initialBranch}`);

const created = yield* driver.createWorktree({
cwd,
path: worktreePath,
refName: initialBranch,
newRefName: "feature/ambiguous-worktree",
});

assert.equal(created.worktree.path, worktreePath);
assert.equal(created.worktree.refName, "feature/ambiguous-worktree");
assert.equal(
yield* git(worktreePath, ["branch", "--show-current"]),
"feature/ambiguous-worktree",
);

yield* driver.removeWorktree({ cwd, path: worktreePath });
}),
);
});

describe("commit context", () => {
Expand Down
34 changes: 32 additions & 2 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,35 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
Effect.map(parseRemoteNamesInGitOrder),
);

const qualifyWorktreeStartPoint = Effect.fn("qualifyWorktreeStartPoint")(function* (
cwd: string,
refName: string,
) {
if (refName.startsWith("refs/")) {
return refName;
}

const isLocalBranch = yield* branchExists(cwd, refName);
if (isLocalBranch) {
return `refs/heads/${refName}`;
}

const remoteNames = yield* listRemoteNames(cwd).pipe(Effect.catch(() => Effect.succeed([])));
const parsedRemoteRef = parseRemoteRefWithRemoteNames(refName, remoteNames);
if (parsedRemoteRef) {
const isRemoteBranch = yield* remoteBranchExists(
cwd,
parsedRemoteRef.remoteName,
parsedRemoteRef.branchName,
);
if (isRemoteBranch) {
return `refs/remotes/${parsedRemoteRef.remoteName}/${parsedRemoteRef.branchName}`;
}
}

return refName;
});

const resolvePublishBranchName = Effect.fn("resolvePublishBranchName")(function* (
cwd: string,
branchName: string,
Expand Down Expand Up @@ -2063,9 +2092,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const sanitizedBranch = targetBranch.replace(/\//g, "-");
const repoName = path.basename(input.cwd);
const worktreePath = input.path ?? path.join(worktreesDir, repoName, sanitizedBranch);
const startPoint = yield* qualifyWorktreeStartPoint(input.cwd, input.refName);
const args = input.newRefName
? ["worktree", "add", "-b", input.newRefName, worktreePath, input.refName]
: ["worktree", "add", worktreePath, input.refName];
? ["worktree", "add", "-b", input.newRefName, worktreePath, startPoint]
: ["worktree", "add", worktreePath, startPoint];

yield* executeGit("GitVcsDriver.createWorktree", input.cwd, args, {
fallbackErrorMessage: "git worktree add failed",
Expand Down
Loading