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

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

Introduction

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

Prototype

public int getPathLength() 

Source Link

Document

Get the path length of the current entry.

Usage

From source file:facade.GitFacade.java

public static Map<String, COGClass> getCOGClassesFromCommit(Repository repository, ObjectId commitID)
        throws Exception {
    Map<String, COGClass> allClasses = new TreeMap<>();
    RevWalk revWalk = new RevWalk(repository);
    RevCommit commit = revWalk.parseCommit(commitID);
    RevTree tree = commit.getTree();/*from   w  ww .j  a v  a 2s .c  om*/

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(TreeFilter.ALL);

    classesInFileMap.clear();

    while (treeWalk.next()) {
        String extension = treeWalk.getPathString().substring(treeWalk.getPathLength() - 4);
        if (!extension.equals("java"))
            continue;

        //System.out.println(treeWalk.getPathString());
        ObjectId id = treeWalk.getObjectId(0);
        ObjectLoader loader = repository.open(id);
        Map<String, COGClass> classes = Source2ClassConverter.convertFromStream(loader.openStream());
        allClasses.putAll(classes);
        classesInFileMap.put(treeWalk.getPathString(), new LinkedList<>(classes.values()));
    }
    return allClasses;
}

From source file:org.eclipse.orion.server.git.servlets.GitTreeHandlerV1.java

License:Open Source License

@Override
protected boolean handleGet(RequestInfo requestInfo) throws ServletException {
    HttpServletRequest request = requestInfo.request;
    HttpServletResponse response = requestInfo.response;
    String gitSegment = requestInfo.gitSegment;
    Repository repo = requestInfo.db;// ww w  . ja  v a  2  s.c  o m
    String pattern = requestInfo.relativePath;
    IPath filePath = requestInfo.filePath;
    String meta = request.getParameter("parts"); //$NON-NLS-1$
    RevWalk walk = null;
    TreeWalk treeWalk = null;
    IPath filterPath = new Path(pattern);
    try {
        if (filterPath.segmentCount() == 0) {
            JSONArray children = new JSONArray();
            URI baseLocation = getURI(request);
            List<Ref> call = new Git(repo).branchList().setListMode(ListMode.ALL).call();
            for (Ref ref : call) {
                String branchName = Repository.shortenRefName(ref.getName());
                JSONObject branch = listEntry(branchName, 0, true, 0, baseLocation,
                        GitUtils.encode(branchName));
                children.put(branch);
            }
            JSONObject result = listEntry(filePath.segment(0), 0, true, 0, baseLocation, null);
            result.put(ProtocolConstants.KEY_CHILDREN, children);
            OrionServlet.writeJSONResponse(request, response, result,
                    JsonURIUnqualificationStrategy.ALL_NO_GIT);
            return true;
        }

        gitSegment = GitUtils.decode(filterPath.segment(0));
        filterPath = filterPath.removeFirstSegments(1);
        pattern = filterPath.toPortableString();
        ObjectId head = repo.resolve(gitSegment);
        if (head == null) {
            throw new Exception("Missing ref in git segment");
        }
        walk = new RevWalk(repo);
        // add try catch to catch failures

        RevCommit commit = walk.parseCommit(head);
        RevTree tree = commit.getTree();
        treeWalk = new TreeWalk(repo);
        treeWalk.addTree(tree);
        treeWalk.setRecursive(false);
        if (!pattern.equals("")) { //$NON-NLS-1$
            PathFilter pathFilter = PathFilter.create(pattern);
            treeWalk.setFilter(pathFilter);
        }
        JSONArray contents = new JSONArray();
        JSONObject result = null;
        ArrayList<JSONObject> parents = new ArrayList<JSONObject>();

        URI baseLocation = ServletResourceHandler.getURI(request);
        Path basePath = new Path(baseLocation.getPath());
        IPath tmp = new Path("/"); //$NON-NLS-1$
        for (int i = 0; i < 5; i++) {
            tmp = tmp.append(basePath.segment(i));
        }
        URI cloneLocation = new URI(baseLocation.getScheme(), baseLocation.getAuthority(),
                tmp.toPortableString(), null, baseLocation.getFragment());
        JSONObject ref = listEntry(gitSegment, 0, true, 0, cloneLocation, GitUtils.encode(gitSegment));

        parents.add(ref);
        parents.add(
                listEntry(new Path(cloneLocation.getPath()).lastSegment(), 0, true, 0, cloneLocation, null));
        URI locationWalk = URIUtil.append(cloneLocation, GitUtils.encode(gitSegment));
        while (treeWalk.next()) {
            if (treeWalk.isSubtree()) {
                if (treeWalk.getPathLength() > pattern.length()) {
                    String name = treeWalk.getNameString();
                    contents.put(listEntry(name, 0, true, 0, locationWalk, name));
                }
                if (treeWalk.getPathLength() <= pattern.length()) {
                    locationWalk = URIUtil.append(locationWalk, treeWalk.getNameString());
                    parents.add(0, listEntry(treeWalk.getNameString(), 0, true, 0, locationWalk, null));
                    treeWalk.enterSubtree();
                }
            } else {
                ObjectId objId = treeWalk.getObjectId(0);
                ObjectLoader loader = repo.open(objId);
                long size = loader.getSize();
                if (treeWalk.getPathLength() == pattern.length()) {
                    if ("meta".equals(meta)) { //$NON-NLS-1$
                        result = listEntry(treeWalk.getNameString(), 0, false, 0, locationWalk,
                                treeWalk.getNameString());
                    } else {
                        return getFileContents(request, response, repo, treeWalk, tree);
                    }
                } else {
                    String name = treeWalk.getNameString();
                    contents.put(listEntry(name, 0, false, size, locationWalk, name));
                }
            }
        }
        if (result == null) {
            result = parents.remove(0);
            result.put("Children", contents); //$NON-NLS-1$
        }
        result.put("Parents", new JSONArray(parents)); //$NON-NLS-1$
        response.setContentType("application/json"); //$NON-NLS-1$
        response.setHeader("Cache-Control", "no-cache"); //$NON-NLS-1$
        response.setHeader("ETag", "\"" + tree.getId().getName() + "\""); //$NON-NLS-1$
        OrionServlet.writeJSONResponse(request, response, result);
        return true;
    } catch (Exception e) {
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "An error occured when requesting commit info.", e));
    } finally {
        if (walk != null)
            walk.release();
        if (treeWalk != null)
            treeWalk.release();
    }
}