-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckCommit.groovy
63 lines (50 loc) · 1.81 KB
/
checkCommit.groovy
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
import com.google.gerrit.extensions.annotations.Listen
import com.google.gerrit.server.events.CommitReceivedEvent
import com.google.gerrit.server.git.GitRepositoryManager
import com.google.gerrit.server.git.validators.CommitValidationException
import com.google.gerrit.server.git.validators.CommitValidationListener
import com.google.gerrit.server.git.validators.CommitValidationMessage
import com.google.inject.Singleton
import com.google.inject.Inject
import org.eclipse.jgit.diff.DiffFormatter
import org.eclipse.jgit.diff.RawText
@Singleton
@Listen
public class DoNotCommitHook implements CommitValidationListener {
@Inject
GitRepositoryManager repoManager
def regex = /DO_?NOT_?COMMIT/
/* This class is just to override the writeAddedLine method with
* the logic to detect lines having incorrect text.
*/
class AddedLineDetector extends DiffFormatter {
def error
AddedLineDetector() {
super(new ByteArrayOutputStream())
}
@Override
public void writeAddedLine(RawText text, int line) {
super.writeAddedLine(text, line)
def lineText = text.getString(line)
if (lineText =~ regex) {
error = lineText
}
}
}
@Override
public List<CommitValidationMessage> onCommitReceived(CommitReceivedEvent receiveEvent) throws CommitValidationException {
def projectName = receiveEvent.project.nameKey
def repo = repoManager.openRepository(projectName)
def df = new AddedLineDetector()
df.setRepository(repo)
// Diff between this commit and the parent (git diff HEAD^..HEAD)
def commit = receiveEvent.commit
def parent = commit.getParent(0)
// This starts the diff and formatting process.
df.format(parent, commit)
if (df.error) {
throw new CommitValidationException("Line: '${df.error}' matches ${regex}")
}
return [ new CommitValidationMessage("free from ${regex}", false) ]
}
}