From a29d7c70d5e26fd07660a0308106d10fdc4e9c29 Mon Sep 17 00:00:00 2001 From: Dawid Weiss Date: Tue, 3 Nov 2020 12:50:05 +0100 Subject: [PATCH] LUCENE-9597: checkWorkingCopyClean shouldn't complain about untracked empty folders (similar to git status). Piggybacking jgit update. (#2061) --- build.gradle | 4 +-- gradle/validation/git-status.gradle | 39 ++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index ada00210ff7..aaf58482a27 100644 --- a/build.gradle +++ b/build.gradle @@ -89,10 +89,10 @@ ext { "ecj": "3.19.0", "javacc": "7.0.4", "jflex": "1.7.0", - "jgit": "5.3.0.201903130848-r", + "jgit": "5.9.0.202009080501-r", "flexmark": "0.61.24", ] - + // Allow definiting external tool locations using system props. externalTool = { name -> def resolved = propertyOrDefault("${name}.exe", name as String) diff --git a/gradle/validation/git-status.gradle b/gradle/validation/git-status.gradle index 79a26fa48be..37c3d8dce39 100644 --- a/gradle/validation/git-status.gradle +++ b/gradle/validation/git-status.gradle @@ -17,9 +17,15 @@ // This verifies local git repository's status. -import org.eclipse.jgit.api.*; -import org.eclipse.jgit.storage.file.FileRepositoryBuilder; -import org.eclipse.jgit.errors.*; +import org.eclipse.jgit.api.* +import org.eclipse.jgit.storage.file.FileRepositoryBuilder +import org.eclipse.jgit.errors.* + +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.SimpleFileVisitor +import java.nio.file.Path +import java.nio.file.attribute.BasicFileAttributes buildscript { repositories { @@ -60,9 +66,29 @@ configure(rootProject) { doFirst { def status = rootProject.ext.gitStatus if (status == null) { - // Ignore the check. This isn't a git checkout. - logger.warn("WARNING: Directory is not a valid GIT checkout (won't check dirty files): ${rootProject.projectDir}") + if (file("${rootProject.projectDir}/.git").exists()) { + // Ignore git worktree branches until jgit supports them. + logger.warn("WARNING: git worktrees are not supported by jgit (won't check dirty files): ${rootProject.projectDir}") + } else { + // Ignore the check. This isn't a git checkout. + logger.warn("WARNING: Directory is not a valid git checkout (won't check dirty files): ${rootProject.projectDir}") + } } else { + // git ignores any folders which are empty (this includes folders with recursively empty sub-folders). + def untrackedNonEmptyFolders = status.untrackedFolders.findAll { path -> + File location = file("${rootProject.projectDir}/${path}") + boolean hasFiles = false + Files.walkFileTree(location.toPath(), new SimpleFileVisitor() { + @Override + FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + hasFiles = true + // Terminate early. + return FileVisitResult.TERMINATE + } + }) + return hasFiles + } + def offenders = [ // Exclude staged changes. These are fine in precommit. // "(added)": status.added, @@ -71,7 +97,8 @@ configure(rootProject) { "(conflicting)": status.conflicting, "(missing)": status.missing, "(modified)": status.modified, - "(untracked)": [status.untracked, status.untrackedFolders].flatten() + "(untracked)": status.untracked, + "(untracked non-empty dir)": untrackedNonEmptyFolders ].collectMany { fileStatus, files -> files.collect {file -> " - ${file} ${fileStatus}" } }.sort()