Fix detection of git repositories to not look for trash inside them

The intent was to not look for trash inside git repositories but it did
not work. It was looking for a file called HEAD inside a folder name
ending with .git but the check was done in the visitFile method of the
SimpleVisitor which is too late. The git repository is already partially
visited by the time the HEAD file is processed.

To skip the git repositories, the check must be done before starting to
visit the folder. Move the check in the preVisitDirectory method.

Replace the logic of looking a file named HEAD inside a folder name
ending with .git by the jgit FileKey.isGitRepository method.

Change-Id: Ib673d8e9e787c09ef2c7a901d8c2b2c795559ede
diff --git a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java
index 54c3677..7e6b4aa 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/fs/DeleteTrashFolders.java
@@ -19,6 +19,8 @@
 import com.google.inject.Inject;
 
 import org.eclipse.jgit.lib.Config;
+import org.eclipse.jgit.lib.RepositoryCache.FileKey;
+import org.eclipse.jgit.util.FS;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -53,22 +55,12 @@
         log.warn("Will delete this folder: {}", dir);
         recursiveDelete(dir);
         return FileVisitResult.SKIP_SUBTREE;
+      } else if (FileKey.isGitRepository(dir.toFile(), FS.DETECTED)) {
+        // We are in a GITDIR and don't expect trash folders inside GITDIR's.
+        return FileVisitResult.SKIP_SUBTREE;
       }
-      return super.preVisitDirectory(dir, attrs);
-    }
 
-    @Override
-    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
-        throws IOException {
-      // Whenever we detect a regular file named 'HEAD' contained in a folder
-      // ending with ".git" then don't try to search for trash folders in this
-      // subtree. We are in a GITDIR and don't expect trash folders inside
-      // GITDIR's.
-      if (attrs.isRegularFile() && file.getFileName().toString().equals("HEAD")
-          && file.getParent().toString().endsWith(".git")) {
-        return FileVisitResult.SKIP_SIBLINGS;
-      }
-      return super.visitFile(file, attrs);
+      return super.preVisitDirectory(dir, attrs);
     }
 
     /**