2012-07-10 79 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

討論