Example usage for org.eclipse.jgit.treewalk.filter PathFilter isDone

List of usage examples for org.eclipse.jgit.treewalk.filter PathFilter isDone

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk.filter PathFilter isDone.

Prototype

public boolean isDone(TreeWalk walker) 

Source Link

Document

Whether the path length of this filter matches the length of the current path of the supplied TreeWalk.

Usage

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

private static TreeWalk getTreeOnPath(MutableObjectId id, Repository repo, ObjectId rev, String path)
        throws MissingObjectException, IOException {

    RevTree tree = new RevWalk(repo).parseTree(rev);

    TreeWalk tw = new TreeWalk(repo);
    tw.reset(tree);/* ww  w.ja  v a 2s .c o m*/
    tw.setRecursive(false);

    if (path == null || path.isEmpty() || "/".equals(path)) {
        id.fromObjectId(tree.getId());
        return tw;
    }

    PathFilter f = PathFilter.create(path);
    tw.setFilter(f);

    while (tw.next()) {
        if (f.isDone(tw)) {
            id.fromObjectId(tw.getObjectId(0));
            if (tw.isSubtree()) {
                tw.enterSubtree();
                return tw;
            } else {
                throw new MissingObjectException(tw.getObjectId(0), Constants.OBJ_TREE);
            }

        } else if (tw.isSubtree()) {
            tw.enterSubtree();
        }
    }
    return null;

}

From source file:org.exist.git.xquery.Status.java

License:Open Source License

public void diff(final Repository repository, final String revstr,
        final WorkingTreeIterator initialWorkingTreeIterator, final StatusBuilder builder, final String folder,
        boolean recursive) throws IOException {

    RevTree tree = null;/*from   w  ww  .java  2 s.co m*/

    ObjectId objectId = repository.resolve(revstr);
    if (objectId != null)
        tree = new RevWalk(repository).parseTree(objectId);
    else
        tree = null;

    PathFilter filter = folder == null || folder.isEmpty() ? null : PathFilter.create(folder);
    IndexDiffFilter indexDiffFilter;

    DirCache dirCache = repository.readDirCache();

    TreeWalk treeWalk = new TreeWalk(repository);
    //      TreeWalk treeWalk = TreeWalk.forPath(repository, folder, tree);
    treeWalk.setRecursive(recursive);
    // add the trees (tree, dirchache, workdir)
    if (tree != null)
        treeWalk.addTree(tree);
    else
        treeWalk.addTree(new EmptyTreeIterator());
    treeWalk.addTree(new DirCacheIterator(dirCache));
    treeWalk.addTree(initialWorkingTreeIterator);
    Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);

    if (filter != null)
        filters.add(filter);
    filters.add(new SkipWorkTreeFilter(INDEX));
    indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
    filters.add(indexDiffFilter);
    treeWalk.setFilter(AndTreeFilter.create(filters));
    if (filter != null) {
        while (treeWalk.next()) {
            if (filter.include(treeWalk)) {
                if (filter.isDone(treeWalk)) {
                    if (treeWalk.isSubtree()) {
                        treeWalk.enterSubtree();
                    }
                    break;
                } else if (treeWalk.isSubtree()) {
                    treeWalk.enterSubtree();
                }
            }
        }
    }

    while (treeWalk.next()) {
        AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, AbstractTreeIterator.class);
        DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class);
        WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class);

        if (dirCacheIterator != null) {
            final DirCacheEntry dirCacheEntry = dirCacheIterator.getDirCacheEntry();
            if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
                builder.conflict(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        }

        if (treeIterator != null) {
            if (dirCacheIterator != null) {
                if (!treeIterator.idEqual(dirCacheIterator)
                        || treeIterator.getEntryRawMode() != dirCacheIterator.getEntryRawMode()) {
                    // in repo, in index, content diff => changed
                    builder.changed(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            } else {
                // in repo, not in index => removed
                builder.removed(treeWalk.getFileMode(2), treeWalk.getPathString());
                if (workingTreeIterator != null) //XXX: 2 statuses
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        } else {
            if (dirCacheIterator != null) {
                // not in repo, in index => added
                builder.added(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                // not in repo, not in index => untracked
                if (workingTreeIterator != null && !workingTreeIterator.isEntryIgnored()) {
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }

        if (dirCacheIterator != null) {
            if (workingTreeIterator == null) {
                // in index, not in workdir => missing
                builder.missing(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                if (dirCacheIterator.getDirCacheEntry() == null) {
                    //XXX: null on collections - to fix
                    //                  builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());

                } else if (workingTreeIterator.isModified(dirCacheIterator.getDirCacheEntry(), true)) {
                    // in index, in workdir, content differs => modified
                    builder.modified(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }
        builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());
    }

    //      for (String path : indexDiffFilter.getUntrackedFolders()) {
    //         builder.untrackedFolders(path);
    //      }
    //      
    for (String path : indexDiffFilter.getIgnoredPaths()) {
        //XXX: to fix FileMode
        builder.ignored(FileMode.REGULAR_FILE, path);
    }
}

From source file:org.modeshape.connector.git.GitFunctionalTest.java

License:Apache License

protected void printTreeContent(String tagOrBranchOrCommit, String parentPath, boolean showCommitInfo)
        throws Exception {
    // Find the commit ...
    ObjectId objId = repository.resolve("modeshape-3.0.0.Final");
    RevWalk walker = new RevWalk(repository);
    if (showCommitInfo) {
        walker.setRetainBody(true);//from   w  ww .j  a va2s  .c o  m
    }
    try {
        RevCommit commit = walker.parseCommit(objId);
        if (showCommitInfo)
            print(commit);
        final TreeWalk tw = new TreeWalk(repository);
        tw.addTree(commit.getTree());
        if ("".equals(parentPath) || "/".equals(parentPath)) {
            // We're already at the top-level
            tw.setRecursive(false);
            print("Getting contents of path ...");
            while (tw.next()) {
                print(tw.getPathString());
            }
        } else {
            PathFilter filter = PathFilter.create(parentPath);
            tw.setFilter(filter);
            print("Finding path ...");
            while (tw.next()) {
                print(tw.getPathString());
                if (filter.isDone(tw)) {
                    break;
                } else if (tw.isSubtree()) {
                    tw.enterSubtree();
                }
            }
            if (tw.isSubtree()) {
                print("Getting contents of path ...");
                tw.enterSubtree();
                while (tw.next()) {
                    print(tw.getPathString());
                }
            } else {
                print("File: " + tw.getPathString());
                // Find the commit that last modified this file ...
                // Is this the most efficient way to do this, 'cuz it's expensive?
                RevCommit lastCommit = git.log().addPath(parentPath).call().iterator().next();
                print("commitMessage", lastCommit.getShortMessage());
                PersonIdent authorIdent = lastCommit.getAuthorIdent();
                if (authorIdent != null) {
                    print("commiter", authorIdent.getName());
                }
            }
        }
    } finally {
        walker.dispose();
    }
}

From source file:org.modeshape.connector.git.GitTree.java

License:Apache License

protected void addInformationForPath(Repository repository, Git git, DocumentWriter writer, RevCommit commit,
        String path, CallSpecification spec, Values values) throws GitAPIException, IOException {
    // Make sure the path is in the canonical form we need ...
    if (path.startsWith("/")) {
        if (path.length() == 1)
            path = "";
        else//from   w  w  w .j  a va  2s .  co m
            path = path.substring(1);
    }

    // Now see if we're actually referring to the "jcr:content" node ...
    boolean isContentNode = false;
    if (path.endsWith(JCR_CONTENT_SUFFIX)) {
        isContentNode = true;
        path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length());
    }

    // Create the TreeWalk that we'll use to navigate the files/directories ...
    final TreeWalk tw = new TreeWalk(repository);
    tw.addTree(commit.getTree());
    if ("".equals(path)) {
        // This is the top-level directory, so we don't need to pre-walk to find anything ...
        tw.setRecursive(false);
        while (tw.next()) {
            String childName = tw.getNameString();
            String childId = spec.childId(childName);
            writer.addChild(childId, childName);
        }
    } else {
        // We need to first find our path *before* we can walk the children ...
        PathFilter filter = PathFilter.create(path);
        tw.setFilter(filter);
        while (tw.next()) {
            if (filter.isDone(tw)) {
                break;
            } else if (tw.isSubtree()) {
                tw.enterSubtree();
            }
        }
        // Now that the TreeWalk is the in right location given by the 'path', we can get the
        if (tw.isSubtree()) {
            // The object at the 'path' is a directory, so go into it ...
            tw.enterSubtree();

            // Find the commit in which this folder was last modified ...
            // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
            RevCommit folderCommit = git.log().addPath(path).call().iterator().next();
            writer.setPrimaryType(GitLexicon.FOLDER);

            // could happen if not enough permissions, for example
            if (folderCommit != null) {
                // Add folder-related properties ...
                String committer = commiterName(folderCommit);
                String author = authorName(folderCommit);
                DateTime committed = values.dateFrom(folderCommit.getCommitTime());
                writer.addProperty(JcrLexicon.CREATED, committed);
                writer.addProperty(JcrLexicon.CREATED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());
            } else {
                connector.getLogger().warn(GitI18n.cannotReadCommit, path);
            }

            // And now walk the contents of the directory ...
            while (tw.next()) {
                String childName = tw.getNameString();
                String childId = spec.childId(childName);
                writer.addChild(childId, childName);
            }
        } else {
            // The path specifies a file (or a content node) ...

            // Find the commit in which this folder was last modified ...
            // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
            RevCommit fileCommit = git.log().addPath(path).call().iterator().next();

            if (isContentNode) {
                writer.setPrimaryType(GitLexicon.RESOURCE);
                if (fileCommit == null) {
                    // could happen if not enough permissions, for example
                    connector.getLogger().warn(GitI18n.cannotReadCommit, path);
                    return;
                }
                // Add file-related properties ...
                String committer = commiterName(fileCommit);
                String author = authorName(fileCommit);
                DateTime committed = values.dateFrom(fileCommit.getCommitTime());

                writer.addProperty(JcrLexicon.LAST_MODIFIED, committed);
                writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());
                // Create the BinaryValue ...
                ObjectId fileObjectId = tw.getObjectId(0);
                ObjectLoader fileLoader = repository.open(fileObjectId);
                BinaryKey key = new BinaryKey(fileObjectId.getName());
                BinaryValue value = values.binaryFor(key, fileLoader.getSize());
                if (value == null) {
                    // It wasn't found in the binary store ...
                    if (fileLoader.isLarge()) {
                        // Too large to hold in memory, so use the binary store (which reads the file immediately) ...
                        value = values.binaryFrom(fileLoader.openStream());
                    } else {
                        // This is small enough to fit into a byte[], but it still may be pretty big ...
                        value = new GitBinaryValue(fileObjectId, fileLoader, connector.getSourceName(), name,
                                connector.getMimeTypeDetector());
                    }
                }
                writer.addProperty(JcrLexicon.DATA, value);
                if (connector.includeMimeType()) {
                    try {
                        String filename = spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content'
                        String mimeType = value.getMimeType(filename);
                        if (mimeType != null)
                            writer.addProperty(JcrLexicon.MIMETYPE, mimeType);
                    } catch (RepositoryException e) {
                        // do nothing
                    } catch (IOException e) {
                        // do nothing
                    }
                }
            } else {
                writer.setPrimaryType(GitLexicon.FILE);
                if (fileCommit == null) {
                    // could happen if not enough permissions, for example
                    connector.getLogger().warn(GitI18n.cannotReadCommit, path);
                    return;
                }
                // Add file-related properties ...
                String committer = commiterName(fileCommit);
                String author = authorName(fileCommit);
                DateTime committed = values.dateFrom(fileCommit.getCommitTime());

                writer.addProperty(JcrLexicon.CREATED, committed);
                writer.addProperty(JcrLexicon.CREATED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());

                // Add the "jcr:content" child node ...
                String childId = spec.childId(JCR_CONTENT);
                writer.addChild(childId, JCR_CONTENT);
            }
        }
    }
}

From source file:playRepository.GitRepository.java

License:Apache License

@Override
public ObjectNode getMetaDataFromPath(String branch, String path) throws IOException, GitAPIException {
    RevCommit headCommit = getRevCommit(branch);
    if (headCommit == null) {
        Logger.debug("GitRepository : init Project - No Head commit");
        return null;
    }/* w  ww . j a v a  2 s.  co  m*/

    RevWalk revWalk = new RevWalk(repository);
    RevTree revTree = revWalk.parseTree(headCommit);
    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(revTree);

    if (path.isEmpty()) {
        return treeAsJson(path, treeWalk, headCommit);
    }

    PathFilter pathFilter = PathFilter.create(path);
    treeWalk.setFilter(pathFilter);
    while (treeWalk.next()) {
        if (pathFilter.isDone(treeWalk)) {
            break;
        } else if (treeWalk.isSubtree()) {
            treeWalk.enterSubtree();
        }
    }

    if (treeWalk.isSubtree()) {
        treeWalk.enterSubtree();
        return treeAsJson(path, treeWalk, headCommit);
    } else {
        try {
            return fileAsJson(treeWalk, headCommit);
        } catch (MissingObjectException e) {
            Logger.debug("Unavailable access. " + branch + "/" + path + " does not exist.");
            return null;
        }
    }
}