1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:07:43 +00:00

patch+LibDiff: Implement 'strip' of filenames when parsing patch

Implement the patch '-p' / '--strip' option, which strips the given
number of leading components from filenames parsed in the patch header.
If not given this option defaults to the basename of that path.
This commit is contained in:
Shannon Booth 2023-07-14 12:07:00 +12:00 committed by Andrew Kaster
parent 87e2b8e343
commit 81df0278b1
4 changed files with 80 additions and 5 deletions

View file

@ -107,3 +107,45 @@ TEST_CASE(basic_addition_patch_from_empty_file)
EXPECT_FILE_EQ(MUST(String::formatted("{}/a", s_test_dir)), "1\n2\n3\n");
}
TEST_CASE(strip_path_to_basename)
{
PatchSetup setup;
auto patch = R"(
--- /dev/null
+++ a/bunch/of/../folders/stripped/to/basename
@@ -0,0 +1 @@
+Hello, friends!
)"sv;
auto file = ""sv;
auto input = MUST(Core::File::open(MUST(String::formatted("{}/basename", s_test_dir)), Core::File::OpenMode::Write));
MUST(input->write_until_depleted(file.bytes()));
run_patch({}, patch, "patching file basename\n"sv);
EXPECT_FILE_EQ(MUST(String::formatted("{}/basename", s_test_dir)), "Hello, friends!\n");
}
TEST_CASE(strip_path_partially)
{
PatchSetup setup;
auto patch = R"(
--- /dev/null
+++ a/bunch/of/../folders/stripped/to/basename
@@ -0,0 +1 @@
+Hello, friends!
)"sv;
MUST(Core::System::mkdir(MUST(String::formatted("{}/to", s_test_dir)), 0755));
auto file = ""sv;
auto input = MUST(Core::File::open(MUST(String::formatted("{}/to/basename", s_test_dir)), Core::File::OpenMode::Write));
MUST(input->write_until_depleted(file.bytes()));
run_patch({ "-p6" }, patch, "patching file to/basename\n"sv);
EXPECT_FILE_EQ(MUST(String::formatted("{}/to/basename", s_test_dir)), "Hello, friends!\n");
}