-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.py
More file actions
executable file
·290 lines (250 loc) · 11.9 KB
/
diff.py
File metadata and controls
executable file
·290 lines (250 loc) · 11.9 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
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
#! /usr/bin/env python
from __future__ import annotations
import difflib
import functools
import itertools
import os
import sys
import tempfile
import webbrowser
from collections import defaultdict
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterable
html_begin = b"""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Diff</title>
<script>
function detailsElementToggleListener(toggleEvent) {
const detailsElement = toggleEvent.target
if (detailsElement.getBoundingClientRect().top < 0) {
detailsElement.scrollIntoView({behavior: "smooth", block: "start"});
}
}
window.addEventListener("load", () => {
const detailsElements = document.getElementsByTagName("details");
for (let i = 0; i < detailsElements.length; ++i) {
detailsElements[i].addEventListener("toggle", detailsElementToggleListener);
}
});
</script>
<style type="text/css">
table.diff {border: medium;}
.diff_header {background-color: #e8f2ff;}
td.diff_header {text-align: right;}
body {background-color: #fdfbf3; display: inline-block; font-family: monospace; padding-bottom: 100vh;}
div {margin: 0px 4px 80px 4px;}
pre {max-width: 50vw; white-space: pre-wrap;}
details {display: inline-block;}
summary {background-color: #e8f2ff; border-width: 1px 1px 1px 1px; border-style: solid; cursor: pointer; padding: 0px 4px 0px 4px; position: sticky; top: 0px;}
details[open] summary {border-bottom-color: #e8f2ff;}
.diff_next {background-color: #e8f2ff;}
.diff_add {background-color: #aaffaa;}
.diff_chg {background-color: #ffff77;}
.diff_sub {background-color: #ffaaaa;}
</style>
</head>
<body>
"""
html_end = b"</body></html>"
added_header = '<span style="color:green;">+++++</span>'
deleted_header = '<span style="color:red;">−−−−−</span>' # noqa: RUF001
rename_detect_real_quick_threshold, rename_detect_quick_threshold, rename_detect_threshold = 0.5, 0.5, 0.5
def read_lines(self: Path) -> Iterable[str]:
with open(self, encoding="utf-8") as self_reader:
yield from self_reader
def read_words(self: Path) -> Iterable[str] | None:
try:
return self.read_text(encoding="utf-8").split()
except UnicodeDecodeError:
return None
Path.relative_to = functools.cache(Path.relative_to)
class Diff:
"""
Compare two regular files directly or two directories recursively.
"""
def __init__(self, left: str, right: str):
self._left_directory, self._right_directory = Path(left).absolute(), Path(right).absolute()
if self._left_directory.is_dir() and self._right_directory.is_dir():
self._matcher = difflib.SequenceMatcher(isjunk=None, autojunk=False)
self._left_right_file_mapping = (
self._changed_only_mapping | self._renamed_only_mapping | self._renamed_and_changed_mapping
)
else:
self._left_right_file_mapping = {self._left_directory: self._right_directory}
self._left_files, self._right_files = set(), set()
self._left_directory = self._right_directory = Path(
os.path.commonpath([self._left_directory.parent, self._right_directory.parent])
)
self._html_diff = difflib.HtmlDiff(wrapcolumn=119)
@staticmethod
def _files_in(directory: Path) -> dict[Path, Path]:
"""
Recursively map the relative paths of all files in the given
directory to their absolute paths.
:param directory: Directory to traverse.
:return: Files in the tree rooted at the given directory.
"""
return {
(file := Path(root) / file_name).relative_to(directory): file
for root, _, file_names in os.walk(directory)
for file_name in file_names
}
@property
def _changed_only_mapping(self) -> dict[Path, Path]:
"""
To each file in the left directory, trivially map the file in the right
directory having the same relative path, if it exists.
:return: Mapping between left and right directory files.
"""
left_files, right_files = self._files_in(self._left_directory), self._files_in(self._right_directory)
common_files = left_files.keys() & right_files.keys()
left_right_file_mapping = {left_files[common_file]: right_files[common_file] for common_file in common_files}
self._left_files = {v for k, v in left_files.items() if k not in common_files}
self._right_files = {v for k, v in right_files.items() if k not in common_files}
return left_right_file_mapping
@functools.cached_property
def _renamed_only_mapping(self) -> dict[Path, Path]:
"""
To each file in the left directory, map the file in the right directory
having the same contents, if it exists.
:return: Mapping between left and right directory files.
"""
left_directory_lookup = defaultdict(list)
for left_file in self._left_files:
left_file_contents = left_file.read_bytes()
# Assume there are no collisions.
left_directory_lookup[hash(left_file_contents)].append(left_file)
left_right_file_mapping = {}
for right_file in self._right_files.copy():
right_file_contents = right_file.read_bytes()
if not (identical_left_files := left_directory_lookup.get(hash(right_file_contents))):
continue
# Arbitrarily pick the last of the identical files.
left_right_file_mapping[(identical_left_file := identical_left_files.pop())] = right_file
self._left_files.remove(identical_left_file)
self._right_files.remove(right_file)
return left_right_file_mapping
@property
def _renamed_and_changed_mapping(self) -> dict[Path, Path]:
"""
To each text file in the left directory, map the text file in the right
directory having similar contents, if it exists.
:return: Mapping between left and right directory files.
"""
left_directory_matches = defaultdict(list)
for left_file in self._left_files:
if not (left_file_contents := read_words(left_file)):
continue
self._matcher.set_seq2(left_file_contents)
for right_file in self._right_files:
if not (right_file_contents := read_words(right_file)):
continue
self._matcher.set_seq1(right_file_contents)
if (
self._matcher.real_quick_ratio() > rename_detect_real_quick_threshold
and self._matcher.quick_ratio() > rename_detect_quick_threshold
and (similarity_ratio := self._matcher.ratio()) > rename_detect_threshold
):
left_directory_matches[left_file].append((similarity_ratio, right_file))
for v in left_directory_matches.values():
v.sort()
# Ensure that the order in which we iterate over the files in the left
# directory is such that the one having the greatest similarity ratio
# with respect to any file in the right directory comes first.
left_directory_matches = dict(
sorted(left_directory_matches.items(), key=lambda kv: kv[1][-1][0], reverse=True)
)
left_right_file_mapping = {}
for left_file, v in left_directory_matches.items():
# Find the file in the right directory which is most similar to
# this file in the left directory.
for _, similar_right_file in reversed(v):
if similar_right_file not in self._right_files:
continue
left_right_file_mapping[left_file] = similar_right_file
self._left_files.remove(left_file)
self._right_files.remove(similar_right_file)
break
return left_right_file_mapping
def report(self) -> Path:
"""
Write HTML tables summarising the recursive differences between two
directories.
:return: File to which tables were written.
"""
left_right_files = sorted(
itertools.chain(
((left_file, None) for left_file in self._left_files),
self._left_right_file_mapping.items(),
((None, right_file) for right_file in self._right_files),
),
key=lambda lr: lr[1].relative_to(self._right_directory)
if lr[1]
else lr[0].relative_to(self._left_directory),
)
with tempfile.NamedTemporaryFile(delete=False, prefix="git-difftool-", suffix=".html") as writer:
writer.write(html_begin)
if commit_metadata := os.getenv("COMMIT_METADATA"):
writer.write(f"<div><pre>{commit_metadata}</pre></div>".encode())
self._report(left_right_files, writer)
writer.write(html_end)
return Path(writer.name)
def _report(self, left_right_files: Iterable[tuple[Path, None] | tuple[Path, Path] | tuple[None, Path]], writer):
left_right_files_len = len(left_right_files)
for pos, (left_file, right_file) in enumerate(left_right_files, 1):
if left_file:
from_desc, from_stat = str(left_file.relative_to(self._left_directory)), left_file.stat()
else:
from_desc = added_header
if right_file:
to_desc, to_stat = str(right_file.relative_to(self._right_directory)), right_file.stat()
else:
to_desc = deleted_header
short_desc = []
if left_file:
short_desc.append(f"{from_stat.st_mode:o}")
if from_desc != to_desc:
short_desc.append(from_desc)
if left_file and right_file and (from_desc != to_desc or from_stat.st_mode != to_stat.st_mode):
short_desc.append("⟼")
if not left_file or (right_file and from_stat.st_mode != to_stat.st_mode):
short_desc.append(f"{to_stat.st_mode:o}")
short_desc.append(to_desc)
writer.write(b" <div><details open><summary>")
writer.write((f"{pos}/{left_right_files_len} ■ " + " ".join(short_desc)).encode())
if left_file in self._renamed_only_mapping:
writer.write(" ■ identical</summary>\n </details></div>\n".encode())
continue
if (not left_file and right_file and to_stat.st_size == 0) or (
left_file and from_stat.st_size == 0 and not right_file
):
writer.write(" ■ empty</summary>\n </details></div>\n".encode())
continue
from_lines = read_lines(left_file) if left_file else []
to_lines = read_lines(right_file) if right_file else []
try:
html_table = self._html_diff.make_table(
from_lines, to_lines, from_desc.center(64, "\u00a0"), to_desc.center(64, "\u00a0"), context=True
)
# They might be identical if only the mode was changed.
if "No Differences Found" in html_table:
writer.write(" ■ identical</summary>\n".encode())
else:
writer.write(b"</summary>\n")
writer.write(html_table.encode())
except UnicodeDecodeError:
writer.write(" ■ binary</summary>\n".encode())
writer.write(b"\n </details></div>\n")
def main():
diff = Diff(sys.argv[1], sys.argv[2])
html_file = diff.report()
print(html_file) # noqa: T201
webbrowser.open(html_file.as_uri())
if __name__ == "__main__":
main()