Search in sources :

Example 1 with PathModel

use of com.gitblit.models.PathModel in project gitblit by gitblit.

the class JGitUtilsTest method testFilesInPath.

@Test
public void testFilesInPath() throws Exception {
    assertEquals(0, JGitUtils.getFilesInPath(null, null, null).size());
    Repository repository = GitBlitSuite.getHelloworldRepository();
    List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
    repository.close();
    assertTrue(files.size() > 10);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PathModel(com.gitblit.models.PathModel) Test(org.junit.Test)

Example 2 with PathModel

use of com.gitblit.models.PathModel in project gitblit by gitblit.

the class JGitUtilsTest method testDocuments.

@Test
public void testDocuments() throws Exception {
    Repository repository = GitBlitSuite.getTicgitRepository();
    List<String> extensions = Arrays.asList(new String[] { ".mkd", ".md" });
    List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
    List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
    repository.close();
    assertTrue(markdownDocs.size() > 0);
    assertTrue(allFiles.size() > markdownDocs.size());
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PathModel(com.gitblit.models.PathModel) Test(org.junit.Test)

Example 3 with PathModel

use of com.gitblit.models.PathModel in project gitblit by gitblit.

the class JGitUtilsTest method testFilesInPath2.

@Test
public void testFilesInPath2() throws Exception {
    assertEquals(0, JGitUtils.getFilesInPath2(null, null, null).size());
    Repository repository = GitBlitSuite.getHelloworldRepository();
    List<PathModel> files = JGitUtils.getFilesInPath2(repository, null, null);
    assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.top, 15), files.size());
    files = JGitUtils.getFilesInPath2(repository, "C", null);
    assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.C.top, 1), files.size());
    files = JGitUtils.getFilesInPath2(repository, "[C++]", null);
    assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.Cpp, 1), files.size());
    files = JGitUtils.getFilesInPath2(repository, "C/C (K&R)", null);
    assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.C.KnR, 1), files.size());
    repository.close();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PathModel(com.gitblit.models.PathModel) Test(org.junit.Test)

Example 4 with PathModel

use of com.gitblit.models.PathModel in project gitblit by gitblit.

the class JGitUtils method getPathModel.

/**
 * Returns a path model by path string
 *
 * @param repo
 * @param path
 * @param filter
 * @param commit
 * @return a path model of the specified object
 */
private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException {
    long size = 0;
    FilestoreModel filestoreItem = null;
    TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree());
    String pathString = path;
    if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
        pathString = PathUtils.getLastPathComponent(pathString);
        size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
        if (isPossibleFilestoreItem(size)) {
            filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0)));
        }
    } else if (tw.isSubtree()) {
        // do not display dirs that are behind in the path
        if (!Strings.isNullOrEmpty(filter)) {
            pathString = path.replaceFirst(filter + "/", "");
        }
        // remove the last slash from path in displayed link
        if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') {
            pathString = pathString.substring(0, pathString.length() - 1);
        }
    }
    return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName());
}
Also used : PathModel(com.gitblit.models.PathModel) FilestoreModel(com.gitblit.models.FilestoreModel) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 5 with PathModel

use of com.gitblit.models.PathModel in project gitblit by gitblit.

the class JGitUtils method getDocuments.

/**
 * Returns the list of files in the repository in the specified commit that
 * match one of the specified extensions. This is a CASE-SENSITIVE search.
 * If the repository does not exist or is empty, an empty list is returned.
 *
 * @param repository
 * @param extensions
 * @param objectId
 * @return list of files in repository with a matching extension
 */
public static List<PathModel> getDocuments(Repository repository, List<String> extensions, String objectId) {
    List<PathModel> list = new ArrayList<PathModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    RevCommit commit = getCommit(repository, objectId);
    final TreeWalk tw = new TreeWalk(repository);
    try {
        tw.addTree(commit.getTree());
        if (extensions != null && extensions.size() > 0) {
            List<TreeFilter> suffixFilters = new ArrayList<TreeFilter>();
            for (String extension : extensions) {
                if (extension.charAt(0) == '.') {
                    suffixFilters.add(PathSuffixFilter.create(extension));
                } else {
                    // escape the . since this is a regexp filter
                    suffixFilters.add(PathSuffixFilter.create("." + extension));
                }
            }
            TreeFilter filter;
            if (suffixFilters.size() == 1) {
                filter = suffixFilters.get(0);
            } else {
                filter = OrTreeFilter.create(suffixFilters);
            }
            tw.setFilter(filter);
            tw.setRecursive(true);
        }
        while (tw.next()) {
            list.add(getPathModel(tw, null, commit));
        }
    } catch (IOException e) {
        error(e, repository, "{0} failed to get documents for commit {1}", commit.getName());
    } finally {
        tw.close();
    }
    Collections.sort(list);
    return list;
}
Also used : PathModel(com.gitblit.models.PathModel) ArrayList(java.util.ArrayList) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) IOException(java.io.IOException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

PathModel (com.gitblit.models.PathModel)13 ArrayList (java.util.ArrayList)6 Repository (org.eclipse.jgit.lib.Repository)6 IOException (java.io.IOException)5 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Test (org.junit.Test)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 HashMap (java.util.HashMap)2 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)2 RefModel (com.gitblit.models.RefModel)1 TicketModel (com.gitblit.models.TicketModel)1 Change (com.gitblit.models.TicketModel.Change)1 ByteFormat (com.gitblit.utils.ByteFormat)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (java.text.ParseException)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1