Applier.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Stream.h>
  7. #include <LibDiff/Applier.h>
  8. #include <LibDiff/Hunks.h>
  9. namespace Diff {
  10. static size_t expected_line_number(HunkLocation const& location)
  11. {
  12. auto line = location.old_range.start_line;
  13. // NOTE: This is to handle the case we are adding a file, e.g for a range such as:
  14. // '@@ -0,0 +1,3 @@'
  15. if (location.old_range.start_line == 0)
  16. ++line;
  17. VERIFY(line != 0);
  18. return line;
  19. }
  20. struct Location {
  21. size_t line_number;
  22. size_t fuzz { 0 };
  23. ssize_t offset { 0 };
  24. };
  25. static Optional<Location> locate_hunk(Vector<StringView> const& content, Hunk const& hunk, ssize_t offset, size_t max_fuzz = 3)
  26. {
  27. // Make a first best guess at where the from-file range is telling us where the hunk should be.
  28. size_t offset_guess = expected_line_number(hunk.location) - 1 + offset;
  29. // If there's no lines surrounding this hunk - it will always succeed, so there is no point in checking any further.
  30. if (hunk.location.old_range.number_of_lines == 0)
  31. return Location { offset_guess, 0, 0 };
  32. size_t patch_prefix_context = 0;
  33. for (auto const& line : hunk.lines) {
  34. if (line.operation != Line::Operation::Context)
  35. break;
  36. ++patch_prefix_context;
  37. }
  38. size_t patch_suffix_context = 0;
  39. for (auto const& line : hunk.lines.in_reverse()) {
  40. if (line.operation != Line::Operation::Context)
  41. break;
  42. ++patch_suffix_context;
  43. }
  44. size_t context = max(patch_prefix_context, patch_suffix_context);
  45. // Look through the file trying to match the hunk for it. If we can't find anything anywhere in the file, then try and
  46. // match the hunk by ignoring an increasing amount of context lines. The number of context lines that are ignored is
  47. // called the 'fuzz'.
  48. for (size_t fuzz = 0; fuzz <= max_fuzz; ++fuzz) {
  49. auto suffix_fuzz = max(fuzz + patch_suffix_context - context, 0);
  50. auto prefix_fuzz = max(fuzz + patch_prefix_context - context, 0);
  51. // If the fuzz is greater than the total number of lines for a hunk, then it may be possible for the hunk to match anything.
  52. if (suffix_fuzz + prefix_fuzz >= hunk.lines.size())
  53. return {};
  54. auto hunk_matches_starting_from_line = [&](size_t line) {
  55. line += prefix_fuzz;
  56. // Ensure that all of the lines in the hunk match starting from 'line', ignoring the specified number of context lines.
  57. return all_of(hunk.lines.begin() + prefix_fuzz, hunk.lines.end() - suffix_fuzz, [&](const Line& hunk_line) {
  58. // Ignore additions in our increment of line and comparison as they are not part of the 'original file'
  59. if (hunk_line.operation == Line::Operation::Addition)
  60. return true;
  61. if (line >= content.size())
  62. return false;
  63. if (content[line] != hunk_line.content)
  64. return false;
  65. ++line;
  66. return true;
  67. });
  68. };
  69. for (size_t line = offset_guess; line < content.size(); ++line) {
  70. if (hunk_matches_starting_from_line(line))
  71. return Location { line, fuzz, static_cast<ssize_t>(line - offset_guess) };
  72. }
  73. for (size_t line = offset_guess; line != 0; --line) {
  74. if (hunk_matches_starting_from_line(line - 1))
  75. return Location { line - 1, fuzz, static_cast<ssize_t>(line - offset_guess) };
  76. }
  77. }
  78. // No bueno.
  79. return {};
  80. }
  81. static ErrorOr<size_t> write_hunk(Stream& out, Hunk const& hunk, Location const& location, Vector<StringView> const& lines)
  82. {
  83. auto line_number = location.line_number;
  84. for (auto const& patch_line : hunk.lines) {
  85. if (patch_line.operation == Line::Operation::Context) {
  86. TRY(out.write_formatted("{}\n", lines.at(line_number)));
  87. ++line_number;
  88. } else if (patch_line.operation == Line::Operation::Addition) {
  89. TRY(out.write_formatted("{}\n", patch_line.content));
  90. } else if (patch_line.operation == Line::Operation::Removal) {
  91. ++line_number;
  92. }
  93. }
  94. return line_number;
  95. }
  96. ErrorOr<void> apply_patch(Stream& out, Vector<StringView> const& lines, Patch const& patch)
  97. {
  98. size_t line_number = 0; // NOTE: relative to 'old' file.
  99. ssize_t offset_error = 0;
  100. for (size_t hunk_num = 0; hunk_num < patch.hunks.size(); ++hunk_num) {
  101. auto const& hunk = patch.hunks[hunk_num];
  102. auto maybe_location = locate_hunk(lines, hunk, offset_error);
  103. if (!maybe_location.has_value())
  104. return Error::from_string_literal("Failed to locate where to apply patch");
  105. auto location = *maybe_location;
  106. offset_error += location.offset;
  107. // Write up until where we have found this latest hunk from the old file.
  108. for (; line_number < location.line_number; ++line_number)
  109. TRY(out.write_formatted("{}\n", lines.at(line_number)));
  110. // Then output the hunk to what we hope is the correct location in the file.
  111. line_number = TRY(write_hunk(out, hunk, location, lines));
  112. }
  113. // We've finished applying all hunks, write out anything from the old file we haven't already.
  114. for (; line_number < lines.size(); ++line_number)
  115. TRY(out.write_formatted("{}\n", lines[line_number]));
  116. return {};
  117. }
  118. }