2011-12-15 64 views
7

我想獲取修訂版XXXXXX和HEAD之間已更改/添加/刪除的文件列表。這是我到目前爲止有:如何用jgit做「git diff --name-status」的等價物?

String oldHash = "a97e5553e37a25bd1a3c99eab303145baed08dbd"; 
Git git = Git.open(new File("/tmp/jgit")); 
Repository repository = git.getRepository(); 
ObjectId old = repository.resolve(oldHash); 
ObjectId head = repository.resolve("HEAD"); 

// how do i get the trees from the obj. id? 
List<<DiffEntry> diffs = git.diff().setNewTree(null).setOldTree(null).call(); 

for(DiffEntry diff : diffs) { 
    // do stuff 
} 

這是去了解它,如果是正確的方式,我該如何要求git.diff樹()?

+0

這是一個很好的問題,即使OP專門只詢問 - 名稱狀態,因爲它適用於t o所有的jgit.Git.diff()類型的問題,恕我直言,這些例子很少,而且API文檔是傾斜的。那謝謝啦!感謝您的答案@ [Kevin Sawicki](http://stackoverflow.com/users/687965/kevin-sawicki)和@ [ktoso](http://stackoverflow.com/users/111024/ktoso)! – 2013-05-09 18:39:46

回答

9

您可以通過調用get樹ID爲HEAD和散列:

ObjectId head = repository.resolve("HEAD^{tree}"); 

和修訂ID:

ObjectId old = repository.resolve(oldHash + "^{tree}"); 

一旦你的樹ID,您就可以創建樹迭代器並得到差異:

ObjectReader reader = repository.newObjectReader(); 
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); 
oldTreeIter.reset(reader, oldId); 
CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); 
newTreeIter.reset(reader, headId); 
List<DiffEntry> diffs= git.diff() 
         .setNewTree(newTreeIter) 
         .setOldTree(oldTreeIter) 
         .call(); 
+0

謝謝!今晚我會給它一個旋風。 – Erik 2011-12-20 08:04:20