Create and checkout branches atomically

- Return the created branch from git createBranch
- Use one API call for branch creation and checkout in the selector
This commit is contained in:
Julius Marminge committed 2026-04-06 09:07:00 -07:00
1 parent e234ed097c
commit 745d10df93
6 files changed
+38 -25

No files matched your search

+12 -6
View File
@@ -2009,12 +2009,6 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
return { branch: targetBranch };
});
const createBranch: GitCoreShape["createBranch"] = (input) =>
executeGit("GitCore.createBranch", input.cwd, ["branch", input.branch], {
timeoutMs: 10_000,
fallbackErrorMessage: "git branch create failed",
}).pipe(Effect.asVoid);
const checkoutBranch: GitCoreShape["checkoutBranch"] = Effect.fn("checkoutBranch")(
function* (input) {
const [localInputExists, remoteExists] = yield* Effect.all(
@@ -2097,6 +2091,18 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
},
);
const createBranch: GitCoreShape["createBranch"] = Effect.fn("createBranch")(function* (input) {
yield* executeGit("GitCore.createBranch", input.cwd, ["branch", input.branch], {
timeoutMs: 10_000,
fallbackErrorMessage: "git branch create failed",
});
if (input.checkout) {
yield* checkoutBranch({ cwd: input.cwd, branch: input.branch });
}
return { branch: input.branch };
});
const initRepo: GitCoreShape["initRepo"] = (input) =>
executeGit("GitCore.initRepo", input.cwd, ["init"], {
timeoutMs: 10_000,
+6 -3
View File
@@ -7,11 +7,12 @@
* @module GitCore
*/
import { ServiceMap } from "effect";
import type { Effect, Scope } from "effect";
import type { Effect } from "effect";
import type {
GitCheckoutInput,
GitCheckoutResult,
GitCreateBranchInput,
GitCreateBranchResult,
GitCreateWorktreeInput,
GitCreateWorktreeResult,
GitInitInput,
@@ -284,14 +285,16 @@ export interface GitCoreShape {
/**
* Create a local branch.
*/
readonly createBranch: (input: GitCreateBranchInput) => Effect.Effect<void, GitCommandError>;
readonly createBranch: (
input: GitCreateBranchInput,
) => Effect.Effect<GitCreateBranchResult, GitCommandError>;
/**
* Checkout an existing branch and refresh its upstream metadata in background.
*/
readonly checkoutBranch: (
input: GitCheckoutInput,
) => Effect.Effect<GitCheckoutResult, GitCommandError, Scope.Scope>;
) => Effect.Effect<GitCheckoutResult, GitCommandError>;
/**
* Initialize a repository in the provided directory.
@@ -106,7 +106,6 @@ export function BranchToolbarBranchSelector({
gitBranchSearchInfiniteQueryOptions({
cwd: branchCwd,
query: deferredTrimmedBranchQuery,
enabled: isBranchMenuOpen,
}),
);
const branches = useMemo(
@@ -256,27 +255,23 @@ export function BranchToolbarBranchSelector({
onComposerFocusRequest?.();
runBranchAction(async () => {
const previousBranch = resolvedActiveBranch;
setOptimisticBranch(name);
try {
await api.git.createBranch({ cwd: branchCwd, branch: name });
try {
await api.git.checkout({ cwd: branchCwd, branch: name });
} catch (error) {
toastManager.add({
type: "error",
title: "Failed to checkout branch.",
description: toBranchActionErrorMessage(error),
});
return;
}
const createBranchResult = await api.git.createBranch({
cwd: branchCwd,
branch: name,
checkout: true,
});
setOptimisticBranch(createBranchResult.branch);
onSetThreadBranch(createBranchResult.branch, activeWorktreePath);
} catch (error) {
setOptimisticBranch(previousBranch);
toastManager.add({
type: "error",
title: "Failed to create branch.",
title: "Failed to create and checkout branch.",
description: toBranchActionErrorMessage(error),
});
return;
}
setOptimisticBranch(name);
+6
View File
@@ -169,9 +169,15 @@ export type GitRemoveWorktreeInput = typeof GitRemoveWorktreeInput.Type;
export const GitCreateBranchInput = Schema.Struct({
cwd: TrimmedNonEmptyStringSchema,
branch: TrimmedNonEmptyStringSchema,
checkout: Schema.optional(Schema.Boolean),
});
export type GitCreateBranchInput = typeof GitCreateBranchInput.Type;
export const GitCreateBranchResult = Schema.Struct({
branch: TrimmedNonEmptyStringSchema,
});
export type GitCreateBranchResult = typeof GitCreateBranchResult.Type;
export const GitCheckoutInput = Schema.Struct({
cwd: TrimmedNonEmptyStringSchema,
branch: TrimmedNonEmptyStringSchema,
+2 -1
View File
@@ -16,6 +16,7 @@ import type {
GitResolvePullRequestResult,
GitStatusInput,
GitStatusResult,
GitCreateBranchResult,
} from "./git";
import type {
ProjectSearchEntriesInput,
@@ -149,7 +150,7 @@ export interface NativeApi {
listBranches: (input: GitListBranchesInput) => Promise<GitListBranchesResult>;
createWorktree: (input: GitCreateWorktreeInput) => Promise<GitCreateWorktreeResult>;
removeWorktree: (input: GitRemoveWorktreeInput) => Promise<void>;
createBranch: (input: GitCreateBranchInput) => Promise<void>;
createBranch: (input: GitCreateBranchInput) => Promise<GitCreateBranchResult>;
checkout: (input: GitCheckoutInput) => Promise<GitCheckoutResult>;
init: (input: GitInitInput) => Promise<void>;
resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>;
+2
View File
@@ -9,6 +9,7 @@ import {
GitCheckoutResult,
GitCommandError,
GitCreateBranchInput,
GitCreateBranchResult,
GitCreateWorktreeInput,
GitCreateWorktreeResult,
GitInitInput,
@@ -222,6 +223,7 @@ export const WsGitRemoveWorktreeRpc = Rpc.make(WS_METHODS.gitRemoveWorktree, {
export const WsGitCreateBranchRpc = Rpc.make(WS_METHODS.gitCreateBranch, {
payload: GitCreateBranchInput,
success: GitCreateBranchResult,
error: GitCommandError,
});