Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions gradle/validation/git-status.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ configure(rootProject) {
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)
Expand All @@ -62,10 +60,19 @@ configure(rootProject) {
}

// Verify git working copy does not have any unstaged modified files.
// Status is captured here (not in gitStatus) so it reflects changes from tests that finalize with this task.
task checkWorkingCopyClean() {
dependsOn gitStatus
doFirst {
def status = rootProject.ext.gitStatus
def status
try {
def repository = new FileRepositoryBuilder()
.setWorkTree(rootProject.projectDir)
.setMustExist(true)
.build()
status = new Git(repository).status().call()
} catch (RepositoryNotFoundException | NoWorkTreeException e) {
status = null
}
if (status == null) {
if (file("${rootProject.projectDir}/.git").exists()) {
// Ignore git worktree branches until jgit supports them.
Expand Down Expand Up @@ -121,4 +128,16 @@ configure(rootProject) {
}
}
}

// Catch tests that write into the working tree.
// On CI: finalize every Test task with the clean check (runs even on test failure).
// Locally: only enforce ordering when both are already scheduled (e.g. via `check`).
allprojects {
tasks.withType(Test).configureEach { testTask ->
rootProject.checkWorkingCopyClean.mustRunAfter(testTask)
if (isCIBuild) {
testTask.finalizedBy(rootProject.checkWorkingCopyClean)
}
}
}
}
Loading