-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibwyag.py
500 lines (394 loc) · 14.8 KB
/
libwyag.py
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
argparser = argparse.ArgumentParser(description="The stupid content tracker")
argsubparsers = argparser.add_subparsers(title="Command", dest="command")
argsubparsers.required = True
argsp = argsubparsers.add_parser("init", help = "Initialize a new, empty repository")
argsp.add_argument("path", metavar="directory", nargs="?", default=".", help="where to create the repository")
argsp = argsubparsers.add_parser("cat-file", help="Provide content of repository objects")
argsp.add_argument("type", metavar="type",choices=["blob", "commit", "tag", "tree"], help="Specify the type")
argsp.add_argument("object", metavar="object",help="The object to display")
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>")
argsp = argsubparsers.add_parser("log", help="Display history of a given commit.")
argsp.add_argument("commit", default="HEAD", nargs="?", help="Commit to start at.")
argsp = argsubparsers.add_parser("ls-tree", help="Pretty-print a tree object.")
argsp.add_argument("-r",
dest="recursive",
action="store_true",
help="Recurse into sub-trees")
argsp.add_argument("tree",
help="A tree-ish object.")
def cmd_ls_tree(args):
repo = repo_find()
ls_tree(repo, args.tree, args.recursive)
def ls_tree(repo, ref, recursive=None, prefix=""):
sha = object_find(repo, ref, fmt=b"tree")
obj = object_read(repo, sha)
for item in obj.items:
if len(item.mode) == 5:
type = item.mode[0:1]
else:
type = item.mode[0:2]
match type: # Determine the type.
case b'04': type = "tree"
case b'10': type = "blob" # A regular file.
case b'12': type = "blob" # A symlink. Blob contents is link target.
case b'16': type = "commit" # A submodule
case _: raise Exception("Weird tree leaf mode {}".format(item.mode))
if not (recursive and type=='tree'): # This is a leaf
print("{0} {1} {2}\t{3}".format(
"0" * (6 - len(item.mode)) + item.mode.decode("ascii"),
type,
item.sha,
os.path.join(prefix, item.path)))
else: # This is a branch, recurse
ls_tree(repo, item.sha, recursive, os.path.join(prefix, item.path))
argsp = argsubparsers.add_parser("checkout", help="Checkout a commit inside of a directory.")
argsp.add_argument("commit", help="The commit or tree to checkout.")
argsp.add_argument("path", help="The EMPTY directory to checkout on.")
def cmd_checkout(args):
repo = repo_find()
obj = object_read(repo, object_find(repo, args.commit))
if obj.fmt == b'commit':
obj = object_read(repo, obj.kvlm[b'tree'].decode("ascii"))
if os.path.exists(args.path):
if not os.path.isdir(args.path):
raise Exception("Not a directory {0}!".format(args.path))
if os.listdir(args.path):
raise Exception("Not empty {0}!".format(args.path))
else:
os.makedirs(args.path)
tree_checkout(repo, obj, os.path.realpath(args.path))
def tree_checkout(repo, tree, path):
for item in tree.items:
obj = object_read(repo, item.sha)
dest = os.path.join(path, item.path)
if obj.fmt == b'tree':
os.mkdir(dest)
tree_checkout(repo, obj, dest)
elif obj.fmt == b'blob':
# @TODO Support symlinks (identified by mode 12****)
with open(dest, 'wb') as f:
f.write(obj.blobdata)
def cmd_log(args):
repo = repo_find()
print("digraph wyaglog{")
print(" node[shape=rect]")
log_graphviz(repo, object_find(repo, args.commit), set())
print("}")
def log_graphviz(repo, sha, seen):
if sha in seen:
return
seen.add(sha)
commit = object_read(repo, sha)
short_hash = sha[0:8]
message = commit.kvlm[None].decode("utf8").strip()
message = message.replace("\\", "\\\\")
message = message.replace("\"", "\\\"")
if "\n" in message: # Keep only the first line
message = message[:message.index("\n")]
print(" c_{0} [label=\"{1}: {2}\"]".format(sha, sha[0:7], message))
assert commit.fmt==b'commit'
if not b'parent' in commit.kvlm.keys():
# Base case: the initial commit.
return
parents = commit.kvlm[b'parent']
if type(parents) != list:
parents = [ parents ]
for p in parents:
p = p.decode("ascii")
print (" c_{0} -> c_{1};".format(sha, p))
log_graphviz(repo, p, seen)
def cmd_hash_object(args):
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 kvlm_parse(raw, start=0, dct=None):
if not dct:
dct = collections.OrderedDict()
spc = raw.find(b' '.start)
nl = raw.find(b'\n'.start)
if (spc < 0) or (nl < spc):
assert nl == start
dct[None] = raw [start+1:]
return dct
# recursive case
key = raw[start:spc]
end = start
while True:
end = raw.find(b'\n', end +1)
if raw[end+1] != ord(' '): break
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
return kvlm_parse(raw, start=end+1, dct=dct)
def kvlm_serialize(kvlm):
ret = b''
for k in kvlm.keys():
# Skip the message itself
if k == None: continue
val = kvlm[k]
# Normalize to a list
if type(val) != list:
val = [ val ]
for v in val:
ret += k + b' ' + (v.replace(b'\n', b'\n ')) + b'\n'
# Append message
ret += b'\n' + kvlm[None] + b'\n'
return ret
def object_hash(fd, fmt, repo=None):
""" Hash object, writing it to repo if provided."""
data = fd.read()
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 cmd_cat_file(args):
repo = repo_find()
cat_file(repo, args.object, fmt=args.type.encode())
def cat_file(repo, obj, fmt=None):
obj = object_read(repo, object_find(repo, obj, fmt=fmt))
sys.stdout.buffer.write(obj.serialize())
def object_find(repo, name, fmt=None, follow=True):
return name
def cmd_init(args):
repo_create(args.path)
def repo_find(path=".", required=True):
path = os.path.realpath(path)
if os.path.realpath(path):
return GitRepository(path)
parent = os.path.realpath(os.path.join(path,".."))
if parent == path:
if required:
raise Exception("NO git directory")
else:
return None
# recursive case
return repo_find(parent, required)
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
# .git/objects/ : the object store, which we’ll introduce in the next section.
# .git/refs/ the reference store, which we’ll discuss a bit later. It contains two subdirectories, heads and tags.
# .git/HEAD, a reference to the current HEAD (more on that later!)
# .git/config, the repository’s configuration file.
# .git/description, holds a free-form description of this repository’s contents, for humans, and is rarely used.
def repo_create(path):
"""Create a new repo at path"""
repo = GitRepository(path, True)
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, "object" , mkdir=True)
assert repo_dir(repo, "refs" ,"tags", mkdir=True)
assert repo_dir(repo, "refs" ,"heads", mkdir=True)
with open(repo_file(repo, "description"), "w") as f:
f.write("Unnamed repository; edit this file 'description' to name the repository.\n")
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_default_config():
ret = configparser.ConfigParser()
ret.add_section("core")
ret.set("core", "repositoryformatversion", "0")
ret.set("core", "filemode", "false")
ret.set("core", "filemode", "false")
return ret
def object_read(repo,sha):
path = repo_file(repo,"object", sha[0:2], sha[2:])
if not os.path.isfile(path):
return None
with open (path,"rb") as f:
raw = zlib.decompress(f.read())
x = raw.find(b' ')
fmt = raw[0:x]
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))
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))
return c(raw[y+1:])
def object_write(obj,repo=None):
data = obj.serialize()
result = obj.fmt + b' ' + str(len(data)).encode() + b'\x00'+ data
sha = hashlib.sha1(result).hexdigest
if repo:
path = repo_file(repo,"objects", sha[0:2], sha[2:], mkdir=True)
if not os.path.exists(path):
with open(path,'wb') as f:
f.write(zlib.compress(result))
return sha
def GitBlob(GitObject):
fmt=b'blob'
def serialize(self):
return self.blobdata
def deserialize(self,data):
self.blobdata = data
def tree_parse(raw):
pos = 0
max = len(raw)
ret = list()
while pos < max:
pos,data = tree_parse_one(raw, pos)
ret.append(data)
return ret
def tree_leaf_sort_key(leaf):
if leaf.mode.startswith(b"10"):
return leaf.path
else:
return leaf.path + "/"
def tree_parse_one(raw, start=0):
x = raw.find(b' ', start)
assert x-start == 5 or x-start==6
mode = raw[start:x]
if len(mode) == 5:
mode = b" " + mode
y = raw.find(b'\x00', x)
path = raw[x+1:y]
sha = format(int.from_bytes(raw[y+1:y+21], "big"), "040x")
return y+21, GitTreeLeaf(mode, path.decode("utf8"), sha)
class GitTreeLeaf (Object):
def __init__(self,mode , path, sha):
self.mode = mode
self.path = path
self.path = path
class GitTree(GitObject):
fmt=b'tree'
def deserialize(self, data):
self.items = tree_parse(data)
def serialize(self):
return tree_serialize(self)
def init(self):
self.items = list()
class GitRepository (object):
"""A git repository"""
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
self.cont = 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):
"""This function MUST be implemented by subclasses.
It must read the object's contents from self.data, a byte string, and do
whatever it takes to convert it into a meaningful representation. What exactly that means depend on each subclass."""
raise Exception("Unimplemented!")
def deserialize(self, data):
raise Exception("Unimplemented!")
def init(self):
pass # Just do nothing. This is a reasonable default!
class GitCommit(GitObject):
fmt=b'commit'
def deserialize(self, data):
self.kvlm = kvlm_parse(data)
def serialize(self):
return kvlm_serialize(self.kvlm)
def init(self):
self.kvlm = dict()
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 "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-tree":
cmd_ls_tree(args)
case "merge":
cmd_merge(args)
case "rebase":
cmd_rebase(args)
case "rev-parse":
cmd_rev_parse(args)
case "rm":
cmd_rm(args)
case "show-ref":
cmd_show_ref(args)
case "tag":
cmd_tag(args)