0

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(); 
Ana Sustic
  • 425
  • 2
  • 17
  • 1
    '...that is not what I want' You need to tell us what you want.This article https://www.codeaffine.com/2014/10/20/git-internals/ explains the low-level JGit APIs that you can use to retrieve a file at a specific version from the repository. – Rüdiger Herrmann Jan 24 '20 at 16:32
  • Hi @rüdiger-herrmann I found the solution in your answer to this question: https://stackoverflow.com/questions/31074116/checkout-a-specific-revision-based-on-the-commit-date-with-jgit. Thank you. – Ana Sustic Jan 24 '20 at 16:49

0 Answers0