-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrgit.py
More file actions
1416 lines (1092 loc) · 43.1 KB
/
Copy pathlibrgit.py
File metadata and controls
1416 lines (1092 loc) · 43.1 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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Dependencies
os -> filesystem
sys -> access command-line arguments
argparse -> Rgit a CLI app, thus argparse is required to parse command line
arguments
collections -> for the extra containers
configparser -> for reading and writing config files
datetime -> for dates/time
fnmatch -> for matching filenames
grp -> Unix groups
pwd -> Unix users, for displaying author
hashlib -> for hashing
zlib -> compression
re -> regular expressions
math -> math functions
"""
from datetime import datetime
from fnmatch import fnmatch
import os, sys, argparse, collections, configparser, grp, pwd, hashlib, zlib, re
from math import ceil
"""RGIT INTERNALS"""
class RgitRepository(object):
'''git repo'''
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 Rgit repository %s" % path)
#Read config 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 GitIgnore(object):
absolute = None
scoped = None
def __init__(self, absolute, scoped):
self.absolute = absolute
self.scoped = scoped
class GitIndexEntry(object):
def __init__(self, ctime=None, mtime=None, dev=None, ino=None,
mode_type=None, mode_perms=None, uid=None, gid=None,
fsize=None, sha=None, flag_assume_valid=None, flag_stage=None, name=None):
#Last time file's metadata was changed: Pair of timestamp (seconds, nanoseconds)
self.ctime = ctime
#Last time file's data was changed: Pair of timestamp (seconds, nanoseconds)
self.mtime = mtime
#device Id containing file
self.dev = dev
#file inode numnber (number associated to file name unique in the file system)
self.ino = ino
#object type b1000 (regular), b1010 (symLink), b1110 (gitLink)
self.mode_type = mode_type
#object permissions (int)
self.mode_perms = mode_perms
#owner id user
self.uid = uid
#owner id group
self.gid = gid
#object size in bytes
self.fsize = fsize
#object sha
self.sha = sha
self.flag_assume_valid = flag_assume_valid
self.flag_stage = flag_stage
#object name (full path)
self.name = name
#begins with DIRC mahic bytes, version number and total number of entries
class GitIndex(object):
version = None
entries = []
#ext = None
#sha = None
def __init__(self, version=2, entries=None):
if not entries:
entries = list()
self.version = version
self.entries = entries
class GitObject(object):
def __init__(self, data=None):
if data != None:
self.deserialize(data)
else:
self.init()
def serialize(self, repo):
raise Exception("Must be called from subclasses for proper implementation")
def deserialize(self, repo):
raise Exception("Must be called from subclasses for proper implementation")
def init(self):
pass
def object_read(repo, sha):
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())
#Object type
x = raw.find(b' ')
format = 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))
#Constructor
match format:
case b'commit' : obj = GitCommit
case b'tree' : obj = GitTree
case b'tag' : obj=GitTag
case b'blob' : obj=GitBlob
case _ : raise Exception("Unknown type {0} for object {1}".format(format.decode("ascii"), sha))
return obj(raw[y+1:])
def object_write(obj, repo=None):
data = obj.serialize()
result = obj.format + 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
class GitBlob(GitObject):
format=b'blob'
def serialize(self):
return self.blobdata
def deserialize(self, data):
self.blobdata = data
class GitCommit(GitObject):
format = b'commit'
def deserialize(self, data):
self.keyvaluelist = keyvaluelist_parse(data)
def serialize(self):
return keyvaluelist_serialize(self.keyvaluelist)
def init(self):
self.keyvaluelist = dict()
""" TREE FORMAT
[mode] space [path] 0x00 [sha-1]
[mode] = up to 6 bytes => file mode in ascii
space = 0x20 ascii space
0x00 = null-terminated path
sha-1 = object's sha-1 in binary encoding on 20 bytes
"""
class GitTreeLeaf(object):
def __init__(self, mode, path, sha):
self.mode = mode
self.path = path
self.sha = sha
class GitTree(GitObject):
format = 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 GitTag(GitCommit):
format = b'tag'
#Path in repo's gitdir
def repo_path(repo, *path):
return os.path.join(repo.gitdir, *path)
#repo_path but creates dirname(*path) if absent
def repo_file(repo, *path, mkdir=False):
if repo_dir(repo, *path[:-1], mkdir=mkdir):
return repo_path(repo, *path)
#repo_path but if mkdir path is absent
def repo_dir(repo, *path, mkdir=False):
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
#makes new repo at path
def repo_create(path):
repo = RgitRepository(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, "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 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_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_find_root(path=".", required=True):
path = os.path.realpath(path)
if os.path.isdir(os.path.join(path, ".git")):
return RgitRepository(path)
parent = os.path.realpath(os.path.join(path, ".."))
if parent == path:
if required:
raise Exception("No git directory.")
else:
return None
return repo_find_root(parent, required)
def object_find(repo, name, format=None, follow = True):
sha = object_resolve(repo, name)
if not sha:
raise Exception("No such reference {0}.".format(name))
if len(sha) > 1:
raise Exception("Ambiguous reference {0}: Candidates are:\n -{1}".format(name, "\n - ".join(sha)))
sha = sha[0]
if not format:
return sha
while True:
obj = object_read(repo, sha)
if obj.format == format:
return sha
if not follow:
return None
if obj.format == b'tag':
sha = obj.keyvaluelist[b'object'].decode("ascii")
elif obj.format == b'commit' and format == b'tree':
sha = obj.keyvaluelist[b'tree'].decode("ascii")
else:
return None
def object_resolve(repo, name):
candidates = list()
hashRE = re.compile(r"[0-9A-Fa-f]{4,40}$")
if not name.strip(): #Abort if empty string
return None
if name == "HEAD":
return [ ref_resolve(repo, "HEAD") ]
if hashRE.match(name):
name = name.lower()
prefix = name[0:2]
path = repo_dir(repo, "objects", prefix, mkdir=False)
if path:
remainder = name[2:]
for file in os.listdir(path):
if file.startswith(remainder):
candidates.append(prefix + file)
as_tag = ref_resolve(repo, "refs/tags/" + name)
if as_tag:
candidates.append(as_tag)
as_branch = ref_resolve(repo, "refs/heads/" + name)
if as_branch:
candidates.append(as_branch)
return candidates
def cat_file(repo, object, format=None):
object = object_read(repo, object_find(repo, object, format=format))
sys.stdout.buffer.write(object.serialize())
def object_hash(file, format, repo=None):
data = file.read()
match format:
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!" % format)
return object_write(obj, repo)
def keyvaluelist_parse(raw, start=0, dct=None):
if not dct:
dct = collections.OrderedDict()
space = raw.find(b' ', start)
newline = raw.find(b'\n', start)
# Baseline case: Blank line | final message
# The remainder of the data is the message => stored in dictionary with None key and returned
if (space < 0) or (newline < space):
assert newline == start
dct[None] =raw[start+1:]
return dct
#key
key = raw[start:space]
#finding the end of value (continuation lines start with a space)
end = start
while True:
end = raw.find(b'\n', end+1)
if raw[end+1] != ord(' '): break
value = raw[space+1:end].replace(b'\n ', b'\n')
#Not overwriting existing content
if key in dct:
if type(dct[key]) == list:
dct[key].append(value)
else:
dct[key] = [ dct[key], value ]
else:
dct[key] = value
return keyvaluelist_parse(raw, start=end+1, dct=dct)
def keyvaluelist_serialize(keyvaluelist):
result = b''
for key in keyvaluelist.keys():
if key == None: continue
value = keyvaluelist[key]
if type(value) != list:
value = [ value ]
for v in value:
result += key + b' ' + (v.replace(b'\n', b'\n ')) + b'\n'
result += b'\n' + keyvaluelist[None] + b'\n'
return result
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.keyvaluelist[None].decode("utf8").strip()
message = message.replace("\\", "\\\\")
message = message.replace("\"", "\\\"")
# Print first line only
if "\n" in message:
message = message[:message.index("\n")]
print(" c_{0} [label=\"{1}: {2}\"]".format(sha, sha[0:7], message))
assert commit.format==b'commit'
#initial commit check
if not b'parent' in commit.keyvaluelist.keys():
return
parents = commit.keyvaluelist[b'parent']
if type(parents) != list:
parents = [ parents ]
for parent in parents:
parent = parent.decode("ascii")
print(" c_{0} -> c_{1};".format(sha, parent))
log_graphviz(repo, parent, seen)
def tree_parse_one(raw, start=0):
x = raw.find(b' ', start)
assert x-start == 5 or x-start==6 or x-start == 7
mode = raw[start:x]
if len(mode) == 5:
#Normalized to 6 bytes
mode = b" " + mode
y = raw.find(b'\x00', x) #null-terminator location
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)
def tree_parse(raw):
pos = 0
max = len(raw)
result = list()
while pos < max:
pos, data = tree_parse_one(raw, pos)
result.append(data)
return result
### Normal git tree sorting behaviour:
# thing (file) => thing.c => thing (directory) (as thing/)
# as can be seen in git source in tree.c
def tree_leaf_sort_key(leaf):
if leaf.mode.startswith(b"10"):
return leaf.path
else:
return leaf.path + "/"
def tree_serialize(obj):
obj.items.sort(key=tree_leaf_sort_key)
result = b''
for item in obj.items:
result +=item.mode
result +=b' '
result += item.path.encode("utf8")
result += b'\x00'
sha = int(item.sha, 16)
result+= sha.to_bytes(20, byteorder="big")
return result
def ls_tree(repo, ref, recursive=None, prefix=""):
sha = object_find(repo, ref, format=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:
case b'04': type = "tree"
case b'10': type = "blob" #normal file
case b'12': type = "blob" #a symlink. blob content is link target
case b'16': type = "commit" # a submodule
case _: raise Exception("Weird tree leaf mode {}".format(item.mode))
# Leaf
if not (recursive and type=='tree'):
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)))
#branch
else:
ls_tree(repo, item.sha, recursive, os.path.join(prefix, item.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.format == b"tree":
os.mkdir(dest)
tree_checkout(repo, obj, dest)
elif obj.format == b"blob":
with open(dest, 'wb') as file:
file.write(obj.blobdata)
def ref_resolve(repo, ref):
path = repo_file(repo, ref)
if not os.path.isfile(path):
return None
with open(path, 'r') as filepath:
data = filepath.read()[:-1]
if data.startswith("ref: "):
return ref_resolve(repo, data[5:])
else:
return data
def ref_list(repo, path=None):
if not path:
path = repo_dir(repo, "refs")
result = collections.OrderedDict()
# Git shows reds sorted
# For the same result rgit uses an OrderedDict and sorts the output
for f in sorted(os.listdir(path)):
can = os.path.join(path, f)
if os.path.isdir(can):
result[f] = ref_list(repo, can)
else:
result[f] = ref_resolve(repo, can)
return result
def show_ref(repo, refs, with_hash=True, prefix=""):
for key, value in refs.items():
if type(value) == str:
print("{0}{1}{2}".format(
value + " " if with_hash else "",
prefix + '/' if prefix else "",
key))
else:
show_ref(repo, value, with_hash=with_hash, prefix="{0}{1}{2}".format(prefix, "/" if prefix else "", key))
def tag_create(repo, name, ref, type):
sha = object_find(repo, ref)
if type == "object":
tag = GitTag(repo)
tag.keyvaluelist = collections.OrderedDict()
tag.keyvaluelist[b'object'] = sha.encode()
tag.keyvaluelist[b'type'] = b'commit'
tag.keyvaluelist[b'tag'] = name.encode()
tag.keyvaluelist[b'tagger'] = b'<name_placeholder> <email_placeholder>'
tag.keyvaluelist[None] = b"<tag_msg>"
tag_sha = object_write(tag)
ref_create(repo, "tags/" + name, tag_sha)
else:
ref_create(repo, "tags/" + name, sha)
def ref_create(repo, ref_name, sha):
with open(repo_file(repo, "refs/" + ref_name), 'w') as filepath:
filepath.write(sha + "\n")
def index_read(repo):
index_file = repo_file(repo, "index")
#Creating Index if it does not exist
if not os.path.exists(index_file):
return GitIndex()
with open(index_file, 'rb') as f:
raw = f.read()
header = raw[:12]
signature = header[:4]
assert signature == b"DIRC" # == DirCache
version = int.from_bytes(header[4:8], "big")
assert version == 2 ### ONLY VERSION 2 IS IMPLEMENTED
count = int.from_bytes(header[8:12], "big")
entries = list()
content = raw[12:]
idx = 0
for i in range(0, count):
ctime_s = int.from_bytes(content[idx: idx+4], "big")
ctime_ns = int.from_bytes(content[idx+4: idx+8], "big")
mtime_s = int.from_bytes(content[idx+8: idx+12], "big")
mtime_ns = int.from_bytes(content[idx+12: idx+16], "big")
dev = int.from_bytes(content[idx+16: idx+20], "big")
ino = int.from_bytes(content[idx+20: idx+24], "big")
#ignore padding
unused = int.from_bytes(content[idx+24: idx+26], "big")
assert 0 == unused
mode = int.from_bytes(content[idx+26: idx+28], "big")
mode_type = mode >> 12
assert mode_type in [0b1000, 0b1010, 0b1110]
mode_perms = mode & 0b0000000111111111
uid = int.from_bytes(content[idx+28: idx+32], "big")
gid = int.from_bytes(content[idx+32: idx+36], "big")
fsize = int.from_bytes(content[idx+36: idx+40], "big")
#sha stored in lowercase hex string
sha = format(int.from_bytes(content[idx+40: idx+60], "big"), "040x")
#flags to be ignored
flags = int.from_bytes(content[idx+60: idx+62], "big")
#parsing flags
flag_assume_valid = (flags & 0b1000000000000000) !=0
flag_extended = (flags & 0b0100000000000000) != 0
assert not flag_extended
flag_stage = flags & 0b0011000000000000
#length of name stored in 12 bits, max value is 0xFFFm 4095. Names can be larger
#so git treats 0xFFF meaning at least 0xFFF and lookks for the final 0x00 to find the end the name
name_length = flags & 0b0000111111111111
idx += 62 # 62 bytes have been read
if name_length < 0xFFF:
assert content[idx+ name_length] == 0x00
raw_name = content[idx:idx+name_length]
idx += name_length + 1
else:
print("Notice: Name is 0x{:X} bytes long.".format(name_length))
# There may be errors when size is larger than 0xFFF
null_idx = content.find('b\x00', idx + 0xFFF)
raw_name = content[idx: null_idx]
idx = null_idx + 1
name = raw_name.decode("utf8")
#data is padded by 8*N bytes for pointer alignment;
#skip the pad
idx = 8*ceil(idx/8)
#adding to entry list
entries.append(GitIndexEntry(ctime=(ctime_s, ctime_ns),
mtime=(mtime_s, mtime_ns),
dev=dev,
ino=ino,
mode_type=mode_type,
mode_perms=mode_perms,
uid=uid,
gid=gid,
fsize=fsize,
sha=sha,
flag_assume_valid = flag_assume_valid,
flag_stage=flag_stage,
name=name))
return GitIndex(version = version, entries = entries)
def gitignore_parse1(raw):
raw = raw.strip() #removing leading / trailing spaces
if not raw or raw[0] == "#": #comments are ignored
return None
elif raw[0] == "!": # ! are always included
return (raw[1:], False)
elif raw[0] =="\\": #\ treats # and ! as literal chars
return (raw[1:], True)
else:
return (raw, True)
def gitignore_parse(lines):
result = list()
for line in lines:
parsed = gitignore_parse1(line)
if parsed:
result.append(parsed)
return result
def gitignore_read(repo):
result = GitIgnore(absolute=list(), scoped=dict())
#Reading local config in .git/info/exclude
repo_file = os.path.join(repo.gitdir, "info/exclude")
if os.path.exists(repo_file):
with open(repo_file, "r") as file:
result.absolute.append(gitignore_parse(file.readlines()))
#Global config
if "XDG_CONFIG_HOME" in os.environ:
config_home = os.environ["XDG_CONFIG_HOME"]
else:
config_home = os.path.expanduser("~/.config")
global_file = os.path.join(config_home, "git/ignore")
if os.path.exists(global_file):
with open(ghlobal_file, "r") as file:
result.absolute.append(gitignore_parse(file.readlines()))
#.gitignore files in the index
index = index_read(repo)
for entry in index.entries:
if entry.name == ".gitignore" or entry.name.endswith("/.gitignore"):
dir_name = os.path.dirname(entry.name)
contents = object_read(repo, entry.sha)
lines = contents.blobdata.decode("utf8").splitlines()
result.scoped[dir_name] = gitignore_parse(lines)
return result
def check_ignore1(rules, path):
result = None
for (pattern, value) in rules:
if fnmatch(path, pattern):
result = value
return result
def check_ignore_scoped(rules, path):
parent = os.path.dirname(path)
while True:
if parent in rules:
result = check_ignore1(rules[parent], path)
if result != None:
return result
if parent == "":
break
parent = os.path.dirname(parent)
return None
def check_ignore_absolute(rules, path):
parent = os.path.dirname(path)
for ruleset in rules:
result = check_ignore1(ruleset, path)
if result != None:
return result
return False
def check_ignore(rules, path):
if os.path.isabs(path):
raise Exception("This function requires path to be relative to the repository's root")
result = check_ignore_scoped(rules.scoped, path)
if result != None:
return result
return check_ignore_absolute(rules.absolute, path)
def branch_get_active(repo):
with open(repo_file(repo, "HEAD"), "r") as file:
head = file.read()
if head.startswith("ref: refs/heads/"):
return(head[16:-1])
else:
return False
def tree_to_dict(repo, ref, prefix=""):
result = dict()
tree_sha = object_find(repo, ref, format=b"tree")
tree = object_read(repo, tree_sha)
for leaf in tree.items:
full_path = os.path.join(prefix, leaf.path)
#reading object to get type
is_subtree = leaf.mode.startswith(b'04')
if is_subtree: #recurse if subdir
result.update(tree_to_dict(repo, leaf.sha, full_path))
else: # store path if blob
result[full_path] = leaf.sha
return result
def index_write(repo, index):
with open(repo_file(repo, "index"), "wb") as file:
#HEADER
#magic bytes
file.write(b"DIRC")
#version number
file.write(index.version.to_bytes(4, "big"))
#number of entries
file.write(len(index.entries).to_bytes(4, "big"))
#Entries
idx = 0
for entry in index.entries:
file.write(entry.ctime[0].to_bytes(4, "big"))
file.write(entry.ctime[1].to_bytes(4, "big"))
file.write(entry.mtime[0].to_bytes(4, "big"))
file.write(entry.mtime[1].to_bytes(4, "big"))
file.write(entry.dev.to_bytes(4, "big"))
file.write(entry.ino.to_bytes(4, "big"))
#Mode
mode = (entry.mode_type << 12) | entry.mode_perms
file.write(mode.to_bytes(4, "big"))
file.write(entry.uid.to_bytes(4, "big"))
file.write(entry.gid.to_bytes(4, "big"))
file.write(entry.fsize.to_bytes(4, "big"))
file.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
# Merging the three parts into one (2 flags + length)
file.write((flag_assume_valid | entry.flag_stage | name_length).to_bytes(2, "big"))
file.write(name_bytes)
file.write((0).to_bytes(1, "big"))
idx += 62 + len(name_bytes) + 1
# Adding padding if necessary
if idx % 8 != 0:
pad = 8 - (idx % 8)
file.write((0).to_bytes(pad, "big"))
idx += pad
def rm(repo, paths, delete=True, skip_missing=False):
#find and read index
index = index_read(repo)
worktree = repo.worktree + os.sep
#making paths absolute
abspaths = list()
for path in paths:
abspath = os.path.abspath(path)
if abspath.startswith(worktree):
abspaths.append(abspath)
else:
raise Exception("Cannot remove paths outside of worktree: {}".format(paths))
kept_entries = list()
remove = list()
for entry in index.entries:
full_path = os.path.join(repo.worktree, entry.name)
if full_path in abspaths:
remove.append(full_path)
abspaths.remove(full_path)
else:
kept_entries.append(entry) #saving entry
if len(abspaths) > 0 and not skip_missing:
raise Exception("Cannot remove paths not in the index: {}".format(abspaths))
if delete:
for path in remove:
os.unlink(path)
index.entries = kept_entries
index_write(repo, index)
def add(repo, paths, delete=True, skip_missing=False):
#remove all paths from index
rm(repo, paths, delete=False, skip_missing=True)
worktree = repo.worktree + os.sep
#convert paths to paris: (absolute, relative_to_worktree)
#delte them form index if present
clean_paths = list()
for path in paths:
abspath = os.path.abspath(path)
if not (abspath.startswith(worktree) and os.path.isfile(abspath)):
raise Exception("Not a file, or outside the worktree: {}".format(paths))
relpath = os.path.relpath(abspath, repo.worktree)
clean_paths.append((abspath, relpath))
#find an read the index; it was modified by rm
index = index_read(repo)
for(abspath, relpath) in clean_paths:
with open(abspath, "rb") as fd:
sha = object_hash(fd, b"blob", repo)
stat = os.stat(abspath)
ctime_s = int(stat.st_ctime)
ctime_ns = stat.st_ctime_ns % 10**9
mtime_s = int(stat.st_mtime)
mtime_ns = stat.st_mtime_ns % 10**9
entry = GitIndexEntry(ctime=(ctime_s, ctime_ns), mtime=(mtime_s, mtime_ns), dev=stat.st_dev, ino=stat.st_ino,
mode_type=0b1000, mode_perms=0o644, uid=stat.st_uid, gid=stat.st_gid,
fsize=stat.st_size, sha=sha, flag_assume_valid=False,
flag_stage=False, name=relpath)
index.entries.append(entry)
#writing index back
index_write(repo, index)
def gitconfig_read():
xdg_config_home = os.environ["XDG_CONFIG_HOME"] if "XDG_CONFIG_HOME" in os.environ else "~/.config"
configfiles = [
os.path.expanduser(os.path.join(xdg_config_home, "git/config")),
os.path.expanduser("~/.gitconfig")
]
config = configparser.ConfigParser()
config.read(configfiles)
return config
def gitconfig_user_get(config):
if "user" in config:
if "name" in config["user"] and "email" in config["user"]:
return "{} <{}>".format(config["user"]["name"], config["user"]["email"])
return "Unknown User"
def tree_from_index(repo, index):
contents = dict()
contents[""] = list()
#Enumerate entries, and making them dictionaries ( key = directory, content = values)
for entry in index.entries:
dirname = os.path.dirname(entry.name)
key = dirname