Skip to content

Commit

Permalink
patch: simplify handling of adding a file
Browse files Browse the repository at this point in the history
Instead of using a separately managed variable, just fully rely on what
the patch header is telling us.
  • Loading branch information
shannonbooth committed Jul 9, 2023
1 parent 9fd8665 commit 1b04d16
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ static std::string guess_filepath(const Patch& patch)
if (patch.index_file_path != "/dev/null" && filesystem::exists(patch.index_file_path))
return patch.index_file_path;

if (patch.operation == Operation::Add)
return patch.new_file_path;

return {};
}

Expand Down Expand Up @@ -410,23 +413,6 @@ int process_patch(const Options& options)

first_patch = false;

std::string file_to_patch;
if (!options.file_to_patch.empty())
file_to_patch = options.file_to_patch;
else
file_to_patch = guess_filepath(patch);

bool looks_like_adding_file = false;

if (patch.operation == Operation::Add) {
if (file_to_patch.empty()) {
file_to_patch = patch.new_file_path;
looks_like_adding_file = true;
} else if (!options.file_to_patch.empty()) {
looks_like_adding_file = true;
}
}

if (patch.operation == Operation::Binary) {
out << "File " << (options.reverse_patch ? patch.new_file_path : patch.old_file_path) << ": git binary diffs are not supported.\n";
had_failure = true;
Expand All @@ -436,6 +422,8 @@ int process_patch(const Options& options)
if (options.verbose)
out << "Hmm... Looks like a " << to_string(info.format) << " diff to me...\n";

auto file_to_patch = options.file_to_patch.empty() ? guess_filepath(patch) : options.file_to_patch;

if (file_to_patch.empty()) {
out << "can't find file to patch at input line " << parser.line_number()
<< "\nPerhaps you "
Expand Down Expand Up @@ -470,7 +458,7 @@ int process_patch(const Options& options)
File tmp_reject_file = File::create_temporary();
RejectWriter reject_writer(patch, tmp_reject_file, options.reject_format);

if (!looks_like_adding_file && !filesystem::is_regular_file(file_to_patch)) {
if (filesystem::exists(file_to_patch) && !filesystem::is_regular_file(file_to_patch)) {
if (should_parse_body)
parser.parse_patch_body(patch);
out << "File " << file_to_patch << " is not a regular file --";
Expand Down Expand Up @@ -502,11 +490,9 @@ int process_patch(const Options& options)
}

File input_file;
if (!looks_like_adding_file || filesystem::exists(file_to_patch)) {
input_file.open(file_to_patch, looks_like_adding_file ? mode : mode | std::ios_base::in);
if (!input_file)
throw std::system_error(errno, std::generic_category(), "Unable to open input file " + file_to_patch);
}
input_file.open(file_to_patch, mode | std::ios_base::in);
if (!input_file && (errno != ENOENT || patch.operation != Operation::Add))
throw std::system_error(errno, std::generic_category(), "Unable to open input file " + file_to_patch);

const auto input_lines = file_as_lines(input_file);

Expand Down Expand Up @@ -550,7 +536,7 @@ int process_patch(const Options& options)
backup.make_backup_for(output_file);

// Ensure that parent directories exist if we are adding a file.
if (looks_like_adding_file)
if (patch.operation == Operation::Add)
ensure_parent_directories(output_file);

const auto new_mode_copy = patch.new_file_mode;
Expand Down

0 comments on commit 1b04d16

Please sign in to comment.