From 0adceb4b64dfe0dd509da33c6d733a47fbf803a2 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 1 Aug 2012 21:21:32 -0400 Subject: Regex exclusions for repository search (issue 103) --- src/com/gitblit/AddIndexedBranch.java | 2 +- src/com/gitblit/GitBlit.java | 3 ++- src/com/gitblit/utils/JGitUtils.java | 38 ++++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/com/gitblit/AddIndexedBranch.java b/src/com/gitblit/AddIndexedBranch.java index 8ead17e0..66997060 100644 --- a/src/com/gitblit/AddIndexedBranch.java +++ b/src/com/gitblit/AddIndexedBranch.java @@ -63,7 +63,7 @@ public class AddIndexedBranch { // determine available repositories File folder = new File(params.folder); - List repoList = JGitUtils.getRepositoryList(folder, false, true, -1); + List repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null); int modCount = 0; int skipCount = 0; diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index d4a59d5a..8f51069e 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -743,7 +743,8 @@ public class GitBlit implements ServletContextListener { return JGitUtils.getRepositoryList(repositoriesFolder, settings.getBoolean(Keys.git.onlyAccessBareRepositories, false), settings.getBoolean(Keys.git.searchRepositoriesSubfolders, true), - settings.getInteger(Keys.git.searchRecursionDepth, -1)); + settings.getInteger(Keys.git.searchRecursionDepth, -1), + settings.getStrings(Keys.git.searchExclusions)); } /** diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index ff701b39..9d2e471a 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -277,16 +278,18 @@ public class JGitUtils { * recurse into subfolders to find grouped repositories * @param depth * optional recursion depth, -1 = infinite recursion + * @param exclusions + * list of regex exclusions for matching to folder names * @return list of repository names */ public static List getRepositoryList(File repositoriesFolder, boolean onlyBare, - boolean searchSubfolders, int depth) { + boolean searchSubfolders, int depth, List exclusions) { List list = new ArrayList(); if (repositoriesFolder == null || !repositoriesFolder.exists()) { return list; } list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder, - onlyBare, searchSubfolders, depth)); + onlyBare, searchSubfolders, depth, exclusions)); StringUtils.sortRepositorynames(list); return list; } @@ -305,18 +308,41 @@ 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 * @return */ private static List getRepositoryList(String basePath, File searchFolder, - boolean onlyBare, boolean searchSubfolders, int depth) { + boolean onlyBare, boolean searchSubfolders, int depth, List exclusions) { 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()) { if (file.isDirectory()) { + boolean exclude = false; + for (Pattern pattern : patterns) { + String path = FileUtils.getRelativePath(baseFile, file).replace('\\', '/'); + if (pattern.matcher(path).find()) { + LOGGER.debug(MessageFormat.format("excluding {0} because of rule {1}", path, pattern.pattern())); + exclude = true; + break; + } + } + if (exclude) { + // skip to next file + continue; + } + File gitDir = FileKey.resolve(new File(searchFolder, file.getName()), FS.DETECTED); if (gitDir != null) { if (onlyBare && gitDir.getName().equals(".git")) { @@ -328,11 +354,13 @@ public class JGitUtils { list.add(repository); } else if (searchSubfolders && file.canRead()) { // look for repositories in subfolders - list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, nextDepth)); + list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, + nextDepth, exclusions)); } } else if (searchSubfolders && file.canRead()) { // look for repositories in subfolders - list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, nextDepth)); + list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, + nextDepth, exclusions)); } } } -- cgit v1.2.3