git-branch-clean/main.js

30 lines
914 B
JavaScript

require('simple-git');
const { promisify } = require('util');
// Local path of the git repository
const repoPath = '/home/username/Projects/project-name';
const git = simpleGit(repoPath);
const gitBranch = promisify(git.branch.bind(git));
const gitPush = promisify(git.push.bind(git));
async function main() {
try {
await git.fetch(['--prune']);
let branches = (await gitBranch(['-r', '--merged', 'HEAD'])).all;
const trunkBranches = ["test", "master", "dev"];
branches = branches.map((branch) => {return branch.substring(7, branch.length)});
branches = branches.filter((element) => !trunkBranches.includes(element));
for (let branch of branches) {
await gitPush(['origin', '--delete', branch]);
console.log(`Branch deleted: ${branch}`);
}
console.log('Merged branches deleted successfully!');
} catch (err) {
console.error('[Error]', err);
}
}
main();