Example usage for org.eclipse.jgit.treewalk.filter PathSuffixFilter create

List of usage examples for org.eclipse.jgit.treewalk.filter PathSuffixFilter create

Introduction

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

Prototype

public static PathSuffixFilter create(String path) 

Source Link

Document

Create a new tree filter for a user supplied path suffix.

Usage

From source file:com.amd.gerrit.plugins.manifestsubscription.VersionedManifests.java

License:Open Source License

@Override
protected void onLoad() throws IOException, ConfigInvalidException {
    manifests = Maps.newHashMap();/*from  w  ww  .j av a  2 s .c o m*/

    String path;
    Manifest manifest;

    try (RevWalk rw = new RevWalk(reader); TreeWalk treewalk = new TreeWalk(reader)) {
        // This happens when someone configured a invalid branch name
        if (getRevision() == null) {
            throw new ConfigInvalidException(refName);
        }
        RevCommit r = rw.parseCommit(getRevision());
        treewalk.addTree(r.getTree());
        treewalk.setRecursive(false);
        treewalk.setFilter(PathSuffixFilter.create(".xml"));
        while (treewalk.next()) {
            if (treewalk.isSubtree()) {
                treewalk.enterSubtree();
            } else {
                path = treewalk.getPathString();
                //TODO: Should this be done more lazily?
                //TODO: difficult to do when reader is not available outside of onLoad?
                try (ByteArrayInputStream input = new ByteArrayInputStream(readFile(path))) {
                    manifest = (Manifest) manifestUnmarshaller.unmarshal(input);
                    manifests.put(path, manifest);
                } catch (JAXBException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //TODO load changed manifest
    //    DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * 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/*w w  w . j a  v  a 2  s. c o m*/
 * @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;
}

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

License:Open Source License

private static TreeFilter[] suffix(final String... paths) {
    final int length = paths.length;
    final TreeFilter[] filters = new TreeFilter[length];
    for (int i = 0; i < length; i++)
        filters[i] = PathSuffixFilter.create(paths[i]);
    return filters;
}

From source file:org.webcat.core.git.GitRepository.java

License:Open Source License

/**
 * Parses an array of Git commits and returns an array of commits that
 * affected the specified object./*from   w  w  w  .  j a v  a2 s.c  o m*/
 *
 * @param repository the Git repository
 * @param commitIds the Git commit IDs to parse
 * @param path the object affected
 * @return an array of {@link GitCommit} objects representing the commits
 */
public NSArray<GitCommit> commitsWithIds(NSArray<ObjectId> commitIds, String path) {
    try {
        RevWalk rw = new RevWalk(repository);

        try {
            rw.sort(RevSort.COMMIT_TIME_DESC);

            if (path != null) {
                rw.setTreeFilter(AndTreeFilter.create(PathSuffixFilter.create(path), TreeFilter.ANY_DIFF));
            } else {
                rw.setTreeFilter(TreeFilter.ALL);
            }

            for (ObjectId commitId : commitIds) {
                rw.markStart(rw.parseCommit(commitId));
            }

            NSMutableArray<GitCommit> commits = new NSMutableArray<GitCommit>();

            for (RevCommit commit : rw) {
                commits.add(new GitCommit(commit));
            }

            return commits;
        } finally {
            rw.release();
        }
    } catch (Exception e) {
        log.error("An exception occurred while parsing the commit: ", e);
        return null;
    }
}

From source file:uk.ac.cam.cl.dtg.segue.database.GitDb.java

License:Apache License

/**
 * This method will configure a treewalk object that can be used to navigate the git repository.
 * /*from w w  w  .  ja  v a 2s  .com*/
 * @param sha
 *            - the version that the treewalk should be configured to search within.
 * @param searchString
 *            - the search string which can be a full path or simply a file extension.
 * @return A preconfigured treewalk object.
 * @throws IOException
 *             - if we cannot access the repo location.
 * @throws UnsupportedOperationException
 *             - if git does not support the operation requested.
 */
public TreeWalk getTreeWalk(final String sha, final String searchString)
        throws IOException, UnsupportedOperationException {
    Validate.notBlank(sha);
    Validate.notNull(searchString);

    ObjectId commitId = gitHandle.getRepository().resolve(sha);
    if (null == commitId) {
        log.error("Failed to buildGitIndex - Unable to locate resource with sha: " + sha);
    } else {
        RevWalk revWalk = new RevWalk(gitHandle.getRepository());
        RevCommit commit = revWalk.parseCommit(commitId);

        RevTree tree = commit.getTree();

        TreeWalk treeWalk = new TreeWalk(gitHandle.getRepository());
        treeWalk.addTree(tree);
        treeWalk.setRecursive(true);
        treeWalk.setFilter(PathSuffixFilter.create(searchString));

        return treeWalk;
    }
    return null;
}

From source file:uk.ac.cam.UROP.twentyfourteen.database.GitDb.java

/**
 * This method will configure a treewalk object that can be used to navigate
 * the git repository./* w ww.j  a v  a 2s .  c  o  m*/
 *
 * @param sha
 *            - the version that the treewalk should be configured to search
 *            within.
 * @param searchString
 *            - the search string which can be a full path or simply a file
 *            extension.
 * @return A preconfigured treewalk object.
 * @throws IOException
 * @throws UnsupportedOperationException
 */
public TreeWalk getTreeWalk(String sha, String searchString) throws IOException, UnsupportedOperationException {
    Validate.notBlank(sha);
    Validate.notNull(searchString);

    ObjectId commitId = gitHandle.getRepository().resolve(sha);
    if (null == commitId) {
        log.error("Failed to buildGitIndex - Unable to locate resource with SHA: " + sha);
    } else {
        RevWalk revWalk = new RevWalk(gitHandle.getRepository());
        RevCommit commit = revWalk.parseCommit(commitId);

        RevTree tree = commit.getTree();

        TreeWalk treeWalk = new TreeWalk(gitHandle.getRepository());
        treeWalk.addTree(tree);
        treeWalk.setRecursive(true);
        treeWalk.setFilter(PathSuffixFilter.create(searchString));

        return treeWalk;
    }
    return null;
}