2023-07-06 12:34:59 +00:00
|
|
|
/*
|
2023-07-15 10:33:22 +00:00
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
2023-07-06 12:34:59 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibDiff/Applier.h>
|
|
|
|
#include <LibDiff/Hunks.h>
|
|
|
|
#include <LibFileSystem/FileSystem.h>
|
|
|
|
#include <LibMain/Main.h>
|
|
|
|
|
2023-07-10 06:48:32 +00:00
|
|
|
static bool is_adding_file(Diff::Patch const& patch)
|
|
|
|
{
|
|
|
|
return patch.hunks[0].location.old_range.start_line == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<ByteBuffer> read_content(StringView path_of_file_to_patch, Diff::Patch const& patch)
|
|
|
|
{
|
|
|
|
auto file_to_patch_or_error = Core::File::open(path_of_file_to_patch, Core::File::OpenMode::Read);
|
|
|
|
|
|
|
|
// Trivial case - no error reading the file.
|
|
|
|
if (!file_to_patch_or_error.is_error())
|
|
|
|
return TRY(file_to_patch_or_error.release_value()->read_until_eof());
|
|
|
|
|
|
|
|
auto const& error = file_to_patch_or_error.error();
|
|
|
|
|
|
|
|
// If the patch is adding a file then it is fine for opening the file to error out if it did not exist.
|
|
|
|
if (!is_adding_file(patch) || !error.is_errno() || error.code() != ENOENT)
|
|
|
|
return file_to_patch_or_error.release_error();
|
|
|
|
|
|
|
|
return ByteBuffer {};
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:34:59 +00:00
|
|
|
static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch const& patch)
|
|
|
|
{
|
2023-07-10 06:48:32 +00:00
|
|
|
ByteBuffer content = TRY(read_content(path_of_file_to_patch, patch));
|
2023-07-06 12:34:59 +00:00
|
|
|
auto lines = StringView(content).lines();
|
|
|
|
|
|
|
|
// Apply patch to a temporary file in case one or more of the hunks fails.
|
|
|
|
char tmp_output[] = "/tmp/patch.XXXXXX";
|
|
|
|
auto tmp_file = TRY(Core::File::adopt_fd(TRY(Core::System::mkstemp(tmp_output)), Core::File::OpenMode::ReadWrite));
|
|
|
|
|
|
|
|
TRY(Diff::apply_patch(*tmp_file, lines, patch));
|
|
|
|
|
|
|
|
return FileSystem::move_file(path_of_file_to_patch, StringView { tmp_output, sizeof(tmp_output) });
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
2023-07-13 23:20:03 +00:00
|
|
|
StringView directory;
|
2023-07-14 00:07:00 +00:00
|
|
|
Optional<size_t> strip_count;
|
2023-07-13 23:20:03 +00:00
|
|
|
|
2023-07-06 12:34:59 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2023-07-13 23:20:03 +00:00
|
|
|
args_parser.add_option(directory, "Change the working directory to <directory> before applying the patch file", "directory", 'd', "directory");
|
2023-07-14 00:07:00 +00:00
|
|
|
args_parser.add_option(strip_count, "Strip given number of leading path components from file names (defaults as basename)", "strip", 'p', "count");
|
2023-07-06 12:34:59 +00:00
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2023-07-13 23:20:03 +00:00
|
|
|
if (!directory.is_null())
|
|
|
|
TRY(Core::System::chdir(directory));
|
|
|
|
|
2023-07-06 12:34:59 +00:00
|
|
|
auto input = TRY(Core::File::standard_input());
|
|
|
|
|
|
|
|
auto patch_content = TRY(input->read_until_eof());
|
|
|
|
|
|
|
|
Diff::Parser parser(patch_content);
|
|
|
|
|
2023-07-11 22:37:26 +00:00
|
|
|
while (!parser.is_eof()) {
|
|
|
|
Diff::Patch patch = TRY(parser.parse_patch(strip_count));
|
|
|
|
|
|
|
|
if (patch.header.format == Diff::Format::Unknown)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// FIXME: Support adding/removing a file, and asking for file to patch as fallback otherwise.
|
|
|
|
StringView to_patch;
|
|
|
|
if (FileSystem::is_regular_file(patch.header.old_file_path)) {
|
|
|
|
to_patch = patch.header.old_file_path;
|
|
|
|
} else if (is_adding_file(patch) || FileSystem::is_regular_file(patch.header.new_file_path)) {
|
|
|
|
to_patch = patch.header.new_file_path;
|
|
|
|
} else {
|
|
|
|
warnln("Unable to determine file to patch");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
outln("patching file {}", to_patch);
|
|
|
|
TRY(do_patch(to_patch, patch));
|
|
|
|
}
|
2023-07-06 12:34:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|