This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
commit.py
322 lines (260 loc) · 9.49 KB
/
commit.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
#!/usr/bin/python -tt
#
# Copyright 2009-2010 Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import datetime
import os
import time
import gitreview.proc as proc
from exceptions import *
import constants
import obj as git_obj
class GitTimezone(datetime.tzinfo):
"""
This class represents the timezone part of a git timestamp.
Timezones are represented as "-HHMM" or "+HHMM".
"""
def __init__(self, tz_str):
self.name = tz_str
tz = int(tz_str)
min_offset = tz % 100
hour_offset = tz / 100
self.offset = datetime.timedelta(hours = hour_offset,
minutes = min_offset)
def utcoffset(self, dt):
return self.offset
def dst(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return self.name
class AuthorInfo(object):
"""
An AuthorInfo object represents the committer or author information
associated with a commit. It contains a name, email address, and
timestamp.
"""
def __init__(self, real_name, email, timestamp):
self.realName = real_name
self.email = email
self.timestamp = timestamp
def __str__(self):
return '%s <%s> %s' % (self.realName, self.email, self.timestamp)
class Commit(git_obj.Object):
"""
This class represents a git commit.
Commit objects always contain fully parsed commit information.
"""
def __init__(self, repo, sha1, tree, parents, author, committer, comment):
git_obj.Object.__init__(self, repo, sha1, constants.OBJ_COMMIT)
self.tree = tree
self.parents = parents
self.author = author
self.committer = committer
self.comment = comment
def __str__(self):
return str(self.sha1)
def __eq__(self, other):
if isinstance(other, Commit):
# If other is a Commit object, compare the SHA1 hashes
return self.sha1 == other.sha1
elif isinstance(other, str):
# If other is a Commit string, it should be a SHA1 hash
# XXX: In the future, we could also check to see if the string
# is a ref name, and compare using that.
return self.sha1 == other
return False
def getSha1(self):
return self.sha1
def getTree(self):
return self.tree
def getParents(self):
return self.parents
def getAuthor(self):
return self.author
def getCommitter(self):
return self.committer
def getComment(self):
return self.comment
def getSummary(self):
return self.comment.split('\n', 1)[0]
def _parse_timestamp(value):
# Note: we may raise ValueError to the caller
(timestamp_str, tz_str) = value.split(' ', 1)
timestamp = int(timestamp_str)
tz = GitTimezone(tz_str)
return datetime.datetime.fromtimestamp(timestamp, tz)
def _parse_author(commit_name, value, type):
try:
(real_name, rest) = value.split(' <', 1)
except ValueError:
msg = 'error parsing %s: no email address found' % (type,)
raise BadCommitError(commit_name, msg)
try:
(email, rest) = rest.split('> ', 1)
except ValueError:
msg = 'error parsing %s: unterminated email address' % (type,)
raise BadCommitError(commit_name, msg)
try:
timestamp = _parse_timestamp(rest)
except ValueError:
msg = 'error parsing %s: malformatted timestamp' % (type,)
raise BadCommitError(commit_name, msg)
return AuthorInfo(real_name, email, timestamp)
def _parse_header(commit_name, header):
tree = None
parents = []
author = None
committer = None
# We accept the headers in any order.
# git itself requires them to be tree, parents, author, committer
for line in header.split('\n'):
try:
(name, value) = line.split(' ', 1)
except ValueError:
msg = 'bad commit header line %r' % (line)
raise BadCommitError(commit_name, msg)
if name == 'tree':
if tree:
msg = 'multiple trees specified'
raise BadCommitError(commit_name, msg)
tree = value
elif name == 'parent':
parents.append(value)
elif name == 'author':
if author:
msg = 'multiple authors specified'
raise BadCommitError(commit_name, msg)
author = _parse_author(commit_name, value, name)
elif name == 'committer':
if committer:
msg = 'multiple committers specified'
raise BadCommitError(commit_name, msg)
committer = _parse_author(commit_name, value, name)
else:
msg = 'unknown header field %r' % (name,)
raise BadCommitError(commit_name, msg)
if not tree:
msg = 'no tree specified'
raise BadCommitError(commit_name, msg)
if not author:
msg = 'no author specified'
raise BadCommitError(commit_name, msg)
if not committer:
msg = 'no committer specified'
raise BadCommitError(commit_name, msg)
return (tree, parents, author, committer)
def _get_current_tzinfo():
if time.daylight:
tz_sec = time.altzone
else:
tz_sec = time.daylight
tz_min = (abs(tz_sec) / 60) % 60
tz_hour = abs(tz_sec) / 3600
if tz_sec > 0:
tz_hour *= -1
tz_str = '%+02d%02d' % (tz_hour, tz_min)
return GitTimezone(tz_str)
def _get_bogus_author():
# we could use datetime.datetime.now(),
# but this way we don't get microseconds, so it looks more like a regular
# git timestamp
now = time.localtime()
current_tz = _get_current_tzinfo()
timestamp = datetime.datetime(now.tm_year, now.tm_mon, now.tm_mday,
now.tm_hour, now.tm_min, now.tm_sec, 0,
current_tz)
return AuthorInfo('No Author Yet', 'nobody@localhost', timestamp)
def get_index_commit(repo):
"""
get_index_commit(repo) --> commit
Get a fake Commit object representing the changes currently in the index.
"""
tree = os.path.join(repo.getGitDir(), 'index')
parents = [constants.COMMIT_HEAD]
author = _get_bogus_author()
committer = _get_bogus_author()
comment = 'Uncommitted changes in the index'
# XXX: it might be better to define a separate class for this
return Commit(repo, constants.COMMIT_INDEX, tree, parents,
author, committer, comment)
def get_working_dir_commit(repo):
"""
get_working_dir_commit(repo) --> commit
Get a fake Commit object representing the changes currently in the working
directory.
"""
tree = repo.getWorkingDir()
if not tree:
tree = '<none>'
parents = [constants.COMMIT_INDEX]
author = _get_bogus_author()
committer = _get_bogus_author()
comment = 'Uncomitted changes in the working directory'
# XXX: it might be better to define a separate class for this
return Commit(repo, constants.COMMIT_WD, tree, parents, author, committer,
comment)
def get_commit(repo, name):
# Handle the special internal commit names COMMIT_INDEX and COMMIT_WD
if name == constants.COMMIT_INDEX:
return get_index_commit(repo)
elif name == constants.COMMIT_WD:
return get_working_dir_commit(repo)
# Get the SHA1 value for this commit.
sha1 = repo.getCommitSha1(name)
# Run "git cat-file commit <name>"
cmd = ['cat-file', 'commit', str(name)]
out = repo.runSimpleGitCmd(cmd)
# Split the header and body
try:
(header, body) = out.split('\n\n', 1)
except ValueError:
# split() resulted in just one value
# Treat it as headers, with an empty body
header = out
if header and header[-1] == '\n':
header = header[:-1]
body = ''
# Parse the header
(tree, parents, author, committer) = _parse_header(name, header)
return Commit(repo, sha1, tree, parents, author, committer, body)
def split_rev_name(name):
"""
Split a revision name into a ref name and suffix.
The suffix starts at the first '^' or '~' character. These characters
may not be part of a ref name. See git-rev-parse(1) for full details.
For example:
split_ref_name('HEAD^^') --> ('HEAD', '^^')
split_ref_name('HEAD~10') --> ('HEAD', '~')
split_ref_name('master') --> ('master', '')
split_ref_name('master^{1}') --> ('master', '^{1}')
"""
# This command shouldn't be called with commit ranges.
if name.find('..') > 0:
raise BadRevisionNameError(name, 'specifies a commit range, '
'not a single commit')
caret_idx = name.find('^')
tilde_idx = name.find('~')
if caret_idx < 0:
if tilde_idx < 0:
# No suffix
return (name, '')
else:
idx = tilde_idx
else:
if tilde_idx < 0:
idx = caret_idx
else:
idx = min(caret_idx, tilde_idx)
return (name[:idx], name[idx:])