forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-grep-commits.js
More file actions
158 lines (140 loc) · 3.68 KB
/
git-grep-commits.js
File metadata and controls
158 lines (140 loc) · 3.68 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
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
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const spawnSync = require('child_process').spawnSync
const Result = require('../lib/result')
// eslint-disable-next-line no-unused-vars
const FileSystem = require('../lib/file_system')
function listCommitsWithLines(fileSystem, options) {
const pattern = '(' + options.denylist.join('|') + ')'
const commits = gitAllCommits(fileSystem.targetDir)
return commits
.map(commit => {
return {
hash: commit,
lines: gitLinesAtCommit(
fileSystem.targetDir,
pattern,
options.ignoreCase,
commit
).filter(line => fileSystem.shouldInclude(line.path))
}
})
.filter(commit => commit.lines.length > 0)
}
/**
* @param targetDir
* @ignore
*/
function gitAllCommits(targetDir) {
const args = ['-C', targetDir, 'rev-list', '--all']
return spawnSync('git', args).stdout.toString().trim().split('\n')
}
/**
* @param targetDir
* @param pattern
* @param ignoreCase
* @param commit
* @ignore
*/
function gitGrep(targetDir, pattern, ignoreCase, commit) {
const args = [
'-C',
targetDir,
'grep',
'-E',
ignoreCase ? '-i' : '',
pattern,
commit
]
return spawnSync('git', args)
.stdout.toString()
.split('\n')
.filter(x => !!x)
}
/**
* @param targetDir
* @param pattern
* @param ignoreCase
* @param commit
* @ignore
*/
function gitLinesAtCommit(targetDir, pattern, ignoreCase, commit) {
const lines = gitGrep(targetDir, pattern, ignoreCase, commit).map(entry => {
const [path, ...rest] = entry.substring(commit.length + 1).split(':')
return { path: path, content: rest.join(':') }
})
return lines
}
/**
* @param fileSystem
* @param options
* @ignore
*/
function listFiles(fileSystem, options) {
const files = []
const commits = listCommitsWithLines(fileSystem, options)
commits.forEach(commit => {
commit.lines.forEach(line => {
const existingFile = files.find(f => f.path === line.path)
if (existingFile) {
const existingCommit = existingFile.commits.find(
c => c.hash === commit.hash
)
if (existingCommit) {
existingCommit.lines.push(line.content)
} else {
existingFile.commits.push({
hash: commit.hash,
lines: [line.content]
})
}
} else {
files.push({
path: line.path,
commits: [{ hash: commit.hash, lines: [line.content] }]
})
}
})
})
return files
}
/**
*
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories
* @param {object} options The rule configuration
* @returns {Result} The lint rule result
* @ignore
*/
function gitGrepCommits(fs, options) {
// backwards compatibility with blacklist
options.denylist = options.denylist || options.blacklist
const files = listFiles(fs, options)
const targets = files.map(file => {
const [firstCommit, ...rest] = file.commits
const restMessage =
rest.length > 0 ? `, and ${rest.length} more commits` : ''
const message = [
`(${
file.path
}) contains denylisted words in commit ${firstCommit.hash.substr(
0,
7
)}${restMessage}.`,
`\tdenylist: ${options.denylist.join(', ')}`
].join('\n')
return {
passed: false,
path: file.path,
message
}
})
if (targets.length === 0) {
const message = [
'No denylisted words found in any commits.',
`\tdenylist: ${options.denylist.join(', ')}`
].join('\n')
return new Result(message, [], true)
}
return new Result('', targets, false)
}
module.exports = gitGrepCommits