2012-07-10 32 views
1

我需要使用git plumbing命令(例如chapter 9 of the git book中使用的那些命令),如git hash-object,git write-tree,git commit-tree和其他所有。有没有一个很好的API在JGit中做这些(我似乎无法找到一个),或者你将如何做一些基本的事情,比如从输出流或文件写入blob /你使用什么而不是git命令?是否有JGit管道API?

回答

1

如果有,应该在JGit

例如,DirCache对象(即,GIT中指数)具有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) 
2

欢迎JGit API。除了包装org.eclipse.jgit.api中的高级瓷器API之外,低级别的API并不与本地git的管道命令密切相关。这是由于JGit是Java库而不是命令行界面。

如果您需要示例,请首先查看> 2000个JGit测试用例。接下来看看EGit如何使用JGit。如果这无助于回来并提出更具体的问题。

1

git的散列的对象<文件>

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() 
    } 
} 

预期输出:323fae03f4606ea9991df8befbb2fca795e648fa
Assigning Git SHA1's without Git

讨论