fetch branches

pull/2879/head
David Disu 2 years ago
parent a70b09d53f
commit 9f0edcbb7d
  1. 16
      apps/remix-ide/src/app/files/dgitProvider.js
  2. 5
      libs/remix-ui/helper/src/lib/remix-ui-helper.ts
  3. 2
      libs/remix-ui/workspace/src/lib/actions/payload.ts
  4. 76
      libs/remix-ui/workspace/src/lib/actions/workspace.ts
  5. 17
      libs/remix-ui/workspace/src/lib/reducers/workspace.ts
  6. 2
      libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx

@ -9,6 +9,7 @@ import {
saveAs saveAs
} from 'file-saver' } from 'file-saver'
import http from 'isomorphic-git/http/web' import http from 'isomorphic-git/http/web'
import { IndexedDBStorage } from './filesystems/indexedDB'
const JSZip = require('jszip') const JSZip = require('jszip')
const path = require('path') const path = require('path')
@ -145,17 +146,16 @@ class DGitProvider extends Plugin {
return status return status
} }
async currentbranch () { async currentbranch (config) {
const name = await git.currentBranch({ console.log('config: ', config)
...await this.getGitConfig() const cmd = config ? config : await this.getGitConfig()
}) const name = await git.currentBranch(cmd)
return name return name
} }
async branches () { async branches (config) {
const cmd = { const cmd = config ? config : await this.getGitConfig()
...await this.getGitConfig()
}
const remotes = await this.remotes() const remotes = await this.remotes()
let branches = [] let branches = []
branches = (await git.listBranches(cmd)).map((branch) => { return { remote: undefined, name: branch } }) branches = (await git.listBranches(cmd)).map((branch) => { return { remote: undefined, name: branch } })

@ -123,3 +123,8 @@ export const shortenHexData = (data) => {
const len = data.length const len = data.length
return data.slice(0, 5) + '...' + data.slice(len - 5, len) return data.slice(0, 5) + '...' + data.slice(len - 5, len)
} }
export const addSlash = (file: string) => {
if (!file.startsWith('/'))file = '/' + file
return file
}

@ -126,7 +126,7 @@ export const createWorkspaceRequest = (promise: Promise<any>) => {
} }
} }
export const createWorkspaceSuccess = (workspaceName: { name: string; isGitRepo: boolean; }) => { export const createWorkspaceSuccess = (workspaceName: { name: string; isGitRepo: boolean; branches?: { remote: any; name: string; }[], currentBranch?: string }) => {
return { return {
type: 'CREATE_WORKSPACE_SUCCESS', type: 'CREATE_WORKSPACE_SUCCESS',
payload: workspaceName payload: workspaceName

@ -2,12 +2,18 @@ import React from 'react'
import { bufferToHex, keccakFromString } from 'ethereumjs-util' import { bufferToHex, keccakFromString } from 'ethereumjs-util'
import axios, { AxiosResponse } from 'axios' 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 { 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 { JSONStandardInput, WorkspaceTemplate } from '../types'
import { QueryParams } from '@remix-project/remix-lib' import { QueryParams } from '@remix-project/remix-lib'
import * as templateWithContent from '@remix-project/remix-ws-templates' import * as templateWithContent from '@remix-project/remix-ws-templates'
import { ROOT_PATH } from '../utils/constants' 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 - ' const LOCALHOST = ' - connect to localhost - '
@ -48,6 +54,25 @@ export const createWorkspace = async (workspaceName: string, workspaceTemplateNa
const promise = createWorkspaceTemplate(workspaceName, workspaceTemplateName) const promise = createWorkspaceTemplate(workspaceName, workspaceTemplateName)
dispatch(createWorkspaceRequest(promise)) dispatch(createWorkspaceRequest(promise))
promise.then(async () => { 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 })) dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo }))
await plugin.setWorkspace({ name: workspaceName, isLocalhost: false }) await plugin.setWorkspace({ name: workspaceName, isLocalhost: false })
await plugin.setWorkspaces(await getWorkspaces()) await plugin.setWorkspaces(await getWorkspaces())
@ -263,7 +288,7 @@ export const switchToWorkspace = async (name: string) => {
if (isActive) await plugin.call('manager', 'deactivatePlugin', 'remixd') if (isActive) await plugin.call('manager', 'deactivatePlugin', 'remixd')
await plugin.fileProviders.workspace.setWorkspace(name) await plugin.fileProviders.workspace.setWorkspace(name)
await plugin.setWorkspace({ name, isLocalhost: false }) await plugin.setWorkspace({ name, isLocalhost: false })
const isGitRepo = await plugin.fileManager.isGitRepo() const isGitRepo = await plugin.fileManager.isGitRepo(name)
dispatch(setMode('browser')) dispatch(setMode('browser'))
dispatch(setCurrentWorkspace({ name, isGitRepo })) dispatch(setCurrentWorkspace({ name, isGitRepo }))
@ -325,11 +350,38 @@ export const getWorkspaces = async (): Promise<{name: string, isGitRepo: boolean
Promise.all(Object.keys(items) Promise.all(Object.keys(items)
.filter((item) => items[item].isDirectory) .filter((item) => items[item].isDirectory)
.map(async (folder) => { .map(async (folder) => {
console.log('folder: ', folder)
const isGitRepo: boolean = await plugin.fileProviders.browser.exists('/' + folder + '/.git') const isGitRepo: boolean = await plugin.fileProviders.browser.exists('/' + folder + '/.git')
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 { return {
name: folder.replace(workspacesPath + '/', ''), name: folder.replace(workspacesPath + '/', ''),
isGitRepo isGitRepo
} }
}
})).then(workspacesList => resolve(workspacesList)) })).then(workspacesList => resolve(workspacesList))
}) })
}) })
@ -397,3 +449,23 @@ export const getRepositoryTitle = async (url: string) => {
return name + counter 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
}

@ -13,6 +13,11 @@ export interface BrowserState {
workspaces: { workspaces: {
name: string; name: string;
isGitRepo: boolean; isGitRepo: boolean;
branches?: {
remote: any;
name: string;
}[],
currentBranch?: string
}[], }[],
files: { [x: string]: Record<string, FileType> }, files: { [x: string]: Record<string, FileType> },
expandPath: string[] expandPath: string[]
@ -117,7 +122,7 @@ export const browserInitialState: BrowserState = {
export const browserReducer = (state = browserInitialState, action: Action) => { export const browserReducer = (state = browserInitialState, action: Action) => {
switch (action.type) { switch (action.type) {
case 'SET_CURRENT_WORKSPACE': { 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] const workspaces = state.browser.workspaces.find(({ name }) => name === payload.name) ? state.browser.workspaces : [...state.browser.workspaces, action.payload]
return { return {
@ -131,7 +136,7 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
} }
case 'SET_WORKSPACES': { 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 { return {
...state, ...state,
@ -429,7 +434,7 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
} }
case 'CREATE_WORKSPACE_SUCCESS': { 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] const workspaces = state.browser.workspaces.find(({ name }) => name === payload.name) ? state.browser.workspaces : [...state.browser.workspaces, action.payload]
return { return {
@ -460,13 +465,15 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
case 'RENAME_WORKSPACE': { case 'RENAME_WORKSPACE': {
const payload = action.payload as { oldName: string, workspaceName: string } const payload = action.payload as { oldName: string, workspaceName: string }
let renamedWorkspace 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)) { if (name && (name !== payload.oldName)) {
return true return true
} else { } else {
renamedWorkspace = { renamedWorkspace = {
name: payload.workspaceName, name: payload.workspaceName,
isGitRepo isGitRepo,
branches,
currentBranch
} }
return false return false
} }

@ -679,7 +679,7 @@ export function Workspace () {
</div> </div>
</div> </div>
</div> </div>
<div className='bg-light border-top' style={{ height: '5%' }}> <div className={`bg-light border-top ${selectedWorkspace && selectedWorkspace.isGitRepo ? 'd-block' : 'd-none'}`} style={{ height: '5%' }}>
<div className='d-flex justify-space-between p-1'> <div className='d-flex justify-space-between p-1'>
<div className="mr-auto text-uppercase text-dark pt-2 pl-2">DGIT</div> <div className="mr-auto text-uppercase text-dark pt-2 pl-2">DGIT</div>
<div className="pt-1 mr-1"> <div className="pt-1 mr-1">

Loading…
Cancel
Save