I want to use the Java JGit library in order to retrieve previously committed revisions of a file. The file gets updated on a daily basis and I need to have access to the pr
If I understand your question correctly, you want to find a commit within a given date range and then read the contents of a specific file form that commit.
Assuming that there is only one commit per date, you can use a RevWalk to To get get the desired commit:
try (RevWalk walk = new RevWalk(repo)) {
walk.markStart(walk.parseCommit(repo.resolve(Constants.HEAD)));
walk.sort(RevSort.COMMIT_TIME_DESC);
walk.setRevFilter(myFilter);
for (RevCommit commit : walk) {
if (commit.getCommitter().getWhen().equals(date)) {
// this is the commit you are looking for
revWalk.parseCommit(commit);
break;
}
}
}
I am not entirely sure if the revWalk.parseCommit(commit); is necessary - it depends on how the RevWalk is set up. Try to run the code without parsing the commit and if the file is found, leave it at that.
Now that you have the desired commit, use a TreeWalk to obtain the content of the file:
try (TreeWalk treeWalk = TreeWalk.forPath(repository, fileName, commit.getTree())) {
InputStream inputStream = repository.open(treeWalk.getObjectId(0), Constants.OBJ_BLOB).openStream();
// use the inputStream
}
fileName holds the repository-relative path to the file in question.