Skip to content
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
2 changes: 2 additions & 0 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ bool Parser::parse_patch_header(Patch& patch, PatchHeaderInfo& header_info, int
patch.operation = Operation::Delete;
else if (hunk.old_file_range.start_line == 0)
patch.operation = Operation::Add;
} else if (hunk.old_file_range.start_line == 0) {
patch.operation = Operation::Add;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,15 @@ int process_patch(const Options& options)
if (options.verbose || file_to_patch.empty())
parser.print_header_info(info, out);

if (file_to_patch.empty())
if (file_to_patch.empty() && !options.force && !options.batch)
file_to_patch = prompt_for_filepath(out);

if (file_to_patch.empty()) {
if (should_parse_body)
parser.parse_patch_body(patch);

if (options.force || options.batch)
out << "No file to patch. ";
out << "Skipping patch.\n";
inform_hunks_failed(out, "ignored", patch.hunks, patch.hunks.size());
out << '\n';
Expand Down
68 changes: 68 additions & 0 deletions tests/test_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,74 @@ PATCH_TEST(add_file)
)");
}

PATCH_TEST(add_file_without_dev_null_header)
{
{
Patch::File file("diff.patch", std::ios_base::out);

file << R"(--- add
+++ add
@@ -0,0 +1,3 @@
+int main()
+{
+}
)";
}

EXPECT_FALSE(Patch::filesystem::exists("add"));

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

EXPECT_EQ(process.stdout_data(), "patching file add\n");
EXPECT_EQ(process.stderr_data(), "");
EXPECT_EQ(process.return_code(), 0);
EXPECT_FILE_EQ("add", R"(int main()
{
}
)");
}

static void file_not_found_does_not_prompt(const char* patch_path, const char* option)
{
{
Patch::File file("diff.patch", std::ios_base::out);

file << R"(--- a 2023-01-03 11:23:35.634966282 +1300
+++ b 2023-01-03 11:23:41.598960625 +1300
@@ -1,3 +1,3 @@
1
-2
+b
3
)";
}

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

EXPECT_EQ(process.stdout_data(), R"(can't find file to patch at input line 3
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|--- a 2023-01-03 11:23:35.634966282 +1300
|+++ b 2023-01-03 11:23:41.598960625 +1300
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
)");
EXPECT_EQ(process.stderr_data(), "");
EXPECT_EQ(process.return_code(), 1);
}

PATCH_TEST(file_not_found_force_does_not_prompt)
{
file_not_found_does_not_prompt(patch_path, "--force");
}

PATCH_TEST(file_not_found_batch_does_not_prompt)
{
file_not_found_does_not_prompt(patch_path, "--batch");
}

PATCH_TEST(add_file_using_basename)
{
{
Expand Down
Loading