1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:14:58 +00:00

patch+LibDiff: Add support for applying patches with preprocessor macro

This commit is contained in:
Shannon Booth 2024-03-02 23:21:51 +13:00 committed by Andreas Kling
parent ee643b6417
commit 4bce61e508
4 changed files with 101 additions and 7 deletions

View file

@ -39,7 +39,7 @@ static ErrorOr<ByteBuffer> read_content(StringView path_of_file_to_patch, Diff::
return ByteBuffer {};
}
static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch const& patch)
static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch const& patch, Optional<StringView> const& define = {})
{
ByteBuffer content = TRY(read_content(path_of_file_to_patch, patch));
auto lines = StringView(content).lines();
@ -49,7 +49,7 @@ static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch cons
auto tmp_file = TRY(Core::File::adopt_fd(TRY(Core::System::mkstemp(tmp_output)), Core::File::OpenMode::ReadWrite));
StringView tmp_path { tmp_output, sizeof(tmp_output) };
TRY(Diff::apply_patch(*tmp_file, lines, patch));
TRY(Diff::apply_patch(*tmp_file, lines, patch, define));
// If the patched file ends up being empty, remove it, as the patch was a removal.
// Note that we cannot simply rely on the patch successfully applying and the patch claiming it is removing the file
@ -67,11 +67,13 @@ static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch cons
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView directory;
Optional<StringView> define;
Optional<size_t> strip_count;
Core::ArgsParser args_parser;
args_parser.add_option(directory, "Change the working directory to <directory> before applying the patch file", "directory", 'd', "directory");
args_parser.add_option(strip_count, "Strip given number of leading path components from file names (defaults as basename)", "strip", 'p', "count");
args_parser.add_option(define, "Apply merged patch content separated by C preprocessor macros", "ifdef", 'D', "define");
args_parser.parse(arguments);
if (!directory.is_null())
@ -100,7 +102,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
outln("patching file {}", to_patch);
TRY(do_patch(to_patch, patch));
TRY(do_patch(to_patch, patch, define));
}
return 0;