首页 > 解决方案 > 如何获取任何存储库文件的 sha 密钥

问题描述

我想单独访问任何存储库文件的 sha 密钥。

在此处输入图像描述

如你看到的。有一个文件结构,其中包括我用 gitpython 创建的 .git 目录。我可以获得用于表示所有文件的存储库 sha 密钥,如下所示;

from git import Repo

repo = Repo("graph/features")
master = repo.head.reference
print(master.commit.hexsha)

我的问题是我想为任何文件(如 file2.txt)生成 sha 密钥。有什么方法可以实现。对不起,我是使用 gitPython 的新手。

标签: pythongitgitpython

解决方案


然后读取文件

>>> import hashlib
>>> file_text = 'content'
>>> hashlib.sha256(file_text.encode()).hexdigest()
'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
>>> 

推荐阅读