Skip to content

Instantly share code, notes, and snippets.

@TurRil
Forked from msabramo/githash.py
Created November 24, 2022 08:51
Show Gist options
  • Save TurRil/73fb7e03c272cfc623acc279f1d0573f to your computer and use it in GitHub Desktop.
Save TurRil/73fb7e03c272cfc623acc279f1d0573f to your computer and use it in GitHub Desktop.

Revisions

  1. @msabramo msabramo renamed this gist Jan 3, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @msabramo msabramo created this gist Jan 3, 2011.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env python

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