use of org.eclipse.jgit.treewalk.TreeWalk in project searchcode-server by boyter.
the class GitService method fetchFileRevision.
/**
* Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
* file so you MAY end up running into serious memory issues, and should be aware of this
*/
public String fetchFileRevision(String repoLocation, String revision, String filePath) throws MissingObjectException, IncorrectObjectTypeException, IOException {
Repository localRepository = new FileRepository(new File(repoLocation));
ObjectId id = localRepository.resolve(revision);
ObjectReader reader = localRepository.newObjectReader();
try {
RevWalk walk = new RevWalk(reader);
RevCommit commit = walk.parseCommit(id);
RevTree tree = commit.getTree();
TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
if (treewalk != null) {
byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
return new String(data, "utf-8");
} else {
return "";
}
} finally {
reader.close();
}
}
Aggregations