Search in sources :

Example 11 with PathModel

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

the class JGitUtils method getFilesInPath.

/**
 * Returns the list of files in the specified folder at the specified
 * commit. If the repository does not exist or is empty, an empty list is
 * returned.
 *
 * @param repository
 * @param path
 *            if unspecified, root folder is assumed.
 * @param commit
 *            if null, HEAD is assumed.
 * @return list of files in specified path
 */
public static List<PathModel> getFilesInPath(Repository repository, String path, RevCommit commit) {
    List<PathModel> list = new ArrayList<PathModel>();
    if (!hasCommits(repository)) {
        return list;
    }
    if (commit == null) {
        commit = getCommit(repository, null);
    }
    final TreeWalk tw = new TreeWalk(repository);
    try {
        tw.addTree(commit.getTree());
        if (!StringUtils.isEmpty(path)) {
            PathFilter f = PathFilter.create(path);
            tw.setFilter(f);
            tw.setRecursive(false);
            boolean foundFolder = false;
            while (tw.next()) {
                if (!foundFolder && tw.isSubtree()) {
                    tw.enterSubtree();
                }
                if (tw.getPathString().equals(path)) {
                    foundFolder = true;
                    continue;
                }
                if (foundFolder) {
                    list.add(getPathModel(tw, path, commit));
                }
            }
        } else {
            tw.setRecursive(false);
            while (tw.next()) {
                list.add(getPathModel(tw, null, commit));
            }
        }
    } catch (IOException e) {
        error(e, repository, "{0} failed to get files for commit {1}", commit.getName());
    } finally {
        tw.close();
    }
    Collections.sort(list);
    return list;
}
Also used : PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) PathModel(com.gitblit.models.PathModel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 12 with PathModel

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

the class MarkupProcessor method getDocs.

private List<MarkupDocument> getDocs(Repository r, String repositoryName, String commitId, List<String> names) {
    List<String> extensions = getAllExtensions();
    String[] encodings = getEncodings();
    Map<String, MarkupDocument> map = new HashMap<String, MarkupDocument>();
    RevCommit commit = JGitUtils.getCommit(r, commitId);
    List<PathModel> paths = JGitUtils.getFilesInPath(r, null, commit);
    for (PathModel path : paths) {
        if (!path.isTree()) {
            String ext = StringUtils.getFileExtension(path.name).toLowerCase();
            String name = StringUtils.stripFileExtension(path.name).toLowerCase();
            if (names.contains(name)) {
                if (StringUtils.isEmpty(ext) || extensions.contains(ext)) {
                    String markup = JGitUtils.getStringContent(r, commit.getTree(), path.name, encodings);
                    MarkupDocument doc = parse(repositoryName, commitId, path.name, markup);
                    map.put(name, doc);
                }
            }
        }
    }
    // return document list in requested order
    List<MarkupDocument> list = new ArrayList<MarkupDocument>();
    for (String name : names) {
        if (map.containsKey(name)) {
            list.add(map.get(name));
        }
    }
    return list;
}
Also used : PathModel(com.gitblit.models.PathModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 13 with PathModel

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

the class MarkupProcessor method hasRootDocs.

public boolean hasRootDocs(Repository r) {
    List<String> roots = getRoots();
    List<String> extensions = getAllExtensions();
    List<PathModel> paths = JGitUtils.getFilesInPath(r, null, null);
    for (PathModel path : paths) {
        if (!path.isTree()) {
            String ext = StringUtils.getFileExtension(path.name).toLowerCase();
            String name = StringUtils.stripFileExtension(path.name).toLowerCase();
            if (roots.contains(name)) {
                if (StringUtils.isEmpty(ext) || extensions.contains(ext)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : PathModel(com.gitblit.models.PathModel)

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