]> source.dussan.org Git - gitblit.git/commitdiff
Regex exclusions for repository search (issue 103)
authorJames Moger <james.moger@gitblit.com>
Thu, 2 Aug 2012 01:21:32 +0000 (21:21 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 2 Aug 2012 01:21:32 +0000 (21:21 -0400)
distrib/gitblit.properties
docs/04_releases.mkd
src/com/gitblit/AddIndexedBranch.java
src/com/gitblit/GitBlit.java
src/com/gitblit/utils/JGitUtils.java
tests/com/gitblit/tests/JGitUtilsTest.java

index f06aaef42a15364fc587a3b8b676410e925dab9f..0923c41be42cc4bd7b888d9b42f3a3c1f4109bf8 100644 (file)
@@ -27,6 +27,16 @@ git.searchRepositoriesSubfolders = true
 # SINCE 1.0.1\r
 git.searchRecursionDepth = -1\r
 \r
+# List of regex exclusion patterns to match against folders found in\r
+# *git.repositoriesFolder*.\r
+# Use forward slashes even on Windows!!\r
+# e.g. test/jgit\.git\r
+#\r
+# SPACE-DELIMITED\r
+# CASE-SENSITIVE\r
+# SINCE 1.0.1\r
+git.searchExclusions =\r
+\r
 # Allow push/pull over http/https with JGit servlet.\r
 # If you do NOT want to allow Git clients to clone/push to Gitblit set this\r
 # to false.  You might want to do this if you are only using ssh:// or git://.\r
index 8a084355efd97d4528b09ecaf271247e34ab1e02..e1972109dd166887e63cb26bfa7a4ce26dae5e6d 100644 (file)
@@ -18,7 +18,10 @@ If you are updating from an 0.9.x release AND you have indexed branches with the
 \r
 #### changes\r
 \r
-- Added *git.searchRecursionDepth=-1* to control how deep Gitblit will recurse into *git.repositoriesFolder* looking for repositories (issue 103)\r
+- Added setting to control how deep Gitblit will recurse into *git.repositoriesFolder* looking for repositories (issue 103)\r
+    **New:** *git.searchRecursionDepth=-1*  \r
+- Added setting to specify regex exclusions for repositories (issue 103)\r
+    **New:** *git.searchExclusions=*  \r
 - Blob page now supports displaying images (issue 6)\r
 - Non-image binary files can now be downloaded using the RAW link\r
 - Updated Polish translation\r
index 8ead17e0f36061282e4081525d810fe67435b56b..66997060ab4ee3f0ea9948e9d8b83772f2b25794 100644 (file)
@@ -63,7 +63,7 @@ public class AddIndexedBranch {
                \r
                // determine available repositories\r
                File folder = new File(params.folder);\r
-               List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1);\r
+               List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);\r
                \r
                int modCount = 0;\r
                int skipCount = 0;\r
index d4a59d5a5333c7a57ad75571578413774da352bb..8f51069e656edf771f1ddb28c1097cc1413ac6f4 100644 (file)
@@ -743,7 +743,8 @@ public class GitBlit implements ServletContextListener {
                return JGitUtils.getRepositoryList(repositoriesFolder, \r
                                settings.getBoolean(Keys.git.onlyAccessBareRepositories, false),\r
                                settings.getBoolean(Keys.git.searchRepositoriesSubfolders, true),\r
-                               settings.getInteger(Keys.git.searchRecursionDepth, -1));\r
+                               settings.getInteger(Keys.git.searchRecursionDepth, -1),\r
+                               settings.getStrings(Keys.git.searchExclusions));\r
        }\r
 \r
        /**\r
index ff701b3941eec6607e8e64f19ef63a4117b6f34e..9d2e471af345a0d7c297ff7b30c9222c6e71fabc 100644 (file)
@@ -29,6 +29,7 @@ import java.util.HashMap;
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
+import java.util.regex.Pattern;\r
 import java.util.zip.ZipEntry;\r
 import java.util.zip.ZipOutputStream;\r
 \r
@@ -277,16 +278,18 @@ public class JGitUtils {
         *            recurse into subfolders to find grouped repositories\r
         * @param depth\r
         *            optional recursion depth, -1 = infinite recursion\r
+        * @param exclusions\r
+        *            list of regex exclusions for matching to folder names\r
         * @return list of repository names\r
         */\r
        public static List<String> getRepositoryList(File repositoriesFolder, boolean onlyBare,\r
-                       boolean searchSubfolders, int depth) {\r
+                       boolean searchSubfolders, int depth, List<String> exclusions) {\r
                List<String> list = new ArrayList<String>();\r
                if (repositoriesFolder == null || !repositoriesFolder.exists()) {\r
                        return list;\r
                }\r
                list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder,\r
-                               onlyBare, searchSubfolders, depth));\r
+                               onlyBare, searchSubfolders, depth, exclusions));\r
                StringUtils.sortRepositorynames(list);\r
                return list;\r
        }\r
@@ -305,18 +308,41 @@ public class JGitUtils {
         *            recurse into subfolders to find grouped repositories\r
         * @param depth\r
         *            recursion depth, -1 = infinite recursion\r
+        * @param exclusions\r
+        *            list of regex exclusions for matching to folder names\r
         * @return\r
         */\r
        private static List<String> getRepositoryList(String basePath, File searchFolder,\r
-                       boolean onlyBare, boolean searchSubfolders, int depth) {\r
+                       boolean onlyBare, boolean searchSubfolders, int depth, List<String> exclusions) {\r
                File baseFile = new File(basePath);\r
                List<String> list = new ArrayList<String>();\r
                if (depth == 0) {\r
                        return list;\r
                }\r
+               List<Pattern> patterns = new ArrayList<Pattern>();\r
+               if (!ArrayUtils.isEmpty(exclusions)) {\r
+                       for (String regex : exclusions) {\r
+                               patterns.add(Pattern.compile(regex));\r
+                       }\r
+               }\r
+               \r
                int nextDepth = (depth == -1) ? -1 : depth - 1;\r
                for (File file : searchFolder.listFiles()) {\r
                        if (file.isDirectory()) {\r
+                               boolean exclude = false;\r
+                               for (Pattern pattern : patterns) {\r
+                                       String path = FileUtils.getRelativePath(baseFile, file).replace('\\',  '/');\r
+                                       if (pattern.matcher(path).find()) {\r
+                                               LOGGER.debug(MessageFormat.format("excluding {0} because of rule {1}", path, pattern.pattern()));\r
+                                               exclude = true;\r
+                                               break;\r
+                                       }\r
+                               }\r
+                               if (exclude) {\r
+                                       // skip to next file\r
+                                       continue;\r
+                               }\r
+\r
                                File gitDir = FileKey.resolve(new File(searchFolder, file.getName()), FS.DETECTED);\r
                                if (gitDir != null) {\r
                                        if (onlyBare && gitDir.getName().equals(".git")) {\r
@@ -328,11 +354,13 @@ public class JGitUtils {
                                                list.add(repository);\r
                                        } else if (searchSubfolders && file.canRead()) {\r
                                                // look for repositories in subfolders\r
-                                               list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, nextDepth));\r
+                                               list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders,\r
+                                                               nextDepth, exclusions));\r
                                        }\r
                                } else if (searchSubfolders && file.canRead()) {\r
                                        // look for repositories in subfolders\r
-                                       list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, nextDepth));\r
+                                       list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders,\r
+                                                       nextDepth, exclusions));\r
                                }\r
                        }\r
                }\r
index addc9347661d6049bc4368cb8469e5948eec6be1..495a00b7fbd6d301afda345702e5e2e7386c749d 100644 (file)
@@ -67,14 +67,32 @@ public class JGitUtilsTest {
 \r
        @Test\r
        public void testFindRepositories() {\r
-               List<String> list = JGitUtils.getRepositoryList(null, false, true, -1);\r
+               List<String> list = JGitUtils.getRepositoryList(null, false, true, -1, null);\r
                assertEquals(0, list.size());\r
-               list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1));\r
+               list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1, null));\r
                assertEquals(0, list.size());\r
-               list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1));\r
+               list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null));\r
                assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);\r
        }\r
 \r
+       @Test\r
+       public void testFindExclusions() {\r
+               List<String> list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null);\r
+               assertTrue("Missing jgit repository?!", list.contains("test/jgit.git"));\r
+\r
+               list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/jgit\\.git"));\r
+               assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));\r
+\r
+               list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/*"));\r
+               assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));\r
+\r
+               list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("(jgit)+"));\r
+               assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));\r
+               assertFalse("Repository exclusion failed!", list.contains("working/jgit"));\r
+               assertFalse("Repository exclusion failed!", list.contains("working/jgit2"));\r
+\r
+       }\r
+\r
        @Test\r
        public void testOpenRepository() throws Exception {\r
                Repository repository = GitBlitSuite.getHelloworldRepository();\r