-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompute-hash
executable file
·47 lines (36 loc) · 1.2 KB
/
compute-hash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
import os
import sys
import time
from hashlib import sha1
from fake_git import check_call, commit_metadata
from tempfile import TemporaryDirectory
os.environ['GIT_PAGER'] = ''
commit_message = 'Add foo'
def hash(object_type, contents):
return sha1(b'%s %d\0%s' % (object_type, len(contents), contents))
file_hash = hash(b'blob', b'hi\n')
print(f'file_hash = {file_hash.hexdigest()}')
tree_hash = hash(b'tree', b'100644 foo\0%b' % file_hash.digest())
print(f'tree_hash = {tree_hash.hexdigest()}')
commit = """tree %s
author %s <%s> %s
committer %s <%s> %s
%s
""" % (tree_hash.hexdigest(),
commit_metadata['name'], commit_metadata['email'], commit_metadata['date'],
commit_metadata['name'], commit_metadata['email'], commit_metadata['date'],
commit_message)
commit = commit.encode('UTF-8')
commit_hash = hash(b'commit', commit)
print(f'commit_hash = {commit_hash.hexdigest()}')
print()
sys.stdout.flush()
with TemporaryDirectory() as t:
os.chdir(t)
with open('foo', 'w') as f:
f.write('hi\n')
check_call(['git', 'init'])
check_call(['git', 'add', 'foo'])
check_call(['git', 'commit', '-m', commit_message])
check_call(['git', 'log', '-m', 'foo'])