-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread-shift
executable file
·87 lines (65 loc) · 2.4 KB
/
read-shift
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
#! /usr/bin/env python3
"""Read the shift chosen for a particular slider in a diff.
usage:
read-shift <old-sha1>:<old-filename> <new-sha1>:<new-filename> [-/+] <line-number>
where
* <sha1>:<filename> names a blob to be diffed.
* [-/+] is '-' if lines are being deleted, '+' if lines are being
added.
* <line-number> is the line number of the first line being
added/deleted, when the slider is shifted to its canonical position.
The line number is relative to the old blob if lines are being
deleted and relative to the new blob if lines are being added.
Output the shift chosen for that slider in a diff read from stdin.
Output the result to stdout as
<old-sha1>:<old-filename> <new-sha1>:<new-filename> [-/+] <line-number> <shift>
"""
import sys
import os
import io
import re
import argparse
sys.path.insert(0, os.path.dirname(sys.argv[0]))
import diff_heuristics
from diff_heuristics import SliderName
from diff_heuristics import iter_file_diffs
from diff_heuristics import find_slider
from diff_heuristics import ParsingError
def main(args):
parser = argparse.ArgumentParser(
description='Read a slider shift from a diff'
)
parser.add_argument('old', type=str)
parser.add_argument('new', type=str)
parser.add_argument('prefix', type=str, choices=['-', '+'])
parser.add_argument('line_number', type=int)
parser.add_argument('--verbose', '-v', action='store_true')
options = parser.parse_args(args)
slidername = SliderName(
options.old, options.new, options.prefix, options.line_number,
)
(old_sha1, old_filename) = slidername.old.split(':', 1)
(new_sha1, new_filename) = slidername.new.split(':', 1)
if options.verbose:
diff_heuristics.verbose = True
input = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
lines = [line.rstrip('\n\r') for line in input.readlines()]
try:
slider = find_slider(
lines,
old_filename, new_filename,
slidername.prefix, slidername.line_number,
)
except ParsingError as e:
print(
'Could not parse following slider: %s\n'
' %s' % (
e, slidername,
),
file=sys.stderr,
)
else:
shift = slider.shift_canonically()
slidername.write(sys.stdout, [shift])
if __name__ == '__main__':
main(sys.argv[1:])