Example usage for org.eclipse.jgit.treewalk TreeWalk setPostOrderTraversal

List of usage examples for org.eclipse.jgit.treewalk TreeWalk setPostOrderTraversal

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk TreeWalk setPostOrderTraversal.

Prototype

public void setPostOrderTraversal(boolean b) 

Source Link

Document

Set the walker to return trees after their children.

Usage

From source file:org.gitective.core.TreeUtils.java

License:Open Source License

/**
 * Visit entries in the given tree//from   w  w  w . j a v  a  2  s  .  c o m
 *
 * @param repository
 * @param treeId
 * @param visitor
 * @return true if fully completed, false if terminated early
 */
public static boolean visit(final Repository repository, final ObjectId treeId, final ITreeVisitor visitor) {
    if (repository == null)
        throw new IllegalArgumentException(Assert.formatNotNull("Repository"));
    if (treeId == null)
        throw new IllegalArgumentException(Assert.formatNotNull("Tree Id"));
    if (visitor == null)
        throw new IllegalArgumentException(Assert.formatNotNull("Visitor"));

    final TreeWalk walk = new TreeWalk(repository);
    walk.setPostOrderTraversal(true);
    final MutableObjectId id = new MutableObjectId();
    try {
        walk.addTree(treeId);
        if (!visit(repository, walk, id, null, visitor))
            return false;
    } catch (IOException e) {
        throw new GitException(e, repository);
    } finally {
        walk.release();
    }
    return true;
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public Map getWorkingTree(@PName("name") String repoName, @PName("path") @POptional String path,
        @PName("depth") @POptional @PDefaultInt(100) Integer depth,
        @PName("includeTypeList") @POptional List<String> includeTypeList,
        @PName("includePathList") @POptional List<String> includePathList,
        @PName("excludePathList") @POptional List<String> excludePathList,
        @PName("mapping") @POptional Map mapping) throws RpcException {
    try {/*from   w w w . j  ava  2  s . c om*/
        if (depth == null)
            depth = 100;
        String gitSpace = System.getProperty("git.repos");
        File gitDir = new File(gitSpace, repoName + "/.git");
        if (!gitDir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100,
                    "GitService.getWorkingTree:Repo(" + repoName + ") not exists");
        }
        File repoDir = new File(gitSpace, repoName);
        Git gitObject = Git.open(new File(gitSpace, repoName));
        TreeWalk treeWalk = new TreeWalk(gitObject.getRepository());
        FileTreeIterator newTree = null;
        String rootPath = "root";
        String type = "sw.project";
        File basePath = repoDir;
        String title = repoName;
        if (path == null) {
            newTree = new FileTreeIterator(gitObject.getRepository());
        } else {
            File f = new File(gitObject.getRepository().getDirectory().getParentFile(), path);
            debug("f:" + f);
            newTree = new FileTreeIterator(f, FS.detect(),
                    gitObject.getRepository().getConfig().get(WorkingTreeOptions.KEY));
            rootPath = path;
            type = "sw.directory";
            title += "/" + path;
            basePath = new File(basePath, path);
        }
        treeWalk.addTree(newTree);
        treeWalk.setRecursive(true);
        Collection<TreeFilter> filterList = new HashSet();
        TreeFilter pathFilter = PathPatternFilter.create("^[a-zA-Z].*$", includePathList, excludePathList, 0);
        filterList.add(pathFilter);
        if (includeTypeList != null && includeTypeList.size() > 0) {
            filterList.add(TypeFilter.create(basePath, includeTypeList));
        }
        if (filterList.size() > 1) {
            TreeFilter andFilter = AndTreeFilter.create(filterList);
            treeWalk.setFilter(andFilter);
        } else {
            treeWalk.setFilter(pathFilter);
        }
        treeWalk.setPostOrderTraversal(true);
        Map<String, Map> parentMap = new HashMap();
        Map root = new HashMap();
        root.put("path", rootPath);
        root.put("title", repoName);
        root.put("value", rootPath);
        root.put("type", type);
        root.put("children", new ArrayList());
        parentMap.put("root", root);
        while (true) {
            if (!treeWalk.next()) {
                break;
            }
            String pathString = new String(treeWalk.getRawPath());
            // String pathString = treeWalk.getPathString();
            Node[] nodes = getNodes(pathString);
            for (int i = 0; i < nodes.length && i < depth; i++) {
                if (parentMap.get(nodes[i].path) == null) {
                    Map map = getNodeMap(nodes[i], i < (nodes.length - 1), basePath, mapping);
                    map.put("children", new ArrayList());
                    parentMap.put(nodes[i].path, map);
                    Map pmap = parentMap.get(nodes[i].parent);
                    if (pmap != null) {
                        List<Map> children = (List) pmap.get("children");
                        children.add(map);
                    }
                }
            }
        }
        // m_js.prettyPrint(true);
        // String ser = m_js.deepSerialize(parentMap.get("root"));
        // debug("Tree" + ser);
        return parentMap.get("root");
    } catch (Exception e) {
        if (e instanceof RpcException)
            throw (RpcException) e;
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.getWorkingTree:", e);
    } finally {
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

public List<String> assetList(@PName("reponame") String repoName, @PName("name") String name,
        @PName("type") String type, @PName("onlyFirst") @POptional @PDefaultBool(false) Boolean onlyFirst)
        throws RpcException {
    try {/* w w w  .ja  v a2  s  . c o m*/
        String gitSpace = System.getProperty("git.repos");
        File gitDir = new File(gitSpace, repoName);
        if (!gitDir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100,
                    "GitService.getContent:Repo(" + repoName + ") not exists");
        }
        List<String> typeList = new ArrayList();
        List<String> hitList = new ArrayList();
        typeList.add(type);
        Git gitObject = Git.open(new File(gitSpace, repoName));
        TreeWalk treeWalk = new TreeWalk(gitObject.getRepository());
        FileTreeIterator newTree = new FileTreeIterator(gitObject.getRepository());
        treeWalk.addTree(newTree);
        treeWalk.setRecursive(true);
        treeWalk.setPostOrderTraversal(true);
        File basePath = new File(gitSpace, repoName);
        while (true) {
            if (!treeWalk.next()) {
                break;
            }
            String pathString = new String(treeWalk.getRawPath());
            File file = new File(basePath, pathString);
            if (file.isDirectory() || pathString.startsWith("store") || pathString.startsWith(".git")) {
                continue;
            }
            if (typeList.contains("all") || typeList.contains(getFileType(file))) {
                if (name == null || name.equals(getBasename(pathString))) {
                    debug("\tTreffer:" + pathString);
                    hitList.add(pathString);
                    if (onlyFirst) {
                        return hitList;
                    }
                }
            }
        }
        return hitList;
    } catch (Exception e) {
        if (e instanceof RpcException)
            throw (RpcException) e;
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.assetList:", e);
    } finally {
    }
}