From 9f0edcbb7dc59fdbdea2a25a0b596801dd0d37ea Mon Sep 17 00:00:00 2001 From: David Disu Date: Mon, 22 Aug 2022 09:22:46 +0100 Subject: [PATCH] fetch branches --- apps/remix-ide/src/app/files/dgitProvider.js | 16 ++-- .../helper/src/lib/remix-ui-helper.ts | 5 ++ .../workspace/src/lib/actions/payload.ts | 2 +- .../workspace/src/lib/actions/workspace.ts | 82 +++++++++++++++++-- .../workspace/src/lib/reducers/workspace.ts | 17 ++-- .../workspace/src/lib/remix-ui-workspace.tsx | 2 +- 6 files changed, 104 insertions(+), 20 deletions(-) diff --git a/apps/remix-ide/src/app/files/dgitProvider.js b/apps/remix-ide/src/app/files/dgitProvider.js index 28f7fb2715..019caec3c7 100644 --- a/apps/remix-ide/src/app/files/dgitProvider.js +++ b/apps/remix-ide/src/app/files/dgitProvider.js @@ -9,6 +9,7 @@ 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') @@ -145,17 +146,16 @@ class DGitProvider extends Plugin { return status } - async currentbranch () { - const name = await git.currentBranch({ - ...await this.getGitConfig() - }) + async currentbranch (config) { + console.log('config: ', config) + const cmd = config ? config : await this.getGitConfig() + const name = await git.currentBranch(cmd) + return name } - async branches () { - const cmd = { - ...await this.getGitConfig() - } + async branches (config) { + const cmd = config ? config : await this.getGitConfig() const remotes = await this.remotes() let branches = [] branches = (await git.listBranches(cmd)).map((branch) => { return { remote: undefined, name: branch } }) diff --git a/libs/remix-ui/helper/src/lib/remix-ui-helper.ts b/libs/remix-ui/helper/src/lib/remix-ui-helper.ts index d9fd9b03b9..02a688b9a6 100644 --- a/libs/remix-ui/helper/src/lib/remix-ui-helper.ts +++ b/libs/remix-ui/helper/src/lib/remix-ui-helper.ts @@ -123,3 +123,8 @@ export const shortenHexData = (data) => { const len = data.length return data.slice(0, 5) + '...' + data.slice(len - 5, len) } + +export const addSlash = (file: string) => { + if (!file.startsWith('/'))file = '/' + file + return file +} diff --git a/libs/remix-ui/workspace/src/lib/actions/payload.ts b/libs/remix-ui/workspace/src/lib/actions/payload.ts index 8c3fb8fc18..e9acca3087 100644 --- a/libs/remix-ui/workspace/src/lib/actions/payload.ts +++ b/libs/remix-ui/workspace/src/lib/actions/payload.ts @@ -126,7 +126,7 @@ export const createWorkspaceRequest = (promise: Promise) => { } } -export const createWorkspaceSuccess = (workspaceName: { name: string; isGitRepo: boolean; }) => { +export const createWorkspaceSuccess = (workspaceName: { name: string; isGitRepo: boolean; branches?: { remote: any; name: string; }[], currentBranch?: string }) => { return { type: 'CREATE_WORKSPACE_SUCCESS', payload: workspaceName diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index 0495875a87..f12fcd4209 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -2,12 +2,18 @@ import React from 'react' import { bufferToHex, keccakFromString } from 'ethereumjs-util' import axios, { AxiosResponse } from 'axios' import { addInputFieldSuccess, cloneRepositoryFailed, cloneRepositoryRequest, cloneRepositorySuccess, createWorkspaceError, createWorkspaceRequest, createWorkspaceSuccess, displayNotification, displayPopUp, fetchWorkspaceDirectoryError, fetchWorkspaceDirectoryRequest, fetchWorkspaceDirectorySuccess, hideNotification, setCurrentWorkspace, setDeleteWorkspace, setMode, setReadOnlyMode, setRenameWorkspace } from './payload' -import { checkSlash, checkSpecialChars } from '@remix-ui/helper' +import { addSlash, checkSlash, checkSpecialChars } from '@remix-ui/helper' import { JSONStandardInput, WorkspaceTemplate } from '../types' import { QueryParams } from '@remix-project/remix-lib' 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' + +declare global { + interface Window { remixFileSystemCallback: IndexedDBStorage; } +} const LOCALHOST = ' - connect to localhost - ' @@ -48,6 +54,25 @@ export const createWorkspace = async (workspaceName: string, workspaceTemplateNa const promise = createWorkspaceTemplate(workspaceName, workspaceTemplateName) dispatch(createWorkspaceRequest(promise)) promise.then(async () => { + if (isGitRepo) { + let branches = [] + let currentBranch = null + + try { + branches = await getGitRepoBranches(`${plugin.fileProviders.workspace.workspacesPath}/${workspaceName}`) + } catch (e) { + console.error(e) + } + try { + currentBranch = await getGitRepoCurrentBranch(`${plugin.fileProviders.workspace.workspacesPath}/${workspaceName}`) + } catch (e) { + console.error(e) + } + + dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo, branches, currentBranch })) + } else { + dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo })) + } dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo })) await plugin.setWorkspace({ name: workspaceName, isLocalhost: false }) await plugin.setWorkspaces(await getWorkspaces()) @@ -263,7 +288,7 @@ export const switchToWorkspace = async (name: string) => { if (isActive) await plugin.call('manager', 'deactivatePlugin', 'remixd') await plugin.fileProviders.workspace.setWorkspace(name) await plugin.setWorkspace({ name, isLocalhost: false }) - const isGitRepo = await plugin.fileManager.isGitRepo() + const isGitRepo = await plugin.fileManager.isGitRepo(name) dispatch(setMode('browser')) dispatch(setCurrentWorkspace({ name, isGitRepo })) @@ -325,10 +350,37 @@ export const getWorkspaces = async (): Promise<{name: string, isGitRepo: boolean Promise.all(Object.keys(items) .filter((item) => items[item].isDirectory) .map(async (folder) => { + console.log('folder: ', folder) const isGitRepo: boolean = await plugin.fileProviders.browser.exists('/' + folder + '/.git') - return { - name: folder.replace(workspacesPath + '/', ''), - isGitRepo + + if (isGitRepo) { + let branches = [] + let currentBranch = null + + try { + branches = await getGitRepoBranches(folder) + } catch (e) { + console.error(e) + } + try { + currentBranch = await getGitRepoCurrentBranch(folder) + } catch (e) { + console.error(e) + } + + console.log('branches: ', branches) + + return { + name: folder.replace(workspacesPath + '/', ''), + isGitRepo, + branches, + currentBranch + } + } else { + return { + name: folder.replace(workspacesPath + '/', ''), + isGitRepo + } } })).then(workspacesList => resolve(workspacesList)) }) @@ -397,3 +449,23 @@ export const getRepositoryTitle = async (url: string) => { return name + counter } + +export const getGitRepoBranches = async (workspacePath: string) => { + const gitConfig: { fs: IndexedDBStorage, dir: string } = { + fs: window.remixFileSystemCallback, + dir: addSlash(workspacePath) + } + const branches: { remote: any; name: string; }[] = await plugin.call('dGitProvider', 'branches', gitConfig) + + return branches +} + +export const getGitRepoCurrentBranch = async (workspaceName: string) => { + const gitConfig: { fs: IndexedDBStorage, dir: string } = { + fs: window.remixFileSystemCallback, + dir: addSlash(workspaceName) + } + const currentBranch: string = await plugin.call('dGitProvider', 'currentbranch', gitConfig) + + return currentBranch +} diff --git a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts index af20d18fa2..ba78892d51 100644 --- a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts @@ -13,6 +13,11 @@ export interface BrowserState { workspaces: { name: string; isGitRepo: boolean; + branches?: { + remote: any; + name: string; + }[], + currentBranch?: string }[], files: { [x: string]: Record }, expandPath: string[] @@ -117,7 +122,7 @@ export const browserInitialState: BrowserState = { export const browserReducer = (state = browserInitialState, action: Action) => { switch (action.type) { case 'SET_CURRENT_WORKSPACE': { - const payload = action.payload as { name: string; isGitRepo: boolean; } + const payload = action.payload as { name: string; isGitRepo: boolean; branches?: { remote: any; name: string; }[], currentBranch?: string } const workspaces = state.browser.workspaces.find(({ name }) => name === payload.name) ? state.browser.workspaces : [...state.browser.workspaces, action.payload] return { @@ -131,7 +136,7 @@ export const browserReducer = (state = browserInitialState, action: Action) => { } case 'SET_WORKSPACES': { - const payload = action.payload as { name: string; isGitRepo: boolean; }[] + const payload = action.payload as { name: string; isGitRepo: boolean; branches?: { remote: any; name: string; }[], currentBranch?: string }[] return { ...state, @@ -429,7 +434,7 @@ export const browserReducer = (state = browserInitialState, action: Action) => { } case 'CREATE_WORKSPACE_SUCCESS': { - const payload = action.payload as { name: string; isGitRepo: boolean; } + const payload = action.payload as { name: string; isGitRepo: boolean; branches?: { remote: any; name: string; }[], currentBranch?: string } const workspaces = state.browser.workspaces.find(({ name }) => name === payload.name) ? state.browser.workspaces : [...state.browser.workspaces, action.payload] return { @@ -460,13 +465,15 @@ export const browserReducer = (state = browserInitialState, action: Action) => { case 'RENAME_WORKSPACE': { const payload = action.payload as { oldName: string, workspaceName: string } let renamedWorkspace - const workspaces = state.browser.workspaces.filter(({ name, isGitRepo }) => { + const workspaces = state.browser.workspaces.filter(({ name, isGitRepo, branches, currentBranch }) => { if (name && (name !== payload.oldName)) { return true } else { renamedWorkspace = { name: payload.workspaceName, - isGitRepo + isGitRepo, + branches, + currentBranch } return false } 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 5f368f9ba2..e953fcbaea 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -679,7 +679,7 @@ export function Workspace () { -
+
DGIT