diff --git a/src/patch.cpp b/src/patch.cpp index bc725c1..2f05279 100644 --- a/src/patch.cpp +++ b/src/patch.cpp @@ -608,27 +608,30 @@ int process_patch(const Options& options) // Nothing else to do other than write to stdout :^) tmp_out_file.write_entire_contents_to(stdout); } else { - if (!options.dry_run) { + bool write_to_file = !options.dry_run; + + // Clean up the file if it looks like it was removed. + // NOTE: we check for file size for the degenerate case that the file is a removal, but has nothing left. + if (options.remove_empty_files == Options::OptionalBool::Yes && patch.operation == Operation::Delete) { + if (tmp_out_file.size() == 0) { + if (!options.dry_run) + remove_file_and_empty_parent_folders(output_file); + write_to_file = false; + } else { + out << "Not deleting file " << output_file << " as content differs from patch\n"; + had_failure = true; + } + } + + if (write_to_file) { if (options.save_backup || (!result.all_hunks_applied_perfectly && !result.was_skipped && options.backup_if_mismatch == Options::OptionalBool::Yes)) backup.make_backup_for(output_file); write_patched_result_to_file(patch, output_file, permission_result, mode, deferred_writer, tmp_out_file); } if (result.failed_hunks == 0) { - if (!options.dry_run && patch.operation == Operation::Rename) + if (write_to_file && patch.operation == Operation::Rename) remove_file_and_empty_parent_folders(file_to_patch); - - // Clean up the file if it looks like it was removed. - // NOTE: we check for file size for the degenerate case that the file is a removal, but has nothing left. - if (options.remove_empty_files == Options::OptionalBool::Yes && patch.operation == Operation::Delete) { - if (filesystem::file_size(output_file) == 0) { - if (!options.dry_run) - remove_file_and_empty_parent_folders(output_file); - } else { - out << "Not deleting file " << output_file << " as content differs from patch\n"; - had_failure = true; - } - } } } } diff --git a/tests/test_basic.cpp b/tests/test_basic.cpp index 54db5d7..4002c38 100644 --- a/tests/test_basic.cpp +++ b/tests/test_basic.cpp @@ -399,6 +399,32 @@ Hunk #1 FAILED at 1. EXPECT_FALSE(Patch::filesystem::exists("1.orig")); } +PATCH_TEST(basic_patch_dry_run_remove_file) +{ + { + Patch::File file("diff.patch", std::ios_base::out); + file << R"( +--- a ++++ /dev/null +@@ -1 +0,0 @@ +-1 +)"; + } + + std::string to_patch = "1\n"; + { + Patch::File file("a", std::ios_base::out); + file << to_patch; + } + + Process process(patch_path, { patch_path, "-i", "diff.patch", "--dry-run", nullptr }); + + EXPECT_EQ(process.stdout_data(), "checking file a\n"); + EXPECT_EQ(process.stderr_data(), ""); + EXPECT_EQ(process.return_code(), 0); + EXPECT_FILE_EQ("a", "1\n"); +} + PATCH_TEST(set_patch_file) { {