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

LibDiff: Make Diff::parse_hunks fallible

Currently the only error that can happen is an OOM. However, in the
future there may be other errors that this function may throw, such as
detecting an invalid patch.
This commit is contained in:
Shannon Booth 2023-06-24 12:44:22 +12:00 committed by Andreas Kling
parent dbd838efdf
commit 23df5748f6
4 changed files with 10 additions and 9 deletions

View file

@ -88,7 +88,7 @@ void EditorWrapper::save()
void EditorWrapper::update_diff() void EditorWrapper::update_diff()
{ {
if (m_git_repo) { if (m_git_repo) {
m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()); m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()).release_value_but_fixme_should_propagate_errors();
editor().update_git_diff_indicators().release_value_but_fixme_should_propagate_errors(); editor().update_git_diff_indicators().release_value_but_fixme_should_propagate_errors();
} }
} }

View file

@ -134,7 +134,7 @@ Gfx::IntRect DiffViewer::separator_rect() const
void DiffViewer::set_content(DeprecatedString const& original, DeprecatedString const& diff) void DiffViewer::set_content(DeprecatedString const& original, DeprecatedString const& diff)
{ {
m_original_lines = split_to_lines(original); m_original_lines = split_to_lines(original);
m_hunks = Diff::parse_hunks(diff); m_hunks = Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors();
if constexpr (DIFF_DEBUG) { if constexpr (DIFF_DEBUG) {
for (size_t i = 0; i < m_original_lines.size(); ++i) for (size_t i = 0; i < m_original_lines.size(); ++i)
@ -149,7 +149,7 @@ DiffViewer::DiffViewer()
DiffViewer::DiffViewer(DeprecatedString const& original, DeprecatedString const& diff) DiffViewer::DiffViewer(DeprecatedString const& original, DeprecatedString const& diff)
: m_original_lines(split_to_lines(original)) : m_original_lines(split_to_lines(original))
, m_hunks(Diff::parse_hunks(diff)) , m_hunks(Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors())
{ {
setup_properties(); setup_properties();
} }

View file

@ -8,11 +8,12 @@
#include <AK/Debug.h> #include <AK/Debug.h>
namespace Diff { namespace Diff {
Vector<Hunk> parse_hunks(DeprecatedString const& diff)
ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff)
{ {
Vector<DeprecatedString> diff_lines = diff.split('\n'); Vector<DeprecatedString> diff_lines = diff.split('\n');
if (diff_lines.is_empty()) if (diff_lines.is_empty())
return {}; return Vector<Hunk> {};
Vector<Hunk> hunks; Vector<Hunk> hunks;
@ -40,12 +41,12 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
hunk.target_start_line = current_location.target_start_line; hunk.target_start_line = current_location.target_start_line;
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') { while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') {
hunk.removed_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)); TRY(hunk.removed_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)));
current_location.apply_offset(1, HunkLocation::LocationType::Original); current_location.apply_offset(1, HunkLocation::LocationType::Original);
++line_index; ++line_index;
} }
while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') { while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') {
hunk.added_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)); TRY(hunk.added_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)));
current_location.apply_offset(1, HunkLocation::LocationType::Target); current_location.apply_offset(1, HunkLocation::LocationType::Target);
++line_index; ++line_index;
} }
@ -54,7 +55,7 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
current_location.apply_offset(1, HunkLocation::LocationType::Both); current_location.apply_offset(1, HunkLocation::LocationType::Both);
++line_index; ++line_index;
} }
hunks.append(hunk); TRY(hunks.try_append(hunk));
} }
if constexpr (HUNKS_DEBUG) { if constexpr (HUNKS_DEBUG) {

View file

@ -32,6 +32,6 @@ struct Hunk {
Vector<DeprecatedString> added_lines; Vector<DeprecatedString> added_lines;
}; };
Vector<Hunk> parse_hunks(DeprecatedString const& diff); ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff);
HunkLocation parse_hunk_location(StringView location_line); HunkLocation parse_hunk_location(StringView location_line);
}; };