Skip to content

Commit

Permalink
hash-object
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Mou committed Apr 12, 2019
1 parent d2ec6fc commit 589de4c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hash-object
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/python
# usage differs from git hash-object

# git1.0 hash-object.c sha1_file.c:write_sha1_file
import hashlib
import os
import sys
import zlib

_, type, filename = sys.argv
contents = open(filename).read()
header = '{} {}\0'.format(type, len(contents))

hasher = hashlib.sha1()
hasher.update(header)
hasher.update(contents)
sha1 = hasher.hexdigest()

sha1_filename = '.zit/objects/{}/{}'.format(sha1[:2], sha1[2:])
dirname = os.path.dirname(sha1_filename)
if not os.path.exists(dirname):
os.mkdir(dirname)

compressed = zlib.compress(header + contents)
open(sha1_filename, 'w').write(compressed)

print sha1

0 comments on commit 589de4c

Please sign in to comment.