-
Notifications
You must be signed in to change notification settings - Fork 17
/
prepare-commit-msg-hook.js
40 lines (30 loc) · 1.3 KB
/
prepare-commit-msg-hook.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
#!/usr/bin/env node
var fs = require('fs')
var options = require('commitplease').getOptions()
var oldMessagePath = options.oldMessagePath
var oldMessageSeconds = options.oldMessageSeconds
try {
// There may be an old message that was previously rejected by us
// Suggest it to the user so they do not have to start from scratch
// Will throw ENOENT if no such file, ask forgiveness not permission
var mtime = new Date(fs.statSync(oldMessagePath).mtime)
// Date.now() - mtime.getTime() is milliseconds, convert to seconds
var fresh = (Date.now() - mtime.getTime()) / 1000 < oldMessageSeconds
// There are many scenarios that trigger the prepare-commit-msg hook
// These scenarios pass different console parameters, see here:
// https://www.kernel.org/pub/software/scm/git/docs/githooks.html
//
// A plain `git commit` is the only scenario that passes 3 entries
// For all other scenarios (like `git commit -m`, squash or merge)
// just delete oldMessagePath, do not actually suggest it to user
var plain = process.argv === 3
if (plain && fresh) {
fs.writeFileSync(process.argv[2], fs.readFileSync(oldMessagePath))
}
fs.unlinkSync(oldMessagePath)
} catch (err) {
if (!/ENOENT/.test(err.message)) {
throw err
}
// there is no old message to reuse, swallow the exception
}