1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

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!
This commit is contained in:
Shannon Booth 2023-06-24 17:33:04 +12:00 committed by Andrew Kaster
parent ef8f0f4f68
commit f690807c5a
8 changed files with 169 additions and 127 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -31,28 +32,21 @@ ErrorOr<Vector<Hunk>> parse_hunks(StringView diff)
++line_index;
continue;
}
if (diff_lines[line_index][0] == ' ') {
current_location.apply_offset(1, HunkLocation::LocationType::Both);
++line_index;
continue;
}
Hunk hunk {};
hunk.original_start_line = current_location.original_start_line;
hunk.target_start_line = current_location.target_start_line;
hunk.location = current_location;
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') {
TRY(hunk.removed_lines.try_append(TRY(String::from_utf8(diff_lines[line_index].substring_view(1, diff_lines[line_index].length() - 1)))));
current_location.apply_offset(1, HunkLocation::LocationType::Original);
++line_index;
}
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') {
TRY(hunk.added_lines.try_append(TRY(String::from_utf8(diff_lines[line_index].substring_view(1, diff_lines[line_index].length() - 1)))));
current_location.apply_offset(1, HunkLocation::LocationType::Target);
++line_index;
}
while (line_index < diff_lines.size()) {
auto const& line = diff_lines[line_index];
char const operation = line[0];
if (operation != ' ' && operation != '+' && operation != '-')
break;
auto const content = line.substring_view(1, line.length() - 1);
TRY(hunk.lines.try_append(Line { Line::operation_from_symbol(operation), TRY(String::from_utf8(content)) }));
while (line_index < diff_lines.size() && diff_lines[line_index][0] == ' ') {
current_location.apply_offset(1, HunkLocation::LocationType::Both);
++line_index;
}
TRY(hunks.try_append(hunk));
@ -60,15 +54,9 @@ ErrorOr<Vector<Hunk>> parse_hunks(StringView diff)
if constexpr (HUNKS_DEBUG) {
for (auto const& hunk : hunks) {
dbgln("Hunk location:");
dbgln(" orig: {}", hunk.original_start_line);
dbgln(" target: {}", hunk.target_start_line);
dbgln(" removed:");
for (auto const& line : hunk.removed_lines)
dbgln("- {}", line);
dbgln(" added:");
for (auto const& line : hunk.added_lines)
dbgln("+ {}", line);
dbgln("{}", hunk.location);
for (auto const& line : hunk.lines)
dbgln("{}", line);
}
}
@ -78,22 +66,21 @@ ErrorOr<Vector<Hunk>> parse_hunks(StringView diff)
HunkLocation parse_hunk_location(StringView location_line)
{
size_t char_index = 0;
struct StartAndLength {
size_t start { 0 };
size_t length { 0 };
};
auto parse_start_and_length_pair = [](StringView raw) {
auto index_of_separator = raw.find(',').value();
auto start = raw.substring_view(0, index_of_separator).to_uint().value();
auto length = raw.substring_view(index_of_separator + 1, raw.length() - index_of_separator - 1).to_uint().value();
auto maybe_index_of_separator = raw.find(',');
if (start != 0)
start--;
size_t start = 0;
size_t length = 0;
if (maybe_index_of_separator.has_value()) {
auto index_of_separator = maybe_index_of_separator.value();
start = raw.substring_view(0, index_of_separator).to_uint().value();
length = raw.substring_view(index_of_separator + 1, raw.length() - index_of_separator - 1).to_uint().value();
} else {
length = 1;
start = raw.to_uint().value();
}
if (length != 0)
length--;
return StartAndLength { start, length };
return Range { start, length };
};
while (char_index < location_line.length() && location_line[char_index++] != '-') {
}
@ -115,21 +102,9 @@ HunkLocation parse_hunk_location(StringView location_line)
size_t target_location_end_index = char_index - 2;
auto original_pair = parse_start_and_length_pair(location_line.substring_view(original_location_start_index, original_location_end_index - original_location_start_index + 1));
auto target_pair = parse_start_and_length_pair(location_line.substring_view(target_location_start_index, target_location_end_index - target_location_start_index + 1));
return { original_pair.start, original_pair.length, target_pair.start, target_pair.length };
}
void HunkLocation::apply_offset(size_t offset, HunkLocation::LocationType type)
{
if (type == LocationType::Original || type == LocationType::Both) {
original_start_line += offset;
original_length -= offset;
}
if (type == LocationType::Target || type == LocationType::Both) {
target_start_line += offset;
target_length -= offset;
}
auto old_range = parse_start_and_length_pair(location_line.substring_view(original_location_start_index, original_location_end_index - original_location_start_index + 1));
auto new_range = parse_start_and_length_pair(location_line.substring_view(target_location_start_index, target_location_end_index - target_location_start_index + 1));
return { old_range, new_range };
}
};