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;
}
Aggregations