Home | History | Annotate | Download | only in checkout
      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 
     29 """WebKit's Python module for interacting with patches."""
     30 
     31 import logging
     32 import re
     33 
     34 _log = logging.getLogger("webkitpy.common.checkout.diff_parser")
     35 
     36 
     37 # FIXME: This is broken. We should compile our regexps up-front
     38 # instead of using a custom cache.
     39 _regexp_compile_cache = {}
     40 
     41 
     42 # FIXME: This function should be removed.
     43 def match(pattern, string):
     44     """Matches the string with the pattern, caching the compiled regexp."""
     45     if not pattern in _regexp_compile_cache:
     46         _regexp_compile_cache[pattern] = re.compile(pattern)
     47     return _regexp_compile_cache[pattern].match(string)
     48 
     49 
     50 # FIXME: This belongs on DiffParser (e.g. as to_svn_diff()).
     51 def git_diff_to_svn_diff(line):
     52     """Converts a git formatted diff line to a svn formatted line.
     53 
     54     Args:
     55       line: A string representing a line of the diff.
     56     """
     57     # FIXME: This list should be a class member on DiffParser.
     58     # These regexp patterns should be compiled once instead of every time.
     59     conversion_patterns = (("^diff --git \w/(.+) \w/(?P<FilePath>.+)", lambda matched: "Index: " + matched.group('FilePath') + "\n"),
     60                            ("^new file.*", lambda matched: "\n"),
     61                            ("^index [0-9a-f]{7}\.\.[0-9a-f]{7} [0-9]{6}", lambda matched: "===================================================================\n"),
     62                            ("^--- \w/(?P<FilePath>.+)", lambda matched: "--- " + matched.group('FilePath') + "\n"),
     63                            ("^\+\+\+ \w/(?P<FilePath>.+)", lambda matched: "+++ " + matched.group('FilePath') + "\n"))
     64 
     65     for pattern, conversion in conversion_patterns:
     66         matched = match(pattern, line)
     67         if matched:
     68             return conversion(matched)
     69     return line
     70 
     71 
     72 # FIXME: This method belongs on DiffParser
     73 def get_diff_converter(first_diff_line):
     74     """Gets a converter function of diff lines.
     75 
     76     Args:
     77       first_diff_line: The first filename line of a diff file.
     78                        If this line is git formatted, we'll return a
     79                        converter from git to SVN.
     80     """
     81     if match(r"^diff --git \w/", first_diff_line):
     82         return git_diff_to_svn_diff
     83     return lambda input: input
     84 
     85 
     86 _INITIAL_STATE = 1
     87 _DECLARED_FILE_PATH = 2
     88 _PROCESSING_CHUNK = 3
     89 
     90 
     91 class DiffFile(object):
     92     """Contains the information for one file in a patch.
     93 
     94     The field "lines" is a list which contains tuples in this format:
     95        (deleted_line_number, new_line_number, line_string)
     96     If deleted_line_number is zero, it means this line is newly added.
     97     If new_line_number is zero, it means this line is deleted.
     98     """
     99     # FIXME: Tuples generally grow into classes.  We should consider
    100     # adding a DiffLine object.
    101 
    102     def added_or_modified_line_numbers(self):
    103         # This logic was moved from patchreader.py, but may not be
    104         # the right API for this object long-term.
    105         return [line[1] for line in self.lines if not line[0]]
    106 
    107     def __init__(self, filename):
    108         self.filename = filename
    109         self.lines = []
    110 
    111     def add_new_line(self, line_number, line):
    112         self.lines.append((0, line_number, line))
    113 
    114     def add_deleted_line(self, line_number, line):
    115         self.lines.append((line_number, 0, line))
    116 
    117     def add_unchanged_line(self, deleted_line_number, new_line_number, line):
    118         self.lines.append((deleted_line_number, new_line_number, line))
    119 
    120 
    121 # If this is going to be called DiffParser, it should be a re-useable parser.
    122 # Otherwise we should rename it to ParsedDiff or just Diff.
    123 class DiffParser(object):
    124     """A parser for a patch file.
    125 
    126     The field "files" is a dict whose key is the filename and value is
    127     a DiffFile object.
    128     """
    129 
    130     def __init__(self, diff_input):
    131         """Parses a diff.
    132 
    133         Args:
    134           diff_input: An iterable object.
    135         """
    136         self.files = self._parse_into_diff_files(diff_input)
    137 
    138     # FIXME: This function is way too long and needs to be broken up.
    139     def _parse_into_diff_files(self, diff_input):
    140         files = {}
    141         state = _INITIAL_STATE
    142         current_file = None
    143         old_diff_line = None
    144         new_diff_line = None
    145         for line in diff_input:
    146             line = line.rstrip("\n")
    147             if state == _INITIAL_STATE:
    148                 transform_line = get_diff_converter(line)
    149             line = transform_line(line)
    150 
    151             file_declaration = match(r"^Index: (?P<FilePath>.+)", line)
    152             if file_declaration:
    153                 filename = file_declaration.group('FilePath')
    154                 current_file = DiffFile(filename)
    155                 files[filename] = current_file
    156                 state = _DECLARED_FILE_PATH
    157                 continue
    158 
    159             lines_changed = match(r"^@@ -(?P<OldStartLine>\d+)(,\d+)? \+(?P<NewStartLine>\d+)(,\d+)? @@", line)
    160             if lines_changed:
    161                 if state != _DECLARED_FILE_PATH and state != _PROCESSING_CHUNK:
    162                     _log.error('Unexpected line change without file path '
    163                                'declaration: %r' % line)
    164                 old_diff_line = int(lines_changed.group('OldStartLine'))
    165                 new_diff_line = int(lines_changed.group('NewStartLine'))
    166                 state = _PROCESSING_CHUNK
    167                 continue
    168 
    169             if state == _PROCESSING_CHUNK:
    170                 if line.startswith('+'):
    171                     current_file.add_new_line(new_diff_line, line[1:])
    172                     new_diff_line += 1
    173                 elif line.startswith('-'):
    174                     current_file.add_deleted_line(old_diff_line, line[1:])
    175                     old_diff_line += 1
    176                 elif line.startswith(' '):
    177                     current_file.add_unchanged_line(old_diff_line, new_diff_line, line[1:])
    178                     old_diff_line += 1
    179                     new_diff_line += 1
    180                 elif line == '\\ No newline at end of file':
    181                     # Nothing to do.  We may still have some added lines.
    182                     pass
    183                 else:
    184                     _log.error('Unexpected diff format when parsing a '
    185                                'chunk: %r' % line)
    186         return files
    187