2020-09-12 17:38:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2023-06-24 05:33:04 +00:00
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
|
2020-09-12 17:38:55 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 17:38:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-06-24 05:33:04 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Format.h>
|
2023-07-02 07:19:22 +00:00
|
|
|
#include <AK/GenericLexer.h>
|
2023-06-24 01:17:24 +00:00
|
|
|
#include <AK/String.h>
|
2023-06-24 01:13:50 +00:00
|
|
|
#include <AK/StringView.h>
|
2020-09-12 17:38:55 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace Diff {
|
|
|
|
|
2023-06-24 05:33:04 +00:00
|
|
|
struct Range {
|
|
|
|
size_t start_line { 0 };
|
|
|
|
size_t number_of_lines { 0 };
|
|
|
|
};
|
|
|
|
|
2020-09-12 17:38:55 +00:00
|
|
|
struct HunkLocation {
|
2023-06-24 05:33:04 +00:00
|
|
|
Range old_range;
|
|
|
|
Range new_range;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Line {
|
|
|
|
enum class Operation {
|
|
|
|
Addition = '+',
|
|
|
|
Removal = '-',
|
|
|
|
Context = ' ',
|
|
|
|
|
2023-07-03 05:16:16 +00:00
|
|
|
// NOTE: This should only be used when deconstructing a hunk into old and new lines (context format)
|
|
|
|
Change = '!',
|
2020-09-12 17:38:55 +00:00
|
|
|
};
|
2023-06-24 05:33:04 +00:00
|
|
|
|
|
|
|
static constexpr Operation operation_from_symbol(char symbol)
|
|
|
|
{
|
|
|
|
switch (symbol) {
|
|
|
|
case '+':
|
|
|
|
return Operation::Addition;
|
|
|
|
case '-':
|
|
|
|
return Operation::Removal;
|
|
|
|
case ' ':
|
|
|
|
return Operation::Context;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation operation;
|
|
|
|
String content;
|
2020-09-12 17:38:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Hunk {
|
2023-06-24 05:33:04 +00:00
|
|
|
HunkLocation location;
|
|
|
|
Vector<Line> lines;
|
2020-09-12 17:38:55 +00:00
|
|
|
};
|
|
|
|
|
2023-07-06 08:52:26 +00:00
|
|
|
enum class Format {
|
|
|
|
Unified,
|
|
|
|
Unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Header {
|
|
|
|
Format format { Format::Unknown };
|
|
|
|
|
|
|
|
String old_file_path;
|
|
|
|
String new_file_path;
|
|
|
|
};
|
|
|
|
|
2023-07-02 07:19:22 +00:00
|
|
|
class Parser : public GenericLexer {
|
|
|
|
public:
|
|
|
|
using GenericLexer::GenericLexer;
|
|
|
|
|
|
|
|
ErrorOr<Vector<Hunk>> parse_hunks();
|
|
|
|
|
2023-07-06 08:52:26 +00:00
|
|
|
ErrorOr<Header> parse_header();
|
|
|
|
|
2023-07-02 07:19:22 +00:00
|
|
|
private:
|
|
|
|
Optional<HunkLocation> consume_unified_location();
|
|
|
|
bool consume_line_number(size_t& number);
|
2020-09-12 17:38:55 +00:00
|
|
|
};
|
2023-06-24 05:33:04 +00:00
|
|
|
|
2023-07-02 07:19:22 +00:00
|
|
|
ErrorOr<Vector<Hunk>> parse_hunks(StringView diff);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-24 05:33:04 +00:00
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Diff::Line::Operation> : Formatter<FormatString> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Diff::Line::Operation operation)
|
|
|
|
{
|
|
|
|
return Formatter<FormatString>::format(builder, "{}"sv, static_cast<char>(operation));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Diff::Line> : Formatter<FormatString> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Diff::Line const& line)
|
|
|
|
{
|
|
|
|
return Formatter<FormatString>::format(builder, "{}{}"sv, line.operation, line.content);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Diff::HunkLocation> : Formatter<FormatString> {
|
|
|
|
static ErrorOr<void> format(FormatBuilder& format_builder, Diff::HunkLocation const& location)
|
|
|
|
{
|
|
|
|
auto& builder = format_builder.builder();
|
|
|
|
TRY(builder.try_appendff("@@ -{}"sv, location.old_range.start_line));
|
|
|
|
|
|
|
|
if (location.old_range.number_of_lines != 1)
|
|
|
|
TRY(builder.try_appendff(",{}", location.old_range.number_of_lines));
|
|
|
|
|
|
|
|
TRY(builder.try_appendff(" +{}", location.new_range.start_line));
|
|
|
|
|
|
|
|
if (location.new_range.number_of_lines != 1)
|
|
|
|
TRY(builder.try_appendff(",{}", location.new_range.number_of_lines));
|
|
|
|
|
|
|
|
return builder.try_appendff(" @@");
|
|
|
|
}
|
|
|
|
};
|