章节 ▾ 第二版

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

Dulwich

还有一种纯Python的Git实现——Dulwich。该项目托管在https://www.dulwich.io/。它旨在提供一个与Git仓库(本地和远程)交互的接口,该接口不直接调用Git,而是使用纯Python。它虽然有可选的C扩展,但能显著提高性能。

Dulwich遵循Git设计,并将API分为两个基本级别:底层(plumbing)和高层(porcelain)。

这是一个使用底层API访问最后一次提交的提交信息的示例

from dulwich.repo import Repo
r = Repo('.')
r.head()
# '57fbe010446356833a6ad1600059d80b1e731e15'

c = r[r.head()]
c
# <Commit 015fc1267258458901a94d228e39f0a378370466>

c.message
# 'Add note about encoding.\n'

要使用高层porcelain API打印提交日志,可以使用

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

延伸阅读

API文档、教程以及许多关于如何使用Dulwich完成特定任务的示例可在官方网站https://www.dulwich.io上找到。