lucene/gradle/validation/git-status.gradle

65 lines
2.0 KiB
Groovy

// 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'
}
}
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)
}
}
configure(rootProject) {
// Verify git working copy does not have any unstaged modified files.
task checkWorkingCopyClean() {
doFirst {
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()
if (offenders) {
throw new GradleException("Working copy is not a clean git checkout, offending files:\n${offenders.join("\n")}")
}
}
}
}
}