Skip to content
Merged
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
6 changes: 6 additions & 0 deletions apps/array/src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ contextBridge.exposeInMainWorld("electronAPI", {
}> => ipcRenderer.invoke("get-diff-stats", repoPath),
getCurrentBranch: (repoPath: string): Promise<string | undefined> =>
ipcRenderer.invoke("get-current-branch", repoPath),
getDefaultBranch: (repoPath: string): Promise<string> =>
ipcRenderer.invoke("get-default-branch", repoPath),
getAllBranches: (repoPath: string): Promise<string[]> =>
ipcRenderer.invoke("get-all-branches", repoPath),
createBranch: (repoPath: string, branchName: string): Promise<void> =>
ipcRenderer.invoke("create-branch", repoPath, branchName),
discardFileChanges: (
repoPath: string,
filePath: string,
Expand Down
61 changes: 61 additions & 0 deletions apps/array/src/main/services/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,36 @@ export const getDefaultBranch = async (
}
};

export const getAllBranches = async (
directoryPath: string,
): Promise<string[]> => {
try {
const { stdout } = await execAsync(
'git branch --list --format="%(refname:short)"',
{
cwd: directoryPath,
},
);
return stdout
.trim()
.split("\n")
.filter(Boolean)
.map((branch) => branch.trim())
.filter((branch) => !branch.startsWith("array/"));
} catch {
return [];
}
};

export const createBranch = async (
directoryPath: string,
branchName: string,
): Promise<void> => {
await execAsync(`git checkout -b "${branchName}"`, {
cwd: directoryPath,
});
};

const getChangedFiles = async (directoryPath: string): Promise<Set<string>> => {
const changedFiles = new Set<string>();

Expand Down Expand Up @@ -785,6 +815,37 @@ export function registerGitIpc(
},
);

ipcMain.handle(
"get-default-branch",
async (
_event: IpcMainInvokeEvent,
directoryPath: string,
): Promise<string> => {
return getDefaultBranch(directoryPath);
},
);

ipcMain.handle(
"get-all-branches",
async (
_event: IpcMainInvokeEvent,
directoryPath: string,
): Promise<string[]> => {
return getAllBranches(directoryPath);
},
);

ipcMain.handle(
"create-branch",
async (
_event: IpcMainInvokeEvent,
directoryPath: string,
branchName: string,
): Promise<void> => {
return createBranch(directoryPath, branchName);
},
);

ipcMain.handle(
"discard-file-changes",
async (
Expand Down
26 changes: 24 additions & 2 deletions apps/array/src/main/services/workspace/workspaceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class WorkspaceService {
async createWorkspace(
options: CreateWorkspaceOptions,
): Promise<WorkspaceInfo> {
const { taskId, mainRepoPath, folderId, folderPath, mode } = options;
const { taskId, mainRepoPath, folderId, folderPath, mode, branch } =
options;
log.info(
`Creating workspace for task ${taskId} in ${mainRepoPath} (mode: ${mode})`,
);
Expand Down Expand Up @@ -117,6 +118,25 @@ export class WorkspaceService {

// Root mode: skip worktree creation entirely
if (mode === "root") {
// try to create the branch, if it already exists just switch to it
if (branch) {
try {
log.info(`Creating/switching to branch ${branch} for task ${taskId}`);
try {
await execAsync(`git checkout -b "${branch}"`, { cwd: folderPath });
log.info(`Created and switched to new branch ${branch}`);
} catch (_error) {
await execAsync(`git checkout "${branch}"`, { cwd: folderPath });
log.info(`Switched to existing branch ${branch}`);
}
} catch (error) {
log.error(`Failed to create/switch to branch ${branch}:`, error);
throw new Error(
`Failed to create/switch to branch ${branch}: ${String(error)}`,
);
}
}

// Save task association without worktree
const associations = getTaskAssociations();
const existingIndex = associations.findIndex((a) => a.taskId === taskId);
Expand Down Expand Up @@ -219,7 +239,9 @@ export class WorkspaceService {
let worktree: WorktreeInfo;

try {
worktree = await worktreeManager.createWorktree();
worktree = await worktreeManager.createWorktree({
baseBranch: branch ?? undefined,
});
log.info(
`Created worktree: ${worktree.worktreeName} at ${worktree.worktreePath}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,15 @@ export function FolderPicker({
onClick={handleOpenFilePicker}
>
<Flex justify="between" align="center" gap="2" width="100%">
<Flex
align="center"
gap="2"
width="250px"
style={{ minWidth: 0, flex: 1 }}
>
<Flex align="center" gap="2">
<FolderIcon size={16} weight="regular" style={{ flexShrink: 0 }} />
<Text
size={size}
style={{
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
maxWidth: "120px",
}}
truncate
>
Expand All @@ -90,13 +86,8 @@ export function FolderPicker({
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button color="gray" variant="outline" size={size}>
<Flex justify="between" align="center" gap="2" width="100%">
<Flex
align="center"
gap="2"
width="250px"
style={{ minWidth: 0, flex: 1 }}
>
<Flex justify="between" align="center" gap="2">
<Flex align="center" gap="2" style={{ minWidth: 0 }}>
<FolderIcon
size={16}
weight="regular"
Expand Down
Loading