more elaborate todos on push capabilities

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-01-23 17:06:33 +00:00
parent 39eebdf629
commit 8dedb6bf17
3 changed files with 44 additions and 20 deletions

View File

@@ -11,6 +11,8 @@ const crypto = require('hypercore-crypto')
const git = require('./git.js') const git = require('./git.js')
const home = require('./home') const home = require('./home')
const fs = require('fs')
const url = process.argv[3] const url = process.argv[3]
const matches = url.match(/pear:\/\/([a-f0-9]{64})\/(.*)/) const matches = url.match(/pear:\/\/([a-f0-9]{64})\/(.*)/)
@@ -59,10 +61,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) await talkToGit(JSON.parse(refsRes.toString()), drive, repoName)
}) })
async function talkToGit (refs, drive) { async function talkToGit (refs, drive, repoName) {
for (const ref in refs) { for (const ref in refs) {
console.warn(refs[ref] + '\t' + ref) console.warn(refs[ref] + '\t' + ref)
} }
@@ -74,34 +76,44 @@ async function talkToGit (refs, drive) {
process.stdout.write('list\n') process.stdout.write('list\n')
process.stdout.write('push\n') process.stdout.write('push\n')
process.stdout.write('fetch\n\n') process.stdout.write('fetch\n\n')
} else if (chunk && chunk.search(/^push/) !== -1) {j } else if (chunk && chunk.search(/^push/) !== -1) {
const [_command, path] = chunk.split(' ') const [_command, path] = chunk.split(' ')
const [src, dst] = path.split(':') const [src, dst] = path.split(':')
const isDelete = !src const isDelete = !src
const isForce = src.startsWith('+') const isForce = src.startsWith('+')
if (!home.getDaemonPid()) { // XXX: it looks like git is trying to establish network connection first, so if it can not
const daemon = spawn('git-peard', opts) // reach push destination it hangs everything on capabilities exchange
home.storeDaemonPid(daemon.pid) // TODO: add timeout
// TODO: remove in case of error or exit but allow unref
// daemon.on('error', home.removeDaemonPid) // FOR TESTING RUN SECOND INSTANCE OF GIT-PEARD
// daemon.on('exit', home.removeDaemonPid) // if (!home.isDaemonRunning()) {
daemon.unref() // const opts = {
} // detached: true,
// TODO: options: // stdio: [ 'ignore', home.getOutStream(), home.getErrStream() ]
// ---- push by pull ---- // }
// - init for share (daemon, etc) // const daemon = spawn('git-peard', opts)
// - push branch to local remote // home.storeDaemonPid(daemon.pid)
// - send rpc command to origin // // 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')
// TODO: get repo name (current dir name instead or name from url)
// if (home.isShared(repoName)) {
// git push branch
// } else {
// share repo with current branch
// }
// - send rpc command to remote (url)
// Mapping of RPC commands to git commands on origin: // Mapping of RPC commands to git commands on origin:
// - normal push - git pull // - normal push - git pull
// - force push - git reset --hard <remote>/<branch> // - force push - git reset --hard <remote>/<branch>
// - delete branch - git branch -D <remote>/<branch> // - delete branch - git branch -D <remote>/<branch>
//
// ---- push by push ----
//
//
process.stdout.write('\n\n') process.stdout.write('\n\n')
process.exit(0) process.exit(0)

View File

@@ -90,6 +90,10 @@ function getDaemonPid () {
} }
} }
function isDaemonRunning () {
return fs.existsSync(`${APP_HOME}/.daemon.pid`)
}
function removeDaemonPid () { function removeDaemonPid () {
try { try {
fs.unlinkSync(`${APP_HOME}/.daemon.pid`) fs.unlinkSync(`${APP_HOME}/.daemon.pid`)
@@ -114,5 +118,6 @@ module.exports = {
getErrStream, getErrStream,
storeDaemonPid, storeDaemonPid,
getDaemonPid, getDaemonPid,
isDaemonRunning,
removeDaemonPid removeDaemonPid
} }

View File

@@ -16,9 +16,16 @@ module.exports = class RPC {
// for example check of peerInfo.publicKey is in a list of allowed keys // for example check of peerInfo.publicKey is in a list of allowed keys
// which can in turn be stored in a .git-daemon-export-ok file // which can in turn be stored in a .git-daemon-export-ok file
/* -- PULL HANDLERS -- */
rpc.respond('get-repos', req => this.getReposHandler(req)) rpc.respond('get-repos', req => this.getReposHandler(req))
rpc.respond('get-refs', async req => await this.getRefsHandler(req)) rpc.respond('get-refs', async req => await this.getRefsHandler(req))
/* -- PUSH HANDLERS -- */
// TODO: reponders to pull requests
// normal push: git pull <url>
// force push: git reset --hard url/<branch>
// delete branch: git branch -D url/<branch>
this.connections[peerInfo.publicKey] = rpc this.connections[peerInfo.publicKey] = rpc
} }