I would like to know if it is possible to retrieve the hash of a commit in a Python script.
For a given script, I can easily determine the hash of a blob by using the simple formula and code as e.g:
from sys import argv
from hashlib import sha1
from cStringIO import StringIO
class githash(object):
def __init__(self):
self.buf = StringIO()
def update(self, data):
self.buf.write(data)
def hexdigest(self):
data = self.buf.getvalue()
h = sha1()
h.update("blob %u\0" % len(data))
h.update(data)
return h.hexdigest()
def githash_data(data):
h = githash()
h.update(data)
return h.hexdigest()
def githash_fileobj(fileobj):
return githash_data(fileobj.read())
if __name__ == '__main__':
for filename in argv[1:]:
fileobj = file(filename)
print(githash_fileobj(fileobj))
However, this method of calculating the same hash as git does for a given file doesn't seem to work for a complete commit. The formula described here by VonC isn't clear to me. Is there a way to either calculate this commit hash in python or retrieve it directly from the bitbucket server?
Edit: I just discovered Stashy. I will look into this.