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:
from dulwich.repo import Repo
r = Repo('.')
r.head()
# '57fbe010446356833a6ad1600059d80b1e731e15'
c = r[r.head()]
c
# <Commit 015fc1267258458901a94d228e39f0a378370466>
c.message
# 'Add note about encoding.\n'
To print a commit log using high-level porcelain API, one can use:
from dulwich import porcelain
porcelain.log('.', max_entries=1)
#commit: 57fbe010446356833a6ad1600059d80b1e731e15
#Author: Jelmer Vernooij <jelmer@jelmer.uk>
#Date: Sat Apr 29 2017 23:57:34 +0000
Further Reading
The official API documentation is available at https://www.dulwich.io/apidocs/dulwich.html
Official tutorial at https://www.dulwich.io/docs/tutorial has many examples of how to do specific tasks with Dulwich