1

我试过了GitPython,但我只是得到了实际的提交 git hash。

import git
repo = git.Repo(search_parent_directories=True)
repo.head.commit.hexsha

但是,为了可追溯性,我想存储特定文件的 git commit 哈希,即此命令的等效项(使用git

git log -n 1 --pretty=format:%h -- experiments/test.yaml

有可能实现GitPython吗?

4

1 回答 1

1

诸如如何获取任何存储库文件的 sha 密钥之类的问题。指向Tree 对象,作为对 git 树的递归遍历的访问,可以访问所有元数据,包括 SHA1 哈希。

self.assertEqual(tree['smmap'], tree / 'smmap')          # access by index and by sub-path
for entry in tree:                                         # intuitive iteration of tree members
    print(entry)
blob = tree.trees[0].blobs[0]                              # let's get a blob in a sub-tree
assert blob.name

blob.hexsha将是 blob 的 SHA1。

于 2018-08-07T04:57:43.943 回答