From 84cdcd29aecb30194e0394c655806dd68937668f Mon Sep 17 00:00:00 2001 From: dzdidi Date: Thu, 8 Feb 2024 18:46:36 +0000 Subject: [PATCH] share use options instead of args, share current branch by default Signed-off-by: dzdidi --- Readme.md | 4 ++-- src/cli.js | 28 ++++++++++++++++++---------- src/git.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index 823e9c7..31d610e 100644 --- a/Readme.md +++ b/Readme.md @@ -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:/` -* `git pear init [-s, --share] ` - 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/). 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 ` later -* `git pear share ` - makes repository sharable +* `git pear init [-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/). 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 ` later +* `git pear share [-p, --path [path (default: ".")]> [-b, --branch [branch name (default: "_current_")] [-v, --visibility (default: "public")]` - share repository, if branch is not specified, default branch will be shared * `git pear unshare ` - stop sharing repository * `git pear list [-s, --shared]` - list all or (only shared) repositories * `git pear list ` - list repositories of a peer diff --git a/src/cli.js b/src/cli.js index 7044267..ea963e4 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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 }) diff --git a/src/git.js b/src/git.js index 41bf38d..789b373 100644 --- a/src/git.js +++ b/src/git.js @@ -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, +}