tags:

views:

311

answers:

2

A while back I was looking for an embeddable distributed version control system in Java, and I think I have found it in JGit, which is a pure Java implementation of git. However, there is not much in the way of sample code or tutorials.

How can I use JGit to retrieve the HEAD version of a certain file (just like "svn cat" or "hg cat" whould do)?

I suppose this involves some rev-tree-walking and am looking for a code sample.

+2  A: 

There is some info at JGit Tutorial (but that also is neither really helpful nor complete and probably outdated since they switched to eclipse where no documentation is available yet).

jitter
+2  A: 

Figured it out by myself. The API is quite low-level, but it's not too bad:

File repoDir = new File("test-git/.git");
// open the repository
Repository repo = new Repository(repoDir);
// find the HEAD
Commit head = repo.mapCommit(Constants.HEAD);
// retrieve the tree in HEAD
Tree tree = head.getTree();
// find a file (as a TreeEntry, which contains the blob object id)
TreeEntry entry = tree.findBlobMember("b/test.txt");
// use the blob id to read the file's data
byte[] data = repo.openBlob(entry.getId()).getBytes();
Thilo