How to "put" a file in JGit?

While I was looking for an embedded distributed version control system in Java , and I think I found it in JGit , which is a pure implementation of Java git. However, there are not many code examples or tutorials.

How can I use JGit to extract the HEAD version of a specific file (same as svn cat or hg cat )?

I assume this is due to some reverse walking, and I am looking for sample code.

+23
java git jgit
Nov 06 '09 at 3:28
source share
6 answers

Unfortunately, Thilo's answer does not work with the latest JGit API. Here is the solution I found:

 File repoDir = new File("test-git"); // open the repository Repository repository = new Repository(repoDir); // find the HEAD ObjectId lastCommitId = repository.resolve(Constants.HEAD); // now we have to get the commit RevWalk revWalk = new RevWalk(repository); RevCommit commit = revWalk.parseCommit(lastCommitId); // and using commit tree find the path RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { return null; } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // and then one can use either InputStream in = loader.openStream() // or loader.copyTo(out) 

I would like it to be easier.

+18
Sep 15 2018-11-11T00:
source share

Here's a simpler version of @morisil's answer, using some of the concepts from @directed, to laugh and test with JGit 2.2.0:

 private String fetchBlob(String revSpec, String path) throws MissingObjectException, IncorrectObjectTypeException, IOException { // Resolve the revision specification final ObjectId id = this.repo.resolve(revSpec); // Makes it simpler to release the allocated resources in one go ObjectReader reader = this.repo.newObjectReader(); try { // Get the commit object for that revision RevWalk walk = new RevWalk(reader); RevCommit commit = walk.parseCommit(id); // Get the revision file tree RevTree tree = commit.getTree(); // .. and narrow it down to the single file path TreeWalk treewalk = TreeWalk.forPath(reader, path, tree); if (treewalk != null) { // use the blob id to read the file data byte[] data = reader.open(treewalk.getObjectId(0)).getBytes(); return new String(data, "utf-8"); } else { return ""; } } finally { reader.release(); } } 

repo is a repository object created in other answers.

+15
Feb 13 '13 at 14:55
source share

I followed @Thilo and @morisil's answer to get this compatible with JGit 1.2.0:

 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(); // 1.2.0 api version here // find a file (as a TreeEntry, which contains the blob object id) TreeWalk treewalk = TreeWalk.forPath(repo, "b/test.txt", tree); // use the blob id to read the file data byte[] data = repo.open(treewalk.getObjectId(0)).getBytes(); 

I have not tested the Java version, but should work. It translates from

 (.getBytes (.open repo (.getObjectId (TreeWalk/forPath repo "b/test.txt" tree) 0))) 

in clojure (following the same setup as the top) that works.

+11
Feb 12 2018-12-12T00:
source share

I thought for myself. The API is pretty low level, but it's not so 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 data byte[] data = repo.openBlob(entry.getId()).getBytes(); 
+5
Nov 08 '09 at 0:42
source share

I started writing a library called gitective , which contains many helpers for working with blobs, commits and trees using JGit and is MIT licensed and available on GitHub.

Get file contents in HEAD commit

 Repository repo = new FileRepository("/repos/project/.git"); String content = BlobUtils.getHeadContent(repo, "src/Buffer.java"); 

Get the contents of a file on a branch

 Repository repo = new FileRepository("/repos/project/.git"); String content = BlobUtils.getContent(repo, "master", "src/Buffer.java"); 

Reset two files

 Repository repo = new FileRepository("/repos/project/.git"); ObjectId current = BlobUtils.getId(repo, "master", "Main.java"); ObjectId previous = BlobUtils.getId(repo, "master~1", "Main.java"); Collection<Edit> edit = BlobUtils.diff(repo, previous, current); 

Additional examples of the provided utilities are described in detail in README .

+4
Nov 03 2018-11-11T00:
source share

There is some information in the JGit Tutorial (but it is also not very useful, it is not complete and probably deprecated as they switched to eclipse , where there is no documentation yet).

+3
Nov 07 '09 at 2:00
source share



All Articles