patch: Add the beginnings of a patch utility
This is still very bare bones, and there is _much_ more to still handle. However, this implements enough functionality to parse a single unified patch read from stdin, and apply it to a file.
This commit is contained in:
parent
828d791a4f
commit
e838b6c8cc
Notes:
sideshowbarker
2024-07-17 04:41:05 +09:00
Author: https://github.com/shannonbooth Commit: https://github.com/SerenityOS/serenity/commit/e838b6c8cc Pull-request: https://github.com/SerenityOS/serenity/pull/19844 Reviewed-by: https://github.com/AtkinsSJ ✅
2 changed files with 62 additions and 1 deletions
|
@ -3,7 +3,7 @@ list(APPEND SPECIAL_TARGETS test install)
|
|||
list(APPEND REQUIRED_TARGETS
|
||||
arp base64 basename cat chmod chown clear comm cp cut date dd df diff dirname dmesg du echo env expr false
|
||||
file find grep groups head host hostname id ifconfig kill killall ln logout ls mkdir mount mv network-settings nproc
|
||||
pgrep pidof ping pkill pmap ps readlink realpath reboot rm rmdir sed route seq shutdown sleep sort stat stty su tail test
|
||||
patch pgrep pidof ping pkill pmap ps readlink realpath reboot rm rmdir sed route seq shutdown sleep sort stat stty su tail test
|
||||
touch tr true umount uname uniq uptime w wc which whoami xargs yes
|
||||
)
|
||||
list(APPEND RECOMMENDED_TARGETS
|
||||
|
@ -123,6 +123,7 @@ target_link_libraries(notify PRIVATE LibGfx LibGUI)
|
|||
target_link_libraries(open PRIVATE LibDesktop LibFileSystem)
|
||||
target_link_libraries(passwd PRIVATE LibCrypt)
|
||||
target_link_libraries(paste PRIVATE LibGUI)
|
||||
target_link_libraries(patch PRIVATE LibDiff LibFileSystem)
|
||||
target_link_libraries(pdf PRIVATE LibGfx LibPDF)
|
||||
target_link_libraries(pgrep PRIVATE LibRegex)
|
||||
target_link_libraries(pixelflut PRIVATE LibImageDecoderClient LibIPC LibGfx)
|
||||
|
|
60
Userland/Utilities/patch.cpp
Normal file
60
Userland/Utilities/patch.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
|
||||
*
|
||||
* 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>
|
||||
|
||||
static ErrorOr<void> do_patch(StringView path_of_file_to_patch, Diff::Patch const& patch)
|
||||
{
|
||||
auto file_to_patch = TRY(Core::File::open(path_of_file_to_patch, Core::File::OpenMode::Read));
|
||||
auto content = TRY(file_to_patch->read_until_eof());
|
||||
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)
|
||||
{
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto input = TRY(Core::File::standard_input());
|
||||
|
||||
auto patch_content = TRY(input->read_until_eof());
|
||||
|
||||
// FIXME: Support multiple patches in the patch file.
|
||||
Diff::Parser parser(patch_content);
|
||||
Diff::Patch patch;
|
||||
patch.header = TRY(parser.parse_header());
|
||||
patch.hunks = TRY(parser.parse_hunks());
|
||||
|
||||
// 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 (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));
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue