3

I'm wondering how I can perform the equivalent of git status with dulwich?

I tried this:

After adding/changing/renaming some files and staging them for commit, this is what I've tried doing:

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)

Outputs the following:

>>> 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...'))

But this output requires that I further process it to detect:

  1. I modified README.txt.
  2. I renamed Makefile to Makefile.mk.
  3. I added TEST.txt to the repository.

The functions in dulwich.diff_tree provide a much nicer interface to tree changes... is this not possible before actually committing?

sholsapp
  • 15,542
  • 10
  • 50
  • 67

2 Answers2

2

You should be able to use dulwich.diff_tree.tree_changes to detect the changes between two trees.

One of the requirements for this is that you add the relevant tree objects to the object store - you can use dulwich.index.commit_index for this.

Daniel F
  • 13,684
  • 11
  • 87
  • 116
jelmer
  • 2,405
  • 14
  • 27
1

For completeness, a working sample:

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)
Catskul
  • 17,916
  • 15
  • 84
  • 113
gameweld
  • 1,319
  • 15
  • 21
  • It appears not to be working since `repo.head()` is of type `str` hence you can't apply `.tree` on it :( –  Jan 24 '16 at 07:45
  • 2
    Appears to be more several api changes. There is now a `dulwich.porcelain.get_tree_changes(repo)` -- "Return add/delete/modify changes to tree by comparing index to HEAD." @Mapio – gameweld Jan 30 '16 at 23:38