4

I'm trying to commit a single blob directly to a repository using jgit. I know how to insert the blob and get its sha1, however I'm having difficulties constructing a tree for this scenario. I can't seem to figure out how to correctly use jgit's tree abstractions (TreeWalk and the like) to recursively construct a tree, almost identical to the previous commits', with only different parent trees of the blob.

What is the idiomatic way to do this in JGit?

The reason I ask, is because I'm writing a program that's a kind of editor for documents living in git repositories. In my case, the whole point of using git is to be able to have multiple versions of the documents (aka branches) at the same time. Since it's an editor I have to be able to commit changes, however since I want to see multiple versions of the document at the same time, checking out, modifying a file and committing using JGit porcelain API is not possible, it has to work directly with git objects.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Jakob Odersky
  • 1,371
  • 11
  • 23
  • 1
    Did you take a look into the CommitCommand code? In createTemporaryIndex() a temporary index is created. You might be able to skip the tree-walking code and adapt the DirCacheEntry creation to your needs. – Rüdiger Herrmann Mar 11 '14 at 09:54

1 Answers1

3

The low-level API you can use for this is TreeFormatter together with CommitBuilder.

An example of using this can be seen here. In this case, it constructs one new tree object with multiple subtrees.

In your case, you probably have to recursively walk the tree and create the new tree objects on the path to the changed file and insert them bottom up. For the rest of the tree, you can use the existing tree IDs and don't have to descend into them. I recommend looking into TreeWalk#setRecursive and TreeWalk#setPostOrderTraversal.

Another option would be to create an in-core DirCache, fill it with DirCacheEntries from the commit and your updated entry, and then call DirCache#writeTree.

robinst
  • 30,027
  • 10
  • 102
  • 108