-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·134 lines (109 loc) · 3.81 KB
/
index.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
#!/usr/bin/env node
const chalk = require('chalk')
const cp = require('child_process')
const readline = require('readline')
// Required to access git's copy of gpg
if (process.platform === 'win32') {
process.env.PATH += ';C:\\Program Files\\Git\\usr\\bin\\'
}
function logFlag(flag, desc) {
console.log(chalk.green.bold(flag), desc)
}
function showHelpAndExit(flag) {
console.log(chalk.cyan.bold(`Usage: git-signed [--help|--join|--sync|--export|commit hash])`))
console.log(``)
logFlag(` --help`, ` Print this help screen`)
logFlag(` --add`, ` Add yourself as a contributor to the project`)
logFlag(` --sync`, ` Import GPG keys from all contributors`)
logFlag(` [commit hash]`, ` Check history from this commit onward (optional)`)
console.log(``)
console.log(chalk.yellow.bold(`If no commit hash is provided, the entire
git history of the repository will be scanned`))
process.exit(flag === '--help' ? 0 : 1)
}
function showGuide() {
console.log(chalk.yellow.bold(`If your username is in the list, make sure`))
console.log(chalk.yellow.bold(`that you have joined the project correctly:`))
console.log('')
console.log(chalk.cyan.bold(` ./node_modules/.bin/git-signed --join`))
console.log('')
console.log(chalk.yellow.bold('Also, make sure to sync the pubkeys from'))
console.log(chalk.yellow.bold('all the contributors on the project:'))
console.log('')
console.log(chalk.cyan.bold(` ./node_modules/.bin/git-signed --sync`))
console.log('')
console.log(
chalk.yellow.bold(`For more information, see`),
chalk.cyan.bold('https://www.npmjs.com/package/git-signed')
)
console.log('')
}
// Command and arguments
const argOne = process.argv[2]
switch (argOne) {
case '--join':
case '--sync':
case '--export':
require(`./commands/${argOne.substring(2)}`)
break
default:
if (argOne && argOne[0] === '-') {
showHelpAndExit(argOne[0])
}
const checkAfter = argOne
const command = 'git'
const args = [
'log',
'--no-merges',
'--pretty=format:%G? %h %aN\t%s'
]
if (checkAfter) {
args.push(`${checkAfter}..`)
}
// We start the process - the subprocess' stderr is piped directly to
// our current process, and if the subprocess exits with an error we exit
// immediately
const proc = cp.spawn(command, args)
proc.stderr.pipe(process.stderr)
proc.on('exit', function (code) {
if (code > 0) {
process.exit(code)
}
})
// We will store all unsigned commits here
let unsignedCommits = []
const rl = readline.createInterface({
input: proc.stdout
})
// We parse the subprocess' stdout line by line, and
// look for lines with no signatures or invalid signatures
rl.on('line', function (data) {
var line = data.toString()
if (line.substring(0, 2) === 'N ') {
unsignedCommits.push(line.substring(2))
}
})
// On readline close, make sure we have no
// unsigned commits
rl.on('close', function () {
if (unsignedCommits.length > 0) {
console.error('')
console.error(chalk.red.bold('The following commits are not signed'))
console.error(chalk.red.bold('------------------------------------'))
console.error('')
unsignedCommits.forEach((line) => {
const [ details, title ] = line.split('\t')
const [ hash, user ] = details.split(' ')
console.error(chalk.red.bold(hash), chalk.yellow.bold(user) + '\t' + chalk.gray(title))
})
console.error('')
console.error(chalk.red.bold('------------------------------------'))
console.error('')
showGuide()
process.exit(1)
}
console.log('')
console.log(chalk.green.bold('!! All commits are signed! Good job! !!'))
console.log('')
})
}