Add UnifiedDiffWriter, a helper for writing diffs.
Review Request #12619 — Created Sept. 22, 2022 and submitted
rbtools.diffs.writers.UnifiedDiffWriter, which is a
io.BytesIOthat allows for consistent, faster building of diffs.
Most of our SCMs have historically built diffs as lines that are then
joined together. While that worked, it wasn't as optimal as a
should be, and it left so much of the diff building up to each
individual SCMClient, not all of which were consistent in how they built
UnifiedDiffWriterprovides a nicer interface with more consistent
results. There's functionality for writing original/modified headers,
hunks, and arbitrary lines. These can also be generated straight from a
DiffFileResult, minimizing the work required by SCMClients.
A writer can take a newline string, and an encoding for any Unicode
strings These will probably never need to be changed (and in fact the
encoding shouldn't be, as encodings in diffs is complicated and not
something that should generally be touched at this level). However, we
have these set so that we can have stable values to refer to during diff
Upcoming changes to SCMClients will switch over to diff tools and the
writer for building diffs.
Unit tests passed.
Made use of this in some upcoming changes successfully.
- Switched types from
Union[bytes, str], to avoid how any usage of
AnyStrwithin a function must all be the same type.
force_unicode()to also take
Union[bytes, str], for the same reason (it affects callers).
UnifiedDiffWriter.write_file_headers(), which wraps the orig/modified functions.
Revision 2 (+1562 -6)
Checks run (2 succeeded)