This repository was archived by the owner on Dec 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (110 loc) · 3.35 KB
/
main.py
File metadata and controls
134 lines (110 loc) · 3.35 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import hashlib
import io
import os
import pathlib
import sys
import zlib
def object_read(sha):
git_path = pathlib.Path('.git')
path = git_path / 'objects' / sha[:2] / sha[2:]
with path.open(mode='rb') as f:
raw = zlib.decompress(f.read())
space = raw.find(b' ')
object_type = raw[:space]
null = raw.find(b'\x00', space)
size = int(raw[space:null].decode('ascii'))
if size != len(raw[null+1:]):
raise Exception('Invalid length')
return raw[null+1:]
def ls_tree(sha):
data = object_read(sha)
tree_items = parse_tree(data)
for item in tree_items:
print(item.decode())
def parse_tree(raw):
pos = 0
tree_items = []
while pos < len(raw):
space = raw.find(b' ', pos)
null = raw.find(b'\x00', space)
item = raw[space+1: null]
tree_items.append(item)
pos = null + 41
return tree_items
def write_tree(directory='.', save=True):
tree_entries = []
exclude = ['.git']
raw_entries = []
for currentpath, folders, files in os.walk(directory, topdown=True):
raw_entries.extend(folders)
raw_entries.extend(files)
for f in raw_entries:
if f in exclude:
raw_entries.remove(f)
break
for f in raw_entries:
path = pathlib.Path(currentpath) / f
filename = path.name.encode()
try:
sha1 = object_sha(path.open(mode='rb')).encode()
f_mode = oct(os.stat(path)[0])[2:].encode()
except PermissionError:
f_mode = b'040000'
sha1 = write_tree(path, save=False).encode()
tree_entry = f_mode + b' ' + filename + b'\x00' + sha1
tree_entries.append(tree_entry)
print(tree_entries)
obj = io.BytesIO(b''.join(tree_entries))
if save:
sha = hash_object(obj, obj_type=b'tree')
print(sha)
else:
sha = object_sha(obj)
return sha
def cat_file(sha):
data = object_read(sha)
print(data.decode())
def object_sha(filename):
data = filename.read()
sha = hashlib.sha1(data).hexdigest()
return sha
def hash_object(filename, obj_type=b'blob'):
data = filename.read()
res = obj_type + b' ' + str(len(data)).encode() + b'\x00' + data
sha = hashlib.sha1(res).hexdigest()
git_d = pathlib.Path('.git')
path = git_d / 'objects' / sha[:2] / sha[2:]
os.makedirs(os.path.dirname(path), exist_ok=True)
with path.open(mode='wb') as f:
f.write(zlib.compress(res))
return sha
def main():
command = sys.argv[1]
try:
option = sys.argv[2]
except IndexError:
pass
if command == 'init':
os.mkdir('.git')
os.mkdir('.git/objects')
os.mkdir('.git/refs')
with open('.git/HEAD', 'w') as f:
f.write('ref: refs/heads/master\n')
print('Initialized git repository')
elif command == 'cat-file' and option == '-p':
sha = sys.argv[3]
cat_file(sha)
elif command == 'hash-object' and option == '-w':
filename = sys.argv[3]
with open(filename, mode='rb') as f:
sha = hash_object(f)
print(sha)
elif command == 'ls-tree' and option == '--name-only':
sha = sys.argv[3]
ls_tree(sha)
elif command == 'write-tree':
write_tree()
else:
print('Unknown command')
if __name__ == '__main__':
main()