-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreviewer.py
399 lines (320 loc) · 12 KB
/
reviewer.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
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
import difflib
import enum
import logging
import re
import subprocess
import sys
logger = logging.getLogger('rom.reviewer')
class LineType(enum.Enum):
GITDIFF = 'diff --git '
INDEX = 'index '
DELETED = 'deleted '
ADDED = 'new file mode '
FILE_OLD = '--- (?:a/)?(.*)'
FILE_NEW = '\+\+\+ (?:b/)?(.*)'
CHUNK = '@@ -?([0-9]+),?([0-9]+)? \+?([0-9]+),?([0-9]+)? @@(.*)'
DIFF = '[+-]'
SIMILARITY = 'similarity index ([0-9]+)%'
RENAME = 'rename (from|to) (.*)'
CONTEXT = ' '
EMPTY = ''
class CallType(enum.Enum):
CHECK_OUTPUT = 0
CHECK_CALL = 1
CALL = 2
class CommitRef(object):
def __init__(self, sha, remote=None, branch=None, tag=None):
self.sha = sha
self.set_remote(remote)
self.branch = branch
self.tag = tag
def set_remote(self, remote):
self.remote = remote
if self.remote:
self.remote_name = re.sub('([a-z]*\://)|\W', '', self.remote, flags=re.I)
else:
self.remote_name = None
def refs(self, use_remote=False):
if use_remote and self.remote_name and self.branch:
return '{}/{}'.format(self.remote_name, self.branch)
if self.branch:
return 'refs/heads/{}'.format(self.branch)
if self.tag:
return 'refs/tags/{}'.format(self.tag)
return None
def __str__(self):
ret = 'sha={}'.format(self.sha[:12])
if self.branch:
ret += ' branch={}'.format(self.branch)
if self.tag:
ret += ' tag={}'.format(self.tag)
if self.remote:
ret += ' remote={}'.format(self.remote)
return ret
def __repr__(self):
return self.__str__()
@staticmethod
def refs_from_patch(patch):
# Helper pattern to match any character that isn't whitespace or )
non_ws = '[^\)^\s]'
# This has pattern has gotten a bit hard to parse, so i'll do my best.
# Start with an opening paren and allow for whitespace
pattern = '\((?:\s*)'
# This is pretty easy, look the "cherry picked from commit" string taking
# into account space or dash between cherry and picked, and allowing for
# multiple spaces after commit.
pattern += 'cherry.picked from commit\s*'
# Then we grab the hexidecimal hash string. Everything after this is
# optional.
pattern += '([0-9a-f]*)'
# Wrap the optional group in parens
pattern += '('
# Optionally gobble up everything (including newlines) until we hit the next
# group. This allows for extra fluff in between the hash and URL (like an
# extra 'from' as seen in http://crosreview.com/1537900). Instead of just .*
# explicitly forbid matching ) since it could go looking through the source
# for a URL (like in http://crosreview.com/1544916). Finally, use a
# non-greedy algorithm to avoid gobbling the URL.
pattern += '[^\)]*?'
# This will match the remote url. It's pretty simple, just matches any
# protocol (git://, http://, https://, madeup://) and then match all
# non-whitespace characters, this is our remote.
pattern += '([a-z]*\://{nws}*)'.format(nws=non_ws)
# Now that we have the URL, eat any whitespace again
pattern += '\s*'
# The next run of non-whitespace characters could either be 'tag' or the
# remote branch. Make it optional in case they want to use the default
# remote branch
pattern += '({nws}*)?'.format(nws=non_ws)
# Eat more whitespace, yum!
pattern += '\s*'
# Finally, if this is a tag, parse the tag name
pattern += '({nws}*)?'.format(nws=non_ws)
# Close the optional paren around remote/branch
pattern += ')?'
# Finally, account for any trailing whitespace
pattern += '\s*'
# Close the paren
pattern += '\)'
regex = re.compile(pattern, flags=(re.I | re.MULTILINE | re.DOTALL))
m = regex.findall(patch)
if not m or not len(m):
return None
ret = []
for s in m:
if s[3] == 'tag' and s[4]:
ret.append(CommitRef(sha=s[0], remote=s[2], tag=s[4]))
else:
ret.append(CommitRef(sha=s[0], remote=s[2], branch=s[3]))
return ret
@staticmethod
def links_from_patch(patch):
pattern = '\s*Link:\s+(\S+)'
regex = re.compile(pattern)
m = regex.findall(patch)
if not m or not len(m):
return None
ret = []
for s in m:
ret.append(s)
return ret
class Reviewer(object):
MAX_CONTEXT = 5
def __init__(self, verbose=False, chatty=False, git_dir=None):
self.verbose = verbose
self.chatty = chatty
self.git_dir = git_dir
if git_dir:
self.git_cmd = ['git', '-C', git_dir ]
else:
self.git_cmd = ['git']
def __strip_commit_msg(self, patch):
regex = re.compile('diff --git ')
for i, l in enumerate(patch):
if regex.match(l):
return patch[i:]
def classify_line(self, line):
for t in LineType:
m = re.match(t.value, line)
if m:
return (t,m)
return None
def __strip_kruft(self, diff, context):
ret = []
ignore = [LineType.CHUNK, LineType.GITDIFF, LineType.INDEX,
LineType.DELETED, LineType.ADDED, LineType.SIMILARITY,
LineType.RENAME, LineType.EMPTY]
include = [LineType.FILE_NEW, LineType.FILE_OLD, LineType.DIFF]
ctx_buffer = []
for l in diff:
if not l:
continue
l_type,_ = self.classify_line(l)
if self.chatty:
logger.debug('%s- "%s"' % (l_type, l))
if not l_type:
logger.error('Could not classify line "{}"'.format(l))
ctx_buffer = []
continue
if l_type == LineType.CONTEXT:
ctx_buffer.append(l)
continue
# If we want to include context in the diffs, include the last N lines of
# context we've collected
if ctx_buffer and context:
ret.extend(ctx_buffer[-context:])
ctx_buffer = []
if l_type in ignore:
continue
elif l_type in include:
ret.append(l)
else:
logger.error('line_type not handled {}: {}'.format(l_type, l))
if ctx_buffer and context:
ret.extend(ctx_buffer[-context:])
return ret
def git(self, cmd, call_type, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, skip_err=False):
run_cmd = self.git_cmd + cmd
logger.debug('GIT: {}'.format(' '.join(run_cmd)))
if call_type == CallType.CHECK_OUTPUT:
return subprocess.check_output(run_cmd, stderr=stderr).decode('UTF-8', errors='replace')
elif call_type == CallType.CHECK_CALL:
try:
subprocess.check_call(run_cmd, stdout=stdout, stderr=stderr)
return 0
except subprocess.CalledProcessError as e:
if not skip_err:
logger.exception('Exception running git: {}'.format(e))
logger.error('FAIL: {}'.format(' '.join(run_cmd)))
raise
return e.returncode
elif call_type == CallType.CALL:
subprocess.call(run_cmd, stdout=stdout, stderr=stderr)
else:
raise ValueError('Invalid call type {}'.format(call_type))
return None
def find_fixes_reference(self, ref):
cmd = ['log', '--format=oneline', '--abbrev-commit', '-i', '--grep',
'Fixes:.*{}'.format(ref.sha[:8]), '{}..{}'.format(ref.sha,
ref.refs(True))]
return self.git(cmd, CallType.CHECK_OUTPUT)
def get_am_from_from_patch(self, patch):
regex = re.compile('\(am from (http.*)\)', flags=re.I)
m = regex.findall(patch)
if not m or not len(m):
return None
return m
def add_or_update_remote(self, ref):
cmd = ['remote', 'set-url', ref.remote_name, ref.remote]
ret = self.git(cmd, CallType.CHECK_CALL, skip_err=True)
if ret == 0:
return
cmd = ['remote', 'add', ref.remote_name, ref.remote]
ret = self.git(cmd, CallType.CHECK_CALL)
if ret != 0:
logger.error('Failed to add remote {} ({})', str(ref), ret)
def fetch_remote(self, ref):
logger.debug('Fetching {}'.format(str(ref)))
self.add_or_update_remote(ref)
cmd = ['fetch', '--prune', '--tags', ref.remote_name, ref.refs()]
ret = self.git(cmd, CallType.CHECK_CALL, skip_err=True)
if ret != 0:
logger.error('Fetch remote ({}) failed: ({})'.format(str(ref), ret))
def checkout(self, ref):
cmd = ['checkout', ref]
logger.debug("Running {}".format(" ".join(cmd)))
self.git(cmd, CallType.CALL)
def checkout_reset(self, path):
cmd = ['checkout', '--', path]
logger.debug('Running {}'.format(' '.join(cmd)))
self.git(cmd, CallType.CALL)
def get_commit_msg_from_sha(self, sha):
cmd = ['log', '-1', sha]
return self.git(cmd, CallType.CHECK_OUTPUT, stderr=None)
def get_cherry_pick_sha_from_local_sha(self, local_sha):
commit_message = self.get_commit_msg_from_sha(local_sha)
# Use the last SHA found in the patch, since it's (probably) most recent,
# and only return the SHA, not the remote/branch
return CommitRef.refs_from_patch(commit_message)[-1].sha
def get_links_from_local_sha(self, local_sha):
commit_message = self.get_commit_msg_from_sha(local_sha)
# Use the last SHA found in the patch, since it's (probably) most recent,
# and only return the SHA, not the remote/branch
return CommitRef.links_from_patch(commit_message)
def get_commit_from_subject(self, subject, surrounding_commit=None):
cmd = ['log', '-F', '--grep', subject, r'--format=%h %s']
if surrounding_commit:
cmd.append('{}~100..'.format(surrounding_commit))
ret = self.git(cmd, CallType.CHECK_OUTPUT, stderr=None).strip()
return ret.splitlines()
def is_sha_in_branch(self, ref, skip_err=False):
cmd = ['merge-base', '--is-ancestor', ref.sha, ref.refs(True)]
try:
ret = self.git(cmd, CallType.CHECK_CALL, skip_err=True)
return ret == 0
except Exception as e:
if not skip_err:
logger.error('git merge-base failed {}: ({})'.format(str(ref), str(e)))
logger.exception('Exception checking sha: {}'.format(e))
raise
def get_commit_from_sha(self, ref):
cmd = ['show', '--minimal', '-U{}'.format(self.MAX_CONTEXT), r'--format=%B',
ref.sha]
ret = self.git(cmd, CallType.CHECK_OUTPUT, stderr=None)
return ret
def strip_special(self, string):
return re.sub('([a-z]*\://)|\W', '', string, flags=re.I)
def fetch_to_tmp_ref(self, remote, ref):
stripped = '{}_{}'.format(self.strip_special(remote),
self.strip_special(ref))
tmp_ref = 'refs/branches/{}'.format(stripped)
cmd = ['fetch', '--prune', remote, '{}:{}'.format(ref, tmp_ref)]
self.git(cmd, CallType.CHECK_CALL)
return tmp_ref
def delete_ref(self, ref):
cmd = ['update-ref', '-d', ref]
self.git(cmd, CallType.CHECK_CALL)
def get_commit_from_remote(self, remote, ref):
tmp_ref = self.fetch_to_tmp_ref(remote, ref)
ret = self.get_commit_from_sha(CommitRef(sha=tmp_ref))
self.delete_ref(tmp_ref)
return ret
def compare_diffs(self, a, b, context=0):
if context > self.MAX_CONTEXT:
raise ValueError('Invalid context given')
a = a.split('\n')
b = b.split('\n')
# strip the commit messages
a = self.__strip_commit_msg(a) or []
b = self.__strip_commit_msg(b) or []
a = self.__strip_kruft(a, context)
b = self.__strip_kruft(b, context)
files = {'new': '', 'old': ''}
printed_files = False
ret = []
differ = difflib.Differ()
for l in differ.compare(a, b):
# strip the differ margin for analyzing the line
line = l[2:]
# if this is a file line, save the file for later printing
l_type,_ = self.classify_line(line)
if l_type == LineType.FILE_OLD:
files['old'] = line
continue # old always comes before new, so loop around to pick up new
elif l_type == LineType.FILE_NEW:
files['new'] = line
printed_files = False
# discard the differ '?' helper and unchanged lines
if l[0] == '?' or not l[:2].strip():
continue
if not printed_files:
ret.append(files['old'])
ret.append(files['new'])
printed_files = True
# don't double print files
if l_type == LineType.FILE_NEW or l_type == LineType.FILE_OLD:
continue
# add the change to the diff
ret.append(l)
return ret