forked from gabrycina/git-python-rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibtft.py
More file actions
435 lines (331 loc) · 13 KB
/
libtft.py
File metadata and controls
435 lines (331 loc) · 13 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import argparse
import collections
import configparser
from datetime import datetime
import grp, pwd
from fnmatch import fnmatch
import hashlib
from math import ceil
import os
import re
import sys
import zlib
from pathlib import Path
argparser = argparse.ArgumentParser(description="The stupidest version control")
argsubparsers = argparser.add_subparsers(title="Commands", dest="command")
argsubparsers.required = True
#subparser for init
argsp = argsubparsers.add_parser("init", help="Initialize a new empty tft repository.")
argsp.add_argument("path", metavar="directory", nargs="?", default=".", help="Where to create the repository.")
#subparser for hash-object
argsp = argsubparsers.add_parser("hash-object", help="Compute object ID and optionally creates a blob from a file")
argsp.add_argument("-t", metavar="type", dest="type", choices=["blob", "commit", "tag", "tree"], default="blob", help="Specify the type")
argsp.add_argument("-w", dest="write", action="store_true", help="Actually write the object into the database")
argsp.add_argument("path", help="Read object from <file>")
def main(argv=sys.argv[1:]):
args = argparser.parse_args(argv)
match args.command:
case "add" : cmd_add(args)
case "cat-file" : cmd_cat_file(args)
case "check-ignore" : cmd_check_ignore(args)
case "checkout" : cmd_checkout(args)
case "commit" : cmd_commit(args)
case "hash-object" : cmd_hash_object(args)
case "init" : cmd_init(args)
case "log" : cmd_log(args)
case "ls-files" : cmd_ls_files(args)
case "ls-tree" : cmd_ls_tree(args)
case "rev-parse" : cmd_rev_parse(args)
case "rm" : cmd_rm(args)
case "show-ref" : cmd_show_ref(args)
case "status" : cmd_status(args)
case "tag" : cmd_tag(args)
case _ : print("Bad command.")\
class GitRepository(object):
worktree = None
gitdir = None
conf = None
def __init__(self, path, force=False):
self.worktree = path
self.gitdir = os.path.join(path, ".git")
if not (force or os.path.isdir(self.gitdir)):
raise Exception("Not a Git repository %s" % path)
# Read configuration file in .git/config
self.conf = configparser.ConfigParser()
cf = repo_file(self, "config")
if cf and os.path.exists(cf):
self.conf.read([cf])
elif not force:
raise Exception("Configuration file missing")
if not force:
vers = int(self.conf.get("core", "repositoryformatversion"))
if vers != 0:
raise Exception("Unsupported repositoryformatversion %s" % vers)
class GitObject (object):
def __init__(self, data=None):
if data != None:
self.deserialize(data)
else:
self.init()
def serialize(self, repo):
raise Exception("Unimplemented!")
def deserialize(self, data):
raise Exception("Unimplemented!")
def init(self):
pass
class GitCommit(GitObject):
# Specify the object format as 'commit'
fmt = b'commit'
def __init__(self):
# Initialize the commit object with an empty key-value list map (KVL)
self.kvlm = {}
def read_data(self, data):
"""Deserialize the data into a key-value list map (KVL)."""
self.kvlm = kvlm_parse(data)
def write_data(self):
"""Serialize the commit's key-value list map (KVL) back into bytes."""
return kvlm_serialize(self.kvlm)
class GitBlob(GitObject):
# Blob format type
fmt = b'blob'
def serialize(self):
"""Returns the blob data."""
return self.blobdata
def deserialize(self, data):
"""Stores the data in the blob."""
self.blobdata = data
def repo_path(repo, *path):
"""Compute path under repo's gitdir."""
return os.path.join(repo.gitdir, *path)
def repo_file(repo, *path, mkdir=False):
"""Same as repo_path, but create dirname(*path) if absent. For
example, repo_file(r, \"refs\", \"remotes\", \"origin\", \"HEAD\") will create
.git/refs/remotes/origin."""
if repo_dir(repo, *path[:-1], mkdir=mkdir):
return repo_path(repo, *path)
def repo_dir(repo, *path, mkdir=False):
"""Same as repo_path, but mkdir *path if absent if mkdir."""
path = repo_path(repo, *path)
if os.path.exists(path):
if (os.path.isdir(path)):
return path
else:
raise Exception("Not a directory %s" % path)
if mkdir:
os.makedirs(path)
return path
else:
return None
def repo_default_config():
ret = configparser.ConfigParser()
ret.add_section("core")
ret.set("core", "repositoryformatversion", "0")
ret.set("core", "filemode", "false")
ret.set("core", "bare", "false")
return ret
def repo_create(path):
"""Create a new repository at path."""
repo = GitRepository(path, True)
# First, we make sure the path either doesn't exist or is an
# empty dir.
if os.path.exists(repo.worktree):
if not os.path.isdir(repo.worktree):
raise Exception ("%s is not a directory!" % path)
if os.path.exists(repo.gitdir) and os.listdir(repo.gitdir):
raise Exception("%s is not empty!" % path)
else:
os.makedirs(repo.worktree)
assert repo_dir(repo, "branches", mkdir=True)
assert repo_dir(repo, "objects", mkdir=True)
assert repo_dir(repo, "refs", "tags", mkdir=True)
assert repo_dir(repo, "refs", "heads", mkdir=True)
# .git/description
with open(repo_file(repo, "description"), "w") as f:
f.write("Unnamed repository; edit this file 'description' to name the repository.\n")
# .git/HEAD
with open(repo_file(repo, "HEAD"), "w") as f:
f.write("ref: refs/heads/master\n")
with open(repo_file(repo, "config"), "w") as f:
config = repo_default_config()
config.write(f)
return repo
def repo_find(path=".", required=True):
""""
Finds the root of the current repository.
"""
#gets the real path resolving symlinks
path = Path(path).resolve()
#check if the path contains the .git directory
path_to_check = path.joinpath(".git")
if path_to_check.is_dir():
return GitObject(path)
#if it doesn't try to get the parent directory of path
parent = path.joinpath("..").resolve()
#if parent directory corresponds to the path it means we've reached the base directory. Git repository isn't found
if parent == path:
if required:
raise Exception("No tft Repository found.")
else:
return None
#otherwise we'll do this again with the parent directory
return repo_find(parent, required)
def object_read(repo, sha):
#read file .git/objects where first two are the directory name, the rest as the file name
path = repo_file(repo, "objects", sha[0:2], sha[2:])
if not os.path.isfile(path):
return None
with open (path, "rb") as f:
raw = zlib.decompress(f.read())
# Read object type "commit", "tree", "blob", "tag"
x = raw.find(b' ')
fmt = raw[0:x]
# Read and validate object size
y = raw.find(b'\x00', x)
size = int(raw[x:y].decode("ascii"))
if size != len(raw)-y-1:
raise Exception("Malformed object {0}: bad length".format(sha))
# Pick the correct constructor depending on the type read above
match fmt:
case b'commit' : c=GitCommit
case b'tree' : c=GitTree
case b'tag' : c=GitTag
case b'blob' : c=GitBlob
case _:
raise Exception("Unknown type {0} for object {1}".format(fmt.decode("ascii"), sha))
# Construct and return an instance of the corresponding Git object type
return c(raw[y+1:])
def object_hash(fd, fmt, repo=None):
"""Hash object, writing it to repo if provided."""
data = fd.read()
# Choose constructor according to fmt argument
match fmt:
case b'commit' : obj=GitCommit(data)
case b'tree' : obj=GitTree(data)
case b'tag' : obj=GitTag(data)
case b'blob' : obj=GitBlob(data)
case _: raise Exception("Unknown type %s!" % fmt)
return object_write(obj, repo)
def object_write(obj, repo=None):
# Serialize object data
data = obj.serialize()
# Add header to serialized data
result = obj.fmt + b' ' + str(len(data)).encode() + b'\x00' + data
# Compute hash
sha = hashlib.sha1(result).hexdigest()
if repo:
# Compute path
path=repo_file(repo, "objects", sha[0:2], sha[2:], mkdir=True)
#Extra check before writing
if not os.path.exists(path):
with open(path, 'wb') as f:
# Compress and write
f.write(zlib.compress(result))
return sha
def object_find(repo, name, fmt=None, follow=True):
"""Just temporary, will implement this fully soon"""
return name
def kvlm_parse(raw, start=0, dct=None):
# dct initialization
if not dct:
dct = collections.OrderedDict()
# Find the next space and the next newline
spc = raw.find(b' ', start)
nl = raw.find(b'\n', start)
# BASE CASE : newline appears before a space or there is no space
if (spc < 0) or (nl < spc):
assert nl == start
dct[None] = raw[start+1:]
return dct
# RECURSIVE CASE : we read a key-value pair and then recurse for the next
key = raw[start:spc]
# Find the end of the value
end = start
while True:
end = raw.find(b'\n', end+1)
if raw[end+1] != ord(' '):
break
# Grab the value and drop the leading space on continuation lines
value = raw[spc+1:end].replace(b'\n ', b'\n')
# Don't overwrite existing data contents
if key in dct:
if type(dct[key]) == list:
dct[key].append(value)
else:
dct[key] = [ dct[key], value ]
else:
dct[key]=value
# Recursive call to parse the rest of the data
return kvlm_parse(raw, start=end+1, dct=dct)
def kvlm_serialize(kvlm):
res = b''
# Output fields
for key in kvlm.keys():
# Skip the message itself
if key == None: continue
val = kvlm[key]
# Normalize to a list
if type(val) != list:
val = [ val ]
# Serialize each value
for v in val:
res += key + b' ' + (v.replace(b'\n', b'\n ')) + b'\n'
# Append message
res += b'\n' + kvlm[None] + b'\n'
return res
def cat_file(repo, obj, fmt=None):
obj = object_read(repo, object_find(repo, obj, fmt=fmt))
sys.stdout.buffer.write(obj.serialize())
#Bride functions
def cmd_init(args):
"""Bridge function to initialize a new repository."""
repo_create(args.path)
def cmd_hash_object(args):
"""Bridge function to compute the hash-name of object and optionally create the blob"""
if args.write:
repo = repo_find()
else:
repo = None
with open(args.path, "rb") as fd:
sha = object_hash(fd, args.type.encode(), repo)
print(sha)
def index_write(repo, index):
# refer to "8.2 Parsing the index" for info about the format
with open(repo_file(repo, "index"), "wb") as f:
# header
f.write(b"DIRC")
f.write(index.version.to_bytes(4, "big"))
f.write(len(index.entries).to_bytes(4, "big"))
# entries
idx = 0;
for entry in index.entries:
f.write(entry.ctime[0].to_bytes(4, "big"))
f.write(entry.ctime[1].to_bytes(4, "big"))
f.write(entry.mtime[0].to_bytes(4, "big"))
f.write(entry.mtime[1].to_bytes(4, "big"))
f.write(entry.dev.to_bytes(4, "big"))
f.write(entry.ino.to_bytes(4, "big"))
mode = (entry.mode_type << 12) | e.mode_perms
f.write(mode.to_bytes(4, "big"))
f.write(entry.uid.to_bytes(4, "big"))
f.write(entry.gid.to_bytes(4, "big"))
f.write(entry.fsize.to_bytes(4, "big"))
f.write(int(entry.sha, 16).to_bytes(20, "big"))
flag_assume_valid = 0x1 << 15 if entry.flag_assume_valid else 0
name_bytes = entry.name.encode("utf8")
bytes_len = len(name_bytes)
if bytes_len >= 0xFFF:
name_length = 0xFFF
else:
name_length = bytes_len
# We merge back three pieces of data (two flags and the
# length of the name) on the same two bytes.
f.write((flag_assume_valid | entry.flag_stage | name_length).to_bytes(2, "big"))
f.write(name_bytes)
f.write((0).to_bytes(1, "big"))
# header length + filename length + final 0x00
idx += 62 + len(name_bytes) + 1
# Add padding if necessary (padded in multiple of 8 bytes)
if idx % 8 != 0:
pad = 8 - (idx % 8)
f.write((0).to_bytes(pad, "big"))
idx += pad