Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ const exec = promisify(require('child_process').exec)
const prompts = require('prompts')

async function run () {
const { stdout: branches } = await exec('git branch -v --sort=-committerdate')
const { stdout: localBranches } = await exec('git branch -v --sort=-committerdate')
const { stdout: remoteBranches } = await exec('git branch -v -r')

const choices = branches
const remoteTrimmedBranches = remoteBranches
.split(/\n/)
.filter(l => !l.includes('origin/HEAD') && l !== '')
.map(l => ` ^ ${l.split('origin/').slice(1).join('origin/')}`)

const choices = localBranches
.split(/\n/)
.concat(remoteTrimmedBranches)
.filter(branch => !!branch.trim())
.map(branch => {
const [, flag, value, hint] = branch.match(/([* ]) +([^ ]+) +(.+)/)
return { value, hint, disabled: flag === '*' }
.map(branch => branch.match(/([*^ ]) +([^ ]+) +(.+)/))
.filter(([, flag, value, hint], i, all) => flag !== '^' ||!all.find(e => e.value === value))
.map(([, flag, value, hint]) => {
return { title: flag === '^' ? `^ ${value}` : value, value, hint, disabled: flag === '*', remote: flag === '^' }
})


const { branch } = await prompts({
type: 'select',
name: 'branch',
Expand All @@ -32,7 +41,7 @@ async function run () {

async function checkout (branch) {
if (!branch) return
const { stdout, stderr } = await exec(`git checkout ${branch}`)
const { stdout, stderr } = await exec(`git checkout '${branch}'`)
process.stdout.write(stdout)
process.stderr.write(stderr)
}
Expand Down