Force push and delete

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-01-25 06:34:46 +00:00
parent 31b6952a06
commit a3d69c7397
4 changed files with 48 additions and 32 deletions

View File

@@ -13,8 +13,7 @@
"bin": { "bin": {
"git-peard": "./src/gitpeard.js", "git-peard": "./src/gitpeard.js",
"git-remote-pear": "./src/git-remote-pear.js", "git-remote-pear": "./src/git-remote-pear.js",
"git-pear": "./src/cli.js", "git-pear": "./src/cli.js"
"rpc": "./src/rpc.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -82,9 +82,6 @@ swarm.on('connection', async (socket) => {
}) })
async function talkToGit (refs, drive, repoName, rpc) { async function talkToGit (refs, drive, repoName, rpc) {
for (const ref in refs) {
console.warn(refs[ref] + '\t' + ref)
}
process.stdin.setEncoding('utf8') process.stdin.setEncoding('utf8')
const didFetch = false const didFetch = false
process.stdin.on('readable', async function () { process.stdin.on('readable', async function () {
@@ -105,15 +102,18 @@ async function talkToGit (refs, drive, repoName, rpc) {
} }
dst = dst.replace('refs/heads/', '').replace('\n\n', '') dst = dst.replace('refs/heads/', '').replace('\n\n', '')
await git.push(dst)
let command let command
if (isDelete) { if (isDelete) {
command = 'delete-branch-from-repo' command = 'd-branch'
} else if (isForce) { } else if (isForce) {
command = 'force-push-to-repo' await git.push(src, isForce)
src = src.replace('+', '')
command = 'f-push'
} else { } else {
command = 'push-to-repo' console.warn('pushing', src, dst)
await git.push(src)
command = 'push'
} }
const publicKey = home.readPk() const publicKey = home.readPk()
@@ -122,15 +122,20 @@ async function talkToGit (refs, drive, repoName, rpc) {
// process.kill(daemonPid || home.getDaemonPid()) // process.kill(daemonPid || home.getDaemonPid())
// home.removeDaemonPid() // home.removeDaemonPid()
// process.stdout.write(res.toString())
process.stdout.write('\n\n') process.stdout.write('\n\n')
process.exit(0) process.exit(0)
} else if (chunk && chunk.search(/^list/) !== -1) { // list && list for-push } 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) { Object.keys(refs).forEach(function (branch, i) {
process.stdout.write(refs[branch] + ' ' + branch + '\n') process.stdout.write(refs[branch] + ' ' + branch + '\n')
}) })
process.stdout.write('\n') process.stdout.write('\n')
} else if (chunk && chunk.search(/^fetch/) !== -1) { } 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 lines = chunk.split(/\n/).filter(l => l !== '')
const targets = [] const targets = []

View File

@@ -31,8 +31,10 @@ async function addRemote (name) {
return await doGit(init) return await doGit(init)
} }
async function push (branch = 'master') { async function push (branch = 'master', force = false) {
const push = spawn('git', ['push', 'pear', branch]) const args = ['push', 'pear', branch]
if (force) args.push('-f')
const push = spawn('git', args)
return await doGit(push) return await doGit(push)
} }

View File

@@ -23,9 +23,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 -- */
rpc.respond('push-to-repo', async req => await this.pushHandler(req)) rpc.respond('push', async req => await this.pushHandler(req))
rpc.respond('force-push-to-repo', req => this.forcePushHandler(req)) rpc.respond('f-push', async req => await this.forcePushHandler(req))
rpc.respond('delete-branch-from-repo', req => this.deleteBranchHandler(req)) rpc.respond('d-branch', async req => await this.deleteBranchHandler(req))
this.connections[peerInfo.publicKey] = rpc this.connections[peerInfo.publicKey] = rpc
} }
@@ -47,7 +47,6 @@ module.exports = class RPC {
async pushHandler (req) { async pushHandler (req) {
const { url, repo, key, branch } = this.parsePushCommand(req) const { url, repo, key, branch } = this.parsePushCommand(req)
// TODO: check ACL // TODO: check ACL
// collect stdout to buffer and return it
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
const process = spawn('git', ['fetch', url, `${branch}:${branch}`], { env: { GIT_DIR: home.getCodePath(repo) } }) const process = spawn('git', ['fetch', url, `${branch}:${branch}`], { env: { GIT_DIR: home.getCodePath(repo) } })
let errBuffer = Buffer.from('') let errBuffer = Buffer.from('')
@@ -55,31 +54,42 @@ module.exports = class RPC {
errBuffer = Buffer.concat([errBuffer, data]) errBuffer = Buffer.concat([errBuffer, data])
}) })
// TODO: write buffer to standard output with ACL
process.on('close', code => { process.on('close', code => {
return code === 0 ? resolve(errBuffer) : reject(errBuffer) return code === 0 ? resolve(errBuffer) : reject(errBuffer)
}) })
}) })
} }
forcePushHandler (req) { async forcePushHandler (req) {
const { url, repo, key, branch } = this.parsePushCommand(req) const { url, repo, key, branch } = this.parsePushCommand(req)
// TODO: // TODO: check ACL
// check ACL return await new Promise((resolve, reject) => {
// collect stdout to buffer and return it const process = spawn('git', ['fetch', url, `${branch}:${branch}`, '--force'], { env: { GIT_DIR: home.getCodePath(repo) } })
// const process = spawn('git', ['reset', '--hard', url, branch], { env: { GIT_DIR: home.getCodePath(repo) } }) let errBuffer = Buffer.from('')
console.error('req', req.toString()) process.stderr.on('data', data => {
console.error('forcePushHandler not implemented') 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) const { url, repo, key, branch } = this.parsePushCommand(req)
// TODO: // TODO: check ACL
// check ACL return await new Promise((resolve, reject) => {
// collect stdout to buffer and return it const process = spawn('git', ['branch', '-D', branch], { env: { GIT_DIR: home.getCodePath(repo) } })
// const process = spawn('git', ['branch', '-d', branch], { env: { GIT_DIR: home.getCodePath(repo) } }) let errBuffer = Buffer.from('')
console.error('req', req.toString()) process.stderr.on('data', data => {
console.error('deleteBranchHandler not implemented') errBuffer = Buffer.concat([errBuffer, data])
})
process.on('close', code => {
return code === 0 ? resolve(errBuffer) : reject(errBuffer)
})
})
} }
parsePushCommand(req) { parsePushCommand(req) {