share use options instead of args, share current branch by default

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-08 18:46:36 +00:00
parent e987208d76
commit 84cdcd29ae
3 changed files with 50 additions and 13 deletions

View File

@@ -38,8 +38,8 @@ All data will be persisted in application directory (default `~/.gitpear`). To c
* `git pear daemon <-s, --start | -k, --stop>` - start or stop daemon
* `git pear key` - print out public key. Share it with your peers so that they can do `git pull pear:<public key>/<repo name>`
* `git pear init [-s, --share] <path>` - It will create [bare repository](https://git-scm.com/docs/git-init#Documentation/git-init.txt---bare) of the same name in application directory (default ~/.gitpear/<repository name>). It will add [git remote](https://git-scm.com/docs/git-remote) in current repository with name `pear`. So just like in traditional flow doing `git push orign`, here we do `git push pear`. By default repository will not be shared. To enable sharing provide `-s` or call `gitpear share <path>` later
* `git pear share <path>` - makes repository sharable
* `git pear init <path> [-s, --share [branch]]` - It will create [bare repository](https://git-scm.com/docs/git-init#Documentation/git-init.txt---bare) of the same name in application directory (default ~/.gitpear/<repository name>). It will add [git remote](https://git-scm.com/docs/git-remote) in current repository with name `pear`. So just like in traditional flow doing `git push orign`, here we do `git push pear`. By default repository will not be shared. To enable sharing provide `-s | --share [branch]` (default branch to share is current) or call `gitpear share <path>` later
* `git pear share [-p, --path [path (default: ".")]> [-b, --branch [branch name (default: "_current_")] [-v, --visibility <private|public> (default: "public")]` - share repository, if branch is not specified, default branch will be shared
* `git pear unshare <path>` - stop sharing repository
* `git pear list [-s, --shared]` - list all or (only shared) repositories
* `git pear list <url>` - list repositories of a peer

View File

@@ -22,7 +22,7 @@ program
.command('init')
.description('initialize a gitpear repo')
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
.option('-s, --share', 'share the repo, default false')
.option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '')
.action(async (p, options) => {
const fullPath = path.resolve(p)
if (!fs.existsSync(path.join(fullPath, '.git'))) {
@@ -51,21 +51,27 @@ program
console.log(`Added git remote for "${name}" as "pear"`)
} catch (e) { }
let branchToShare = await git.getCurrentBranch()
if (options.share && options.share !== true) {
branchToShare = options.share
}
if (options.share) {
try { home.shareAppFolder(name) } catch (e) { }
try { acl.setACL(name) } catch (e) { }
try { await git.push() } catch (e) { }
console.log(`Shared "${name}" project`)
try { await git.push(branchToShare) } catch (e) { }
console.log(`Shared "${name}" project, ${branchToShare} branch`)
}
})
program
.command('share')
.description('share a gitpear repo')
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
.addArgument(new commander.Argument('[v]', 'visibility of the repo').default('public'))
.action(async (p, v, options) => {
const fullPath = path.resolve(p)
.option('-b, --branch [b]', 'branch to share, default is current branch', '')
.option('-v, --visibility [v]', 'visibility of the repo', 'public')
.option('-p, --path [p]', 'path to the repo', '.')
.action(async (options) => {
const fullPath = path.resolve(options.path)
if (!fs.existsSync(path.join(fullPath, '.git'))) {
console.error('Not a git repo')
process.exit(1)
@@ -77,10 +83,12 @@ program
process.exit(1)
}
const currentBranch = await git.getCurrentBranch()
const branchToShare = options.branch || currentBranch
try { home.shareAppFolder(name) } catch (e) { }
try { acl.setACL(name, { visibility: v }) } catch (e) { }
try { await git.push() } catch (e) { }
console.log(`Shared "${name}" project, as ${v} repo`)
try { acl.setACL(name, { visibility: options.visibility }) } catch (e) { }
try { await git.push(branchToShare) } catch (e) { }
console.log(`Shared "${name}" project, ${branchToShare} branch, as ${options.visibility} repo`)
return
})

View File

@@ -20,6 +20,25 @@ async function getCommit () {
})
}
async function getCurrentBranch () {
return await new Promise((resolve, reject) => {
const process = spawn('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
let outBuffer = Buffer.from('')
process.stdout.on('data', data => {
outBuffer = Buffer.concat([outBuffer, data])
})
let errBuffer = Buffer.from('')
process.stderr.on('err', data => {
errBuffer = Buffer.concat([errBuffer, data])
})
process.on('close', code => {
return code === 0 ? resolve(outBuffer.toString().replace('\n', '')) : reject(errBuffer)
})
})
}
async function lsPromise (url) {
const ls = spawn('git', ['ls-remote', url])
const res = {}
@@ -201,4 +220,14 @@ async function unpackStream (packStream) {
})
}
module.exports = { lsPromise, uploadPack, unpackFile, unpackStream, createBareRepo, addRemote, push, getCommit }
module.exports = {
lsPromise,
uploadPack,
unpackFile,
unpackStream,
createBareRepo,
addRemote,
push,
getCommit,
getCurrentBranch,
}