I want to read the content of a file at a certain commit in a certain branch: I am currently, using this code to read the content of a file at a certain commit ignoring the branch.
public static String readContentOfFileAtCommit(String commitStr, String fileName)
throws IOException {
String content = null;
ObjectId lastCommitId = currentRepo.resolve(commitStr);
try (RevWalk revWalk = new RevWalk(currentRepo)) {
RevCommit commit = revWalk.parseCommit(lastCommitId);
RevTree tree = commit.getTree();
try (TreeWalk treeWalk = new TreeWalk(currentRepo)) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create(fileName));
if (!treeWalk.next()) {
throw new IllegalStateException("Did not find expected file:" + fileName);
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = currentRepo.open(objectId);
content = new String(loader.getBytes());
}
revWalk.dispose();
}
return content;
}
My goal is to get the content of a file at a certain commit that is done on a certain branch.