4

I need to use git plumbing commands (such as those used in chapter 9 of the git book) like git hash-object, git write-tree, git commit-tree and all the rest. Is there a nice API to do these in JGit (I can't seem to find one) or how would you do something basic like write from an output stream or file to a blob/what do you use instead of the git commands?

Callum Rogers
  • 15,630
  • 17
  • 67
  • 90

4 Answers4

3

Welcome to the JGit API. Other than the high-level porcelain API in package org.eclipse.jgit.api the low level API isn't built in close correlation to the plumbing commands of native git. This is due to the fact that JGit is a Java library and not a command line interface.

If you need examples first look at the > 2000 JGit test cases. Next have a look into how EGit is using JGit. If this doesn't help come back and ask more specific questions.

David Nehme
  • 21,379
  • 8
  • 78
  • 117
Matthias Sohn
  • 339
  • 2
  • 4
1

If there is, it should be in JGit.

For instance, the DirCache object (ie, the Git Index) has a WriteTree function:

/**
 * Write all index trees to the object store, returning the root tree.
 *
 * @param ow
 *   the writer to use when serializing to the store. The caller is
 *   responsible for flushing the inserter before trying to use the
 *   returned tree identity.
 * @return identity for the root tree.
 * @throws UnmergedPathException
 *   one or more paths contain higher-order stages (stage > 0),
 *   which cannot be stored in a tree object.
 * @throws IllegalStateException
 *   one or more paths contain an invalid mode which should never
 *   appear in a tree object.
 * @throws IOException
 *   an unexpected error occurred writing to the object store.
 */
public ObjectId writeTree(final ObjectInserter ow)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

git hash-object <file>

import java.io.*;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectInserter.Formatter;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;

public class GitHashObject {
    public static void main(String[] args) throws IOException {
        File file = File.createTempFile("foobar", ".txt");
        FileOutputStream out = new FileOutputStream(file);
        out.write("foobar\n".getBytes());
        out.close();
        System.out.println(gitHashObject(file));
    }

    public static String gitHashObject(File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        Formatter formatter = new ObjectInserter.Formatter();
        ObjectId objectId = formatter.idFor(OBJ_BLOB, file.length(), in);
        in.close();
        return objectId.getName(); // or objectId.name()
    }
}

Expected output : 323fae03f4606ea9991df8befbb2fca795e648fa
as discussed in Assigning Git SHA1's without Git

Community
  • 1
  • 1
Frédéric
  • 565
  • 1
  • 5
  • 10
0

I'm looking for same thing like you. I use git as a k-v database to describe our datastructure. So I want to wrap git style Plumbing APIs or Jgit style Plumbing APIs as CRUD APIs. Then I found org.eclipse.jgit.lib.ObjectInserter.

https://download.eclipse.org/jgit/site/5.10.0.202012080955-r/apidocs/index.html

https://download.eclipse.org/jgit/site/5.10.0.202012080955-r/apidocs/org/eclipse/jgit/lib/ObjectInserter.html

I think most C requirments of CRUD can be achieved by wrapping ObjectInserter.

Rubik
  • 33
  • 5