2020-01-27 12:05:34 -05:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-01-27 14:47:02 -05:00
|
|
|
// This verifies local git repository's status.
|
2020-01-27 11:29:59 -05:00
|
|
|
|
|
|
|
import org.eclipse.jgit.api.*;
|
|
|
|
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
|
|
|
import org.eclipse.jgit.errors.*;
|
|
|
|
|
|
|
|
buildscript {
|
|
|
|
repositories {
|
|
|
|
mavenCentral()
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies {
|
2020-02-20 07:54:07 -05:00
|
|
|
classpath "org.eclipse.jgit:org.eclipse.jgit:${scriptDepVersions['jgit']}"
|
2020-01-27 11:29:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:56:35 -04:00
|
|
|
configure(rootProject) {
|
|
|
|
task gitStatus() {
|
|
|
|
doFirst {
|
|
|
|
try {
|
|
|
|
def repository = new FileRepositoryBuilder()
|
|
|
|
.setWorkTree(rootProject.projectDir)
|
|
|
|
.setMustExist(true)
|
|
|
|
.build()
|
2020-01-27 11:29:59 -05:00
|
|
|
|
2020-04-04 14:56:35 -04:00
|
|
|
def ref = repository.findRef("HEAD").getObjectId()
|
|
|
|
project.ext.gitRev = ref.name()
|
|
|
|
project.ext.gitRevShort = ref.abbreviate(8).name()
|
|
|
|
project.ext.gitStatus = new Git(repository).status().call()
|
|
|
|
} catch (RepositoryNotFoundException | NoWorkTreeException e) {
|
|
|
|
project.ext.gitRev = "(not a git checkout)"
|
|
|
|
project.ext.gitRevShort = "(not a git checkout)"
|
|
|
|
project.ext.gitStatus = null
|
|
|
|
} catch (NotSupportedException e) {
|
|
|
|
throw new GradleException("jgit does not support git repository version at this location: ${dir}", e)
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 11:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify git working copy does not have any unstaged modified files.
|
|
|
|
task checkWorkingCopyClean() {
|
2020-04-04 14:56:35 -04:00
|
|
|
dependsOn gitStatus
|
2020-01-27 11:29:59 -05:00
|
|
|
doFirst {
|
2020-04-04 14:56:35 -04:00
|
|
|
def status = rootProject.ext.gitStatus
|
2020-01-27 11:29:59 -05:00
|
|
|
if (status == null) {
|
|
|
|
// Ignore the check. This isn't a git checkout.
|
2020-04-04 14:56:35 -04:00
|
|
|
logger.warn("WARNING: Directory is not a valid GIT checkout (won't check dirty files): ${rootProject.projectDir}")
|
2020-01-27 11:29:59 -05:00
|
|
|
} 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) {
|
2020-01-27 14:47:02 -05:00
|
|
|
def checkProp = "validation.git.failOnModified"
|
|
|
|
def shouldFail = Boolean.valueOf(propertyOrDefault(checkProp, true))
|
|
|
|
def message = "Working copy is not a clean git checkout" +
|
|
|
|
(shouldFail ? " (skip with -P${checkProp}=false)" : "") +
|
|
|
|
", offending files:\n${offenders.join("\n")}"
|
|
|
|
|
|
|
|
if (shouldFail) {
|
|
|
|
throw new GradleException(message)
|
|
|
|
} else {
|
|
|
|
logger.lifecycle("NOTE: ${message}")
|
|
|
|
}
|
2020-01-27 11:29:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|