2012-06-05 24 views
3

我想知道如何執行相當於git status與德威?編程`git狀態`與德威

我嘗試這樣做:

添加/更改/重命名了一些文件和分期他們提交後,這是我試着做:

from dulwich.repo import Repo 
from dulwich.index import changes_from_tree 
r = Repo('my-git-repo') 
index = r.open_index() 
changes = index.changes_from_tree(r.object_store, r['HEAD'].tree) 

輸出如下:

>>> list(changes) 
(('Makefile', None), (33188, None), ('9b20...', None)) 
(('test/README.txt', 'test/README.txt'), (33188, 33188), ('484b...', '4f89...')) 
((None, 'Makefile.mk'), (None, 33188), (None, '9b20...')) 
((None, 'TEST.txt'), (None, 33188), (None, '2a02...')) 

但是這個輸出要求我進一步處理它來檢測:

  1. 我修改了README.txt
  2. 我將其更名爲MakefileMakefile.mk
  3. 我將TEST.txt添加到存儲庫。

dulwich.diff_tree中的函數提供了更好的樹更改界面......在實際提交之前這是不可能的嗎?

回答

2

您應該可以使用dulwich.diff_tree.tree_changes來檢測兩棵樹之間的變化。

對此的一個要求是將相關的樹對象添加到對象存儲 - 您可以使用dulwich.index.commit_index來實現此目的。

1

爲了完整,工作示例:

from dulwich.repo import Repo 
from dulwich.diff_tree import tree_changes 

repo = Repo("./") 

index = repo.open_index() 

try: 
    head_tree = repo.head().tree 
except KeyError: # in case of empty tree 
    head_tree = dulwich.objects.Tree() 

changes = list(tree_changes(repo, head_tree, index.commit(repo.object_store))) 


for change in changes: 
    print "%s: %s"%(change.type,change.new.path) 
+0

看來,因爲'repo.head沒有工作()'是類型'str'因此你不能辦理'.tree'就可以了: ( – Mapio

+0

似乎是更多的api更改。現在有一個'dulwich.porcelain.get_tree_changes(回購)' - 「通過比較索引與HEAD返回添加/刪除/修改樹的更改。」@Mapio – gameweld