mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +00:00
diff: Add support for writing diffs in unified format with context
This commit is contained in:
parent
55a3dfec10
commit
2c9db2de05
1 changed files with 14 additions and 3 deletions
|
@ -18,11 +18,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
TRY(Core::System::pledge("stdio rpath"));
|
TRY(Core::System::pledge("stdio rpath"));
|
||||||
|
|
||||||
Core::ArgsParser parser;
|
Core::ArgsParser parser;
|
||||||
|
|
||||||
|
bool unified = false;
|
||||||
StringView filename1;
|
StringView filename1;
|
||||||
StringView filename2;
|
StringView filename2;
|
||||||
|
|
||||||
parser.add_positional_argument(filename1, "First file to compare", "file1", Core::ArgsParser::Required::Yes);
|
parser.add_positional_argument(filename1, "First file to compare", "file1", Core::ArgsParser::Required::Yes);
|
||||||
parser.add_positional_argument(filename2, "Second file to compare", "file2", Core::ArgsParser::Required::Yes);
|
parser.add_positional_argument(filename2, "Second file to compare", "file2", Core::ArgsParser::Required::Yes);
|
||||||
|
parser.add_option(unified, "Write diff in unified format", nullptr, 'u');
|
||||||
parser.parse(arguments);
|
parser.parse(arguments);
|
||||||
|
|
||||||
auto file1 = TRY(Core::File::open(filename1, Core::File::OpenMode::Read));
|
auto file1 = TRY(Core::File::open(filename1, Core::File::OpenMode::Read));
|
||||||
|
@ -31,9 +34,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
auto const color_output = TRY(Core::System::isatty(STDOUT_FILENO)) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No;
|
auto const color_output = TRY(Core::System::isatty(STDOUT_FILENO)) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No;
|
||||||
|
|
||||||
auto hunks = TRY(Diff::from_text(TRY(file1->read_until_eof()), TRY(file2->read_until_eof())));
|
size_t context = unified ? 3 : 0;
|
||||||
for (auto const& hunk : hunks)
|
auto hunks = TRY(Diff::from_text(TRY(file1->read_until_eof()), TRY(file2->read_until_eof()), context));
|
||||||
TRY(Diff::write_normal(hunk, *out, color_output));
|
|
||||||
|
if (unified) {
|
||||||
|
TRY(Diff::write_unified_header(filename1, filename2, *out));
|
||||||
|
for (auto const& hunk : hunks)
|
||||||
|
TRY(Diff::write_unified(hunk, *out, color_output));
|
||||||
|
} else {
|
||||||
|
for (auto const& hunk : hunks)
|
||||||
|
TRY(Diff::write_normal(hunk, *out, color_output));
|
||||||
|
}
|
||||||
|
|
||||||
return hunks.is_empty() ? 0 : 1;
|
return hunks.is_empty() ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue