diff_parser.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (C) 2009 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """WebKit's Python module for interacting with patches."""
  29. import logging
  30. import re
  31. _regexp_compile_cache = {}
  32. def match(pattern, string):
  33. """Matches the string with the pattern, caching the compiled regexp."""
  34. if not pattern in _regexp_compile_cache:
  35. _regexp_compile_cache[pattern] = re.compile(pattern)
  36. return _regexp_compile_cache[pattern].match(string)
  37. def git_diff_to_svn_diff(line):
  38. """Converts a git formatted diff line to a svn formatted line.
  39. Args:
  40. line: A string representing a line of the diff.
  41. """
  42. conversion_patterns = (("^diff --git a/(.+) b/(?P<FilePath>.+)", lambda matched: "Index: " + matched.group('FilePath') + "\n"),
  43. ("^new file.*", lambda matched: "\n"),
  44. ("^index [0-9a-f]{7}\.\.[0-9a-f]{7} [0-9]{6}", lambda matched: "===================================================================\n"),
  45. ("^--- a/(?P<FilePath>.+)", lambda matched: "--- " + matched.group('FilePath') + "\n"),
  46. ("^\+\+\+ b/(?P<FilePath>.+)", lambda matched: "+++ " + matched.group('FilePath') + "\n"))
  47. for pattern, conversion in conversion_patterns:
  48. matched = match(pattern, line)
  49. if matched:
  50. return conversion(matched)
  51. return line
  52. def get_diff_converter(first_diff_line):
  53. """Gets a converter function of diff lines.
  54. Args:
  55. first_diff_line: The first filename line of a diff file.
  56. If this line is git formatted, we'll return a
  57. converter from git to SVN.
  58. """
  59. if match(r"^diff --git a/", first_diff_line):
  60. return git_diff_to_svn_diff
  61. return lambda input: input
  62. _INITIAL_STATE = 1
  63. _DECLARED_FILE_PATH = 2
  64. _PROCESSING_CHUNK = 3
  65. class DiffFile:
  66. """Contains the information for one file in a patch.
  67. The field "lines" is a list which contains tuples in this format:
  68. (deleted_line_number, new_line_number, line_string)
  69. If deleted_line_number is zero, it means this line is newly added.
  70. If new_line_number is zero, it means this line is deleted.
  71. """
  72. def __init__(self, filename):
  73. self.filename = filename
  74. self.lines = []
  75. def add_new_line(self, line_number, line):
  76. self.lines.append((0, line_number, line))
  77. def add_deleted_line(self, line_number, line):
  78. self.lines.append((line_number, 0, line))
  79. def add_unchanged_line(self, deleted_line_number, new_line_number, line):
  80. self.lines.append((deleted_line_number, new_line_number, line))
  81. class DiffParser:
  82. """A parser for a patch file.
  83. The field "files" is a dict whose key is the filename and value is
  84. a DiffFile object.
  85. """
  86. def __init__(self, diff_input):
  87. """Parses a diff.
  88. Args:
  89. diff_input: An iterable object.
  90. """
  91. state = _INITIAL_STATE
  92. self.files = {}
  93. current_file = None
  94. old_diff_line = None
  95. new_diff_line = None
  96. for line in diff_input:
  97. line = line.rstrip("\n")
  98. if state == _INITIAL_STATE:
  99. transform_line = get_diff_converter(line)
  100. line = transform_line(line)
  101. file_declaration = match(r"^Index: (?P<FilePath>.+)", line)
  102. if file_declaration:
  103. filename = file_declaration.group('FilePath')
  104. current_file = DiffFile(filename)
  105. self.files[filename] = current_file
  106. state = _DECLARED_FILE_PATH
  107. continue
  108. lines_changed = match(r"^@@ -(?P<OldStartLine>\d+)(,\d+)? \+(?P<NewStartLine>\d+)(,\d+)? @@", line)
  109. if lines_changed:
  110. if state != _DECLARED_FILE_PATH and state != _PROCESSING_CHUNK:
  111. logging.error('Unexpected line change without file path declaration: %r' % line)
  112. old_diff_line = int(lines_changed.group('OldStartLine'))
  113. new_diff_line = int(lines_changed.group('NewStartLine'))
  114. state = _PROCESSING_CHUNK
  115. continue
  116. if state == _PROCESSING_CHUNK:
  117. if line.startswith('+'):
  118. current_file.add_new_line(new_diff_line, line[1:])
  119. new_diff_line += 1
  120. elif line.startswith('-'):
  121. current_file.add_deleted_line(old_diff_line, line[1:])
  122. old_diff_line += 1
  123. elif line.startswith(' '):
  124. current_file.add_unchanged_line(old_diff_line, new_diff_line, line[1:])
  125. old_diff_line += 1
  126. new_diff_line += 1
  127. elif line == '\\ No newline at end of file':
  128. # Nothing to do. We may still have some added lines.
  129. pass
  130. else:
  131. logging.error('Unexpected diff format when parsing a chunk: %r' % line)