Skip to content

Commit

Permalink
patch: factor out a common inform_hunks_failed function
Browse files Browse the repository at this point in the history
We already had two places which implemented this logic, and I am
thinking of adding a third. Factor out a function to show this user
input.
  • Loading branch information
shannonbooth committed Feb 4, 2024
1 parent 1ea414d commit 76a4b65
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/patch.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 <algorithm>
#include <cassert>
Expand Down Expand Up @@ -175,13 +175,18 @@ static std::string reject_path(const Options& options, const std::string& output
return options.reject_file_path.empty() ? output_file + ".rej" : options.reject_file_path;
}

static void refuse_to_patch(std::ostream& out, std::ios_base::openmode mode, const std::string& output_file, const Patch& patch, const Options& options)
static void inform_hunks_failed(std::ostream& out, const char* reason, const std::vector<Hunk>& hunks, size_t number_of_hunks_failed)
{
out << " refusing to patch\n"
<< patch.hunks.size() << " out of " << patch.hunks.size() << " hunk";
if (patch.hunks.size() > 1)
out << number_of_hunks_failed << " out of " << hunks.size() << " hunk";
if (hunks.size() > 1)
out << 's';
out << " ignored";
out << ' ' << reason;
}

static void refuse_to_patch(std::ostream& out, std::ios_base::openmode mode, const std::string& output_file, const Patch& patch, const Options& options)
{
out << " refusing to patch\n";
inform_hunks_failed(out, "ignored", patch.hunks, patch.hunks.size());

if (!options.dry_run) {
const auto reject_file = reject_path(options, output_file);
Expand Down Expand Up @@ -578,11 +583,8 @@ int process_patch(const Options& options)

if (result.failed_hunks != 0) {
had_failure = true;
const char* reason = result.was_skipped ? " ignored" : " FAILED";
out << result.failed_hunks << " out of " << patch.hunks.size() << " hunk";
if (patch.hunks.size() > 1)
out << 's';
out << reason;
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;
Expand Down

0 comments on commit 76a4b65

Please sign in to comment.