Search in sources :

Example 1 with CountingOutputStream

use of org.eclipse.jgit.util.io.CountingOutputStream in project OpenGrok by OpenGrok.

the class GitRepository method getHistoryRev.

/**
 * Try to get file contents for given revision.
 *
 * @param out a required OutputStream
 * @param fullpath full pathname of the file
 * @param rev revision string
 * @return a defined instance with {@code success} == {@code true} if no
 * error occurred and with non-zero {@code iterations} if some data was transferred
 */
private HistoryRevResult getHistoryRev(OutputStream out, String fullpath, String rev) {
    HistoryRevResult result = new HistoryRevResult();
    File directory = new File(getDirectoryName());
    String filename;
    result.success = false;
    try {
        filename = getGitFilePath(Paths.get(getCanonicalDirectoryName()).relativize(Paths.get(fullpath)).toString());
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, String.format("Failed to relativize '%s' in for repository '%s'", fullpath, directory), e);
        return result;
    }
    try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(directory.getAbsolutePath())) {
        ObjectId commitId = repository.resolve(rev);
        // A RevWalk allows walking over commits based on some filtering that is defined.
        try (RevWalk revWalk = new RevWalk(repository)) {
            RevCommit commit = revWalk.parseCommit(commitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            // Now try to find a specific file.
            try (TreeWalk treeWalk = new TreeWalk(repository)) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create(filename));
                if (!treeWalk.next()) {
                    LOGGER.log(Level.FINEST, "Did not find expected file ''{0}'' in revision {1} " + "for repository ''{2}''", new Object[] { filename, rev, directory });
                    return result;
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                CountingOutputStream countingOutputStream = new CountingOutputStream(out);
                loader.copyTo(countingOutputStream);
                result.iterations = countingOutputStream.getCount();
                result.success = true;
            }
            revWalk.dispose();
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, String.format("Failed to get file '%s' in revision %s for repository '%s'", filename, rev, directory), e);
    }
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CountingOutputStream(org.eclipse.jgit.util.io.CountingOutputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) Repository(org.eclipse.jgit.lib.Repository) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1 CountingOutputStream (org.eclipse.jgit.util.io.CountingOutputStream)1