ladybird/Userland/Libraries/LibDiff/Format.cpp
Shannon Booth f690807c5a LibDiff: Change underlying representation of Hunk to allow context
The existing hunk data structure does not contain any way to easily
store information about context surrounding the additions and removals
in a hunk. While this does work fine for normal diffs (where there is
never any surrounding context) this data structure is quite limiting for
other use cases.

Without support for surrounding context it is not possible to:
 * Add support for unified or context format to the diff utility to
   output surrounding context.
 * Be able to implement a patch utility that uses the surrounding
   context to reliably locate where to apply a patch when a hunk range
   does not apply perfectly.

This patch changes Diff::Hunk such that its data structure more closely
resembles a unified diff. Each line in a hunk is now either a change,
removal, addition or context.

Allowing hunks to have context inside of them exposes that HackStudio
heavily relies on there being no context in the hunks that it uses for
its' git gutter implementation. The fix here is simple - ask git to
produce us a diff that has no context in it!
2023-07-02 11:18:11 -06:00

68 lines
2.4 KiB
C++

/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Format.h"
#include <AK/DeprecatedString.h>
#include <AK/Stream.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
namespace Diff {
DeprecatedString generate_only_additions(StringView text)
{
auto lines = text.split_view('\n', SplitBehavior::KeepEmpty);
StringBuilder builder;
builder.appendff("@@ -0,0 +1,{} @@\n", lines.size());
for (auto const& line : lines) {
builder.appendff("+{}\n", line);
}
return builder.to_deprecated_string();
}
ErrorOr<void> write_normal(Hunk const& hunk, Stream& stream, ColorOutput color_output)
{
// Source line(s)
TRY(stream.write_formatted("{}", hunk.location.old_range.start_line));
if (hunk.location.old_range.number_of_lines > 1)
TRY(stream.write_formatted(",{}", (hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines - 1)));
// Action
if (hunk.location.old_range.number_of_lines > 0 && hunk.location.new_range.number_of_lines > 0)
TRY(stream.write_formatted("c"));
else if (hunk.location.new_range.number_of_lines > 0)
TRY(stream.write_formatted("a"));
else
TRY(stream.write_formatted("d"));
// Target line(s)
TRY(stream.write_formatted("{}", hunk.location.new_range.start_line));
if (hunk.location.new_range.number_of_lines > 1)
TRY(stream.write_formatted(",{}", (hunk.location.new_range.start_line + hunk.location.new_range.number_of_lines - 1)));
TRY(stream.write_formatted("\n"));
for (auto const& line : hunk.lines) {
VERIFY(line.operation == Line::Operation::Removal || line.operation == Line::Operation::Addition);
if (line.operation == Line::Operation::Addition) {
if (color_output == ColorOutput::Yes)
TRY(stream.write_formatted("\033[32;1m> {}\033[0m\n", line.content));
else
TRY(stream.write_formatted("> {}\n", line.content));
} else {
if (color_output == ColorOutput::Yes)
TRY(stream.write_formatted("\033[31;1m< {}\033[0m\n", line.content));
else
TRY(stream.write_formatted("< {}\n", line.content));
}
}
return {};
}
}