clean local branches without origin connection (#26)
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
|
||||
import {
|
||||
confirmMessage,
|
||||
defaultMenuSettings,
|
||||
getSettings,
|
||||
} from '../../../../globals';
|
||||
const { MultiSelect } = require('enquirer');
|
||||
|
||||
module.exports = {
|
||||
name: 'clean_local_branches',
|
||||
alias: ['clb'],
|
||||
description: 'Clean local branches without origin connection (wf g clb)',
|
||||
hidden: false,
|
||||
run: async (toolbox: GluegunMenuToolbox) => {
|
||||
const { filesystem, print, system, strings } = toolbox;
|
||||
|
||||
// Check if current path is a git repo
|
||||
const currentPath = strings.trim(await system.run('pwd'));
|
||||
const isGitRepo = await filesystem.existsAsync(`${currentPath}/.git`);
|
||||
if (!isGitRepo) {
|
||||
print.error(`
|
||||
The Clean Local Branches command requires that you are in a git repository.
|
||||
`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get remote branches
|
||||
const remoteBranches = await system.run(`git branch -r`);
|
||||
const remoteBranchesArray = remoteBranches
|
||||
.split('\n')
|
||||
.map((branch) => strings.trim(branch.replace('origin/', '')));
|
||||
|
||||
// Get all local branches
|
||||
const localBranches = await system.run(`git branch`);
|
||||
const localBranchesArray = localBranches
|
||||
.split('\n')
|
||||
.map((branch) => strings.trim(branch.replace('*', '').trim()))
|
||||
.filter((branch) => branch !== '');
|
||||
|
||||
// Find local branches that don't have matching remote branches
|
||||
const branchesToDelete = localBranchesArray.filter(
|
||||
(localBranch) =>
|
||||
!remoteBranchesArray.includes(localBranch) &&
|
||||
localBranch !== 'master' &&
|
||||
localBranch !== 'main'
|
||||
);
|
||||
|
||||
if (branchesToDelete.length > 0) {
|
||||
print.info(
|
||||
`Found ${branchesToDelete.length} local branches without a remote connection.`
|
||||
);
|
||||
|
||||
// Create MultiSelect prompt with branches
|
||||
const prompt = new MultiSelect({
|
||||
name: 'branches',
|
||||
message:
|
||||
'Select branches to delete (use space to select, enter to confirm):',
|
||||
choices: branchesToDelete,
|
||||
result(names) {
|
||||
return this.map(names);
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
// Get selected branches
|
||||
const selectedBranches = await prompt.run();
|
||||
|
||||
if (Object.keys(selectedBranches).length === 0) {
|
||||
print.info('No branches selected for deletion.');
|
||||
|
||||
if (toolbox.fromMenu()) {
|
||||
await toolbox.menu.showMenu('workflow git', defaultMenuSettings);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Confirm deletion of selected branches
|
||||
if (
|
||||
await confirmMessage(
|
||||
`Delete ${Object.keys(selectedBranches).length} selected branches?`
|
||||
)
|
||||
) {
|
||||
for (const branch of Object.keys(selectedBranches)) {
|
||||
const deleteBranch = await system.run(`git branch -D ${branch}`);
|
||||
print.fancy(deleteBranch);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
print.error('Branch selection canceled or failed.');
|
||||
}
|
||||
} else {
|
||||
print.info(`No local branches without remote connections found.`);
|
||||
}
|
||||
|
||||
if (toolbox.fromMenu()) {
|
||||
await toolbox.menu.showMenu('workflow git', defaultMenuSettings);
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
module.exports = {
|
||||
name: 'clean_old_branches',
|
||||
alias: ['cb'],
|
||||
description: 'Prune remote and clean local branches if gone (cb)',
|
||||
description: 'Prune remote and clean local branches if gone (wf g cb)',
|
||||
hidden: false,
|
||||
run: async (toolbox: GluegunMenuToolbox) => {
|
||||
const { filesystem, print, system, strings } = toolbox;
|
||||
|
||||
Reference in New Issue
Block a user