Created
February 23, 2017 15:12
-
-
Save JamieMason/8445356b3600565184adb2c4a071326e to your computer and use it in GitHub Desktop.
List all remote git branches which have been merged into master and can be safely deleted.
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
const chalk = require('chalk'); | |
const execa = require('execa'); | |
const SPLIT = '~~~~'; | |
execa('git', [ | |
'for-each-ref', | |
'--sort=-committerdate', | |
'--sort=-authoremail', | |
'refs/remotes/', | |
'--merged', | |
'origin/master', | |
`--format="%(HEAD) %(refname:short)${SPLIT}%(objectname:short)${SPLIT}%(contents:subject)${SPLIT}%(authorname)${SPLIT}%(authoremail)${SPLIT}%(committerdate:relative)"` | |
]) | |
.then(getLines) | |
.then(lines => lines.map(readLine)) | |
.then(groupByAuthor) | |
.then(toArray) | |
.then(writeReport); | |
function getLines(result) { | |
return result.stdout.split('\n') | |
} | |
function readLine(line) { | |
const fields = line.replace(/^"|"$/g, '').trim().split(SPLIT); | |
return { | |
shortRefName: fields[0].replace('origin/', ''), | |
shortObjectName: fields[1], | |
subject: fields[2], | |
authorName: fields[3], | |
authorEmail: fields[4].replace(/>|</g, ''), | |
relativeCommitterDate: fields[5] | |
}; | |
} | |
function groupByAuthor(commits) { | |
return commits.reduce(function(commitsByAuthorEmail, commit) { | |
const key = commit.authorEmail.toLowerCase(); | |
(commitsByAuthorEmail[key] = commitsByAuthorEmail[key] || { | |
authorName: commit.authorName, | |
authorEmail: commit.authorEmail, | |
commits: [] | |
}); | |
commitsByAuthorEmail[key].commits.push(commit); | |
return commitsByAuthorEmail; | |
}, {}); | |
} | |
function toArray(object) { | |
return Object.keys(object).map(key => object[key]); | |
} | |
function writeReport(authors) { | |
authors.forEach(function(author) { | |
console.log(chalk.yellow('%s %s'), author.authorName, author.authorEmail); | |
console.log(chalk.grey('git push origin --delete ' + author.commits.map(commit => commit.shortRefName).join(' '))); | |
console.log(''); | |
author.commits.forEach(function(commit) { | |
console.log(' %s (%s) %s', commit.shortRefName, commit.relativeCommitterDate, chalk.grey(commit.subject)); | |
}); | |
console.log(''); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output is something like this but in colour;