mirror of
https://github.com/aljazceru/gitpear.git
synced 2025-12-17 22:24:22 +01:00
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const { spawn } = require('child_process')
|
||||||
const ProtomuxRPC = require('protomux-rpc')
|
const ProtomuxRPC = require('protomux-rpc')
|
||||||
|
|
||||||
const RAM = require('random-access-memory')
|
const RAM = require('random-access-memory')
|
||||||
@@ -25,7 +26,23 @@ const targetKey = matches[1]
|
|||||||
const repoName = matches[2]
|
const repoName = matches[2]
|
||||||
|
|
||||||
const store = new Corestore(RAM)
|
const store = new Corestore(RAM)
|
||||||
const swarm = new Hyperswarm()
|
const swarm = new Hyperswarm({ keypair: home.getKeyPair() })
|
||||||
|
|
||||||
|
let daemonPid
|
||||||
|
if (!home.isDaemonRunning()) {
|
||||||
|
const opts = {
|
||||||
|
detached: true,
|
||||||
|
stdio: [ 'ignore', home.getOutStream(), home.getErrStream() ]
|
||||||
|
}
|
||||||
|
const daemon = spawn('git-peard', opts)
|
||||||
|
daemonPid = daemon.pid
|
||||||
|
home.storeDaemonPid(daemonPid)
|
||||||
|
// TODO: remove in case of error or exit but allow unref
|
||||||
|
// daemon.on('error', home.removeDaemonPid)
|
||||||
|
// daemon.on('exit', home.removeDaemonPid)
|
||||||
|
console.error('started daemon', daemonPid)
|
||||||
|
daemon.unref()
|
||||||
|
}
|
||||||
|
|
||||||
swarm.join(crypto.discoveryKey(Buffer.from(targetKey, 'hex')), { server: false })
|
swarm.join(crypto.discoveryKey(Buffer.from(targetKey, 'hex')), { server: false })
|
||||||
|
|
||||||
@@ -61,10 +78,10 @@ swarm.on('connection', async (socket) => {
|
|||||||
|
|
||||||
const refsRes = await rpc.request('get-refs', Buffer.from(repoName))
|
const refsRes = await rpc.request('get-refs', Buffer.from(repoName))
|
||||||
|
|
||||||
await talkToGit(JSON.parse(refsRes.toString()), drive, repoName)
|
await talkToGit(JSON.parse(refsRes.toString()), drive, repoName, rpc)
|
||||||
})
|
})
|
||||||
|
|
||||||
async function talkToGit (refs, drive, repoName) {
|
async function talkToGit (refs, drive, repoName, rpc) {
|
||||||
for (const ref in refs) {
|
for (const ref in refs) {
|
||||||
console.warn(refs[ref] + '\t' + ref)
|
console.warn(refs[ref] + '\t' + ref)
|
||||||
}
|
}
|
||||||
@@ -83,37 +100,28 @@ async function talkToGit (refs, drive, repoName) {
|
|||||||
const isDelete = !src
|
const isDelete = !src
|
||||||
const isForce = src.startsWith('+')
|
const isForce = src.startsWith('+')
|
||||||
|
|
||||||
// XXX: it looks like git is trying to establish network connection first, so if it can not
|
if (!home.isShared(repoName)) {
|
||||||
// reach push destination it hangs everything on capabilities exchange
|
home.shareAppFolder(name)
|
||||||
// TODO: add timeout
|
}
|
||||||
|
await git.push(src.replace('refs/heads/', ''))
|
||||||
|
|
||||||
// FOR TESTING RUN SECOND INSTANCE OF GIT-PEARD
|
console.error('_command', _command)
|
||||||
// if (!home.isDaemonRunning()) {
|
let command
|
||||||
// const opts = {
|
if (isDelete) {
|
||||||
// detached: true,
|
command = 'delete-branch-from-repo'
|
||||||
// stdio: [ 'ignore', home.getOutStream(), home.getErrStream() ]
|
} else if (isForce) {
|
||||||
// }
|
command = 'force-push-to-repo'
|
||||||
// const daemon = spawn('git-peard', opts)
|
} else {
|
||||||
// home.storeDaemonPid(daemon.pid)
|
command = 'push-to-repo'
|
||||||
// // TODO: remove in case of error or exit but allow unref
|
}
|
||||||
// // daemon.on('error', home.removeDaemonPid)
|
|
||||||
// // daemon.on('exit', home.removeDaemonPid)
|
|
||||||
// daemon.unref()
|
|
||||||
// }
|
|
||||||
|
|
||||||
console.error('TODO')
|
const publicKey = home.readPk()
|
||||||
// TODO: get repo name (current dir name instead or name from url)
|
const res = await rpc.request(command, Buffer.from(repoName + ':' + dst + ':' + publicKey))
|
||||||
// if (home.isShared(repoName)) {
|
|
||||||
// git push branch
|
|
||||||
// } else {
|
|
||||||
// share repo with current branch
|
|
||||||
// }
|
|
||||||
|
|
||||||
// - send rpc command to remote (url)
|
console.error('killing', daemonPid)
|
||||||
// Mapping of RPC commands to git commands on origin:
|
process.kill(daemonPid || home.getDaemonPid())
|
||||||
// - normal push - git pull
|
console.error('killed')
|
||||||
// - force push - git reset --hard <remote>/<branch>
|
home.removeDaemonPid()
|
||||||
// - delete branch - git branch -D <remote>/<branch>
|
|
||||||
|
|
||||||
process.stdout.write('\n\n')
|
process.stdout.write('\n\n')
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
|
|||||||
22
src/rpc.js
22
src/rpc.js
@@ -21,10 +21,9 @@ module.exports = class RPC {
|
|||||||
rpc.respond('get-refs', async req => await this.getRefsHandler(req))
|
rpc.respond('get-refs', async req => await this.getRefsHandler(req))
|
||||||
|
|
||||||
/* -- PUSH HANDLERS -- */
|
/* -- PUSH HANDLERS -- */
|
||||||
// TODO: reponders to pull requests
|
rpc.respond('push-to-repo', async req => this.pushHandler(req))
|
||||||
// normal push: git pull <url>
|
rpc.respond('force-push-to-repo', req => this.forcePushHandler(req))
|
||||||
// force push: git reset --hard url/<branch>
|
rpc.respond('delete-branch-from-repo', req => this.deleteBranchHandler(req))
|
||||||
// delete branch: git branch -D url/<branch>
|
|
||||||
|
|
||||||
this.connections[peerInfo.publicKey] = rpc
|
this.connections[peerInfo.publicKey] = rpc
|
||||||
}
|
}
|
||||||
@@ -42,4 +41,19 @@ module.exports = class RPC {
|
|||||||
|
|
||||||
return Buffer.from(JSON.stringify(res))
|
return Buffer.from(JSON.stringify(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pushHandler (req) {
|
||||||
|
console.error('req', req.toString())
|
||||||
|
console.error('pushHandler not implemented')
|
||||||
|
}
|
||||||
|
|
||||||
|
forcePushHandler (req) {
|
||||||
|
console.error('req', req.toString())
|
||||||
|
console.error('forcePushHandler not implemented')
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteBranchHandler (req) {
|
||||||
|
console.error('req', req.toString())
|
||||||
|
console.error('deleteBranchHandler not implemented')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user