/* * 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. */ // This verifies local git repository's status. 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:${scriptDepVersions['jgit']}" } } 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) { 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}") } } } } } }