]> source.dussan.org Git - gitblit.git/commitdiff
Compile regex patterns once and use matches instead of find (issue 103)
authorJames Moger <james.moger@gitblit.com>
Thu, 2 Aug 2012 19:53:51 +0000 (15:53 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 2 Aug 2012 19:53:51 +0000 (15:53 -0400)
src/com/gitblit/utils/JGitUtils.java
tests/com/gitblit/tests/JGitUtilsTest.java

index 9d2e471af345a0d7c297ff7b30c9222c6e71fabc..4415982a524f1acf4fd5dc0dc015ce79d9443f8d 100644 (file)
@@ -288,8 +288,14 @@ public class JGitUtils {
                if (repositoriesFolder == null || !repositoriesFolder.exists()) {\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
                list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder,\r
-                               onlyBare, searchSubfolders, depth, exclusions));\r
+                               onlyBare, searchSubfolders, depth, patterns));\r
                StringUtils.sortRepositorynames(list);\r
                return list;\r
        }\r
@@ -308,23 +314,17 @@ 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
+        * @param patterns\r
+        *            list of regex patterns 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, List<String> exclusions) {\r
+                       boolean onlyBare, boolean searchSubfolders, int depth, List<Pattern> patterns) {\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
@@ -332,7 +332,7 @@ public class JGitUtils {
                                boolean exclude = false;\r
                                for (Pattern pattern : patterns) {\r
                                        String path = FileUtils.getRelativePath(baseFile, file).replace('\\',  '/');\r
-                                       if (pattern.matcher(path).find()) {\r
+                                       if (pattern.matcher(path).matches()) {\r
                                                LOGGER.debug(MessageFormat.format("excluding {0} because of rule {1}", path, pattern.pattern()));\r
                                                exclude = true;\r
                                                break;\r
@@ -355,12 +355,12 @@ public class JGitUtils {
                                        } else if (searchSubfolders && file.canRead()) {\r
                                                // look for repositories in subfolders\r
                                                list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders,\r
-                                                               nextDepth, exclusions));\r
+                                                               nextDepth, patterns));\r
                                        }\r
                                } else if (searchSubfolders && file.canRead()) {\r
                                        // look for repositories in subfolders\r
                                        list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders,\r
-                                                       nextDepth, exclusions));\r
+                                                       nextDepth, patterns));\r
                                }\r
                        }\r
                }\r
index 495a00b7fbd6d301afda345702e5e2e7386c749d..ee60d05c253c2a0cc32add405f8b4f32afd4ecab 100644 (file)
@@ -86,7 +86,7 @@ public class JGitUtilsTest {
                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
+               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