Skip to content

Commit

Permalink
feat: 支持删除所有用户已经合并的本地和远程分支&默认只删除当前用户分支
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 1. 支持删除所有用户已经合并的本地和远程分支 2. 默认只删除当前用户分支 3. 修改init交互 4. 增加cleartype配置项
  • Loading branch information
mengshang918 committed Oct 23, 2020
1 parent 85fafa4 commit b61421c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
12 changes: 10 additions & 2 deletions src/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author: jiangxiaowei
* @Date: 2020-10-10 11:54:44
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2020-10-21 19:05:30
* @Last Modified time: 2020-10-23 15:11:10
*/
const { program } = require('commander')
const parseArgs = require('minimist')
Expand All @@ -24,6 +24,11 @@ module.exports = (gitUser) => {
.option('-remote,--remotename <origin>', '远程仓库名字', 'origin')
.option('--branchreg <RegExp>', '匹配的分支正则')
.option('--ignorereg <RegExp>', '忽略的分支正则')
.option(
'-t,--cleartype <all|current|custom>',
'删除所有用户合并分支|只删除当前用户分支|删除自定义用户分支',
'current'
)
.parse(process.argv)

const {
Expand All @@ -33,13 +38,15 @@ module.exports = (gitUser) => {
init,
yes,
main,
cleartype,
user,
remotename,
} = program
if (
!['local', 'remote', 'all'].includes(position) ||
(branchreg && !validateStrIsReg(branchreg)) ||
(ignorereg && !validateStrIsReg(ignorereg))
(ignorereg && !validateStrIsReg(ignorereg)) ||
!['all', 'current', 'custom'].includes(cleartype)
) {
program.help()
}
Expand All @@ -48,6 +55,7 @@ module.exports = (gitUser) => {
answers: {
...(hasKey(args, ['m', 'main']) && { main }),
...(hasKey(args, ['u', 'user']) && { user }),
...(hasKey(args, ['t', 'cleartype']) && { clearType: cleartype }),
...(hasKey(args, ['p', 'position']) && { clearPosition: position }),
...(hasKey(args, ['remote', 'remotename']) && { remoteName: remotename }),
isReg: !!branchreg,
Expand Down
29 changes: 16 additions & 13 deletions src/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author: jiangxiaowei
* @Date: 2020-09-29 16:39:30
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2020-10-22 11:51:32
* @Last Modified time: 2020-10-23 18:30:52
*/
/* {
main: 'master',
Expand Down Expand Up @@ -46,24 +46,27 @@ module.exports = async (branchLocal, branchRemote, options) => {
isIgnore,
branchRegStr,
ignoreRegStr,
clearType,
currentUser,
} = options
const filterUser = clearType === 'current' ? currentUser : user
// 当前用户的本地分支
let allLocalBranch = []
// 当前用户的远程分支
let allRemoteBranch = []
try {
// 匹配远程 具有Authored by的
const regRemote = new RegExp(
`^refs\\/remotes\\/${remoteName}\\/\\S+\\sAuthored\\sby:\\s${user}$`
`^refs\\/remotes\\/${remoteName}\\/\\S+\\sAuthored\\sby:\\s${filterUser}$`
)
// 匹配local 具有Authored by的
const regLocal = new RegExp(
`^refs\\/heads\\/\\S+\\sAuthored\\sby:\\s${user}$`
`^refs\\/heads\\/\\S+\\sAuthored\\sby:\\s${filterUser}$`
)
allLocalBranch = (
await execa('git', [
'for-each-ref',
`--format=%(refname)%(if:equals=${user})%(authorname)%(then) Authored by: %(authorname)%(end)`,
`--format=%(refname)%(if:equals=${filterUser})%(authorname)%(then) Authored by: %(authorname)%(end)`,
'refs/heads/**',
])
).stdout
Expand All @@ -72,12 +75,12 @@ module.exports = async (branchLocal, branchRemote, options) => {
.map((item) => {
return item
.replace('refs/heads/', '')
.replace(` Authored by: ${user}`, '')
.replace(` Authored by: ${filterUser}`, '')
})
allRemoteBranch = (
await execa('git', [
'for-each-ref',
`--format=%(refname)%(if:equals=${user})%(authorname)%(then) Authored by: %(authorname)%(end)`,
`--format=%(refname)%(if:equals=${filterUser})%(authorname)%(then) Authored by: %(authorname)%(end)`,
`refs/remotes/${remoteName}/**`,
])
).stdout
Expand All @@ -86,7 +89,7 @@ module.exports = async (branchLocal, branchRemote, options) => {
.map((item) => {
return item
.replace('refs/remotes/', '')
.replace(` Authored by: ${user}`, '')
.replace(` Authored by: ${filterUser}`, '')
})
} catch (error) {
log(chalk.red(error))
Expand All @@ -106,15 +109,15 @@ module.exports = async (branchLocal, branchRemote, options) => {
const deleteLocalBranch = branchLocal.filter(
(item) =>
!reg.test(item) &&
allLocalBranch.includes(item) &&
(clearType === 'all' || allLocalBranch.includes(item)) &&
(!branchReg || branchReg.test(item)) &&
(!ignoreReg || !ignoreReg.test(item))
)
// 即将被删除的远程分支
const deleteRemoteBranch = branchRemote.filter(
(item) =>
!regRemote.test(item) &&
allRemoteBranch.includes(item) &&
(clearType === 'all' || allRemoteBranch.includes(item)) &&
(!branchReg || branchReg.test(item)) &&
(!ignoreReg || !ignoreReg.test(item))
)
Expand Down Expand Up @@ -146,17 +149,17 @@ module.exports = async (branchLocal, branchRemote, options) => {
// 删除本地分支
deleteLocal &&
deleteLocal.length > 0 &&
deleteLocal.map(async (item) => {
deleteLocal.map((item) => {
spinner.start(`删除本地分支${item}`)
await execa('git', ['branch', '-D', item])
execa.sync('git', ['branch', '-D', item])
spinner.succeed()
})
// 删除远程分支
deleteRemote &&
deleteRemote.length > 0 &&
deleteRemote.map(async (item) => {
deleteRemote.map((item) => {
spinner.start(`删除远程分支${item.replace(`${remoteName}/`, '')}`)
await execa('git', [
execa.sync('git', [
'push',
remoteName,
'--delete',
Expand Down
41 changes: 29 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @Author: jiangxiaowei
* @Date: 2020-09-29 16:39:41
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2020-10-22 11:58:46
* @Last Modified time: 2020-10-23 18:29:43
*/
const fs = require('fs')
const inquirer = require('inquirer')
Expand All @@ -22,10 +22,10 @@ const spinner = new ora('统计待删除分支信息')
/**
* 分支删除
* @param {object} options 兜底值
* @param {object} answers 交互式获取到的对象
* @param {object} answers 配置文件获取到的对象
* @param {object} config 配置文件获取到的对象
* @param {object} answers 参数方式获取到的对象
*/
const startInit = async (options, answers, config) => {
const startInit = async (options, config, answers) => {
const {
main,
clearPosition,
Expand All @@ -49,8 +49,13 @@ const startInit = async (options, answers, config) => {
break
case 'remote':
spinner.start('git pull')
await execa('git', ['pull'])
spinner.succeed()
try {
await execa('git', ['pull'])
spinner.succeed()
} catch (error) {
log('\n' + error.stderr)
spinner.fail('git pull失败,获取的远程分支信息可能不准确')
}
spinner.start('删除无效的本地远程分支')
// 删除无效的本地远程分支。
await execa('git', ['remote', 'prune', remoteName])
Expand All @@ -60,8 +65,13 @@ const startInit = async (options, answers, config) => {
break
case 'all':
spinner.start('git pull')
await execa('git', ['pull'])
spinner.succeed()
try {
await execa('git', ['pull'])
spinner.succeed()
} catch (error) {
log('\n' + error.stderr)
spinner.fail('git pull失败,获取的远程分支信息可能不准确')
}
spinner.start('删除无效的本地远程分支')
// 删除无效的本地远程分支。
await execa('git', ['remote', 'prune', remoteName])
Expand All @@ -78,6 +88,8 @@ const startInit = async (options, answers, config) => {
...config,
...answers,
spinner,
// 当前git账号
currentUser: options.user,
})
}

Expand Down Expand Up @@ -129,12 +141,17 @@ const createConfigWay = (showUI, options, answers) => {
try {
// 设置默认git账号。用来控制仅删除当前账号下的分支,避免删除其他人分支
const res = await execa('git', ['config', 'user.name'])
const index = questions.findIndex((item) => item.name === 'user')
questions[index].default = res.stdout

const index = questions.findIndex((item) => item.name === 'clearType')
const userIndex = questions.findIndex((item) => item.name === 'user')
questions[userIndex].default = res.stdout
const i = questions[index].choices.findIndex(
(item) => item.value === 'current'
)
questions[index].choices[i].name = `只删除当前用户(${res.stdout})创建的分支`
// 兜底值
const options = {
main: 'master',
clearType: 'current',
// 默认当前.git 账户
user: res.stdout,
// 'local' | 'remote' | 'all'
Expand Down Expand Up @@ -167,7 +184,7 @@ const createConfigWay = (showUI, options, answers) => {
} else {
exitConfig()
const config = safeLoad(fs.readFileSync('./.branchclear.yml'))
startInit(options, answers, config)
startInit(options, config, answers)
}
} catch (error) {
log(chalk.red(error))
Expand Down
24 changes: 22 additions & 2 deletions src/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* 交互问题
* @Author: jiangxiaowei
* @Date: 2020-10-10 11:54:57
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2020-10-10 11:54:57
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2020-10-22 18:05:27
*/
const { validateReg } = require('./utils')

Expand All @@ -14,10 +14,30 @@ module.exports = [
name: 'main',
default: 'master',
},
{
type: 'list',
message: '请选择删除分支类型',
choices: [
{
name: '删除所有用户已经合并的分支',
value: 'all',
},
{
name: '只删除当前用户创建的分支',
value: 'current',
},
{
name: '自定义用户',
value: 'custom',
},
],
name: 'clearType',
},
{
type: 'input',
message: '请输入git账号,仅会删除git账号下的分支',
name: 'user',
when: (answers) => answers.clearType === 'custom',
default: '',
},
{
Expand Down

0 comments on commit b61421c

Please sign in to comment.