Add a precommit placeholder task and working copy's git status check.

This commit is contained in:
Dawid Weiss 2020-01-03 15:22:36 +01:00
parent 363f2e3654
commit 8b03a7104e
3 changed files with 67 additions and 0 deletions

View File

@ -38,8 +38,10 @@ apply from: file('gradle/maven/defaults-maven.gradle')
apply from: file('gradle/defaults-idea.gradle')
// Validation tasks
apply from: file('gradle/validation/precommit.gradle')
apply from: file('gradle/validation/forbidden-apis.gradle')
apply from: file('gradle/validation/jar-checks.gradle')
apply from: file('gradle/validation/git-status.gradle')
// Additional development aids.
apply from: file('gradle/maven/maven-local.gradle')

View File

@ -0,0 +1,55 @@
// This adds top-level 'precommit' task with essential
// precommit validation checks.
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.errors.*;
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.eclipse.jgit:org.eclipse.jgit:5.3.0.201903130848-r'
classpath 'commons-codec:commons-codec:1.6'
}
}
configure(rootProject) {
// Verify git working copy does not have any modified files.
task checkWorkingCopyPristine() {
doFirst {
def gitDir = rootProject.projectDir
try {
def repository = new FileRepositoryBuilder()
.setWorkTree(gitDir)
.setMustExist(true)
.build()
def status = new Git(repository).status().call()
if (!status.clean) {
def offenders = [
"(added)": status.added,
"(changed)": status.changed,
"(conflicting)": status.conflicting,
"(missing)": status.missing,
"(modified)": status.modified,
"(removed)": status.removed,
"(untracked)": [status.untracked, status.untrackedFolders].flatten()
].collectMany { fileStatus, files ->
files.collect {file -> " - ${file} ${fileStatus}" }
}.sort()
throw new GradleException("Working copy is not a clean git checkout, offending files:\n${offenders.join("\n")}")
}
} catch (RepositoryNotFoundException | NoWorkTreeException | NotSupportedException e) {
logger.warn("WARNING: Directory is not a valid GIT checkout (won't check dirty files): ${gitDir}")
}
}
}
// Attach to precommit.
precommit.dependsOn checkWorkingCopyPristine
}

View File

@ -0,0 +1,10 @@
// This adds top-level 'precommit' task.
// Validation tasks attach themselves to it in their own
// respective build scripts.
configure(rootProject) {
task precommit() {
group = 'Precommit'
description = "All precommit checks"
}
}