0

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.

Sascha Frinken
  • 3,134
  • 1
  • 24
  • 27
Yılmaz edis
  • 146
  • 3
  • 13

2 Answers2

1

Read files then

>>> import hashlib
>>> file_text = 'content'
>>> hashlib.sha256(file_text.encode()).hexdigest()
'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
>>> 
tnusraddinov
  • 660
  • 7
  • 13
0
repo = Repo.init(path).git
index = Repo(path).index

# add file to repo
repo.add(filename)
index.commit(message)

path: repository (.git) path

filename : which file do you want to add

message : commit message


after that use method as fallow;

repo = Repo(path)
commit = repo.head.commit

sha = commit.hexsha
Yılmaz edis
  • 146
  • 3
  • 13