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
1 change: 1 addition & 0 deletions apps/desktop/src/settings/DesktopClientSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const clientSettings: ClientSettings = {
autoOpenPlanSidebar: false,
confirmThreadArchive: true,
confirmThreadDelete: false,
deleteRemoteBranchOnDelete: true,
dismissedProviderUpdateNotificationKeys: [],
diffIgnoreWhitespace: true,
diffWordWrap: true,
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/git/GitWorkflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
type VcsSwitchRefResult,
type VcsCreateRefInput,
type VcsCreateRefResult,
type VcsDeleteBranchInput,
type VcsDeleteBranchResult,
type VcsCreateWorktreeInput,
type VcsCreateWorktreeResult,
type VcsListRefsInput,
Expand Down Expand Up @@ -67,6 +69,9 @@ export interface GitWorkflowServiceShape {
readonly switchRef: (
input: VcsSwitchRefInput,
) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
readonly deleteBranch: (
input: VcsDeleteBranchInput,
) => Effect.Effect<VcsDeleteBranchResult, GitCommandError>;
readonly renameBranch: (input: {
readonly cwd: string;
readonly oldBranch: string;
Expand Down Expand Up @@ -306,6 +311,10 @@ export const make = Effect.fn("makeGitWorkflowService")(function* () {
ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
Effect.andThen(Effect.scoped(git.switchRef(input))),
),
deleteBranch: (input) =>
ensureGitCommand("GitWorkflowService.deleteBranch", input.cwd).pipe(
Effect.andThen(git.deleteBranch(input)),
),
renameBranch: (input) =>
ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
Effect.andThen(git.renameBranch(input)),
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/vcs/GitVcsDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
type VcsSwitchRefResult,
type VcsCreateRefInput,
type VcsCreateRefResult,
type VcsDeleteBranchInput,
type VcsDeleteBranchResult,
type VcsCreateWorktreeInput,
type VcsCreateWorktreeResult,
type ReviewDiffPreviewInput,
Expand Down Expand Up @@ -217,6 +219,9 @@ export interface GitVcsDriverShape {
readonly switchRef: (
input: VcsSwitchRefInput,
) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
readonly deleteBranch: (
input: VcsDeleteBranchInput,
) => Effect.Effect<VcsDeleteBranchResult, GitCommandError>;
readonly initRepo: (input: VcsInitInput) => Effect.Effect<void, GitCommandError>;
readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
}
Expand Down
63 changes: 63 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ function commandLabel(args: readonly string[]): string {
return `git ${args.join(" ")}`;
}

function isMissingRemoteBranchError(stderr: string): boolean {
const normalized = stderr.toLowerCase();
return (
normalized.includes("remote ref does not exist") ||
normalized.includes("does not exist") ||
normalized.includes("unable to delete")
);
}

function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): string | null {
const trimmed = value.trim();
const prefix = `refs/remotes/${remoteName}/`;
Expand Down Expand Up @@ -2280,6 +2289,59 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
},
);

const deleteBranch: GitVcsDriver.GitVcsDriverShape["deleteBranch"] = Effect.fn("deleteBranch")(
function* (input) {
const isRemoteRef = input.isRemote === true;

let deletedLocal = false;
if (!isRemoteRef) {
yield* executeGit(
"GitVcsDriver.deleteBranch.local",
input.cwd,
["branch", input.force ? "-D" : "-d", "--", input.refName],
{
timeoutMs: 10_000,
fallbackErrorMessage: "git branch delete failed",
},
);
deletedLocal = true;
}

let deletedRemote = false;
if (isRemoteRef || input.deleteRemote) {
const remoteName = input.remoteName
? input.remoteName
: yield* resolvePrimaryRemoteName(input.cwd).pipe(
Effect.catch((error) => (isRemoteRef ? Effect.fail(error) : Effect.succeed(null))),
);
const remoteBranch = isRemoteRef
? deriveLocalBranchNameFromRemoteRef(input.refName)
: input.refName;
if (remoteName && remoteBranch) {
const pushArgs = ["push", remoteName, "--delete", remoteBranch];
const result = yield* executeGit(
"GitVcsDriver.deleteBranch.remote",
input.cwd,
pushArgs,
{ timeoutMs: 30_000, allowNonZeroExit: true },
);
if (result.exitCode === 0) {
deletedRemote = true;
} else if (!isMissingRemoteBranchError(result.stderr)) {
return yield* createGitCommandError(
"GitVcsDriver.deleteBranch.remote",
input.cwd,
pushArgs,
result.stderr.trim() || "git push --delete failed",
);
}
}
}

return { refName: input.refName, deletedLocal, deletedRemote };
},
);

const initRepo: GitVcsDriver.GitVcsDriverShape["initRepo"] = (input) =>
executeGit("GitVcsDriver.initRepo", input.cwd, ["init"], {
timeoutMs: 10_000,
Expand Down Expand Up @@ -2329,6 +2391,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
renameBranch,
createRef,
switchRef,
deleteBranch,
initRepo,
listLocalBranchNames,
});
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,12 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
gitWorkflow.switchRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
{ "rpc.aggregate": "vcs" },
),
[WS_METHODS.vcsDeleteBranch]: (input) =>
observeRpcEffect(
WS_METHODS.vcsDeleteBranch,
gitWorkflow.deleteBranch(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
{ "rpc.aggregate": "vcs" },
),
[WS_METHODS.vcsInit]: (input) =>
observeRpcEffect(
WS_METHODS.vcsInit,
Expand Down
Loading
Loading