Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: also write reject file when outputting to stdout #70

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,20 @@ int process_patch(const Options& options)

Result result = apply_patch(tmp_out_file, reject_writer, input_lines, patch, options, out);

if (result.failed_hunks != 0) {
had_failure = true;
const char* reason = result.was_skipped ? "ignored" : "FAILED";
inform_hunks_failed(out, reason, patch.hunks, result.failed_hunks);
if (!options.dry_run) {
const auto reject_file = reject_path(options, output_file);
out << " -- saving rejects to file " << reject_file;

File file(reject_file, mode | std::ios::trunc);
tmp_reject_file.write_entire_contents_to(file);
}
out << '\n';
}

if (output_to_stdout) {
// Nothing else to do other than write to stdout :^)
tmp_out_file.write_entire_contents_to(stdout);
Expand All @@ -600,19 +614,7 @@ int process_patch(const Options& options)
write_patched_result_to_file(patch, output_file, permission_result, mode, deferred_writer, tmp_out_file);
}

if (result.failed_hunks != 0) {
had_failure = true;
const char* reason = result.was_skipped ? "ignored" : "FAILED";
inform_hunks_failed(out, reason, patch.hunks, result.failed_hunks);
if (!options.dry_run) {
const auto reject_file = reject_path(options, output_file);
out << " -- saving rejects to file " << reject_file;

File file(reject_file, mode | std::ios::trunc);
tmp_reject_file.write_entire_contents_to(file);
}
out << '\n';
} else {
if (result.failed_hunks == 0) {
if (!options.dry_run && patch.operation == Operation::Rename)
remove_file_and_empty_parent_folders(file_to_patch);

Expand Down
36 changes: 35 additions & 1 deletion tests/test_basic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright 2022-2023 Shannon Booth <[email protected]>
// Copyright 2022-2024 Shannon Booth <[email protected]>

#include <patch/file.h>
#include <patch/process.h>
Expand Down Expand Up @@ -329,6 +329,40 @@ PATCH_TEST(basic_patch_with_dry_run_to_stdout)
EXPECT_FILE_EQ("to_patch", to_patch);
}

PATCH_TEST(failed_patch_to_stdout)
{
const std::string patch = R"(--- 1 2022-06-26 12:22:22.161398905 +1200
+++ 2 2022-06-26 12:22:44.105278030 +1200
@@ -1,3 +1,2 @@
1
-2
3
)";
{
Patch::File file("diff.patch", std::ios_base::out);
file << patch;
file.close();
}

std::string to_patch = "a\nb\nc\n";
{
Patch::File file("1", std::ios_base::out);
file << to_patch;
file.close();
}

Process process(patch_path, { patch_path, "-i", "diff.patch", "-o", "-", "--force", nullptr });

EXPECT_EQ(process.stdout_data(), to_patch);
EXPECT_EQ(process.stderr_data(), R"(patching file - (read from 1)
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file -.rej
)");
EXPECT_EQ(process.return_code(), 1);
EXPECT_FILE_EQ("1", to_patch);
EXPECT_FILE_EQ("-.rej", patch);
}

PATCH_TEST(failed_patch_dry_run)
{
{
Expand Down
Loading