-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrastapatch
executable file
·518 lines (435 loc) · 14.3 KB
/
rastapatch
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python3
# coding: utf8
#
# Rastapatch
# ==========
#
# A patch file colorizer. Highlights changed words (as good as it can guess) in
# unidiff patch files.
#
# (c) Copyright 2012-2014 by Neels Janosch Hofmeyr <[email protected]> (GPLv3)
#
# This file is part of Rastapatch file colorizer.
#
# The following three paragraphs of licensing information do not apply to the
# part included from termcolor.py, which is clearly marked with its own terms.
#
# Rastapatch file colorizer is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Rastapatch file colorizer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Rastapatch file colorizer. If not, see <http://www.gnu.org/licenses/>.
# SNIP: included termcolor.py from http://pypi.python.org/pypi/termcolor/1.0.0
########################################################################
# Copyright (c) 2008-2011 Volvox Development Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author: Konstantin Lepa <[email protected]>
"""ANSII Color formatting for output in terminal."""
import os
import re
__ALL__ = [ 'colored' ]
VERSION = (1, 0, 0)
ATTRIBUTES = dict(
list(zip([
'bold',
'dark',
'',
'underline',
'blink',
'',
'reverse',
'concealed'
],
list(range(1, 9))
))
)
del ATTRIBUTES['']
HIGHLIGHTS = dict(
list(zip([
'on_grey',
'on_red',
'on_green',
'on_yellow',
'on_blue',
'on_magenta',
'on_cyan',
'on_white'
],
list(range(40, 48))
))
)
COLORS = dict(
list(zip([
'grey',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
],
list(range(30, 38))
))
)
RESET = '\033[0m'
def colored(text, color=None, on_color=None, attrs=None):
"""Colorize text.
Available text colors:
red, green, yellow, blue, magenta, cyan, white.
Available text highlights:
on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
Available attributes:
bold, dark, underline, blink, reverse, concealed.
Example:
colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
colored('Hello, World!', 'green')
"""
if not text:
return text
if os.getenv('ANSI_COLORS_DISABLED') is None:
fmt_str = '\033[%dm%s'
if color is not None:
text = fmt_str % (COLORS[color], text)
if on_color is not None:
text = fmt_str % (HIGHLIGHTS[on_color], text)
if attrs is not None:
for attr in attrs:
text = fmt_str % (ATTRIBUTES[attr], text)
text += RESET
return text
########################################################################
# SNIP: end of termcolor.py from http://pypi.python.org/pypi/termcolor/1.0.0
# GPLv3 License applies from this line on.
from sys import argv, stdin
import os.path
from difflib import SequenceMatcher
progname = os.path.basename(argv[0])
__doc__ = """
This is %s%s%s, a patch file colorizer. Highlights changed words (as good
as it can guess) in unidiff patch files.
(c) Copyright 2012-2014 by Neels Janosch Hofmeyr <[email protected]>, published
under the General Public License v3, except for the part copied from
termcolor.py, which is clearly marked with its own terms in the source.
Naming:
I am not a Rasta in any way; the colors I chose to colorize the diffs,
completely by random, ended up resembling "rasta colors", that's all.
The project home page is http://hofmeyr.de/rastapatch/
Source available at https://github.com/neeels/rastapatch
Usage:
rastapatch myfix.patch
svn diff | rastapatch | less -R
""" % (
colored('ras', 'red'),
colored('ta', 'yellow'),
colored('patch', 'green'))
def col_heads(s):
return colored(s, 'yellow')
def col_left(s):
return colored(s, 'red')
def col_right(s):
return colored(s, 'green')
def col_left_matching(s):
return colored(s, 'blue')
def col_right_matching(s):
return s #colored(s, 'blue')
def col_err(s):
return colored(s, None, 'on_red')
def split_lines(a, b):
s = SequenceMatcher(None, a, b)
ai = 0
bi = 0
r = []
for m in s.get_matching_blocks():
mismatch_a = None
mismatch_b = None
match = None
if m.a > ai:
mismatch_a = a[ai:m.a]
ai = m.a
if m.b > bi:
mismatch_b = b[bi:m.b]
bi = m.b
if m.size > 0:
ai = m.a + m.size
bi = m.b + m.size
match = a[m.a:m.a + m.size]
r.append(((mismatch_a, mismatch_b), match))
return r
def color_diffs(a, b):
aa = []
bb = []
for mismatches, match in split_lines(a, b):
if mismatches[0] is not None:
aa.append( col_left(mismatches[0]) )
if mismatches[1] is not None:
bb.append( col_right(mismatches[1]) )
if match is not None:
if len(match) <= 2:
aa.append( col_left(match) )
bb.append( col_right(match) )
else:
aa.append( col_left_matching(match) )
bb.append( col_right_matching(match) )
return (''.join(aa), ''.join(bb))
def lines_similarity(l, r):
mismatch_count = 0
match_count = 0
for mismatches, match in split_lines(l, r):
ml, mr = mismatches
mismatch_count += max(len(ml or ''), len(mr or ''))
match_count += len(match or '')
if not (match_count + mismatch_count):
ret = 1.0
else:
ret = float(match_count) / (match_count + mismatch_count)
return ret
def find_best_similarity(find_line, in_list, from_idx, for_len):
best_match_idx = from_idx
best_match_score = 0
for_len = min(int(for_len), len(in_list) - from_idx)
for l_idx in range(from_idx, for_len):
score = lines_similarity(find_line, in_list[l_idx])
if score > best_match_score:
best_match_score = score
best_match_idx = l_idx
return best_match_idx
def match_lines(ll, rr):
ret = []
# avoid modifying rr list
ll_pos = 0
rr_pos = 0
# find the best match where to start off matching ll to rr.
# A common pattern is removing lots of lines before a change, like
#
# --- ll
# +++ rr
# - foo();
# - bar();
# - baz();
# - val = 23;
# - call_function(val);
# + call_function();
#
# Now we want the 'call_function' lines to match up, instead of matching the
# '- foo();' with '+ call_function();', so the user gets the benefit of
# colored indication of the similarity of the 'call_function' lines.
#
# But also, if the patch moved a very early line all the way to the bottom
# of a diff chunk, don't skip the start just for one line. Try to maintain
# a reasonable amount of remaining lines for rr and ll to match up later.
ll_lines_remain = len(ll)
rr_lines_remain = len(rr) - rr_pos
reasonable_shift = ll_lines_remain - rr_lines_remain
reasonable_shift += rr_lines_remain / 3
if reasonable_shift > 0:
rr_start = find_best_similarity(rr[0], ll, 0, reasonable_shift + 1)
for n in range(rr_start):
ret.append((ll[ll_pos], None))
ll_pos += 1
while ll_pos < len(ll):
right_line = None
ll_lines_remain = len(ll) - ll_pos
rr_lines_remain = len(rr) - rr_pos
left_line = ll[ll_pos]
ll_pos += 1
if rr_lines_remain:
# find the best match where left_line matches a line in rr.
# A common pattern is adding a line before a change, like
#
# --- ll
# +++ rr
# - call_function();
# + val = 23;
# + call_function(val);
#
# Now we want the 'call_function' lines to match up, instead of matching the
# '- call_function();' with '+ val = 23;', so the user gets the benefit of
# colored indication of the similarity of the 'call_function' lines.
#
# But also, if the patch moved a very early line all the way to the bottom
# of a diff chunk, don't skip the start just for one line. Try to maintain
# a reasonable amount of remaining lines for rr and ll to match up later.
reasonable_shift = rr_lines_remain - ll_lines_remain
reasonable_shift += ll_lines_remain / 3
if reasonable_shift > 0:
best_match_idx = find_best_similarity(left_line, rr, rr_pos, reasonable_shift + 1)
skip_r_lines = best_match_idx - rr_pos
for n in range(skip_r_lines):
ret.append((None, rr[rr_pos]))
rr_pos += 1
right_line = rr[rr_pos]
rr_pos += 1
ret.append((left_line, right_line))
# if ll ended before rr was through
for rr_end in range(len(rr) - rr_pos):
ret.append((None, rr[rr_pos]))
rr_pos += 1
return ret
def color_match_lines(ll, rr):
r = []
for left, right in match_lines(ll, rr):
if left is not None and right is not None:
r.append( color_diffs(left, right) )
else:
if left is not None:
r.append( ( col_left(left), None ) )
if right is not None:
r.append( ( None, col_right(right) ) )
return r
re_ansi_color = re.compile(r'\033\[([0-9]+)m')
re_whitespace_error = re.compile(r'[ \t]+$| +\t')
def mark_whitespace_errors(line):
if len(line) < 1:
return line
# This is not trivial to get right. ANSI coloring for whitespace errors and for the
# patch colorization interfere with each other. We don't want to evaluate whitespace
# error coloring as patch differences. But also, once patch diff colors are in the
# string, we can no longer reliably detect whitespace errors (if there are colors
# at line ends or between spaces/tabs). So the only way is to first extract existing
# colors, then detect whitespace errors, then combine coloring later.
colorless = []
colorless_pos = 0
line_pos = 0
colors = []
for m in re_ansi_color.finditer(line):
colorless_section = line[line_pos:m.start()]
colorless.append( colorless_section )
colorless_pos += len(colorless_section)
color = int(m.group(1))
colors.append((colorless_pos, color))
line_pos = m.end()
colorless.append(line[line_pos:])
line = ''.join(colorless)
# ok, now find whitespace errors.
# we're outputting diffs, so the leading space of unchanged diff output doesn't count
for m in re_whitespace_error.finditer(line[1:]):
# override that part with the red background color
err_start = 1 + m.start()
err_end = 1 + m.end()
prev_color = 0
new_colors = []
started_err = False
c = 0
while c < len(colors):
c_pos, c_col = colors[c]
if c_pos <= err_start:
# up to the error's start, copy colors over as normal
prev_color = c_col
new_colors.append((c_pos, c_col))
elif c_pos < err_end:
# up to the error's end, just remember the last color
prev_color = c_col
else:
break;
c += 1
new_colors.append((err_start, HIGHLIGHTS['on_red']))
new_colors.append((err_end, 0))
if prev_color:
new_colors.append((err_end, prev_color))
# now copy all those after the whitespace error as normal
while c < len(colors):
c_pos, c_col = colors[c]
c += 1
if c_pos < err_end:
continue
new_colors.append((c_pos, c_col))
colors = new_colors
# now weave those colors back in
bits = []
pos = 0
last_col = 0
for c_pos, c_col in colors:
if c_pos > pos:
bits.append(line[pos:c_pos])
bits.append('\033[%dm' % c_col)
last_col = c_col
pos = c_pos
bits.append(line[pos:])
if last_col:
bits.append(RESET)
return ''.join(bits)
def output(line):
print(mark_whitespace_errors(line))
if __name__ == '__main__':
path_arg = 0
infile = None
if len(argv) < 2:
infile = stdin
path = '-'
elif argv[1] == '-h':
print(__doc__)
exit(0)
elif argv[1] == '--':
path_arg = 2
else:
path_arg = 1
if len(argv) > (path_arg+1):
print('rastapatch: too many arguments')
exit(1)
if len(argv) <= path_arg:
print('rastapatch: too few arguments')
exit(1)
if path_arg:
path = argv[path_arg]
infile = open(path)
leftlines = []
rightlines = []
def print_left_right_lines():
if leftlines and rightlines:
matched = color_match_lines(leftlines, rightlines)
for ll,rl in matched:
if ll is not None:
output(ll)
for ll,rl in matched:
if rl is not None:
output(rl)
else:
for ll in leftlines:
if ll is not None:
output(col_left(ll))
for rl in rightlines:
if rl is not None:
output(col_right(rl))
for line in infile.readlines():
if line.endswith('\n'):
line = line[:-1]
if line.startswith('-'):
leftlines.append(line)
elif line.startswith('+'):
rightlines.append(line)
else:
print_left_right_lines()
leftlines = []
rightlines = []
if line.startswith('@@ '):
output(col_heads(line))
else:
output(line)
print_left_right_lines();
# vim: tabstop=2 shiftwidth=2 expandtab nocindent autoindent