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

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

Introduction

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

Prototype

public static PathFilter create(String path) 

Source Link

Document

Create a new tree filter for a user supplied path.

Usage

From source file:ShowFileDiff.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    Repository repository = null; //CookbookHelper.openJGitCookbookRepository();

    // the diff works on TreeIterators, we prepare two for the two branches
    AbstractTreeIterator oldTreeParser = prepareTreeParser(repository,
            "09c65401f3730eb3e619c33bf31e2376fb393727");
    AbstractTreeIterator newTreeParser = prepareTreeParser(repository,
            "aa31703b65774e4a06010824601e56375a70078c");

    // then the procelain diff-command returns a list of diff entries
    List<DiffEntry> diff = new Git(repository).diff().setOldTree(oldTreeParser).setNewTree(newTreeParser)
            .setPathFilter(PathFilter.create("README.md")).call();
    for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
        DiffFormatter formatter = new DiffFormatter(System.out);
        formatter.setRepository(repository);
        formatter.format(entry);//  www. ja  v  a  2s  .c om
    }

    repository.close();
}

From source file:MyDiffFormatter.java

License:Eclipse Distribution License

/**
* Determine the differences between two trees.
*
* No output is created, instead only the file paths that are different are
* returned. Callers may choose to format these paths themselves, or convert
* them into {@link FileHeader} instances with a complete edit list by
*
* @param a/*w w w. java2  s.co m*/
*            the old (or previous) side.
* @param b
*            the new (or updated) side.
* @return the paths that are different.
* @throws IOException
*             trees cannot be read or file contents cannot be read.
*/
public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b) throws IOException {
    assertHaveRepository();

    TreeWalk walk = new TreeWalk(reader);
    walk.addTree(a);
    walk.addTree(b);
    walk.setRecursive(true);

    TreeFilter filter = getDiffTreeFilterFor(a, b);
    if (pathFilter instanceof FollowFilter) {
        walk.setFilter(AndTreeFilter.create(PathFilter.create(((FollowFilter) pathFilter).getPath()), filter));
    } else {
        walk.setFilter(AndTreeFilter.create(pathFilter, filter));
    }

    source = new ContentSource.Pair(source(a), source(b));

    List<DiffEntry> files = DiffEntry.scan(walk);
    if (pathFilter instanceof FollowFilter && isAdd(files)) {
        // The file we are following was added here, find where it
        // came from so we can properly show the rename or copy,
        // then continue digging backwards.
        //
        a.reset();
        b.reset();
        walk.reset();
        walk.addTree(a);
        walk.addTree(b);
        walk.setFilter(filter);

        if (renameDetector == null)
            setDetectRenames(true);
        files = updateFollowFilter(detectRenames(DiffEntry.scan(walk)));

    } else if (renameDetector != null)
        files = detectRenames(files);

    return files;
}

From source file:ch.sourcepond.maven.release.scm.git.GitRepository.java

License:Apache License

private void filterOutOtherModulesChanges(final String modulePath, final List<String> childModules,
        final RevWalk walk) {
    final boolean isRootModule = ".".equals(modulePath);
    final boolean isMultiModuleProject = !isRootModule || !childModules.isEmpty();
    final List<TreeFilter> treeFilters = new LinkedList<>();
    treeFilters.add(TreeFilter.ANY_DIFF);
    if (isMultiModuleProject) {
        if (!isRootModule) {
            // for sub-modules, look for changes only in the sub-module
            // path...
            treeFilters.add(PathFilter.create(modulePath));
        }/*from  ww w . j av a  2s  . c  om*/

        // ... but ignore any sub-modules of the current sub-module, because
        // they can change independently of the current module
        for (final String childModule : childModules) {
            final String path = isRootModule ? childModule : modulePath + "/" + childModule;
            treeFilters.add(PathFilter.create(path).negate());
        }

    }
    final TreeFilter treeFilter = treeFilters.size() == 1 ? treeFilters.get(0)
            : AndTreeFilter.create(treeFilters);
    walk.setTreeFilter(treeFilter);
}

From source file:co.bledo.gitmin.servlet.Review.java

License:Apache License

private String getFileContent(Repository repo, RevCommit commit, String path)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    // and using commit's tree find the path
    RevTree tree = commit.getTree();/*w  ww  . ja  va2 s.  c o  m*/
    TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create(path));
    if (!treeWalk.next()) {
        return "";
    }
    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repo.open(objectId);

    // and then one can use either
    //InputStream in = loader.openStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    loader.copyTo(out);
    return out.toString();
}

From source file:com.addthis.hydra.job.store.JobStoreGit.java

License:Apache License

/**
 * Get the diff of a file against a particular commit
 *
 * @param jobId    The job config to diff
 * @param commitId If specified, the commit to compare against; otherwise, compare against the latest revision
 * @return A string description of the diff, comparable to the output of "git diff filename"
 * @throws GitAPIException If there is a problem fetching the diff from git
 * @throws IOException     If there is a problem reading from the file
 *//*from ww  w  . j a  v  a  2s .c o m*/
public String getDiff(String jobId, String commitId) throws GitAPIException, IOException {
    OutputStream out = new ByteArrayOutputStream();
    DiffCommand diff = git.diff().setPathFilter(PathFilter.create(getPathForJobId(jobId))).setOutputStream(out)
            .setSourcePrefix("old:").setDestinationPrefix("new:");
    if (commitId != null) {
        diff.setOldTree(getTreeIterator(commitId));
        diff.setNewTree(getTreeIterator("HEAD"));
    }
    diff.call();
    return out.toString();
}

From source file:com.buildautomation.jgit.api.GetFileAttributes.java

License:Apache License

private static void printFile(Repository repository, RevTree tree) throws IOException {
    // now try to find a specific file
    try (TreeWalk treeWalk = new TreeWalk(repository)) {
        treeWalk.addTree(tree);//from  ww  w .ja  v a2  s. com
        treeWalk.setRecursive(false);
        treeWalk.setFilter(PathFilter.create("README.md"));
        if (!treeWalk.next()) {
            throw new IllegalStateException("Did not find expected file 'README.md'");
        }

        // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit
        // set
        FileMode fileMode = treeWalk.getFileMode(0);
        ObjectLoader loader = repository.open(treeWalk.getObjectId(0));
        System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType()
                + ", mode: " + fileMode + " size: " + loader.getSize());
    }
}

From source file:com.buildautomation.jgit.api.GetFileAttributes.java

License:Apache License

private static void printDirectory(Repository repository, RevTree tree) throws IOException {
    // look at directory, this has FileMode.TREE
    try (TreeWalk treeWalk = new TreeWalk(repository)) {
        treeWalk.addTree(tree);/*from w  ww.  j a v  a 2 s. c o m*/
        treeWalk.setRecursive(false);
        treeWalk.setFilter(PathFilter.create("src"));
        if (!treeWalk.next()) {
            throw new IllegalStateException("Did not find expected file 'README.md'");
        }

        // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true
        FileMode fileMode = treeWalk.getFileMode(0);
        System.out.println("src: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: "
                + fileMode);
    }
}

From source file:com.buildautomation.jgit.api.ReadFileFromCommit.java

License:Apache License

public static void readFileFromCommit() throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // find the HEAD
        ObjectId lastCommitId = repository.resolve(Constants.HEAD);

        // a RevWalk allows to walk over commits based on some filtering that is defined
        try (RevWalk revWalk = new RevWalk(repository)) {
            RevCommit commit = revWalk.parseCommit(lastCommitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            System.out.println("Having tree: " + tree);

            // now try to find a specific file
            try (TreeWalk treeWalk = new TreeWalk(repository)) {
                treeWalk.addTree(tree);/*  www  .ja v a 2  s. co  m*/
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create("README.md"));
                if (!treeWalk.next()) {
                    throw new IllegalStateException("Did not find expected file 'README.md'");
                }

                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);

                // and then one can the loader to read the file
                loader.copyTo(System.out);
            }

            revWalk.dispose();
        }
    }
}

From source file:com.buildautomation.jgit.api.ShowBlame.java

License:Apache License

private static int countFiles(Repository repository, ObjectId commitID, String name) throws IOException {
    try (RevWalk revWalk = new RevWalk(repository)) {
        RevCommit commit = revWalk.parseCommit(commitID);
        RevTree tree = commit.getTree();
        System.out.println("Having tree: " + tree);

        // now try to find a specific file
        try (TreeWalk treeWalk = new TreeWalk(repository)) {
            treeWalk.addTree(tree);//  w ww .  j a  va2s.  com
            treeWalk.setRecursive(true);
            treeWalk.setFilter(PathFilter.create(name));
            if (!treeWalk.next()) {
                throw new IllegalStateException("Did not find expected file 'README.md'");
            }

            ObjectId objectId = treeWalk.getObjectId(0);
            ObjectLoader loader = repository.open(objectId);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // and then one can the loader to read the file
            loader.copyTo(stream);

            revWalk.dispose();

            return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray())).size();
        }
    }
}

From source file:com.buildautomation.jgit.api.ShowFileDiff.java

License:Apache License

public static void showFileDiff() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // the diff works on TreeIterators, we prepare two for the two branches
        AbstractTreeIterator oldTreeParser = prepareTreeParser(repository,
                "09c65401f3730eb3e619c33bf31e2376fb393727");
        AbstractTreeIterator newTreeParser = prepareTreeParser(repository,
                "aa31703b65774e4a06010824601e56375a70078c");

        // then the procelain diff-command returns a list of diff entries
        try (Git git = new Git(repository)) {
            List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser)
                    .setPathFilter(PathFilter.create("README.md")).call();
            for (DiffEntry entry : diff) {
                System.out.println(
                        "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
                try (DiffFormatter formatter = new DiffFormatter(System.out)) {
                    formatter.setRepository(repository);
                    formatter.format(entry);
                }/*from  www  . j av  a 2s .c om*/
            }
        }
    }
}