章節 ▾ 第二版

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 上找到。