-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (86 loc) · 2.9 KB
/
index.js
File metadata and controls
109 lines (86 loc) · 2.9 KB
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
var fs = require('fs')
var ini = require('ini')
var path = require('path')
var chalk = require('chalk')
var Git = require('git-tools')
var objectAssign = require('object-assign')
var validate = require('./lib/validate')
var sanitize = require('./lib/sanitize')
function getOptions () {
var pkg = path.join(process.cwd(), 'package.json')
var npm = path.join(process.cwd(), '.npmrc')
pkg = fs.existsSync(pkg) && require(pkg) || {}
npm = fs.existsSync(npm) && ini.parse(fs.readFileSync(npm, 'utf8')) || {}
pkg = pkg.commitplease || {}
npm = npm.commitplease || {}
return objectAssign(pkg, npm)
}
function runValidate (message, options) {
var errors = validate(sanitize(message), options)
if (errors.length) {
console.error('Invalid commit message, please fix:\n')
console.error(chalk.red('- ' + errors.join('\n- ')))
console.error()
console.error('Commit message was:')
console.error()
console.error(chalk.green(sanitize(message)))
if (options.style === undefined || options.style === 'jquery') {
console.error('\nSee https://bit.ly/jquery-guidelines')
} else if (options.style === 'angular') {
console.error('\nSee https://bit.ly/angular-guidelines')
}
process.exit(1)
}
}
module.exports = function () {
var argv = process.argv.slice(2)
var help = argv.some(function (value) {
if (value === '-h' || value === '--help') {
return true
}
})
if (argv.length > 1 || help) {
console.log(
'Usage: commitplease [committish]\n\n' +
'committish a commit range passed to git log\n\n' +
'Examples:\n\n' +
'1. Check all commits on branch master:\n' +
'commitplease master\n\n' +
'2. Check all commits on branch feature but not on master:\n' +
'commitplease master..feature\n\n' +
'3. Check the latest 1 commit (n works too):\n' +
'commitplease -1\n\n' +
'4. Check all commits between 84991d and 2021ce\n' +
'commitplease 84991d..2021ce\n\n' +
'5. Check all commits starting with 84991d\n' +
'commitplease 84991d..\n\n' +
'Docs on git commit ranges: https://bit.ly/commit-range'
)
process.exit(0)
}
var options = getOptions()
var message = path.join('.git', 'COMMIT_EDITMSG')
if (path.normalize(argv[0]) === message) {
runValidate(fs.readFileSync(message, 'utf8').toString(), options)
process.exit(0)
}
var committish = 'HEAD'
if (argv.length !== 0) {
committish = argv[0]
}
var repo = new Git(process.cwd())
var secret = '--++== CoMMiTPLeaSe ==++--'
var format = '--format=%B' + secret
repo.exec('log', format, committish, function (error, messages) {
if (error) {
console.error(error)
process.exit(1)
}
messages = messages.trim().split(secret)
messages.pop()
for (var i = 0; i < messages.length; ++i) {
runValidate(messages[i], options)
}
})
}
module.exports.getOptions = getOptions