2020-01-03 09:22:36 -05:00
|
|
|
// 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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 03:36:14 -05:00
|
|
|
def gitStatus(dir) {
|
|
|
|
try {
|
|
|
|
def repository = new FileRepositoryBuilder()
|
|
|
|
.setWorkTree(dir)
|
|
|
|
.setMustExist(true)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
def status = new Git(repository).status().call()
|
|
|
|
return status
|
|
|
|
} catch (RepositoryNotFoundException | NoWorkTreeException e) {
|
|
|
|
logger.warn("WARNING: Directory is not a valid GIT checkout (won't check dirty files): ${dir}")
|
|
|
|
return null
|
|
|
|
} catch (NotSupportedException e) {
|
|
|
|
throw new GradleException("jgit does not support git repository version at this location: ${dir}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 09:22:36 -05:00
|
|
|
configure(rootProject) {
|
2020-01-20 03:36:14 -05:00
|
|
|
// Verify git working copy does not have any unstaged modified files.
|
|
|
|
task checkWorkingCopyClean() {
|
2020-01-03 09:22:36 -05:00
|
|
|
doFirst {
|
2020-01-20 03:36:14 -05:00
|
|
|
def status = gitStatus(rootProject.projectDir)
|
|
|
|
if (status == null) {
|
|
|
|
// Ignore the check. This isn't a git checkout.
|
|
|
|
} else {
|
|
|
|
def offenders = [
|
|
|
|
// Exclude staged changes. These are fine in precommit.
|
|
|
|
// "(added)": status.added,
|
|
|
|
// "(changed)": status.changed,
|
|
|
|
// "(removed)": status.removed,
|
|
|
|
"(conflicting)": status.conflicting,
|
|
|
|
"(missing)": status.missing,
|
|
|
|
"(modified)": status.modified,
|
|
|
|
"(untracked)": [status.untracked, status.untrackedFolders].flatten()
|
|
|
|
].collectMany { fileStatus, files ->
|
|
|
|
files.collect {file -> " - ${file} ${fileStatus}" }
|
|
|
|
}.sort()
|
2020-01-03 09:22:36 -05:00
|
|
|
|
2020-01-20 03:36:14 -05:00
|
|
|
if (offenders) {
|
2020-01-03 09:22:36 -05:00
|
|
|
throw new GradleException("Working copy is not a clean git checkout, offending files:\n${offenders.join("\n")}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|