From bb55f5aec092b22ee4b86152c0e0111df48eb34e Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 2 Aug 2012 15:53:51 -0400 Subject: [PATCH] Compile regex patterns once and use matches instead of find (issue 103) --- src/com/gitblit/utils/JGitUtils.java | 26 +++++++++++----------- tests/com/gitblit/tests/JGitUtilsTest.java | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 9d2e471a..4415982a 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -288,8 +288,14 @@ public class JGitUtils { if (repositoriesFolder == null || !repositoriesFolder.exists()) { return list; } + List patterns = new ArrayList(); + if (!ArrayUtils.isEmpty(exclusions)) { + for (String regex : exclusions) { + patterns.add(Pattern.compile(regex)); + } + } list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder, - onlyBare, searchSubfolders, depth, exclusions)); + onlyBare, searchSubfolders, depth, patterns)); StringUtils.sortRepositorynames(list); return list; } @@ -308,23 +314,17 @@ public class JGitUtils { * recurse into subfolders to find grouped repositories * @param depth * recursion depth, -1 = infinite recursion - * @param exclusions - * list of regex exclusions for matching to folder names + * @param patterns + * list of regex patterns for matching to folder names * @return */ private static List getRepositoryList(String basePath, File searchFolder, - boolean onlyBare, boolean searchSubfolders, int depth, List exclusions) { + boolean onlyBare, boolean searchSubfolders, int depth, List patterns) { File baseFile = new File(basePath); List list = new ArrayList(); if (depth == 0) { return list; } - List patterns = new ArrayList(); - if (!ArrayUtils.isEmpty(exclusions)) { - for (String regex : exclusions) { - patterns.add(Pattern.compile(regex)); - } - } int nextDepth = (depth == -1) ? -1 : depth - 1; for (File file : searchFolder.listFiles()) { @@ -332,7 +332,7 @@ public class JGitUtils { boolean exclude = false; for (Pattern pattern : patterns) { String path = FileUtils.getRelativePath(baseFile, file).replace('\\', '/'); - if (pattern.matcher(path).find()) { + if (pattern.matcher(path).matches()) { LOGGER.debug(MessageFormat.format("excluding {0} because of rule {1}", path, pattern.pattern())); exclude = true; break; @@ -355,12 +355,12 @@ public class JGitUtils { } else if (searchSubfolders && file.canRead()) { // look for repositories in subfolders list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, - nextDepth, exclusions)); + nextDepth, patterns)); } } else if (searchSubfolders && file.canRead()) { // look for repositories in subfolders list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, - nextDepth, exclusions)); + nextDepth, patterns)); } } } diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java index 495a00b7..ee60d05c 100644 --- a/tests/com/gitblit/tests/JGitUtilsTest.java +++ b/tests/com/gitblit/tests/JGitUtilsTest.java @@ -86,7 +86,7 @@ public class JGitUtilsTest { list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/*")); assertFalse("Repository exclusion failed!", list.contains("test/jgit.git")); - list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("(jgit)+")); + list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList(".*jgit.*")); assertFalse("Repository exclusion failed!", list.contains("test/jgit.git")); assertFalse("Repository exclusion failed!", list.contains("working/jgit")); assertFalse("Repository exclusion failed!", list.contains("working/jgit2")); -- 2.39.5