1

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))

Source

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.

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • What part is not clear in https://stackoverflow.com/a/35433481/6309? – VonC Jul 09 '20 at 17:31
  • @VonC For the blob hash of a file, I could easily make my own python function to compute it. I gave the code I found on GIT as an example. However, for the commit hash, I have no idea how to translate your post into a python function/script which gives me the hash of a commit. – Mathieu Jul 10 '20 at 09:12

0 Answers0