From 589de4c183c6b9055d77a206f8b8a064965b0c36 Mon Sep 17 00:00:00 2001 From: Joe Mou Date: Fri, 12 Apr 2019 00:26:45 -0400 Subject: [PATCH] hash-object --- hash-object | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 hash-object diff --git a/hash-object b/hash-object new file mode 100755 index 0000000..45ba698 --- /dev/null +++ b/hash-object @@ -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