parent
8e249dc88d
commit
0ac2809d54
@ -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 { |
||||
Plugin |
||||
} from '@remixproject/engine' |
||||
import http from 'isomorphic-git/http/web' |
||||
|
||||
const currentbranch = async (input: currentBranchInput, defaultConfig: isoGitConfig ) => { |
||||
console.log('CURRENT BRANCH', input) |
||||
import { Octokit } from "octokit" |
||||
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 { |
||||
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` |
||||
const remoteName = await git.getConfig({ |
||||
...fsConfig, |
||||
path: `branch.${name}.remote` |
||||
}) |
||||
if (remoteName) { |
||||
const remoteUrl = await git.getConfig({ |
||||
...fsConfig, |
||||
path: `remote.${remoteName}.url` |
||||
}) |
||||
if (remoteName) { |
||||
const remoteUrl = await git.getConfig({ |
||||
...defaultConfig, |
||||
path: `remote.${remoteName}.url` |
||||
}) |
||||
remote = { name: remoteName, url: remoteUrl } |
||||
} |
||||
remote = { name: remoteName, url: remoteUrl } |
||||
} |
||||
|
||||
} catch (e) { |
||||
// do nothing
|
||||
} catch (e) { |
||||
// 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) |
||||
console.log('REMOTE', remote) |
||||
|
||||
return { |
||||
remote: remote, |
||||
name: 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 (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) |
||||
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] |
||||
const getGitHubUser = async (input: { token: string }): Promise<{ |
||||
user: GitHubUser, |
||||
emails: userEmails, |
||||
scopes: string[] |
||||
}> => { |
||||
try { |
||||
const octokit = new Octokit({ |
||||
auth: input.token |
||||
}) |
||||
|
||||
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 |
||||
} catch (e) { |
||||
console.log(e) |
||||
return [] |
||||
return auth |
||||
} |
||||
} |
||||
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[] = [] |
||||
try { |
||||
remotes = (await git.listRemotes({ ...defaultConfig })).map((remote) => { return { name: remote.remote, url: remote.url } } |
||||
) |
||||
} catch (e) { |
||||
// do nothing
|
||||
const url = new URL(input.remote.url) |
||||
if (url.hostname.includes('localhost')) { |
||||
config = { |
||||
...config, |
||||
corsProxy: null, |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (input.provider && input.provider === 'github') { |
||||
config = { |
||||
...config, |
||||
corsProxy: 'https://corsproxy.remixproject.org/', |
||||
} |
||||
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 (input.provider && input.provider === 'localhost') { |
||||
config = { |
||||
...config, |
||||
corsProxy: null |
||||
} |
||||
} |
||||
|
||||
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 |
||||
} |
||||
try { |
||||
if ((A && await A.type()) === 'tree' || B && (await B.type()) === 'tree') { |
||||
return |
||||
} |
||||
} catch (e) { |
||||
// ignore
|
||||
} |
||||
} catch (e) { |
||||
// ignore
|
||||
} |
||||
|
||||
// generate ids
|
||||
const Aoid = A && await A.oid() || undefined |
||||
const Boid = B && await B.oid() || undefined |
||||
// generate ids
|
||||
const Aoid = A && await A.oid() || undefined |
||||
const Boid = B && await B.oid() || undefined |
||||
|
||||
const commitChange: Partial<commitChange> = { |
||||
hashModified: commitHash1, |
||||
hashOriginal: commitHash2, |
||||
path: filepath, |
||||
} |
||||
const commitChange: Partial<commitChange> = { |
||||
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 |
||||
}, |
||||
}) |
||||
// 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 result |
||||
} |
||||
|
||||
const compareBranches = async ({ branch, remote }: compareBranchesInput, defaultConfig: isoGitConfig) => { |
||||
const compareBranches = async ({ branch, remote }: compareBranchesInput, fsConfig: isoGitFSConfig) => { |
||||
|
||||
// Get current branch commits
|
||||
const headCommits = await git.log({ |
||||
...defaultConfig, |
||||
ref: branch.name, |
||||
}); |
||||
// Get current branch commits
|
||||
const headCommits = await git.log({ |
||||
...fsConfig, |
||||
ref: branch.name, |
||||
}); |
||||
|
||||
// Get remote branch commits
|
||||
const remoteCommits = await git.log({ |
||||
...defaultConfig, |
||||
ref: `${remote.name}/${branch.name}`, |
||||
}); |
||||
// Get remote branch commits
|
||||
const remoteCommits = await git.log({ |
||||
...fsConfig, |
||||
ref: `${remote.name}/${branch.name}`, |
||||
}); |
||||
|
||||
// Convert arrays of commit objects to sets of commit SHAs
|
||||
const headCommitSHAs = new Set(headCommits.map(commit => commit.oid)); |
||||
const remoteCommitSHAs = new Set(remoteCommits.map(commit => commit.oid)); |
||||
// Convert arrays of commit objects to sets of commit SHAs
|
||||
const headCommitSHAs = new Set(headCommits.map(commit => commit.oid)); |
||||
const remoteCommitSHAs = new Set(remoteCommits.map(commit => commit.oid)); |
||||
|
||||
// Filter out commits that are only in the remote branch
|
||||
const uniqueRemoteCommits = remoteCommits.filter(commit => !headCommitSHAs.has(commit.oid)); |
||||
// Filter out commits that are only in the remote branch
|
||||
const uniqueRemoteCommits = remoteCommits.filter(commit => !headCommitSHAs.has(commit.oid)); |
||||
|
||||
// filter out commits that are only in the local branch
|
||||
const uniqueHeadCommits = headCommits.filter(commit => !remoteCommitSHAs.has(commit.oid)); |
||||
// filter out commits that are only in the local branch
|
||||
const uniqueHeadCommits = headCommits.filter(commit => !remoteCommitSHAs.has(commit.oid)); |
||||
|
||||
return { |
||||
uniqueHeadCommits, |
||||
uniqueRemoteCommits, |
||||
}; |
||||
} |
||||
return { |
||||
uniqueHeadCommits, |
||||
uniqueRemoteCommits, |
||||
}; |
||||
} |
||||
|
||||
export const isoGit = { |
||||
currentbranch, |
||||
remotes, |
||||
branches, |
||||
getCommitChanges, |
||||
compareBranches |
||||
} |
||||
export const isoGit = { |
||||
currentbranch, |
||||
remotes, |
||||
branches, |
||||
getCommitChanges, |
||||
compareBranches, |
||||
addIsomorphicGitProxyConfig, |
||||
push, |
||||
pull, |
||||
fetch, |
||||
getGitHubUser |
||||
} |
Loading…
Reference in new issue