From c0952b0053ed88b9af2f604a50c5f0e54d792fb1 Mon Sep 17 00:00:00 2001 From: David Disu Date: Thu, 29 Sep 2022 07:51:49 +0100 Subject: [PATCH] Fixed switching to remote branches --- .../workspace/src/lib/actions/workspace.ts | 58 +++++++++++++++++-- .../workspace/src/lib/contexts/index.ts | 3 +- .../src/lib/providers/FileSystemProvider.tsx | 15 +++-- .../workspace/src/lib/remix-ui-workspace.tsx | 5 +- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index c99ffd9e1e..f1a3dfbcb6 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -61,13 +61,12 @@ export const createWorkspace = async (workspaceName: string, workspaceTemplateNa await plugin.workspaceCreated(workspaceName) if (isGitRepo) { - await plugin.call('dGitProvider', 'init') + await plugin.call('dGitProvider', 'init', { branch: 'main' }) const isActive = await plugin.call('manager', 'isActive', 'dgit') if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') } if (!isEmpty) await loadWorkspacePreset(workspaceTemplateName, opts) - cb && cb(null, workspaceName) }).catch((error) => { dispatch(createWorkspaceError({ error })) @@ -454,7 +453,7 @@ export const showAllBranches = async () => { plugin.call('menuicons', 'select', 'dgit') } -export const switchToBranch = async (branch: string) => { +export const switchBranch = async (branch: string) => { const localChanges = await hasLocalChanges() if (Array.isArray(localChanges) && localChanges.length > 0) { @@ -493,8 +492,8 @@ export const switchToBranch = async (branch: string) => { } } -export const switchToNewBranch = async (branch: string) => { - const promise = plugin.call('dGitProvider', 'branch', { ref: branch }, false) +export const createNewBranch = async (branch: string) => { + const promise = plugin.call('dGitProvider', 'branch', { ref: branch, checkout: true }, false) dispatch(cloneRepositoryRequest()) promise.then(async () => { @@ -512,6 +511,55 @@ export const switchToNewBranch = async (branch: string) => { return promise } +export const checkoutRemoteBranch = async (branch: string, remote: string) => { + const localChanges = await hasLocalChanges() + + if (Array.isArray(localChanges) && localChanges.length > 0) { + const cloneModal = { + id: 'checkoutRemoteBranch', + title: 'Checkout Remote Branch', + message: `Your local changes to the following files would be overwritten by checkout.\n + ${localChanges.join('\n')}\n + Do you want to continue?`, + modalType: 'modal', + okLabel: 'Force Checkout', + okFn: async () => { + dispatch(cloneRepositoryRequest()) + plugin.call('dGitProvider', 'checkout', { ref: branch, remote, force: true }, false).then(async () => { + await fetchWorkspaceDirectory(ROOT_PATH) + dispatch(setCurrentWorkspaceCurrentBranch(branch)) + const workspacesPath = plugin.fileProviders.workspace.workspacesPath + const workspaceName = plugin.fileProviders.workspace.workspace + const branches = await getGitRepoBranches(workspacesPath + '/' + workspaceName) + + dispatch(setCurrentWorkspaceBranches(branches)) + dispatch(cloneRepositorySuccess()) + }).catch(() => { + dispatch(cloneRepositoryFailed()) + }) + }, + cancelLabel: 'Cancel', + cancelFn: () => {}, + hideFn: () => {} + } + plugin.call('notification', 'modal', cloneModal) + } else { + dispatch(cloneRepositoryRequest()) + plugin.call('dGitProvider', 'checkout', { ref: branch, remote, force: true }, false).then(async () => { + await fetchWorkspaceDirectory(ROOT_PATH) + dispatch(setCurrentWorkspaceCurrentBranch(branch)) + const workspacesPath = plugin.fileProviders.workspace.workspacesPath + const workspaceName = plugin.fileProviders.workspace.workspace + const branches = await getGitRepoBranches(workspacesPath + '/' + workspaceName) + + dispatch(setCurrentWorkspaceBranches(branches)) + dispatch(cloneRepositorySuccess()) + }).catch(() => { + dispatch(cloneRepositoryFailed()) + }) + } +} + export const hasLocalChanges = async () => { const filesStatus = await plugin.call('dGitProvider', 'status') const uncommittedFiles = getUncommittedFiles(filesStatus) diff --git a/libs/remix-ui/workspace/src/lib/contexts/index.ts b/libs/remix-ui/workspace/src/lib/contexts/index.ts index edec716765..2e199c2f60 100644 --- a/libs/remix-ui/workspace/src/lib/contexts/index.ts +++ b/libs/remix-ui/workspace/src/lib/contexts/index.ts @@ -35,6 +35,7 @@ export const FileSystemContext = createContext<{ dispatchMoveFolder: (src: string, dest: string) => Promise, dispatchShowAllBranches: () => Promise, dispatchSwitchToBranch: (branch: string) => Promise, - dispatchSwitchToNewBranch: (branch: string) => Promise + dispatchCreateNewBranch: (branch: string) => Promise, + dispatchCheckoutRemoteBranch: (branch: string, remote: string) => Promise }>(null) \ No newline at end of file diff --git a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx index b85b23b838..2b01004d50 100644 --- a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx +++ b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx @@ -8,7 +8,7 @@ import { browserReducer, browserInitialState } from '../reducers/workspace' import { initWorkspace, fetchDirectory, removeInputField, deleteWorkspace, clearPopUp, publishToGist, createNewFile, setFocusElement, createNewFolder, deletePath, renamePath, copyFile, copyFolder, runScript, emitContextMenuEvent, handleClickFile, handleExpandPath, addInputField, createWorkspace, fetchWorkspaceDirectory, renameWorkspace, switchToWorkspace, uploadFile, handleDownloadFiles, restoreBackupZip, cloneRepository, moveFile, moveFolder, - showAllBranches, switchToBranch, switchToNewBranch + showAllBranches, switchBranch, createNewBranch, checkoutRemoteBranch } from '../actions' import { Modal, WorkspaceProps, WorkspaceTemplate } from '../types' // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -144,11 +144,15 @@ export const FileSystemProvider = (props: WorkspaceProps) => { } const dispatchSwitchToBranch = async (branch: string) => { - await switchToBranch(branch) + await switchBranch(branch) } - const dispatchSwitchToNewBranch = async (branch: string) => { - await switchToNewBranch(branch) + const dispatchCreateNewBranch = async (branch: string) => { + await createNewBranch(branch) + } + + const dispatchCheckoutRemoteBranch = async (branch: string, remote: string) => { + await checkoutRemoteBranch(branch, remote) } useEffect(() => { @@ -258,7 +262,8 @@ export const FileSystemProvider = (props: WorkspaceProps) => { dispatchMoveFolder, dispatchShowAllBranches, dispatchSwitchToBranch, - dispatchSwitchToNewBranch + dispatchCreateNewBranch, + dispatchCheckoutRemoteBranch } return ( diff --git a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx index 829dc191a4..4789d73f48 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -220,18 +220,19 @@ export function Workspace () { const switchToBranch = async (branch: { remote: any, name: string }) => { try { if (branch.remote) { - await global.dispatchSwitchToNewBranch(branch.name) + await global.dispatchCheckoutRemoteBranch(branch.name, branch.remote) } else { await global.dispatchSwitchToBranch(branch.name) } } catch (e) { + console.error(e) global.modal('Checkout Git Branch', e.message, 'OK', () => {}) } } const switchToNewBranch = async () => { try { - await global.dispatchSwitchToNewBranch(branchFilter) + await global.dispatchCreateNewBranch(branchFilter) } catch (e) { global.modal('Checkout Git Branch', e.message, 'OK', () => {}) }