-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygit.py
More file actions
executable file
·193 lines (167 loc) · 6.03 KB
/
pygit.py
File metadata and controls
executable file
·193 lines (167 loc) · 6.03 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
import argparse
import os
import time
import hashlib
import pathlib
from pygittree import PyGitTree
################# UTILS FUNCTION #################
ROOT_DIR = pathlib.Path(__file__).parent.absolute()
def gen_path(fnames):
return os.path.join(ROOT_DIR, fnames)
def init(args):
os.makedirs(".pygit/objects", exist_ok=True)
os.makedirs(".pygit/refs/heads", exist_ok=True)
files = ["HEAD", 'index']
for fname in files:
with open(os.path.join(ROOT_DIR, f".pygit/{fname}"), 'w') as f:
if fname == "HEAD":
f.write("ref: refs/heads/master")
pass
def in_index(content, fname, blob):
return f"{fname} {blob}" in content
def gen_blob_dir(blob):
return blob[:2], blob[2:]
def gen_hash(key):
return hashlib.sha1(key.encode(encoding="utf-8"))
def regen_hash(blob):
dirname, fname = gen_blob_dir(blob)
blobpath = gen_path(f".pygit/objects/{dirname}/{fname}")
if os.path.isfile(blobpath):
t = time.time()
return gen_hash(f"{blob} {t}").hexdigest()
else:
return blob
def gen_blob(content, blob):
dirname, filename = gen_blob_dir(blob)
dirpath = gen_path(f".pygit/objects/{dirname}")
filepath = gen_path(f".pygit/objects/{dirname}/{filename}")
os.makedirs(dirpath, exist_ok=True)
with open(filepath, "w") as f:
f.write(content)
def gen_index(fname, content):
indexpath = gen_path(f".pygit/index")
with open(indexpath, "r+") as f:
lines = f.readlines()
newlines = []
for line in lines:
if line != "\n" and fname not in line:
newlines.append(line)
newlines.append(f"{fname} {content}\n")
f.seek(0)
f.write("".join(newlines))
f.truncate()
def find_ref_path():
headpath = gen_path(".pygit/HEAD")
with open(headpath, "r") as f:
line = f.readline().replace("ref: ", "")
return line
def pprint(root):
print("------")
print(root.name, root.value)
print("Children")
print("".join([f"{child.name} {child.value}" for child in root.children]))
print("-----")
for child in root.children:
pprint(child)
###################################################
def add_helper(fnames):
fpath = gen_path(fnames)
if os.path.isfile(fpath):
with open(fpath, "r") as f:
lines = "".join(f.readlines())
blob = gen_hash(lines).hexdigest()
gen_blob(lines, blob)
gen_index(fnames, blob)
elif os.path.isdir(fpath):
for f in os.listdir(fpath):
add_helper(os.path.join(fnames, f))
def gen_tree_blob(blob):
dirname, filename = gen_blob_dir(blob)
dirpath = gen_path(f".pygit/objects/{dirname}")
filepath = gen_path(f".pygit/objects/{dirname}/{filename}")
os.makedirs(dirpath, exist_ok=True)
with open(filepath, "w") as f:
pass
def gen_tree_from_path(root, path, value):
paths = path.split("/")
for idx in range(len(paths) - 1, 0, -1):
child = PyGitTree(paths[idx], value.replace("\n", ""))
parent = PyGitTree(paths[idx - 1])
parent = root.find(parent) or parent
if parent.value == "":
treename = paths[idx - 1]
blob = gen_hash(f"tree {treename}").hexdigest()
blob = regen_hash(blob)
parent.value = blob
gen_tree_blob(blob)
parent.add_child(child)
return parent
def gen_commit_blob(message, root_blob):
blob = gen_hash(f"{message} {root_blob}").hexdigest()
dirname, filename = gen_blob_dir(blob)
gen_tree_blob(blob)
filepath = gen_path(f".pygit/objects/{dirname}/{filename}")
refpath = find_ref_path()
parent = ""
if refpath != "":
branchpath = gen_path(f".pygit/{refpath}")
if os.path.isfile(branchpath):
with open(branchpath, "r") as f:
line = f.readline()
parent = f"\nparent {line}"
with open(filepath, "w") as f:
content = f"tree {root_blob}"
content += parent
content += f" \n\n{message}"
f.write(content)
return blob
def gen_pygittree_history(root):
if root.children:
for child in root.children:
blob = root.value
dirname, filename = gen_blob_dir(blob)
with open(gen_path(f".pygit/objects/{dirname}/{filename}"), "a") as f:
ftype = "tree" if child.children else "blob"
f.write(f"{ftype} {child.value} {child.name}\n")
gen_pygittree_history(child)
def set_branch(commit_blob):
with open(gen_path(".pygit/HEAD"), "r") as f:
ref = f.read().replace("ref: ", "")
filepath = gen_path(f".pygit/{ref}")
with open(filepath, "w") as f:
f.write(commit_blob)
def commit_helper(message):
root = PyGitTree()
blob = gen_hash("tree root").hexdigest()
blob = regen_hash(blob)
root.value = blob
gen_tree_blob(blob)
with open(gen_path(".pygit/index"), "r") as f:
lines = f.readlines()
for line in lines:
path, value = line.split(" ")
child = gen_tree_from_path(root, path, value)
root.add_child(child)
gen_pygittree_history(root)
commit_blob = gen_commit_blob(message, root.value)
set_branch(commit_blob)
def commit(args):
message = args.message
commit_helper(message)
def add(args):
files = args.file
add_helper(files)
if __name__ == '__main__':
argparse = argparse.ArgumentParser()
subparsers = argparse.add_subparsers(help="Subcommand help")
initparser = subparsers.add_parser("init", help="Initialize a .pygit repo")
initparser.set_defaults(func=init)
addparser = subparsers.add_parser("add", help="Add file to be tracked")
addparser.add_argument('file')
addparser.set_defaults(func=add)
commitparser = subparsers.add_parser("commit", help="Stage changes")
commitparser.add_argument("-m", "--message", dest="message", help="Adding a commit message")
commitparser.set_defaults(func=commit)
arguments = argparse.parse_args()
arguments.func(arguments)