Search code examples
pythongitgitpython

how do I get sha key for any repository' file


I want to access sha key for any repository' files individually.

enter image description here

As you can see. There are a file structure and that is include a .git directory which I created with gitpython. I can get repository sha key for represent all files as follow;

from git import Repo

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

My question is that I want to generate sha key for any file such as file2.txt. Are there any method to realize that. I am sorry, I am new about using gitPython.


Solution

  • Read files then

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