Last active
August 29, 2015 13:57
-
-
Save hellatan/9767297 to your computer and use it in GitHub Desktop.
Delete a list of branches from the remote and/or locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"branches": ["branch-1", "branch-2", "branch-3"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
//DO NOT PUT "use strict" HERE OR THE LOGGER WILL NOT BE ABLE TO LOG ERROR | |
/* | |
1. node.js must be installed | |
2. added +x to this script | |
3. put this script anywhere, but run the shell command from within the desired repository | |
*/ | |
/** | |
* @var {Array} branches to delete from the github remote | |
* a uri to a json file can be passed in as | |
* the `--list` argument: --list=./branchesExample.json | |
*/ | |
var branches = []; | |
var ogBranchLength = 0; | |
var fs = require('fs'); | |
var util = require('util'); | |
var sys = require('sys'); | |
var exec = require('child_process').exec; | |
// for terminal input | |
var readline = require('readline'); | |
var rl = readline.createInterface(process.stdin, process.stdout); | |
var cwd = process.cwd(); | |
var UNIX_ERROR_LOG_ROOT = cwd + '/logs'; | |
var DELETE_LOCAL_BRANCH_VALUE = 'Y'; | |
var logFile = UNIX_ERROR_LOG_ROOT + "/branches_delete.log"; | |
util.log('Deleting branches from Github'); | |
function logDivider() { | |
if (branches.length) { | |
console.log(''); | |
console.log('------------------'); | |
console.log(''); | |
} | |
} | |
function delBranches(answer) { | |
if (branches.length) { | |
var branch = branches.shift(); | |
util.log('deleting ' + branch + ' from origin'); | |
exec('git push origin :' + branch, function (err, stdout, stderr) { | |
if (stderr) { | |
/* | |
// TODO: write to log file | |
// Test or create the directory, we do it synchronously to never loose any log message | |
try{ | |
if (!fs.statSync(unix_error_log_root).isDirectory()){ | |
fs.mkdirSync(unix_error_log_root, 0600); | |
} | |
}catch(e){ | |
fs.mkdirSync(unix_error_log_root, 0600); | |
} | |
if (!fs.existsSync(logFile)) { | |
fs.writeFileSync(logFile); | |
} | |
stderr = fs.createWriteStream(logFile, { flags: 'a' }); | |
*/ | |
// if the branch isn't present on the remote | |
// an error will be output | |
util.log(stderr); | |
} | |
if (answer === DELETE_LOCAL_BRANCH_VALUE) { | |
delLocalBranch(branch, answer, delBranches); | |
} else { | |
delBranches(answer); | |
logDivider(); | |
} | |
}); | |
} else { | |
util.log('there are no branches to delete'); | |
util.log('total branches deleted: ' + ogBranchLength); | |
// process.exit(0); | |
pruneLocal(); | |
} | |
} | |
function delLocalBranch(branch, answer, cb) { | |
util.log('deleting ' + branch + ' locally'); | |
exec('git branch -D ' + branch, function (err, stdout, stderr) { | |
if (stderr) { | |
// if the branch isn't present | |
// an error will be output | |
util.log(stderr); | |
} | |
logDivider(); | |
if (cb) { | |
cb(answer); | |
} | |
}); | |
} | |
function startUp() { | |
ogBranchLength = branches.length; | |
rl.question('Delete local branches: [Yn] ', function (answer) { | |
if (!answer) { | |
startUp(); | |
return; | |
} | |
if (answer === DELETE_LOCAL_BRANCH_VALUE) { | |
util.log('Branches will be deleted locally'); | |
} | |
delBranches(answer); | |
}); | |
} | |
function pruneLocal() { | |
rl.question('Prune local repo of stale branches: [Yn] ', function (answer) { | |
if (!answer) { | |
pruneLocal(); | |
return; | |
} | |
if (answer === DELETE_LOCAL_BRANCH_VALUE) { | |
exec('git remote prune origin', function (err, stdout, stderr) { | |
if (stderr) { | |
util.log(stderr); | |
} | |
console.log('local repo has been pruned of stale branches'); | |
console.log('good bye'); | |
process.exit(0); | |
}); | |
} else { | |
console.log('no pruning took place'); | |
process.exit(0); | |
} | |
rl.close(); | |
}); | |
} | |
var args = process.argv.slice(2); | |
if (args.length) { | |
args.forEach(function (val, index, array) { | |
if (val.indexOf('--list') > -1 && val.indexOf('=') > -1) { | |
var list = val.split('='); | |
var file = list[1]; | |
fs.readFile(file, 'utf8', function (err, data) { | |
if (err) { | |
throw err; | |
} | |
data = JSON.parse(data); | |
if (!data.branches) { | |
util.log('missing `branches`<array> property of list of branches'); | |
util.log('exiting process'); | |
process.exit(0); | |
} | |
branches = data.branches; | |
startUp(); | |
}); | |
} | |
}); | |
} else { | |
if (!branches.length) { | |
util.log('you must have at least one branch to delete.'); | |
util.log('exiting process'); | |
process.exit(0); | |
} | |
startUp(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment