Add UnifiedDiffWriter, a helper for writing diffs.
Review Request #12619 — Created Sept. 22, 2022 and submitted
This introduces
rbtools.diffs.writers.UnifiedDiffWriter
, which is a
io.BytesIO
that 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 aBytesIO
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
diffs.
UnifiedDiffWriter
provides 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
building.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.
Summary | ID |
---|---|
3e3f9e23b7551d9ded935c84830b195ccb6a394c |
- Change Summary:
-
- Switched types from
AnyStr
toUnion[bytes, str]
, to avoid how any usage ofAnyStr
within a function must all be the same type. - Updated
force_bytes()
andforce_unicode()
to also takeUnion[bytes, str]
, for the same reason (it affects callers). - Added
UnifiedDiffWriter.write_file_headers()
, which wraps the orig/modified functions. - Added
UnifiedDiffWriter.write_binary_files_differ()
.
- Switched types from
- Commits:
-
Summary ID ffc35d33174211d4e34731702760758bd79b83f3 3e3f9e23b7551d9ded935c84830b195ccb6a394c