-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathunified_diff.py
More file actions
218 lines (191 loc) · 6.39 KB
/
Copy pathunified_diff.py
File metadata and controls
218 lines (191 loc) · 6.39 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
from dataclasses import dataclass
from itertools import groupby
from typing import List, Optional, Tuple, Union
import re
from unidiff import PatchSet
@dataclass
class Code:
"""Matches DiffRange in webdiff codes.ts"""
type: str
"""One of "replace" | "delete" | "insert" | "equal" | "skip"."""
before: Tuple[int, int]
"""Line range on left side; zero-based, half-open interval."""
after: Tuple[int, int]
"""Line range on right side; zero-based, half-open interval."""
header: Optional[str] = None
def read_codes(p: PatchSet) -> Union[List[Code], None]:
pf = p[0] # PatchedFile
out = []
last_source = 0
last_target = 0
if pf.is_binary_file:
return None
for hunk in pf:
header = hunk.section_header or None
if hunk.source_start != last_source + 1:
out.append(
Code(
'skip',
(last_source, hunk.source_start - 1),
(last_target, hunk.target_start - 1),
header,
)
)
last_source = hunk.source_start
last_target = hunk.target_start
header = None
for type, chunk in groupby(hunk, lambda line: line.line_type):
lines = [*chunk]
first = lines[0]
last = lines[-1]
if type == ' ':
out.append(
Code(
'equal',
(first.source_line_no - 1, last.source_line_no),
(first.target_line_no - 1, last.target_line_no),
header,
)
)
header = None
elif type == '-':
out.append(
Code(
'delete',
(first.source_line_no - 1, last.source_line_no),
(last_target, last_target),
header,
)
)
header = None
elif type == '+':
out.append(
Code(
'insert',
(last_source, last_source),
(first.target_line_no - 1, last.target_line_no),
header,
)
)
header = None
last_source = last.source_line_no or last_source
last_target = last.target_line_no or last_target
# We don't have enough context to know whether there's a skip at the end
# (missing the number of lines in the file).
return out
def add_replaces(codes: List[Code]) -> List[Code]:
"""Replace paired delete + insert codes with replace."""
out = []
i = 0
while i < len(codes):
c = codes[i]
if c.type == 'delete' and i < len(codes) - 1:
nc = codes[i + 1]
if nc.type == 'insert':
out.append(
Code(
'replace',
(c.before[0], nc.before[1]),
(c.after[0], nc.after[1]),
c.header,
)
)
i += 2
continue
out.append(c)
i += 1
return out
def diff_to_codes(diff: str, after_num_lines=None) -> Union[List[Code], None]:
"""Convert a unified diff to a list of codes for codediff.js.
This only considers the first file in the diff.
If it's a binary diff, returns None.
"""
p = PatchSet.from_string(diff)
if len(p) == 0:
if after_num_lines is None:
return None
return [Code('equal', (0, after_num_lines), (0, after_num_lines))]
codes = read_codes(p)
if not codes:
return None # binary file
# Go through and combine sequential delete/insert into "replace"
codes = add_replaces(codes)
if after_num_lines:
(_, a2) = codes[-1].before
(_, b2) = codes[-1].after
end_skip = after_num_lines - b2
if end_skip:
codes.append(
Code(
'skip',
(a2, a2 + end_skip),
(b2, b2 + end_skip),
None,
)
)
return codes
# See https://git-scm.com/docs/git-diff#_raw_output_format
@dataclass
class RawDiffLine:
src_mode: str
"""e.g. 100644; 000000 for creation/unmerged"""
dst_mode: str
src_sha: str
"""sha1 or 0 if creation/unmerged"""
dst_sha: str
status: str
"""A, C (copy), D, M, R, T (change in type), U (unmerged), X (bug)"""
path: str
score: Union[int, None] = None
"""Only for R or C"""
dst_path: Union[str, None] = None
"""Only set when status=C or R."""
num_add: Union[int, None] = None
"""Num added lines from diffstat. None for binary files."""
num_delete: Union[int, None] = None
"""Num removed lines from diffstat. None for binary files."""
def parse_raw_diff_line(parts: list[str]) -> RawDiffLine:
meta = parts[0]
path = parts[1]
dst_path = parts[2] if len(parts) > 2 else None
m = meta[1:].split(' ')
src_mode, dst_mode, src_sha, dst_sha, status = m
score = None
if len(status) > 1:
score = int(status[1:])
status = status[0]
return RawDiffLine(
src_mode=src_mode,
dst_mode=dst_mode,
src_sha=src_sha,
dst_sha=dst_sha,
status=status,
path=path,
score=score,
dst_path=dst_path,
)
def parse_raw_diff(diff: str) -> List[RawDiffLine]:
# each diff line can be two or three parts. The parts and lines are both null-delimited.
# The "lines" start with ":".
parts = diff.strip('\0').split('\0')
lines = []
numstats = []
curline = None
for part in parts:
if part.startswith(':'):
curline = [part]
lines.append(curline)
elif m := re.match(r'(\d+|-)\t(\d+|-)\t', part):
# numstat line
add, drop = m.group(1), m.group(2)
curline = [int(add) if add != '-' else None, int(drop) if drop != '-' else None]
numstats.append(curline)
else:
curline.append(part)
diff_lines = []
for line, stats in zip(lines, numstats):
diff = parse_raw_diff_line(line)
diff.num_add = stats[0]
diff.num_delete = stats[1]
diff_lines.append(diff)
return diff_lines