mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 13:49:16 +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:
parent
dbd838efdf
commit
23df5748f6
Notes:
sideshowbarker
2024-07-17 17:49:11 +09:00
Author: https://github.com/shannonbooth
Commit: 23df5748f6
Pull-request: https://github.com/SerenityOS/serenity/pull/19592
Reviewed-by: https://github.com/kennethmyhra ✅
4 changed files with 10 additions and 9 deletions
|
@ -88,7 +88,7 @@ void EditorWrapper::save()
|
|||
void EditorWrapper::update_diff()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ Gfx::IntRect DiffViewer::separator_rect() const
|
|||
void DiffViewer::set_content(DeprecatedString const& original, DeprecatedString const& diff)
|
||||
{
|
||||
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) {
|
||||
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)
|
||||
: 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();
|
||||
}
|
||||
|
|
|
@ -8,11 +8,12 @@
|
|||
#include <AK/Debug.h>
|
||||
|
||||
namespace Diff {
|
||||
Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
||||
|
||||
ErrorOr<Vector<Hunk>> parse_hunks(DeprecatedString const& diff)
|
||||
{
|
||||
Vector<DeprecatedString> diff_lines = diff.split('\n');
|
||||
if (diff_lines.is_empty())
|
||||
return {};
|
||||
return Vector<Hunk> {};
|
||||
|
||||
Vector<Hunk> hunks;
|
||||
|
||||
|
@ -40,12 +41,12 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
|||
hunk.target_start_line = current_location.target_start_line;
|
||||
|
||||
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);
|
||||
++line_index;
|
||||
}
|
||||
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);
|
||||
++line_index;
|
||||
}
|
||||
|
@ -54,7 +55,7 @@ Vector<Hunk> parse_hunks(DeprecatedString const& diff)
|
|||
current_location.apply_offset(1, HunkLocation::LocationType::Both);
|
||||
++line_index;
|
||||
}
|
||||
hunks.append(hunk);
|
||||
TRY(hunks.try_append(hunk));
|
||||
}
|
||||
|
||||
if constexpr (HUNKS_DEBUG) {
|
||||
|
|
|
@ -32,6 +32,6 @@ struct Hunk {
|
|||
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);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue