From 0adceb4b64dfe0dd509da33c6d733a47fbf803a2 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Wed, 01 Aug 2012 21:21:32 -0400 Subject: [PATCH] Regex exclusions for repository search (issue 103) --- src/com/gitblit/utils/JGitUtils.java | 64 +++++++++++++++++++++++++++---- 1 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 72a8ab3..9d2e471 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -29,12 +29,14 @@ 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; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.FetchCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry.ChangeType; import org.eclipse.jgit.diff.DiffFormatter; @@ -257,8 +259,12 @@ * @return Repository */ public static Repository createRepository(File repositoriesFolder, String name) { - Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call(); - return git.getRepository(); + try { + Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call(); + return git.getRepository(); + } catch (GitAPIException e) { + throw new RuntimeException(e); + } } /** @@ -270,16 +276,20 @@ * false all repositories are included. * @param searchSubfolders * 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<String> getRepositoryList(File repositoriesFolder, boolean onlyBare, - boolean searchSubfolders) { + boolean searchSubfolders, int depth, List<String> exclusions) { List<String> list = new ArrayList<String>(); if (repositoriesFolder == null || !repositoriesFolder.exists()) { return list; } list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder, - onlyBare, searchSubfolders)); + onlyBare, searchSubfolders, depth, exclusions)); StringUtils.sortRepositorynames(list); return list; } @@ -296,25 +306,61 @@ * repositories are included. * @param searchSubfolders * 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<String> getRepositoryList(String basePath, File searchFolder, - boolean onlyBare, boolean searchSubfolders) { + boolean onlyBare, boolean searchSubfolders, int depth, List<String> exclusions) { File baseFile = new File(basePath); List<String> list = new ArrayList<String>(); + if (depth == 0) { + return list; + } + List<Pattern> patterns = new ArrayList<Pattern>(); + 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")) { continue; } - // determine repository name relative to base path - String repository = FileUtils.getRelativePath(baseFile, file); - list.add(repository); + if (gitDir.equals(file) || gitDir.getParentFile().equals(file)) { + // determine repository name relative to base path + String repository = FileUtils.getRelativePath(baseFile, file); + list.add(repository); + } else if (searchSubfolders && file.canRead()) { + // look for repositories in subfolders + 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)); + list.addAll(getRepositoryList(basePath, file, onlyBare, searchSubfolders, + nextDepth, exclusions)); } } } -- Gitblit v1.9.1