-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrotten.js
executable file
·177 lines (159 loc) · 6.01 KB
/
rotten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
var path = require('path')
var exec = require('child_process').exec
var async = require('async')
var colors = require('colors')
var optimist =
require('optimist')
.alias('h', 'help')
.alias('r', 'repo')
.default('r', process.cwd())
.describe('r', 'the repo youd like to examine for rotting code')
.alias('p', 'prod')
.default('p', 'master')
.describe('p', 'the branch you have running in production')
.alias('c', 'mostcommits')
.default('c', false)
.describe('c', 'show branches with the most commits first (defaults to'
+ 'showing oldest commits first)')
.default('keep', false)
.describe('keep', 'don\'t run "harvested" checks (if you want to keep'
+ ' merged branches)')
.usage('Usage: $0 --repo /path-to-git-repo --prod master')
var help = optimist.help()
var argv = optimist.argv
if (argv.help) {
console.log(help)
return
}
var repoDir = path.resolve(__dirname, argv.repo)
var prod = argv.prod
function git(args, cb) {
// args = '--git-dir ' + repoDir + '/.git ' + args
// console.log('git ' + args)
// return exec([ 'git', args ], cb)
return exec('git ' + args, { cwd: repoDir }, cb)
}
function trim (s) { return s.trim() }
function identity (s) { return s }
function main () {
console.log('Running against', repoDir.green)
console.log('Checking that branches are in production branch', prod.green)
git('branch -r', function (err, stdout, stderr) {
if (err) handleError(new Error(err.message).stack)
var branches = stdout.split('\n').map(trim).filter(identity)
var inprod = []
var notinprod = []
var prodRegex = new RegExp('/' + prod + '$')
var dot = 0
async.forEachSeries(branches, function (branch, cb) {
if (prodRegex.test(branch)) return cb() // ignore e.g. origin/master
if (dot++ % 5 === 0) process.stdout.write('.')
// git log dt-bsr --not --remotes="*/release"
// --format="%H | %ae | %ce | %ar | %cr | %ct"
git('log ' + branch + ' --not --remotes="*/' + prod
+ '" --format="%H | %ae | %ce | %ar | %cr | %ct"', onBranch)
function onBranch (err, stdout, stderr) {
if (err) console.log('\n', new Error(err.message).stack)
var commits = stdout.split('\n').map(trim).filter(identity)
.map(function (s) {
var fields = s.split(' | ')
return {
sha: fields[0]
, author: fields[1]
, committer: fields[2]
, authordateago: fields[3]
, committerdateago: fields[4]
, committertimestamp: fields[5] // unix timestamp
}
})
if (commits.length === 0) {
inprod.push({ branch: branch, commits: commits })
} else {
notinprod.push({ branch: branch, commits: commits })
}
cb()
}
}, function (err) {
if (err) handleError(new Error(err.message).stack)
console.log('')
if (!argv.keep && inprod.length) {
inprod = inprod.reverse()
inprod.forEach(function (info) {
console.log(' ' + info.branch.green
+ ' all in prod, please delete remote branch')
})
console.log()
console.log("==Paste the following to delete them all==".red)
var deleteThese =
inprod.map(function (info) {
// take everything after the slash
var branchName = info.branch.substr(info.branch.indexOf("/") + 1)
return 'git push origin :refs/heads/' + branchName.red + '; git branch -D '
+ branchName.red + ';'
})
.join('\n')
console.log(deleteThese)
console.log()
console.log()
} else if (!argv.keep) {
console.log('==Congrats, repo is clean; all branches already merged'
+ ' into '.green + prod.magenta + '=='.green)
}
if (notinprod.length) {
console.log('==Branches waiting to get into prod (or plain rotten).'
+ ' Most oldest/most commits first=='.red)
if (argv.mostcommits) {
notinprod.sort(function (a, b) {
return b.commits.length - a.commits.length
})
} else {
// oldest first
notinprod.sort(function (a, b) {
return a.commits[0].committertimestamp
- b.commits[0].committertimestamp
})
}
notinprod.forEach(function (info) {
var latest = info.commits[0]
console.log(' ' + ('' + info.commits.length).red + ' '
+ info.branch.red + ': ' + (info.commits.length + '').red
+ ' commits waiting. Latest commit: Author %s %s. Committer %s %s.'
, latest.author, latest.authordateago, latest.committer.green
, latest.committerdateago)
})
} else {
console.log(
'==Congrats, repo has no remote branches waiting to get into '.green
+ prod.magenta + '. You are a superstar=='.green)
}
console.log('#rotten is the number of branches you need to merge to prod')
if (!argv.keep) {
console.log('harvested: branches already in prod that need to be deleted')
}
console.log('Please tweet your score! (With the #rotten hashtag :)')
console.log(' Your rotten score is '
+ ('#rotten:' + notinprod.length + (argv.keep ? '' : '/harvested:' + inprod.length)).green)
process.exit(0)
})
})
}
process.on('uncaughtException', function (err) {
if (err) handleError(new Error(err.message).stack)
process.exit(1)
})
function handleError(err) {
if (/spawn Unknown system errno 23/.test(err.message)) {
console.log('\n', new Error(err.message).stack)
console.log('ERROR. Please be sure you\'ve specified a branch that exists.'
+ '\nI have reason to believe that the branch ' + prod.red +' does not'
+ ' exist in this repo.')
} else {
console.log('\n', new Error(err.message).stack)
console.log('Please report bugs to https://github.com/dtrejo/rotten,'
+ ' thank you. Please be sure that your ulimit is set very high!')
}
}
if (require.main === module) {
main()
}