forked from Nginlion/git-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
98 lines (80 loc) · 3.46 KB
/
objects.py
File metadata and controls
98 lines (80 loc) · 3.46 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
'''
Created on Jun 13, 2014
@author: lzrak47
'''
import binascii
import os
import re
import stat
import zlib
from constants import OBJECT_DIR
from utils import cal_sha1, read_file
class BaseObject(object):
'''
git base object
'''
def __init__(self, final_content=None, sha1=None):
'''
Constructor
'''
if sha1:
self.sha1 = sha1
self.path = os.path.join(OBJECT_DIR, self.sha1[:2], self.sha1[2:])
self.content = read_file(self.path)
else:
self.content = zlib.compress(final_content)
self.sha1 = cal_sha1(final_content)
self.path = os.path.join(OBJECT_DIR, self.sha1[:2], self.sha1[2:])
class Blob(BaseObject):
def __init__(self, raw_content=None, sha1=None):
if sha1:
super(Blob, self).__init__(sha1=sha1)
final_content = zlib.decompress(self.content)
self.raw_content = final_content[final_content.find('\0') + 1:]
else:
final_content = 'blob %d\0%s' % (len(raw_content), raw_content)
super(Blob, self).__init__(final_content)
class Tree(BaseObject):
def __init__(self, args=None, sha1=None):
if sha1:
super(Tree, self).__init__(sha1=sha1)
final_content = zlib.decompress(self.content)
self.raw_content = final_content[final_content.find('\0') + 1:]
self.objects = re.findall('(\d+) ([^\0]+)\0(.{20})', self.raw_content, re.S)
else:
raw_content = ''
for arg in args:
raw_content += '%04o %s\0%s' % (arg['mode'], arg['name'], binascii.unhexlify(arg['sha1']))
final_content = 'tree %d\0%s' % (len(raw_content), raw_content)
super(Tree, self).__init__(final_content)
def parse_objects(self):
res = {}
queue = list(self.objects)
while queue:
object = queue.pop(0)
mode, name, sha1 = object[0], object[1], binascii.hexlify(object[2])
if stat.S_ISDIR(int(mode, 8)):
new_objects = Tree(sha1=sha1).objects
for new_object in new_objects:
queue.append([new_object[0], os.path.join(name, new_object[1]), new_object[2]])
else:
res[name] = {'mode' : int(mode, 8), 'sha1' : sha1}
return res
class Commit(BaseObject):
def __init__(self, **kwargs):
if kwargs['sha1']:
super(Commit, self).__init__(sha1=kwargs['sha1'])
final_content = zlib.decompress(self.content)
res = re.findall('parent (\w+)\n', final_content)
self.parent_sha1 = res[0] if res else None
self.raw_content = final_content[final_content.find('tree'):]
self.tree = re.findall('tree (\w+)\n', final_content)[0]
else:
raw_content = 'tree %s\n' % (kwargs['tree_sha1'])
if kwargs['parent_sha1']:
raw_content += 'parent %s\n' % (kwargs['parent_sha1'])
raw_content += 'author %s %s %s %s\ncommitter %s %s %s %s\n\n%s\n' \
% (kwargs['name'], kwargs['email'], kwargs['timestamp'], kwargs['timezone'] , \
kwargs['name'], kwargs['email'], kwargs['timestamp'], kwargs['timezone'] , kwargs['msg'])
final_content = 'commit %d\0%s' % (len(raw_content), raw_content)
super(Commit, self).__init__(final_content)