-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathobjects.py
More file actions
72 lines (40 loc) · 1.69 KB
/
objects.py
File metadata and controls
72 lines (40 loc) · 1.69 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
'''
Created on Jun 13, 2014
@author: lzrak47
'''
import binascii
import os
import zlib
from utils import cal_sha1
class BaseObject(object):
'''
git base object
'''
def __init__(self, workspace, content):
'''
Constructor
'''
self.content = zlib.compress(content)
self.sha1 = cal_sha1(content)
self.path = os.path.join(workspace, '.git', 'objects', self.sha1[:2], self.sha1[2:])
class Blob(BaseObject):
def __init__(self, workspace, content):
real_content = 'blob %d\0%s' % (len(content), content)
super(Blob, self).__init__(workspace, real_content)
class Tree(BaseObject):
def __init__(self, workspace, args):
content = ''
for arg in args:
content += '%04o %s\0%s' % (arg['mode'], arg['name'], binascii.unhexlify(arg['sha1']))
real_content = 'tree %d\0%s' % (len(content), content)
super(Tree, self).__init__(workspace, real_content)
class Commit(BaseObject):
def __init__(self, workspace, **kwargs):
content = 'tree %s\n' % (kwargs['tree_sha1'])
if kwargs['parent_sha1']:
content += 'parent %s\n' % (kwargs['parent_sha1'])
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'])
real_content = 'commit %d\0%s' % (len(content), content)
super(Commit, self).__init__(workspace, real_content)