parent
252bde448e
commit
47c086996b
@ -1,159 +1,329 @@ |
|||||||
import { branch, commitChange, compareBranchesInput, currentBranchInput, isoGitConfig, remote } from "@remix-api" |
import { GitHubUser, author, branch, commitChange, compareBranchesInput, currentBranchInput, fetchInputType, isoGitFSConfig, isoGitProxyConfig, pullInputType, pushInputType, remote, userEmails } from "@remix-api" |
||||||
import git from 'isomorphic-git' |
import git from 'isomorphic-git' |
||||||
|
import { |
||||||
|
Plugin |
||||||
|
} from '@remixproject/engine' |
||||||
|
import http from 'isomorphic-git/http/web' |
||||||
|
|
||||||
const currentbranch = async (input: currentBranchInput, defaultConfig: isoGitConfig ) => { |
import { Octokit } from "octokit" |
||||||
console.log('CURRENT BRANCH', input) |
import { ElectronBasePluginClient } from "@remixproject/plugin-electron" |
||||||
|
const currentbranch = async (input: currentBranchInput, fsConfig: isoGitFSConfig) => { |
||||||
|
console.log('CURRENT BRANCH', input) |
||||||
|
|
||||||
|
try { |
||||||
|
const cmd = input ? fsConfig ? { ...fsConfig, ...input } : input : fsConfig |
||||||
|
|
||||||
|
const name = await git.currentBranch(cmd) |
||||||
|
let remote: remote = undefined |
||||||
try { |
try { |
||||||
const cmd = input ? defaultConfig ? { ...defaultConfig, ...input } : input : defaultConfig |
const remoteName = await git.getConfig({ |
||||||
|
...fsConfig, |
||||||
const name = await git.currentBranch(cmd) |
path: `branch.${name}.remote` |
||||||
let remote: remote = undefined |
}) |
||||||
try { |
if (remoteName) { |
||||||
const remoteName = await git.getConfig({ |
const remoteUrl = await git.getConfig({ |
||||||
...defaultConfig, |
...fsConfig, |
||||||
path: `branch.${name}.remote` |
path: `remote.${remoteName}.url` |
||||||
}) |
}) |
||||||
if (remoteName) { |
remote = { name: remoteName, url: remoteUrl } |
||||||
const remoteUrl = await git.getConfig({ |
} |
||||||
...defaultConfig, |
|
||||||
path: `remote.${remoteName}.url` |
|
||||||
}) |
|
||||||
remote = { name: remoteName, url: remoteUrl } |
|
||||||
} |
|
||||||
|
|
||||||
} catch (e) { |
} catch (e) { |
||||||
// do nothing
|
// do nothing
|
||||||
|
} |
||||||
|
console.log('NAME', name) |
||||||
|
console.log('REMOTE', remote) |
||||||
|
|
||||||
|
return { |
||||||
|
remote: remote, |
||||||
|
name: name || '' |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
return undefined |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const branches = async (fsConfig: isoGitFSConfig) => { |
||||||
|
try { |
||||||
|
|
||||||
|
const remotes = await isoGit.remotes(fsConfig) |
||||||
|
let branches: branch[] = [] |
||||||
|
branches = (await git.listBranches(fsConfig)).map((branch) => { return { remote: undefined, name: branch } }) |
||||||
|
for (const remote of remotes) { |
||||||
|
const cmd = { |
||||||
|
...fsConfig, |
||||||
|
remote: remote.name |
||||||
} |
} |
||||||
console.log('NAME', name) |
const remotebranches = (await git.listBranches(cmd)).map((branch) => { return { remote: remote, name: branch } }) |
||||||
console.log('REMOTE', remote) |
branches = [...branches, ...remotebranches] |
||||||
|
} |
||||||
return { |
return branches |
||||||
remote: remote, |
} catch (e) { |
||||||
name: name || '' |
console.log(e) |
||||||
|
return [] |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const remotes = async (fsConfig: isoGitFSConfig) => { |
||||||
|
|
||||||
|
let remotes: remote[] = [] |
||||||
|
try { |
||||||
|
remotes = (await git.listRemotes({ ...fsConfig })).map((remote) => { return { name: remote.remote, url: remote.url } } |
||||||
|
) |
||||||
|
} catch (e) { |
||||||
|
// do nothing
|
||||||
|
} |
||||||
|
return remotes |
||||||
|
} |
||||||
|
|
||||||
|
const push = async (input: pushInputType, fsConfig: isoGitFSConfig, plugin: Plugin | ElectronBasePluginClient) => { |
||||||
|
const cmd = { |
||||||
|
force: input.force, |
||||||
|
ref: input.ref.name, |
||||||
|
remoteRef: input.remoteRef && input.remoteRef.name, |
||||||
|
remote: input.remote.name, |
||||||
|
author: await getAuthor(input, plugin), |
||||||
|
input, |
||||||
|
} |
||||||
|
|
||||||
|
const proxy = await isoGit.addIsomorphicGitProxyConfig(input, plugin) |
||||||
|
console.log({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
return await git.push({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
} |
||||||
|
|
||||||
|
const pull = async (input: pullInputType, fsConfig: isoGitFSConfig, plugin: Plugin | ElectronBasePluginClient) => { |
||||||
|
const cmd = { |
||||||
|
ref: input.ref.name, |
||||||
|
remoteRef: input.remoteRef && input.remoteRef.name, |
||||||
|
author: await getAuthor(input, plugin), |
||||||
|
remote: input.remote.name, |
||||||
|
input, |
||||||
|
} |
||||||
|
const proxy = await isoGit.addIsomorphicGitProxyConfig(input, plugin) |
||||||
|
console.log({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
return await git.pull({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
} |
||||||
|
|
||||||
|
const fetch = async (input: fetchInputType, fsConfig: isoGitFSConfig, plugin: Plugin | ElectronBasePluginClient) => { |
||||||
|
const cmd = { |
||||||
|
ref: input.ref && input.ref.name, |
||||||
|
remoteRef: input.remoteRef && input.remoteRef.name, |
||||||
|
author: await getAuthor(input, plugin), |
||||||
|
remote: input.remote && input.remote.name, |
||||||
|
depth: input.depth || 5, |
||||||
|
singleBranch: input.singleBranch, |
||||||
|
relative: input.relative, |
||||||
|
input |
||||||
|
} |
||||||
|
const proxy = await isoGit.addIsomorphicGitProxyConfig(input, plugin) |
||||||
|
console.log({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
return await git.fetch({ ...fsConfig, ...cmd, ...proxy }) |
||||||
|
} |
||||||
|
|
||||||
|
const getAuthor = async (input, plugin: any) => { |
||||||
|
const author: author = { |
||||||
|
name: '', |
||||||
|
email: '' |
||||||
|
} |
||||||
|
if (input && input.name && input.email) { |
||||||
|
author.name = input.name |
||||||
|
author.email = input.email |
||||||
|
} else { |
||||||
|
const username = await plugin.call('config' as any, 'getAppParameter', 'settings/github-user-name') |
||||||
|
const email = await plugin.call('config' as any, 'getAppParameter', 'settings/github-email') |
||||||
|
const token = await plugin.call('config' as any, 'getAppParameter', 'settings/gist-access-token') |
||||||
|
if (username && email) { |
||||||
|
author.name = username |
||||||
|
author.email = email |
||||||
|
} else if (token) { |
||||||
|
|
||||||
|
const gitHubUser = await isoGit.getGitHubUser({ token }) |
||||||
|
|
||||||
|
if (gitHubUser) { |
||||||
|
author.name = gitHubUser.user.login |
||||||
} |
} |
||||||
} catch (e) { |
|
||||||
return undefined |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
return author |
||||||
|
} |
||||||
|
|
||||||
const branches = async (defaultConfig: isoGitConfig ) => { |
|
||||||
try { |
|
||||||
|
|
||||||
const remotes = await isoGit.remotes(defaultConfig) |
const getGitHubUser = async (input: { token: string }): Promise<{ |
||||||
let branches: branch[] = [] |
user: GitHubUser, |
||||||
branches = (await git.listBranches(defaultConfig)).map((branch) => { return { remote: undefined, name: branch } }) |
emails: userEmails, |
||||||
for (const remote of remotes) { |
scopes: string[] |
||||||
const cmd = { |
}> => { |
||||||
...defaultConfig, |
try { |
||||||
remote: remote.name |
const octokit = new Octokit({ |
||||||
} |
auth: input.token |
||||||
const remotebranches = (await git.listBranches(cmd)).map((branch) => { return { remote: remote, name: branch } }) |
}) |
||||||
branches = [...branches, ...remotebranches] |
|
||||||
|
const user = await octokit.request('GET /user') |
||||||
|
const emails = await octokit.request('GET /user/emails') |
||||||
|
|
||||||
|
const scopes = user.headers['x-oauth-scopes'] || '' |
||||||
|
|
||||||
|
console.log('USER', user.data) |
||||||
|
|
||||||
|
return { |
||||||
|
user: user.data, |
||||||
|
emails: emails.data, |
||||||
|
scopes: scopes && scopes.split(',') |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
return null |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const addIsomorphicGitProxyConfig = async (input: { |
||||||
|
url?: string, |
||||||
|
remote?: remote, |
||||||
|
provider?: 'github' | 'localhost', |
||||||
|
token?: string, |
||||||
|
}, plugin: any) => { |
||||||
|
|
||||||
|
const token = await plugin.call('config' as any, 'getAppParameter', 'settings/gist-access-token') |
||||||
|
console.log('TOKEN', token) |
||||||
|
|
||||||
|
let config: isoGitProxyConfig = { |
||||||
|
corsProxy: 'https://corsproxy.remixproject.org/', |
||||||
|
http, |
||||||
|
onAuth: url => { |
||||||
|
url |
||||||
|
const auth = { |
||||||
|
username: input.token || token, |
||||||
|
password: '' |
||||||
} |
} |
||||||
return branches |
return auth |
||||||
} catch (e) { |
|
||||||
console.log(e) |
|
||||||
return [] |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
if (input.url) { |
||||||
|
|
||||||
const remotes = async(defaultConfig: isoGitConfig) => { |
const url = new URL(input.url) |
||||||
|
if (url.hostname.includes('localhost')) { |
||||||
|
config = { |
||||||
|
...config, |
||||||
|
corsProxy: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if ((input.remote && input.remote.url)) { |
||||||
|
|
||||||
let remotes: remote[] = [] |
const url = new URL(input.remote.url) |
||||||
try { |
if (url.hostname.includes('localhost')) { |
||||||
remotes = (await git.listRemotes({ ...defaultConfig })).map((remote) => { return { name: remote.remote, url: remote.url } } |
config = { |
||||||
) |
...config, |
||||||
} catch (e) { |
corsProxy: null, |
||||||
// do nothing
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (input.provider && input.provider === 'github') { |
||||||
|
config = { |
||||||
|
...config, |
||||||
|
corsProxy: 'https://corsproxy.remixproject.org/', |
||||||
} |
} |
||||||
return remotes |
|
||||||
} |
} |
||||||
|
|
||||||
const getCommitChanges = async (commitHash1: string, commitHash2: string, defaultConfig: isoGitConfig) => { |
if (input.provider && input.provider === 'localhost') { |
||||||
const result: commitChange[] = await git.walk({ |
config = { |
||||||
...defaultConfig, |
...config, |
||||||
trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })], |
corsProxy: null |
||||||
map: async function (filepath, [A, B]) { |
} |
||||||
|
} |
||||||
|
|
||||||
|
return config |
||||||
|
} |
||||||
|
|
||||||
|
const getCommitChanges = async (commitHash1: string, commitHash2: string, fsConfig: isoGitFSConfig) => { |
||||||
|
const result: commitChange[] = await git.walk({ |
||||||
|
...fsConfig, |
||||||
|
trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })], |
||||||
|
map: async function (filepath, [A, B]) { |
||||||
|
|
||||||
if (filepath === '.') { |
if (filepath === '.') { |
||||||
|
return |
||||||
|
} |
||||||
|
try { |
||||||
|
if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { |
||||||
return |
return |
||||||
} |
} |
||||||
try { |
} catch (e) { |
||||||
if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { |
// ignore
|
||||||
return |
} |
||||||
} |
|
||||||
} catch (e) { |
|
||||||
// ignore
|
|
||||||
} |
|
||||||
|
|
||||||
// generate ids
|
// generate ids
|
||||||
const Aoid = A && await A.oid() || undefined |
const Aoid = A && await A.oid() || undefined |
||||||
const Boid = B && await B.oid() || undefined |
const Boid = B && await B.oid() || undefined |
||||||
|
|
||||||
const commitChange: Partial<commitChange> = { |
const commitChange: Partial<commitChange> = { |
||||||
hashModified: commitHash1, |
hashModified: commitHash1, |
||||||
hashOriginal: commitHash2, |
hashOriginal: commitHash2, |
||||||
path: filepath, |
path: filepath, |
||||||
} |
} |
||||||
|
|
||||||
// determine modification type
|
// determine modification type
|
||||||
if (Aoid !== Boid) { |
if (Aoid !== Boid) { |
||||||
commitChange.type = "modified" |
commitChange.type = "modified" |
||||||
} |
} |
||||||
if (Aoid === undefined) { |
if (Aoid === undefined) { |
||||||
commitChange.type = "deleted" |
commitChange.type = "deleted" |
||||||
} |
} |
||||||
if (Boid === undefined || !commitHash2) { |
if (Boid === undefined || !commitHash2) { |
||||||
commitChange.type = "added" |
commitChange.type = "added" |
||||||
} |
} |
||||||
if (Aoid === undefined && Boid === undefined) { |
if (Aoid === undefined && Boid === undefined) { |
||||||
commitChange.type = "unknown" |
commitChange.type = "unknown" |
||||||
} |
} |
||||||
if (commitChange.type) |
if (commitChange.type) |
||||||
return commitChange |
return commitChange |
||||||
else |
else |
||||||
return undefined |
return undefined |
||||||
}, |
}, |
||||||
}) |
}) |
||||||
|
|
||||||
return result |
return result |
||||||
} |
} |
||||||
|
|
||||||
const compareBranches = async ({ branch, remote }: compareBranchesInput, defaultConfig: isoGitConfig) => { |
const compareBranches = async ({ branch, remote }: compareBranchesInput, fsConfig: isoGitFSConfig) => { |
||||||
|
|
||||||
// Get current branch commits
|
// Get current branch commits
|
||||||
const headCommits = await git.log({ |
const headCommits = await git.log({ |
||||||
...defaultConfig, |
...fsConfig, |
||||||
ref: branch.name, |
ref: branch.name, |
||||||
}); |
}); |
||||||
|
|
||||||
// Get remote branch commits
|
// Get remote branch commits
|
||||||
const remoteCommits = await git.log({ |
const remoteCommits = await git.log({ |
||||||
...defaultConfig, |
...fsConfig, |
||||||
ref: `${remote.name}/${branch.name}`, |
ref: `${remote.name}/${branch.name}`, |
||||||
}); |
}); |
||||||
|
|
||||||
// Convert arrays of commit objects to sets of commit SHAs
|
// Convert arrays of commit objects to sets of commit SHAs
|
||||||
const headCommitSHAs = new Set(headCommits.map(commit => commit.oid)); |
const headCommitSHAs = new Set(headCommits.map(commit => commit.oid)); |
||||||
const remoteCommitSHAs = new Set(remoteCommits.map(commit => commit.oid)); |
const remoteCommitSHAs = new Set(remoteCommits.map(commit => commit.oid)); |
||||||
|
|
||||||
// Filter out commits that are only in the remote branch
|
// Filter out commits that are only in the remote branch
|
||||||
const uniqueRemoteCommits = remoteCommits.filter(commit => !headCommitSHAs.has(commit.oid)); |
const uniqueRemoteCommits = remoteCommits.filter(commit => !headCommitSHAs.has(commit.oid)); |
||||||
|
|
||||||
// filter out commits that are only in the local branch
|
// filter out commits that are only in the local branch
|
||||||
const uniqueHeadCommits = headCommits.filter(commit => !remoteCommitSHAs.has(commit.oid)); |
const uniqueHeadCommits = headCommits.filter(commit => !remoteCommitSHAs.has(commit.oid)); |
||||||
|
|
||||||
return { |
return { |
||||||
uniqueHeadCommits, |
uniqueHeadCommits, |
||||||
uniqueRemoteCommits, |
uniqueRemoteCommits, |
||||||
}; |
}; |
||||||
} |
} |
||||||
|
|
||||||
export const isoGit = { |
export const isoGit = { |
||||||
currentbranch, |
currentbranch, |
||||||
remotes, |
remotes, |
||||||
branches, |
branches, |
||||||
getCommitChanges, |
getCommitChanges, |
||||||
compareBranches |
compareBranches, |
||||||
} |
addIsomorphicGitProxyConfig, |
||||||
|
push, |
||||||
|
pull, |
||||||
|
fetch, |
||||||
|
getGitHubUser |
||||||
|
} |
Loading…
Reference in new issue