From a3d69c7397567e91577e766a0d064cc6621b15ee Mon Sep 17 00:00:00 2001 From: dzdidi Date: Thu, 25 Jan 2024 06:34:46 +0000 Subject: [PATCH] Force push and delete Signed-off-by: dzdidi --- package.json | 3 +-- src/git-remote-pear.js | 23 ++++++++++++-------- src/git.js | 6 ++++-- src/rpc.js | 48 +++++++++++++++++++++++++----------------- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index b6a9b5b..5299b9b 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,7 @@ "bin": { "git-peard": "./src/gitpeard.js", "git-remote-pear": "./src/git-remote-pear.js", - "git-pear": "./src/cli.js", - "rpc": "./src/rpc.js" + "git-pear": "./src/cli.js" }, "repository": { "type": "git", diff --git a/src/git-remote-pear.js b/src/git-remote-pear.js index 7149045..166e6b0 100755 --- a/src/git-remote-pear.js +++ b/src/git-remote-pear.js @@ -82,9 +82,6 @@ swarm.on('connection', async (socket) => { }) async function talkToGit (refs, drive, repoName, rpc) { - for (const ref in refs) { - console.warn(refs[ref] + '\t' + ref) - } process.stdin.setEncoding('utf8') const didFetch = false process.stdin.on('readable', async function () { @@ -103,17 +100,20 @@ async function talkToGit (refs, drive, repoName, rpc) { if (!home.isShared(repoName)) { home.shareAppFolder(name) } - + dst = dst.replace('refs/heads/', '').replace('\n\n', '') - await git.push(dst) let command if (isDelete) { - command = 'delete-branch-from-repo' + command = 'd-branch' } else if (isForce) { - command = 'force-push-to-repo' + await git.push(src, isForce) + src = src.replace('+', '') + command = 'f-push' } else { - command = 'push-to-repo' + console.warn('pushing', src, dst) + await git.push(src) + command = 'push' } const publicKey = home.readPk() @@ -122,15 +122,20 @@ async function talkToGit (refs, drive, repoName, rpc) { // process.kill(daemonPid || home.getDaemonPid()) // home.removeDaemonPid() - // process.stdout.write(res.toString()) process.stdout.write('\n\n') process.exit(0) } else if (chunk && chunk.search(/^list/) !== -1) { // list && list for-push + for (const ref in refs) { + console.warn(refs[ref] + '\t' + ref) + } Object.keys(refs).forEach(function (branch, i) { process.stdout.write(refs[branch] + ' ' + branch + '\n') }) process.stdout.write('\n') } else if (chunk && chunk.search(/^fetch/) !== -1) { + for (const ref in refs) { + console.warn(refs[ref] + '\t' + ref) + } const lines = chunk.split(/\n/).filter(l => l !== '') const targets = [] diff --git a/src/git.js b/src/git.js index f733555..3cb7c69 100644 --- a/src/git.js +++ b/src/git.js @@ -31,8 +31,10 @@ async function addRemote (name) { return await doGit(init) } -async function push (branch = 'master') { - const push = spawn('git', ['push', 'pear', branch]) +async function push (branch = 'master', force = false) { + const args = ['push', 'pear', branch] + if (force) args.push('-f') + const push = spawn('git', args) return await doGit(push) } diff --git a/src/rpc.js b/src/rpc.js index e105b13..8ab4fd4 100755 --- a/src/rpc.js +++ b/src/rpc.js @@ -23,9 +23,9 @@ module.exports = class RPC { rpc.respond('get-refs', async req => await this.getRefsHandler(req)) /* -- PUSH HANDLERS -- */ - rpc.respond('push-to-repo', async req => await this.pushHandler(req)) - rpc.respond('force-push-to-repo', req => this.forcePushHandler(req)) - rpc.respond('delete-branch-from-repo', req => this.deleteBranchHandler(req)) + rpc.respond('push', async req => await this.pushHandler(req)) + rpc.respond('f-push', async req => await this.forcePushHandler(req)) + rpc.respond('d-branch', async req => await this.deleteBranchHandler(req)) this.connections[peerInfo.publicKey] = rpc } @@ -47,7 +47,6 @@ module.exports = class RPC { async pushHandler (req) { const { url, repo, key, branch } = this.parsePushCommand(req) // TODO: check ACL - // collect stdout to buffer and return it return await new Promise((resolve, reject) => { const process = spawn('git', ['fetch', url, `${branch}:${branch}`], { env: { GIT_DIR: home.getCodePath(repo) } }) let errBuffer = Buffer.from('') @@ -55,31 +54,42 @@ module.exports = class RPC { errBuffer = Buffer.concat([errBuffer, data]) }) - // TODO: write buffer to standard output with ACL process.on('close', code => { return code === 0 ? resolve(errBuffer) : reject(errBuffer) }) }) } - forcePushHandler (req) { + async forcePushHandler (req) { const { url, repo, key, branch } = this.parsePushCommand(req) - // TODO: - // check ACL - // collect stdout to buffer and return it - // const process = spawn('git', ['reset', '--hard', url, branch], { env: { GIT_DIR: home.getCodePath(repo) } }) - console.error('req', req.toString()) - console.error('forcePushHandler not implemented') + // TODO: check ACL + return await new Promise((resolve, reject) => { + const process = spawn('git', ['fetch', url, `${branch}:${branch}`, '--force'], { env: { GIT_DIR: home.getCodePath(repo) } }) + let errBuffer = Buffer.from('') + process.stderr.on('data', data => { + errBuffer = Buffer.concat([errBuffer, data]) + }) + + process.on('close', code => { + return code === 0 ? resolve(errBuffer) : reject(errBuffer) + }) + }) } - deleteBranchHandler (req) { + async deleteBranchHandler (req) { const { url, repo, key, branch } = this.parsePushCommand(req) - // TODO: - // check ACL - // collect stdout to buffer and return it - // const process = spawn('git', ['branch', '-d', branch], { env: { GIT_DIR: home.getCodePath(repo) } }) - console.error('req', req.toString()) - console.error('deleteBranchHandler not implemented') + // TODO: check ACL + return await new Promise((resolve, reject) => { + const process = spawn('git', ['branch', '-D', branch], { env: { GIT_DIR: home.getCodePath(repo) } }) + let errBuffer = Buffer.from('') + process.stderr.on('data', data => { + errBuffer = Buffer.concat([errBuffer, data]) + }) + + process.on('close', code => { + return code === 0 ? resolve(errBuffer) : reject(errBuffer) + }) + }) } parsePushCommand(req) {