我正在尝试读取提交哈希及其父哈希,以便从
.git/
目录构建提交图。目前,我有这样的东西:
import zlib
import os
...
for current, subs, files in os.walk('.'):
for filename in files:
# in format ##/#{38}
path = os.path.join(current, filename)[2:]
# 'info/' and 'pack/' exist
# don't worry about packed files
# assume empty (excluding . and ..)
with open(path, 'rb') as file:
# returns bytes object
# assuming UTF-8 encoding (default) vs. legacy
# https://git-scm.com/docs/git-commit#_discussion
# .decode() also defaults to utf-8
print(zlib.decompress(file.read()).decode())
然而,我注意到这不是我想要的。上面的代码旨在最终遍历所有
.git/objects/
并将提交及其父母解析为一个列表,以帮助我构建提交图。截至目前,zlib
减压似乎没有按照我喜欢的方式产生输出。我已经阅读了 Pro Git 中的相关部分,特别是:git-scm.com/book/en/v2/Git-Internals-Git-Objects,其中包含针对 Ruby 的说明。我如何在 Python 中完成此操作?