-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.js
More file actions
executable file
·90 lines (75 loc) · 2.44 KB
/
execute.js
File metadata and controls
executable file
·90 lines (75 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const select = require('cli-select')
const exec = require('child_process').execSync
const Fuse = require('fuse.js')
const chalk = require('chalk')
module.exports = async (branch, merge, force) => {
async function start(result, branch, merge, force) {
let options = result
.trim()
.split('\n')
.map(x => x.replace('*','').trim())
.map(x => {return {branch: x}})
let fuseOpts = {
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
includeScore: true,
keys: [
"branch"
]
}
let fuse = new Fuse(options, fuseOpts)
let searchResults = fuse.search(branch)
searchResults.sort((a,b) => {
if(a.score == b.score) return a.item.branch.length - b.item.branch.length
return a.score - b.score
})
options = searchResults
.filter(x => x.score < 0.4)
.slice(0, 10)
.map(x => x.item.branch)
if(options.length == 0) {
console.error(chalk.red('no matching branches found'))
process.exit(1)
}
let selected = ''
if(force) {
selected = options[0]
} else {
let response = await select({
values: options,
selected: chalk.yellow('▶'),
unselected: ' ',
indentation: 2,
valueRenderer: (value, selected) => {
if (selected) {
return chalk.yellow(value)
}
return chalk.white(value)
},
})
selected = response.value
}
if(merge) {
console.log('merging branch: ' + chalk.green(selected))
exec('git merge ' + selected)
} else {
console.log('checking out branch: ' + chalk.green(selected))
exec('git checkout ' + selected)
}
}
let list = exec('git branch').toString()
if(!branch) {
console.error(chalk.red('no branch name provided'))
process.exit(1)
}
try {
await start(list, branch, merge, force)
} catch(e) {
// if(e) console.error(e)
process.exit(1)
}
}