parent
fe86f5927b
commit
dc8cf19bf1
@ -1,36 +0,0 @@ |
||||
import { PluginClient } from "@remixproject/plugin"; |
||||
import { Profile } from "@remixproject/plugin-utils"; |
||||
import { ElectronBasePlugin, ElectronBasePluginClient } from "@remixproject/plugin-electron" |
||||
|
||||
const profile: Profile = { |
||||
name: 'git', |
||||
displayName: 'Git', |
||||
description: 'Git plugin', |
||||
} |
||||
|
||||
export class GitPlugin extends ElectronBasePlugin { |
||||
clients: GitPluginClient[] = [] |
||||
constructor() { |
||||
super(profile, clientProfile, GitPluginClient) |
||||
} |
||||
|
||||
} |
||||
|
||||
const clientProfile: Profile = { |
||||
name: 'git', |
||||
displayName: 'Git', |
||||
description: 'Git plugin', |
||||
methods: ['log', 'status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout', 'branch', 'merge', 'reset', 'revert', 'diff', 'stash', 'apply', 'cherryPick', 'rebase', 'tag', 'fetch', 'remote', 'config', 'show', 'init', 'help', 'version'] |
||||
} |
||||
|
||||
// TODO: implement all native OS git commands
|
||||
class GitPluginClient extends ElectronBasePluginClient { |
||||
|
||||
constructor(webContentsId: number, profile: Profile) { |
||||
super(webContentsId, profile) |
||||
this.onload(() => { |
||||
console.log('GitPluginClient onload') |
||||
}) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,133 @@ |
||||
import { exec } from 'child_process'; |
||||
import { CommitObject, ReadCommitResult } from 'isomorphic-git'; |
||||
import { promisify } from 'util'; |
||||
const execAsync = promisify(exec); |
||||
|
||||
const statusTransFormMatrix = (status: string) => { |
||||
switch (status) { |
||||
case '??': |
||||
return [0, 2, 0] |
||||
case 'A ': |
||||
return [0, 2, 2] |
||||
case 'M ': |
||||
return [1, 2, 2] |
||||
case 'MM': |
||||
return [1, 2, 3] |
||||
case ' M': |
||||
return [1, 2, 1] |
||||
case ' D': |
||||
return [0, 2, 0] |
||||
case 'D ': |
||||
return [1, 0, 0] |
||||
case 'AM': |
||||
return [0, 2, 3] |
||||
default: |
||||
return [-1, -1, -1] |
||||
} |
||||
} |
||||
|
||||
export const gitProxy = { |
||||
|
||||
version: async () => { |
||||
try { |
||||
const result = await execAsync('git --version'); |
||||
console.log('git --version', result.stdout) |
||||
return result.stdout |
||||
} catch (error) { |
||||
return false; |
||||
} |
||||
}, |
||||
|
||||
|
||||
|
||||
clone: async (url: string, path: string) => { |
||||
const { stdout, stderr } = await execAsync(`git clone ${url} ${path}`); |
||||
console.log('stdout:', stdout); |
||||
console.log('stderr:', stderr); |
||||
}, |
||||
|
||||
status: async (path: string) => { |
||||
const result = await execAsync('git status --porcelain -uall', { cwd: path }) |
||||
//console.log('git status --porcelain -uall', result.stdout)
|
||||
// parse the result.stdout
|
||||
const lines = result.stdout.split('\n') |
||||
const files: any = [] |
||||
const fileNames: any = [] |
||||
//console.log('lines', lines)
|
||||
lines.forEach((line: string) => { |
||||
// get the first two characters of the line
|
||||
const status = line.slice(0, 2) |
||||
|
||||
const file = line.split(' ').pop() |
||||
|
||||
//console.log('line', line)
|
||||
if (status && file) { |
||||
fileNames.push(file) |
||||
files.push([ |
||||
file, |
||||
...statusTransFormMatrix(status) |
||||
]) |
||||
} |
||||
} |
||||
) |
||||
// sort files by first column
|
||||
files.sort((a: any, b: any) => { |
||||
if (a[0] < b[0]) { |
||||
return -1 |
||||
} |
||||
if (a[0] > b[0]) { |
||||
return 1 |
||||
} |
||||
return 0 |
||||
}) |
||||
|
||||
|
||||
return files |
||||
}, |
||||
|
||||
|
||||
// buggy, doesn't work properly yet on windows
|
||||
log: async (path: string, ref: string) => { |
||||
const result = await execAsync('git log ' + ref + ' --pretty=format:"{ oid:%H, message:"%s", author:"%an", email: "%ae", timestamp:"%at", tree: "%T", committer: "%cn", committer-email: "%ce", committer-timestamp: "%ct", parent: "%P" }" -n 20', { cwd: path }) |
||||
console.log('git log', result.stdout) |
||||
const lines = result.stdout.split('\n') |
||||
const commits: ReadCommitResult[] = [] |
||||
console.log('lines', lines) |
||||
lines.forEach((line: string) => { |
||||
console.log('line', normalizeJson(line)) |
||||
line = normalizeJson(line) |
||||
const data = JSON.parse(line) |
||||
let commit: ReadCommitResult = {} as ReadCommitResult |
||||
commit.oid = data.oid |
||||
commit.commit = {} as CommitObject |
||||
commit.commit.message = data.message |
||||
commit.commit.tree = data.tree |
||||
commit.commit.committer = {} as any |
||||
commit.commit.committer.name = data.committer |
||||
commit.commit.committer.email = data['committer-email'] |
||||
commit.commit.committer.timestamp = data['committer-timestamp'] |
||||
commit.commit.author = {} as any |
||||
commit.commit.author.name = data.author |
||||
commit.commit.author.email = data.email |
||||
commit.commit.author.timestamp = data.timestamp |
||||
commit.commit.parent = [data.parent] |
||||
console.log('commit', commit) |
||||
commits.push(commit) |
||||
|
||||
}) |
||||
|
||||
return commits |
||||
|
||||
} |
||||
} |
||||
|
||||
function normalizeJson(str: string){ |
||||
return str.replace(/[\s\n\r\t]/gs, '').replace(/,([}\]])/gs, '$1') |
||||
.replace(/([,{\[]|)(?:("|'|)([\w_\- ]+)\2:|)("|'|)(.*?)\4([,}\]])/gs, (str, start, q1, index, q2, item, end) => { |
||||
item = item.replace(/"/gsi, '').trim(); |
||||
if(index){index = '"'+index.replace(/"/gsi, '').trim()+'"';} |
||||
if(!item.match(/^[0-9]+(\.[0-9]+|)$/) && !['true','false'].includes(item)){item = '"'+item+'"';} |
||||
if(index){return start+index+':'+item+end;} |
||||
return start+item+end; |
||||
}); |
||||
} |
Loading…
Reference in new issue