Skip to content

Commit

Permalink
patch: do not issue spurious warning removing a file in dry-run
Browse files Browse the repository at this point in the history
Since dry run was not writing to the output file, the file size was
being determined as non-empty. To fix this, simply check for this on the
temporary file which has been written to, even on a dry run.
  • Loading branch information
shannonbooth committed Feb 11, 2024
1 parent b230da8 commit bd7ff50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/test_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
{
Expand Down

0 comments on commit bd7ff50

Please sign in to comment.