I would like to programatically get the content of a file with a specific revision using JGit.
For example:
git rev-list HEAD e973758b-6c2f-4788-9f4e-a5569ee76a48.yaml
gives two revisions:
b24a61030523189f67c24a7a166b2542e1db712c
08ab732b4d6e7f08b821ae8e95cedc623c407647
and say, I want to retrieve the file from revision 08ab732b4d6e7f08b821ae8e95cedc623c407647
.
I tried with this code but that creates a detached branch for the whole repository and that is not what I want.
File targetDirectory = new File("/Users/jGit/");
Git git = Git.init()
.setDirectory(targetDirectory)
.call();
String commitID = getVersionName();
git.checkout().setName(commitID).call();
Solution thanks to @rüdiger-herrmann in Checkout a specific revision based on the commit date with JGit
Node root = getNode();
String revisionID = getVersionName();
System.out.println("Commit ID is "+revisionID);
Repository repository = git.getRepository();
RevWalk walk = new RevWalk(repository);
walk.reset();
ObjectId id = repository.resolve(revisionID);
RevCommit commit = walk.parseCommit(id);
System.out.println("Found Commit again: " + commit);
walk.dispose();
String fileName = root.getIdentifier() + ".yaml";
TreeWalk treeWalk = TreeWalk.forPath( repository, fileName, commit.getTree() );
InputStream yamlFile = repository.open( treeWalk.getObjectId( 0 ), Constants.OBJ_BLOB ).openStream();
// use the inputStream
treeWalk.close();