James Moger
2012-08-01 0adceb4b64dfe0dd509da33c6d733a47fbf803a2
Regex exclusions for repository search (issue-103)
6 files modified
82 ■■■■ changed files
distrib/gitblit.properties 10 ●●●●● patch | view | raw | blame | history
docs/04_releases.mkd 5 ●●●● patch | view | raw | blame | history
src/com/gitblit/AddIndexedBranch.java 2 ●●● patch | view | raw | blame | history
src/com/gitblit/GitBlit.java 3 ●●●● patch | view | raw | blame | history
src/com/gitblit/utils/JGitUtils.java 38 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/JGitUtilsTest.java 24 ●●●● patch | view | raw | blame | history
distrib/gitblit.properties
@@ -27,6 +27,16 @@
# SINCE 1.0.1
git.searchRecursionDepth = -1
# List of regex exclusion patterns to match against folders found in
# *git.repositoriesFolder*.
# Use forward slashes even on Windows!!
# e.g. test/jgit\.git
#
# SPACE-DELIMITED
# CASE-SENSITIVE
# SINCE 1.0.1
git.searchExclusions =
# Allow push/pull over http/https with JGit servlet.
# If you do NOT want to allow Git clients to clone/push to Gitblit set this
# to false.  You might want to do this if you are only using ssh:// or git://.
docs/04_releases.mkd
@@ -18,7 +18,10 @@
#### changes
- Added *git.searchRecursionDepth=-1* to control how deep Gitblit will recurse into *git.repositoriesFolder* looking for repositories (issue 103)
- Added setting to control how deep Gitblit will recurse into *git.repositoriesFolder* looking for repositories (issue 103)
    **New:** *git.searchRecursionDepth=-1*
- Added setting to specify regex exclusions for repositories (issue 103)
    **New:** *git.searchExclusions=*
- Blob page now supports displaying images (issue 6)
- Non-image binary files can now be downloaded using the RAW link
- Updated Polish translation
src/com/gitblit/AddIndexedBranch.java
@@ -63,7 +63,7 @@
        
        // determine available repositories
        File folder = new File(params.folder);
        List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1);
        List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
        
        int modCount = 0;
        int skipCount = 0;
src/com/gitblit/GitBlit.java
@@ -743,7 +743,8 @@
        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));
    }
    /**
src/com/gitblit/utils/JGitUtils.java
@@ -29,6 +29,7 @@
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 @@
     *            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, int depth) {
            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, depth));
                onlyBare, searchSubfolders, depth, exclusions));
        StringUtils.sortRepositorynames(list);
        return list;
    }
@@ -305,18 +308,41 @@
     *            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, int depth) {
            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")) {
@@ -328,11 +354,13 @@
                        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));
                }
            }
        }
tests/com/gitblit/tests/JGitUtilsTest.java
@@ -67,15 +67,33 @@
    @Test
    public void testFindRepositories() {
        List<String> list = JGitUtils.getRepositoryList(null, false, true, -1);
        List<String> list = JGitUtils.getRepositoryList(null, false, true, -1, null);
        assertEquals(0, list.size());
        list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1));
        list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1, null));
        assertEquals(0, list.size());
        list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1));
        list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null));
        assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
    }
    @Test
    public void testFindExclusions() {
        List<String> list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null);
        assertTrue("Missing jgit repository?!", list.contains("test/jgit.git"));
        list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/jgit\\.git"));
        assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
        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)+"));
        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"));
    }
    @Test
    public void testOpenRepository() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        repository.close();