From 6121fbb9ae42d8924a7a079727ee1ebe61939df4 Mon Sep 17 00:00:00 2001 From: wendrul <53628737+wendrul@users.noreply.github.com> Date: Thu, 11 Sep 2025 10:55:16 +0200 Subject: [PATCH] workspace forks: fix cli build errors (#6580) --- cli/src/commands/workspace/fork.ts | 55 ++++--------------------- cli/src/commands/workspace/workspace.ts | 16 +++++-- cli/src/core/conf.ts | 2 +- cli/src/utils/git.ts | 4 +- 4 files changed, 24 insertions(+), 53 deletions(-) diff --git a/cli/src/commands/workspace/fork.ts b/cli/src/commands/workspace/fork.ts index 947edca64a..222f47c9a8 100644 --- a/cli/src/commands/workspace/fork.ts +++ b/cli/src/commands/workspace/fork.ts @@ -13,33 +13,10 @@ import { tryResolveBranchWorkspace } from "../../core/context.ts"; // Run ./gen_wm_client.sh to regenerate after backend changes // import * as wmill from "../../../gen/services.gen.ts"; -async function runGitCommand( - args: string[], -): Promise<{ success: boolean; output: string }> { - try { - const command = new Deno.Command("git", { - args, - stdout: "piped", - stderr: "piped", - }); - - const { code, stdout, stderr } = await command.output(); - const output = new TextDecoder().decode(code === 0 ? stdout : stderr); - - return { - success: code === 0, - output: output.trim(), - }; - } catch (error) { - return { - success: false, - output: `Failed to execute git command: ${error.message}`, - }; - } -} - async function createWorkspaceFork( - opts: GlobalOptions, + opts: GlobalOptions & { + createWorkspaceName: string | undefined; + }, workspaceName: string | undefined, workspaceId: string | undefined = undefined, ) { @@ -56,6 +33,9 @@ async function createWorkspaceFork( log.info(`You are forking workspace (${workspace.workspaceId})`) const currentBranch = getCurrentGitBranch() + if (!currentBranch) { + throw new Error("Could not get git branch name"); + } const originalBranchIfForked = getOriginalBranchForWorkspaceForks(currentBranch); let clonedBranchName: string | null; @@ -129,7 +109,7 @@ async function createWorkspaceFork( const result = await wmill.createWorkspaceFork({ requestBody: { id: trueWorkspaceId, - name: workspaceName, + name: opts.createWorkspaceName ?? trueWorkspaceId, username: undefined, // Let the server handle username color: undefined, parent_workspace_id: workspace.workspaceId, @@ -221,23 +201,4 @@ async function deleteWorkspaceFork( await removeWorkspace(name, silent, opts); } -const forkCommand = new Command() - .description("Create a forked workspace and git branch") - .arguments("[workspace_id:string]") - .option( - "--create-workspace-name ", - "Specify the workspace name. Ignored if --create is not specified or the workspace already exists. Will default to the workspace id." - ) - .action(async (opts: GlobalOptions, workspace_id: string) => { - await requireLogin(opts); - await createWorkspaceFork(opts, workspace_id, undefined); - }); - -const deleteForkCommand = new Command() - .description("Delete a forked workspace and git branch") - .arguments("") - .action(async (opts: GlobalOptions, name: string, silent: boolean) => { - await deleteWorkspaceFork(opts, silent, name); - }); - -export { forkCommand, deleteForkCommand }; +export { createWorkspaceFork, deleteWorkspaceFork }; diff --git a/cli/src/commands/workspace/workspace.ts b/cli/src/commands/workspace/workspace.ts index 8270de39df..e67617a562 100644 --- a/cli/src/commands/workspace/workspace.ts +++ b/cli/src/commands/workspace/workspace.ts @@ -4,7 +4,7 @@ import { getActiveWorkspaceConfigFilePath, getWorkspaceConfigFilePath } from ".. import { loginInteractive, tryGetLoginInfo } from "../../core/login.ts"; import { colors, Command, Confirm, Input, log, setClient, Table } from "../../../deps.ts"; import { requireLogin } from "../../core/auth.ts"; -import { forkCommand, deleteForkCommand } from "./fork.ts"; +import { createWorkspaceFork, deleteWorkspaceFork } from "./fork.ts"; import * as wmill from "../../../gen/services.gen.ts"; @@ -466,7 +466,17 @@ const command = new Command() .description("Remove workspace binding from the current Git branch") .option("--branch ", "Specify branch (defaults to current)") .action((opts) => bind(opts as any, false)) - .command("fork", forkCommand) - .command("delete-fork", deleteForkCommand); + .command("fork") + .description("Create a forked workspace") + .arguments("[workspace_name:string] [workspace_id:string]") + .option( + "--create-workspace-name ", + "Specify the workspace name. Ignored if --create is not specified or the workspace already exists. Will default to the workspace id." + ) + .action(createWorkspaceFork as any) + .command("delete-fork") + .description("Delete a forked workspace and git branch") + .arguments("") + .action(deleteWorkspaceFork as any); export default command; diff --git a/cli/src/core/conf.ts b/cli/src/core/conf.ts index 18a8902079..ebe871959f 100644 --- a/cli/src/core/conf.ts +++ b/cli/src/core/conf.ts @@ -397,7 +397,7 @@ export async function validateBranchConfiguration(skipValidation?: boolean, auto export async function getEffectiveSettings(config: SyncOptions, promotion?: string, skipBranchValidation?: boolean, suppressLogs?: boolean): Promise { // Start with top-level settings from config const { gitBranches, ...topLevelSettings } = config; - let effective = { ...topLevelSettings }; + const effective = { ...topLevelSettings }; if (isGitRepository()) { const branch = getCurrentGitBranch(); diff --git a/cli/src/utils/git.ts b/cli/src/utils/git.ts index 85d68052fc..f2e8a6a3ea 100644 --- a/cli/src/utils/git.ts +++ b/cli/src/utils/git.ts @@ -16,8 +16,8 @@ export function getCurrentGitBranch(): string | null { } } -export function getOriginalBranchForWorkspaceForks(branchName: string): string | null { - if (!branchName.startsWith(WM_FORK_PREFIX)) { +export function getOriginalBranchForWorkspaceForks(branchName: string | null): string | null { + if (!branchName || !branchName.startsWith(WM_FORK_PREFIX)) { return null }