-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite-tree
executable file
·33 lines (27 loc) · 930 Bytes
/
write-tree
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
#!/usr/bin/python
# write-tree.c
import subprocess
import index
def write_tree(entries, base):
i = 0
buf = ''
while i < len(entries):
filename, mode, rawsha1 = entries[i]
# for directories
if not filename.startswith(base):
break
filename = filename[len(base):]
if '/' in filename:
filename = filename.split('/', 1)[0]
num_written, sha1 = write_tree(entries[i:], base + filename + '/')
i += num_written
i -= 1 # write the directory entry, but don't count it
mode = 040000
rawsha1 = bytearray.fromhex(sha1)
buf += '{:o} {}\0{}'.format(mode, filename, rawsha1)
i += 1
with open('.zit/tree.tmp', 'w') as f:
f.write(buf)
return i, subprocess.check_output(['hash-object', 'tree', '.zit/tree.tmp']).strip()
_, sha1 = write_tree(index.read_index(), '')
print sha1