- Notifications
You must be signed in to change notification settings - Fork68
Expand file tree
/
Copy pathcommit.py
More file actions
259 lines (222 loc) · 8.15 KB
/
commit.py
File metadata and controls
259 lines (222 loc) · 8.15 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
# commit.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
fromgitdb.typimportObjectType
fromtreeimportTree
fromcStringIOimportStringIO
importbase
fromgitdb.utilimport (
hex_to_bin,
Actor,
)
fromutilimport (
Traversable,
Serializable,
altz_to_utctz_str,
parse_actor_and_date
)
importsys
__all__= ('Commit', )
classCommit(base.Object,Traversable,Serializable):
"""Wraps a git Commit object.
This class will act lazily on some of its attributes and will query the
value on demand only if it involves calling the git binary."""
# ENVIRONMENT VARIABLES
# read when creating new commits
env_author_date="GIT_AUTHOR_DATE"
env_committer_date="GIT_COMMITTER_DATE"
# CONFIGURATION KEYS
conf_encoding='i18n.commitencoding'
# INVARIANTS
default_encoding="UTF-8"
# object configuration
type=ObjectType.commit
type_id=ObjectType.commit_id
__slots__= ("tree",
"author","authored_date","author_tz_offset",
"committer","committed_date","committer_tz_offset",
"message","parents","encoding")
_id_attribute_="binsha"
def__init__(self,odb,binsha,tree=None,author=None,authored_date=None,author_tz_offset=None,
committer=None,committed_date=None,committer_tz_offset=None,
message=None,parents=None,encoding=None):
"""Instantiate a new Commit. All keyword arguments taking None as default will
be implicitly set on first query.
:param binsha: 20 byte sha1
:param parents: tuple( Commit, ... )
is a tuple of commit ids or actual Commits
:param tree: Tree
Tree object
:param author: Actor
is the author string ( will be implicitly converted into an Actor object )
:param authored_date: int_seconds_since_epoch
is the authored DateTime - use time.gmtime() to convert it into a
different format
:param author_tz_offset: int_seconds_west_of_utc
is the timezone that the authored_date is in
:param committer: Actor
is the committer string
:param committed_date: int_seconds_since_epoch
is the committed DateTime - use time.gmtime() to convert it into a
different format
:param committer_tz_offset: int_seconds_west_of_utc
is the timezone that the authored_date is in
:param message: string
is the commit message
:param encoding: string
encoding of the message, defaults to UTF-8
:param parents:
List or tuple of Commit objects which are our parent(s) in the commit
dependency graph
:return: git.Commit
:note: Timezone information is in the same format and in the same sign
as what time.altzone returns. The sign is inverted compared to git's
UTC timezone."""
super(Commit,self).__init__(odb,binsha)
iftreeisnotNone:
assertisinstance(tree,Tree),"Tree needs to be a Tree instance, was %s"%type(tree)
iftreeisnotNone:
self.tree=tree
ifauthorisnotNone:
self.author=author
ifauthored_dateisnotNone:
self.authored_date=authored_date
ifauthor_tz_offsetisnotNone:
self.author_tz_offset=author_tz_offset
ifcommitterisnotNone:
self.committer=committer
ifcommitted_dateisnotNone:
self.committed_date=committed_date
ifcommitter_tz_offsetisnotNone:
self.committer_tz_offset=committer_tz_offset
ifmessageisnotNone:
self.message=message
ifparentsisnotNone:
self.parents=parents
ifencodingisnotNone:
self.encoding=encoding
@classmethod
def_get_intermediate_items(cls,commit):
returncommit.parents
def_set_cache_(self,attr):
ifattrinCommit.__slots__:
# read the data in a chunk, its faster - then provide a file wrapper
binsha,typename,self.size,stream=self.odb.stream(self.binsha)
self._deserialize(StringIO(stream.read()))
else:
super(Commit,self)._set_cache_(attr)
# END handle attrs
@property
defsummary(self):
""":return: First line of the commit message"""
returnself.message.split('\n',1)[0]
@classmethod
def_iter_from_process_or_stream(cls,odb,proc_or_stream):
"""Parse out commit information into a list of Commit objects
We expect one-line per commit, and parse the actual commit information directly
from our lighting fast object database
:param proc: git-rev-list process instance - one sha per line
:return: iterator returning Commit objects"""
stream=proc_or_stream
ifnothasattr(stream,'readline'):
stream=proc_or_stream.stdout
readline=stream.readline
whileTrue:
line=readline()
ifnotline:
break
hexsha=line.strip()
iflen(hexsha)>40:
# split additional information, as returned by bisect for instance
hexsha,rest=line.split(None,1)
# END handle extra info
assertlen(hexsha)==40,"Invalid line: %s"%hexsha
yieldcls(odb,hex_to_bin(hexsha))
# END for each line in stream
#{ Serializable Implementation
def_serialize(self,stream):
write=stream.write
write("tree %s\n"%self.tree)
forpinself.parents:
write("parent %s\n"%p)
a=self.author
aname=a.name
ifisinstance(aname,unicode):
aname=aname.encode(self.encoding)
# END handle unicode in name
c=self.committer
fmt="%s %s <%s> %s %s\n"
write(fmt% ("author",aname,a.email,
self.authored_date,
altz_to_utctz_str(self.author_tz_offset)))
# encode committer
aname=c.name
ifisinstance(aname,unicode):
aname=aname.encode(self.encoding)
# END handle unicode in name
write(fmt% ("committer",aname,c.email,
self.committed_date,
altz_to_utctz_str(self.committer_tz_offset)))
ifself.encoding!=self.default_encoding:
write("encoding %s\n"%self.encoding)
write("\n")
# write plain bytes, be sure its encoded according to our encoding
ifisinstance(self.message,unicode):
write(self.message.encode(self.encoding))
else:
write(self.message)
# END handle encoding
returnself
def_deserialize(self,stream):
""":param from_rev_list: if true, the stream format is coming from the rev-list command
Otherwise it is assumed to be a plain data stream from our object"""
readline=stream.readline
self.tree=Tree(self.odb,hex_to_bin(readline().split()[1]),Tree.tree_id<<12,'')
self.parents=list()
next_line=None
whileTrue:
parent_line=readline()
ifnotparent_line.startswith('parent'):
next_line=parent_line
break
# END abort reading parents
self.parents.append(type(self)(self.odb,hex_to_bin(parent_line.split()[-1])))
# END for each parent line
self.parents=tuple(self.parents)
self.author,self.authored_date,self.author_tz_offset=parse_actor_and_date(next_line)
self.committer,self.committed_date,self.committer_tz_offset=parse_actor_and_date(readline())
# now we can have the encoding line, or an empty line followed by the optional
# message.
self.encoding=self.default_encoding
# read encoding or empty line to separate message
enc=readline()
enc=enc.strip()
ifenc:
self.encoding=enc[enc.find(' ')+1:]
# now comes the message separator
readline()
# END handle encoding
# decode the authors name
try:
self.author.name=self.author.name.decode(self.encoding)
exceptUnicodeDecodeError:
print>>sys.stderr,"Failed to decode author name '%s' using encoding %s"% (self.author.name,self.encoding)
# END handle author's encoding
# decode committer name
try:
self.committer.name=self.committer.name.decode(self.encoding)
exceptUnicodeDecodeError:
print>>sys.stderr,"Failed to decode committer name '%s' using encoding %s"% (self.committer.name,self.encoding)
# END handle author's encoding
# a stream from our data simply gives us the plain message
# The end of our message stream is marked with a newline that we strip
self.message=stream.read()
try:
self.message=self.message.decode(self.encoding)
exceptUnicodeDecodeError:
print>>sys.stderr,"Failed to decode message '%s' using encoding %s"% (self.message,self.encoding)
# END exception handling
returnself
#} END serializable implementation