1#!/usr/bin/env python
2##  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3##
4##  Use of this source code is governed by a BSD-style license
5##  that can be found in the LICENSE file in the root of the source
6##  tree. An additional intellectual property rights grant can be found
7##  in the file PATENTS.  All contributing project authors may
8##  be found in the AUTHORS file in the root of the source tree.
9##
10"""Classes for representing diff pieces."""
11
12__author__ = "jkoleszar@google.com"
13
14import re
15
16
17class DiffLines(object):
18    """A container for one half of a diff."""
19
20    def __init__(self, filename, offset, length):
21        self.filename = filename
22        self.offset = offset
23        self.length = length
24        self.lines = []
25        self.delta_line_nums = []
26
27    def Append(self, line):
28        l = len(self.lines)
29        if line[0] != " ":
30            self.delta_line_nums.append(self.offset + l)
31        self.lines.append(line[1:])
32        assert l+1 <= self.length
33
34    def Complete(self):
35        return len(self.lines) == self.length
36
37    def __contains__(self, item):
38        return item >= self.offset and item <= self.offset + self.length - 1
39
40
41class DiffHunk(object):
42    """A container for one diff hunk, consisting of two DiffLines."""
43
44    def __init__(self, header, file_a, file_b, start_a, len_a, start_b, len_b):
45        self.header = header
46        self.left = DiffLines(file_a, start_a, len_a)
47        self.right = DiffLines(file_b, start_b, len_b)
48        self.lines = []
49
50    def Append(self, line):
51        """Adds a line to the DiffHunk and its DiffLines children."""
52        if line[0] == "-":
53            self.left.Append(line)
54        elif line[0] == "+":
55            self.right.Append(line)
56        elif line[0] == " ":
57            self.left.Append(line)
58            self.right.Append(line)
59        elif line[0] == "\\":
60            # Ignore newline messages from git diff.
61            pass
62        else:
63            assert False, ("Unrecognized character at start of diff line "
64                           "%r" % line[0])
65        self.lines.append(line)
66
67    def Complete(self):
68        return self.left.Complete() and self.right.Complete()
69
70    def __repr__(self):
71        return "DiffHunk(%s, %s, len %d)" % (
72            self.left.filename, self.right.filename,
73            max(self.left.length, self.right.length))
74
75
76def ParseDiffHunks(stream):
77    """Walk a file-like object, yielding DiffHunks as they're parsed."""
78
79    file_regex = re.compile(r"(\+\+\+|---) (\S+)")
80    range_regex = re.compile(r"@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?")
81    hunk = None
82    while True:
83        line = stream.readline()
84        if not line:
85            break
86
87        if hunk is None:
88            # Parse file names
89            diff_file = file_regex.match(line)
90            if diff_file:
91              if line.startswith("---"):
92                  a_line = line
93                  a = diff_file.group(2)
94                  continue
95              if line.startswith("+++"):
96                  b_line = line
97                  b = diff_file.group(2)
98                  continue
99
100            # Parse offset/lengths
101            diffrange = range_regex.match(line)
102            if diffrange:
103                if diffrange.group(2):
104                    start_a = int(diffrange.group(1))
105                    len_a = int(diffrange.group(3))
106                else:
107                    start_a = 1
108                    len_a = int(diffrange.group(1))
109
110                if diffrange.group(5):
111                    start_b = int(diffrange.group(4))
112                    len_b = int(diffrange.group(6))
113                else:
114                    start_b = 1
115                    len_b = int(diffrange.group(4))
116
117                header = [a_line, b_line, line]
118                hunk = DiffHunk(header, a, b, start_a, len_a, start_b, len_b)
119        else:
120            # Add the current line to the hunk
121            hunk.Append(line)
122
123            # See if the whole hunk has been parsed. If so, yield it and prepare
124            # for the next hunk.
125            if hunk.Complete():
126                yield hunk
127                hunk = None
128
129    # Partial hunks are a parse error
130    assert hunk is None
131