diff --git a/src/applier.cpp b/src/applier.cpp index 660ca13..e558d10 100644 --- a/src/applier.cpp +++ b/src/applier.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: BSD-3-Clause // Copyright 2022-2026 Shannon Booth +#include #include #include #include @@ -163,7 +164,7 @@ static void print_hunk_statistics(std::ostream& out, size_t hunk_num, bool skipp out << " with fuzz " << location.fuzz; if (offset_error != 0) { out << " (offset " << offset_error << " line"; - if (offset_error > 1) + if (std::abs(offset_error) > 1) out << "s"; out << ")"; } diff --git a/tests/test_applier.cpp b/tests/test_applier.cpp index 414221e..ea36b37 100644 --- a/tests/test_applier.cpp +++ b/tests/test_applier.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BSD-3-Clause -// Copyright 2022-2023 Shannon Booth +// Copyright 2022-2026 Shannon Booth #include #include @@ -976,6 +976,43 @@ int main() )"); } +PATCH_TEST(applier_context_diff_with_negative_offset) +{ + { + Patch::File file("diff.patch", std::ios_base::out); + file << R"( +--- a.c 2022-10-23 21:25:13.856207590 +1300 ++++ b.c 2022-10-23 21:25:25.511912172 +1300 +@@ -3,4 +3,4 @@ + int main() + { +- return 0; ++ return 1; + } +)"; + } + + { + Patch::File file("a.c", std::ios_base::out); + file << R"(int main() +{ + return 0; +} +)"; + } + + Process process(patch_path, { patch_path, "-i", "diff.patch", nullptr }); + EXPECT_EQ(process.stdout_data(), "patching file a.c\nHunk #1 succeeded at 1 (offset -2 lines).\n"); + EXPECT_EQ(process.stderr_data(), ""); + EXPECT_EQ(process.return_code(), 0); + + EXPECT_FILE_EQ("a.c", R"(int main() +{ + return 1; +} +)"); +} + PATCH_TEST(applier_unified_diff_with_only_whitespace_change_ignore_whitespace) { {