Fixed switching to remote branches

pull/5370/head
David Disu 2 years ago
parent ff367f16d0
commit c0952b0053
  1. 58
      libs/remix-ui/workspace/src/lib/actions/workspace.ts
  2. 3
      libs/remix-ui/workspace/src/lib/contexts/index.ts
  3. 15
      libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx
  4. 5
      libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx

@ -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)

@ -35,6 +35,7 @@ export const FileSystemContext = createContext<{
dispatchMoveFolder: (src: string, dest: string) => Promise<void>,
dispatchShowAllBranches: () => Promise<void>,
dispatchSwitchToBranch: (branch: string) => Promise<void>,
dispatchSwitchToNewBranch: (branch: string) => Promise<void>
dispatchCreateNewBranch: (branch: string) => Promise<void>,
dispatchCheckoutRemoteBranch: (branch: string, remote: string) => Promise<void>
}>(null)

@ -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 (
<FileSystemContext.Provider value={value}>

@ -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', () => {})
}

Loading…
Cancel
Save