diff --git a/apps/remix-ide/src/app/files/dgitProvider.ts b/apps/remix-ide/src/app/files/dgitProvider.ts index 51d6a7f32d..3f0a510bd2 100644 --- a/apps/remix-ide/src/app/files/dgitProvider.ts +++ b/apps/remix-ide/src/app/files/dgitProvider.ts @@ -20,11 +20,12 @@ import { OctokitResponse } from '@octokit/types' import { Endpoints } from "@octokit/types" import { IndexedDBStorage } from './filesystems/indexedDB' import { GitHubUser, branch, commitChange, remote, userEmails } from '@remix-ui/git' -import { checkoutInputType, statusInput, logInputType, author, pagedCommits, remoteCommitsInputType, cloneInputType, fetchInputType, pullInputType, pushInputType, currentBranchInput, branchInputType, addInputType, rmInputType, resolveRefInput, readBlobInput, repositoriesInput, commitInputType, branchDifference, compareBranchesInput, initInputType} from '@remix-api' +import { checkoutInputType, statusInput, logInputType, author, pagedCommits, remoteCommitsInputType, cloneInputType, fetchInputType, pullInputType, pushInputType, currentBranchInput, branchInputType, addInputType, rmInputType, resolveRefInput, readBlobInput, repositoriesInput, commitInputType, branchDifference, compareBranchesInput, initInputType, isoGitConfig} from '@remix-api' import { LibraryProfile, StatusEvents } from '@remixproject/plugin-utils' import { ITerminal } from '@remixproject/plugin-api/src/lib/terminal' import { partial } from 'lodash' import { CustomRemixApi } from '@remix-api' +import { isoGit } from "libs/remix-git/src/isogit" declare global { interface Window { remixFileSystemCallback: IndexedDBStorage; remixFileSystem: any; } @@ -334,53 +335,7 @@ class DGitProvider extends Plugin { return result } - const result: commitChange[] = await git.walk({ - ...await this.addIsomorphicGitConfigFS(), - trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })], - map: async function (filepath, [A, B]) { - - if (filepath === '.') { - return - } - try { - if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { - return - } - } catch (e) { - // ignore - } - - // generate ids - const Aoid = A && await A.oid() || undefined - const Boid = B && await B.oid() || undefined - - const commitChange: Partial = { - hashModified: commitHash1, - hashOriginal: commitHash2, - path: filepath, - } - - // determine modification type - if (Aoid !== Boid) { - commitChange.type = "modified" - } - if (Aoid === undefined) { - commitChange.type = "deleted" - } - if (Boid === undefined || !commitHash2) { - commitChange.type = "added" - } - if (Aoid === undefined && Boid === undefined) { - commitChange.type = "unknown" - } - if (commitChange.type) - return commitChange - else - return undefined - }, - }) - - return result + return await isoGit.getCommitChanges(commitHash1, commitHash2, await this.addIsomorphicGitConfigFS()) } async remotes(): Promise { @@ -388,14 +343,7 @@ class DGitProvider extends Plugin { return await this.call('isogit', 'remotes') } - let remotes: remote[] = [] - try { - remotes = (await git.listRemotes({ ...await this.addIsomorphicGitConfigFS() })).map((remote) => { return { name: remote.remote, url: remote.url } } - ) - } catch (e) { - // do nothing - } - return remotes + return await isoGit.remotes(await this.addIsomorphicGitConfigFS()) } async branch(cmd: branchInputType): Promise { @@ -424,61 +372,20 @@ class DGitProvider extends Plugin { return await this.call('isogit', 'currentbranch', input) } - try { - const defaultConfig = await this.addIsomorphicGitConfigFS() - const cmd = input ? defaultConfig ? { ...defaultConfig, ...input } : input : defaultConfig - const name = await git.currentBranch(cmd) - let remote: remote = undefined - try { - const remoteName = await git.getConfig({ - ...defaultConfig, - path: `branch.${name}.remote` - }) - if (remoteName) { - const remoteUrl = await git.getConfig({ - ...defaultConfig, - path: `remote.${remoteName}.url` - }) - remote = { name: remoteName, url: remoteUrl } - } - - } catch (e) { - // do nothing - } - - return { - remote: remote, - name: name || '' - } - } catch (e) { - return undefined - } + const defaultConfig = await this.addIsomorphicGitConfigFS() + return await isoGit.currentbranch(input, defaultConfig) } - async branches(config): Promise { + async branches(config: isoGitConfig): Promise { if ((Registry.getInstance().get('platform').api.isDesktop())) { const branches = await this.call('isogit', 'branches') console.log(branches) return branches } - - try { - const defaultConfig = await this.addIsomorphicGitConfigFS() - const cmd = config ? defaultConfig ? { ...defaultConfig, ...config } : config : defaultConfig - const remotes = await this.remotes() - let branches: branch[] = [] - branches = (await git.listBranches(cmd)).map((branch) => { return { remote: undefined, name: branch } }) - for (const remote of remotes) { - cmd.remote = remote.name - const remotebranches = (await git.listBranches(cmd)).map((branch) => { return { remote: remote, name: branch } }) - branches = [...branches, ...remotebranches] - } - return branches - } catch (e) { - console.log(e) - return [] - } + const defaultConfig = await this.addIsomorphicGitConfigFS() + const cmd = config ? defaultConfig ? { ...defaultConfig, ...config } : config : defaultConfig + return await isoGit.branches(cmd) } async commit(cmd: commitInputType): Promise { diff --git a/apps/remixdesktop/src/plugins/isoGitPlugin.ts b/apps/remixdesktop/src/plugins/isoGitPlugin.ts index 4aef5b9f36..ce6c3268ba 100644 --- a/apps/remixdesktop/src/plugins/isoGitPlugin.ts +++ b/apps/remixdesktop/src/plugins/isoGitPlugin.ts @@ -351,20 +351,13 @@ class IsoGitPluginClient extends ElectronBasePluginClient { - async remotes () { + async remotes() { console.log('REMOTES') if (!this.workingDir || this.workingDir === '') { return [] } - let remotes: remote[] = [] - try { - remotes = (await git.listRemotes({ ...await this.getGitConfig() })).map((remote) => { return { name: remote.remote, url: remote.url } } - ) - } catch (e) { - // do nothing - } - console.log('REMOTES', remotes) - return remotes + const defaultConfig = await this.getGitConfig() + return await isoGit.remotes(defaultConfig) } async currentbranch(input: currentBranchInput) { @@ -372,40 +365,8 @@ class IsoGitPluginClient extends ElectronBasePluginClient { if (!this.workingDir || this.workingDir === '') { return '' } - - try { - const defaultConfig = await this.getGitConfig() - console.log(isoGit) - const cmd = input ? defaultConfig ? { ...defaultConfig, ...input } : input : defaultConfig - - const name = await git.currentBranch(cmd) - let remote: remote = undefined - try { - const remoteName = await git.getConfig({ - ...defaultConfig, - path: `branch.${name}.remote` - }) - if (remoteName) { - const remoteUrl = await git.getConfig({ - ...defaultConfig, - path: `remote.${remoteName}.url` - }) - remote = { name: remoteName, url: remoteUrl } - } - - } catch (e) { - // do nothing - } - console.log('NAME', name) - console.log('REMOTE', remote) - - return { - remote: remote, - name: name || '' - } - } catch (e) { - return undefined - } + const defaultConfig = await this.getGitConfig() + return await isoGit.currentbranch(input, defaultConfig) } @@ -414,22 +375,9 @@ class IsoGitPluginClient extends ElectronBasePluginClient { if (!this.workingDir || this.workingDir === '') { return [] } - try { - const defaultConfig = await this.getGitConfig() - const cmd = config ? defaultConfig ? { ...defaultConfig, ...config } : config : defaultConfig - const remotes = await this.remotes() - let branches: branch[] = [] - branches = (await git.listBranches(cmd)).map((branch) => { return { remote: undefined, name: branch } }) - for (const remote of remotes) { - cmd.remote = remote.name - const remotebranches = (await git.listBranches(cmd)).map((branch) => { return { remote: remote, name: branch } }) - branches = [...branches, ...remotebranches] - } - return branches - } catch (e) { - console.log(e) - return [] - } + + const defaultConfig = await this.getGitConfig() + return await isoGit.branches(defaultConfig) } @@ -438,53 +386,7 @@ class IsoGitPluginClient extends ElectronBasePluginClient { } async getCommitChanges(commitHash1: string, commitHash2: string): Promise { - const result: commitChange[] = await git.walk({ - ...await this.getGitConfig(), - trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })], - map: async function (filepath, [A, B]) { - - if (filepath === '.') { - return - } - try { - if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { - return - } - } catch (e) { - // ignore - } - - // generate ids - const Aoid = A && await A.oid() || undefined - const Boid = B && await B.oid() || undefined - - const commitChange: Partial = { - hashModified: commitHash1, - hashOriginal: commitHash2, - path: filepath, - } - - // determine modification type - if (Aoid !== Boid) { - commitChange.type = "modified" - } - if (Aoid === undefined) { - commitChange.type = "deleted" - } - if (Boid === undefined || !commitHash2) { - commitChange.type = "added" - } - if (Aoid === undefined && Boid === undefined) { - commitChange.type = "unknown" - } - if (commitChange.type) - return commitChange - else - return undefined - }, - }) - - return result + return await isoGit.getCommitChanges(commitHash1, commitHash2, await this.getGitConfig()) } async compareBranches({ branch, remote }: compareBranchesInput): Promise { diff --git a/apps/remixdesktop/tsconfig.json b/apps/remixdesktop/tsconfig.json index 66bf3b35e4..871cb5452b 100644 --- a/apps/remixdesktop/tsconfig.json +++ b/apps/remixdesktop/tsconfig.json @@ -20,7 +20,7 @@ "../../libs/remix-api/src/lib/types/git.ts" ], "@remix-git": [ - "../../libs/remix-git/src/index.ts", + "../../libs/remix-git/" ], }, "typeRoots": [ @@ -32,6 +32,5 @@ }, "include": [ "src/**/*", - "../../libs/remix-git/src/index.ts" ] } \ No newline at end of file diff --git a/libs/remix-git/index.ts b/libs/remix-git/index.ts index ae218b1ee3..7285bb292a 100644 --- a/libs/remix-git/index.ts +++ b/libs/remix-git/index.ts @@ -1 +1 @@ -export * from './src/index' \ No newline at end of file +export { isoGit } from './src/isogit' \ No newline at end of file diff --git a/libs/remix-git/src/index.ts b/libs/remix-git/src/index.ts deleted file mode 100644 index 9ba55884b0..0000000000 --- a/libs/remix-git/src/index.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { currentBranchInput, isoGitConfig, remote } from "@remix-api" -import git from 'isomorphic-git' - -const currentbranch = async (input: currentBranchInput, defaultConfig: isoGitConfig ) => { - console.log('CURRENT BRANCH', input) - - try { - const cmd = input ? defaultConfig ? { ...defaultConfig, ...input } : input : defaultConfig - - const name = await git.currentBranch(cmd) - let remote: remote = undefined - try { - const remoteName = await git.getConfig({ - ...defaultConfig, - path: `branch.${name}.remote` - }) - if (remoteName) { - const remoteUrl = await git.getConfig({ - ...defaultConfig, - path: `remote.${remoteName}.url` - }) - remote = { name: remoteName, url: remoteUrl } - } - - } catch (e) { - // do nothing - } - console.log('NAME', name) - console.log('REMOTE', remote) - - return { - remote: remote, - name: name || '' - } - } catch (e) { - return undefined - } - } - - export const isoGit = { - currentbranch - } \ No newline at end of file diff --git a/libs/remix-git/src/isogit.ts b/libs/remix-git/src/isogit.ts new file mode 100644 index 0000000000..44ad3fcb3e --- /dev/null +++ b/libs/remix-git/src/isogit.ts @@ -0,0 +1,128 @@ +import { branch, commitChange, currentBranchInput, isoGitConfig, remote } from "@remix-api" +import git from 'isomorphic-git' + +const currentbranch = async (input: currentBranchInput, defaultConfig: isoGitConfig ) => { + console.log('CURRENT BRANCH', input) + + try { + const cmd = input ? defaultConfig ? { ...defaultConfig, ...input } : input : defaultConfig + + const name = await git.currentBranch(cmd) + let remote: remote = undefined + try { + const remoteName = await git.getConfig({ + ...defaultConfig, + path: `branch.${name}.remote` + }) + if (remoteName) { + const remoteUrl = await git.getConfig({ + ...defaultConfig, + path: `remote.${remoteName}.url` + }) + remote = { name: remoteName, url: remoteUrl } + } + + } catch (e) { + // do nothing + } + console.log('NAME', name) + console.log('REMOTE', remote) + + return { + remote: remote, + name: name || '' + } + } catch (e) { + return undefined + } + } + + const branches = async (defaultConfig: isoGitConfig ) => { + try { + + const remotes = await isoGit.remotes(defaultConfig) + let branches: branch[] = [] + branches = (await git.listBranches(defaultConfig)).map((branch) => { return { remote: undefined, name: branch } }) + for (const remote of remotes) { + const cmd = { + ...defaultConfig, + remote: remote.name + } + const remotebranches = (await git.listBranches(cmd)).map((branch) => { return { remote: remote, name: branch } }) + branches = [...branches, ...remotebranches] + } + return branches + } catch (e) { + console.log(e) + return [] + } + } + + const remotes = async(defaultConfig: isoGitConfig) => { + + let remotes: remote[] = [] + try { + remotes = (await git.listRemotes({ ...defaultConfig })).map((remote) => { return { name: remote.remote, url: remote.url } } + ) + } catch (e) { + // do nothing + } + return remotes + } + + const getCommitChanges = async (commitHash1: string, commitHash2: string, defaultConfig: isoGitConfig) => { + const result: commitChange[] = await git.walk({ + ...defaultConfig, + trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })], + map: async function (filepath, [A, B]) { + + if (filepath === '.') { + return + } + try { + if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { + return + } + } catch (e) { + // ignore + } + + // generate ids + const Aoid = A && await A.oid() || undefined + const Boid = B && await B.oid() || undefined + + const commitChange: Partial = { + hashModified: commitHash1, + hashOriginal: commitHash2, + path: filepath, + } + + // determine modification type + if (Aoid !== Boid) { + commitChange.type = "modified" + } + if (Aoid === undefined) { + commitChange.type = "deleted" + } + if (Boid === undefined || !commitHash2) { + commitChange.type = "added" + } + if (Aoid === undefined && Boid === undefined) { + commitChange.type = "unknown" + } + if (commitChange.type) + return commitChange + else + return undefined + }, + }) + + return result + } + + export const isoGit = { + currentbranch, + remotes, + branches, + getCommitChanges + } \ No newline at end of file diff --git a/libs/remix-ui/git/src/lib/gitactions.ts b/libs/remix-ui/git/src/lib/gitactions.ts index 3362e00d8d..d27fe7b3f5 100644 --- a/libs/remix-ui/git/src/lib/gitactions.ts +++ b/libs/remix-ui/git/src/lib/gitactions.ts @@ -48,7 +48,7 @@ export const init = async () => { export const getBranches = async () => { - const branches = await plugin.call('dgitApi', "branches") + const branches = await plugin.call('dgitApi', 'branches') dispatch(setBranches(branches)); } diff --git a/tsconfig.paths.json b/tsconfig.paths.json index 3b4433ee92..83c51dba1b 100644 --- a/tsconfig.paths.json +++ b/tsconfig.paths.json @@ -180,6 +180,9 @@ ], "@remix-api": [ "libs/remix-api/src/index.ts" + ], + "@remix-git": [ + "libs/remix-git/src/index.ts" ] } }