A2.5 附录 B: 在你的应用中嵌入 Git - Dulwich

Dulwich

There is also a pure-Python Git implementation - Dulwich. The project is hosted under https://www.dulwich.io/ It aims to provide an interface to git repositories (both local and remote) that doesn’t call out to git directly but instead uses pure Python. It has an optional C extensions though, that significantly improve the performance.

Dulwich follows git design and separate two basic levels of API: plumbing and porcelain.

Here is an example of using the lower level API to access the commit message of the last commit:

  1. from dulwich.repo import Repo
  2. r = Repo('.')
  3. r.head()
  4. # '57fbe010446356833a6ad1600059d80b1e731e15'
  5. c = r[r.head()]
  6. c
  7. # <Commit 015fc1267258458901a94d228e39f0a378370466>
  8. c.message
  9. # 'Add note about encoding.\n'

To print a commit log using high-level porcelain API, one can use:

  1. from dulwich import porcelain
  2. porcelain.log('.', max_entries=1)
  3. #commit: 57fbe010446356833a6ad1600059d80b1e731e15
  4. #Author: Jelmer Vernooij <jelmer@jelmer.uk>
  5. #Date: Sat Apr 29 2017 23:57:34 +0000

Further Reading