From 8b454795360cb046677ebce1cafc50decff5e01f Mon Sep 17 00:00:00 2001 From: David Disu Date: Fri, 16 Sep 2022 15:02:23 +0100 Subject: [PATCH] List uncommitted files --- apps/remix-ide/src/app/files/dgitProvider.js | 1 - .../workspace/src/lib/actions/workspace.ts | 80 +++++++++++-------- .../src/lib/utils/gitStatusFilter.ts | 8 ++ 3 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 libs/remix-ui/workspace/src/lib/utils/gitStatusFilter.ts diff --git a/apps/remix-ide/src/app/files/dgitProvider.js b/apps/remix-ide/src/app/files/dgitProvider.js index 3522188f74..c8e8753ad1 100644 --- a/apps/remix-ide/src/app/files/dgitProvider.js +++ b/apps/remix-ide/src/app/files/dgitProvider.js @@ -9,7 +9,6 @@ import { saveAs } from 'file-saver' import http from 'isomorphic-git/http/web' -import { IndexedDBStorage } from './filesystems/indexedDB' const JSZip = require('jszip') const path = require('path') diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index 898a73d046..c99ffd9e1e 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -10,6 +10,7 @@ import * as templateWithContent from '@remix-project/remix-ws-templates' import { ROOT_PATH } from '../utils/constants' // eslint-disable-next-line @nrwl/nx/enforce-module-boundaries import { IndexedDBStorage } from '../../../../../../apps/remix-ide/src/app/files/filesystems/indexedDB' +import { getUncommittedFiles } from '../utils/gitStatusFilter' declare global { interface Window { remixFileSystemCallback: IndexedDBStorage; } @@ -431,7 +432,7 @@ export const getGitRepoBranches = async (workspacePath: string) => { fs: window.remixFileSystemCallback, dir: addSlash(workspacePath) } - const branches: { remote: any; name: string; }[] = await plugin.call('dGitProvider', 'branches', gitConfig) + const branches: { remote: any; name: string; }[] = await plugin.call('dGitProvider', 'branches', { ...gitConfig }) return branches } @@ -441,7 +442,7 @@ export const getGitRepoCurrentBranch = async (workspaceName: string) => { fs: window.remixFileSystemCallback, dir: addSlash(workspaceName) } - const currentBranch: string = await plugin.call('dGitProvider', 'currentbranch', gitConfig) + const currentBranch: string = await plugin.call('dGitProvider', 'currentbranch', { ...gitConfig }) return currentBranch } @@ -454,36 +455,56 @@ export const showAllBranches = async () => { } export const switchToBranch = async (branch: string) => { - const gitConfig = { - ref: branch + const localChanges = await hasLocalChanges() + + if (Array.isArray(localChanges) && localChanges.length > 0) { + const cloneModal = { + id: 'switchBranch', + title: 'Switch Git 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, force: true }, false).then(async () => { + await fetchWorkspaceDirectory(ROOT_PATH) + dispatch(setCurrentWorkspaceCurrentBranch(branch)) + dispatch(cloneRepositorySuccess()) + }).catch(() => { + dispatch(cloneRepositoryFailed()) + }) + }, + cancelLabel: 'Cancel', + cancelFn: () => {}, + hideFn: () => {} + } + plugin.call('notification', 'modal', cloneModal) + } else { + dispatch(cloneRepositoryRequest()) + plugin.call('dGitProvider', 'checkout', { ref: branch, force: true }, false).then(async () => { + await fetchWorkspaceDirectory(ROOT_PATH) + dispatch(setCurrentWorkspaceCurrentBranch(branch)) + dispatch(cloneRepositorySuccess()) + }).catch(() => { + dispatch(cloneRepositoryFailed()) + }) } - const promise = plugin.call('dGitProvider', 'checkout', gitConfig, false) - - dispatch(cloneRepositoryRequest()) - promise.then(async () => { - await fetchWorkspaceDirectory(ROOT_PATH) - dispatch(setCurrentWorkspaceCurrentBranch(branch)) - dispatch(cloneRepositorySuccess()) - }).catch(() => { - dispatch(cloneRepositoryFailed()) - }) - return promise } export const switchToNewBranch = async (branch: string) => { - const gitConfig = { - ref: branch - } - const promise = plugin.call('dGitProvider', 'branch', gitConfig, false) + const promise = plugin.call('dGitProvider', 'branch', { ref: branch }, false) dispatch(cloneRepositoryRequest()) promise.then(async () => { await fetchWorkspaceDirectory(ROOT_PATH) dispatch(setCurrentWorkspaceCurrentBranch(branch)) - // const workspacesPath = plugin.fileProviders.workspace.workspacesPath - // const branches = await getGitRepoBranches(workspacesPath + '/' + branch) + const workspacesPath = plugin.fileProviders.workspace.workspacesPath + const workspaceName = plugin.fileProviders.workspace.workspace + const branches = await getGitRepoBranches(workspacesPath + '/' + workspaceName) - // dispatch(setCurrentWorkspaceBranches(branches)) + dispatch(setCurrentWorkspaceBranches(branches)) dispatch(cloneRepositorySuccess()) }).catch(() => { dispatch(cloneRepositoryFailed()) @@ -491,16 +512,9 @@ export const switchToNewBranch = async (branch: string) => { return promise } -export const hasLocalChanges = async (branch: string) => { - const staged = await plugin.call('dGitProvider', 'lsfiles', { ref: branch }) - const untracked = await plugin.call('dGitProvider', 'unstagedStatus', { ref: branch }) - const deleted = await plugin.call('dGitProvider', 'deleteStatus', { ref: branch }) - const modified = await plugin.call('dGitProvider', 'modifiedStatus', { ref: branch }) - - console.log('staged: ', staged) - console.log('untracked: ', untracked) - console.log('deleted: ', deleted) - console.log('modified: ', modified) +export const hasLocalChanges = async () => { + const filesStatus = await plugin.call('dGitProvider', 'status') + const uncommittedFiles = getUncommittedFiles(filesStatus) - return deleted.length > 0 || staged.length > 0 || untracked.length > 0 || modified.length > 0 + return uncommittedFiles } diff --git a/libs/remix-ui/workspace/src/lib/utils/gitStatusFilter.ts b/libs/remix-ui/workspace/src/lib/utils/gitStatusFilter.ts new file mode 100644 index 0000000000..1b95e12538 --- /dev/null +++ b/libs/remix-ui/workspace/src/lib/utils/gitStatusFilter.ts @@ -0,0 +1,8 @@ +const FILE = 0, HEAD = 1, WORKDIR = 2, STAGE = 3 + +export const getUncommittedFiles = (statusMatrix: Array>) => { + statusMatrix = statusMatrix.filter(row => (row[HEAD] !== row[WORKDIR]) || (row[HEAD] !== row[STAGE])) + const uncommitedFiles = statusMatrix.map(row => row[FILE]) + + return uncommitedFiles +} \ No newline at end of file