~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Guillermo Gonzalez
  • Date: 2008-08-16 18:43:33 UTC
  • mto: (3668.1.1 trunk) (3703.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 3669.
  • Revision ID: guillo.gonzo@gmail.com-20080816184333-51of2k7wqi6nwtjo
 added _mac_mkdtemp, that chown tmp dir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley
 
1
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
 
#    This program is free software; you can redistribute it and/or modify
5
 
#    it under the terms of the GNU General Public License as published by
6
 
#    the Free Software Foundation; either version 2 of the License, or
7
 
#    (at your option) any later version.
8
 
#
9
 
#    This program is distributed in the hope that it will be useful,
10
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#    GNU General Public License for more details.
13
 
#
14
 
#    You should have received a copy of the GNU General Public License
15
 
#    along with this program; if not, write to the Free Software
16
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
import re
17
18
 
18
19
 
19
20
class PatchSyntax(Exception):
94
95
 
95
96
 
96
97
def hunk_from_header(line):
97
 
    if not line.startswith("@@") or not line.endswith("@@\n") \
98
 
        or not len(line) > 4:
99
 
        raise MalformedHunkHeader("Does not start and end with @@.", line)
 
98
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
 
99
    if matches is None:
 
100
        raise MalformedHunkHeader("Does not match format.", line)
100
101
    try:
101
 
        (orig, mod) = line[3:-4].split(" ")
102
 
    except Exception, e:
 
102
        (orig, mod) = matches.group(1).split(" ")
 
103
    except (ValueError, IndexError), e:
103
104
        raise MalformedHunkHeader(str(e), line)
104
105
    if not orig.startswith('-') or not mod.startswith('+'):
105
106
        raise MalformedHunkHeader("Positions don't start with + or -.", line)
106
107
    try:
107
108
        (orig_pos, orig_range) = parse_range(orig[1:])
108
109
        (mod_pos, mod_range) = parse_range(mod[1:])
109
 
    except Exception, e:
 
110
    except (ValueError, IndexError), e:
110
111
        raise MalformedHunkHeader(str(e), line)
111
112
    if mod_range < 0 or orig_range < 0:
112
113
        raise MalformedHunkHeader("Hunk range is negative", line)
113
 
    return Hunk(orig_pos, orig_range, mod_pos, mod_range)
 
114
    tail = matches.group(3)
 
115
    return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
114
116
 
115
117
 
116
118
class HunkLine:
170
172
 
171
173
 
172
174
class Hunk:
173
 
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
 
175
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
174
176
        self.orig_pos = orig_pos
175
177
        self.orig_range = orig_range
176
178
        self.mod_pos = mod_pos
177
179
        self.mod_range = mod_range
 
180
        self.tail = tail
178
181
        self.lines = []
179
182
 
180
183
    def get_header(self):
181
 
        return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos, 
182
 
                                                   self.orig_range),
183
 
                                    self.range_str(self.mod_pos, 
184
 
                                                   self.mod_range))
 
184
        if self.tail is None:
 
185
            tail_str = ''
 
186
        else:
 
187
            tail_str = ' ' + self.tail
 
188
        return "@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
 
189
                                                     self.orig_range),
 
190
                                      self.range_str(self.mod_pos,
 
191
                                                     self.mod_range),
 
192
                                      tail_str)
185
193
 
186
194
    def range_str(self, pos, range):
187
195
        """Return a file range, special-casing for 1-line files.
212
220
            return self.shift_to_mod_lines(pos)
213
221
 
214
222
    def shift_to_mod_lines(self, pos):
215
 
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
216
223
        position = self.orig_pos-1
217
224
        shift = 0
218
225
        for line in self.lines:
316
323
 
317
324
def iter_file_patch(iter_lines):
318
325
    saved_lines = []
 
326
    orig_range = 0
319
327
    for line in iter_lines:
320
328
        if line.startswith('=== ') or line.startswith('*** '):
321
329
            continue
322
330
        if line.startswith('#'):
323
331
            continue
 
332
        elif orig_range > 0:
 
333
            if line.startswith('-') or line.startswith(' '):
 
334
                orig_range -= 1
324
335
        elif line.startswith('--- '):
325
336
            if len(saved_lines) > 0:
326
337
                yield saved_lines
327
338
            saved_lines = []
 
339
        elif line.startswith('@@'):
 
340
            hunk = hunk_from_header(line)
 
341
            orig_range = hunk.orig_range
328
342
        saved_lines.append(line)
329
343
    if len(saved_lines) > 0:
330
344
        yield saved_lines
340
354
    last_line = None
341
355
    for line in iter_lines:
342
356
        if line == NO_NL:
343
 
            assert last_line.endswith('\n')
 
357
            if not last_line.endswith('\n'):
 
358
                raise AssertionError()
344
359
            last_line = last_line[:-1]
345
360
            line = None
346
361
        if last_line is not None:
400
415
                if isinstance(hunk_line, ContextLine):
401
416
                    yield orig_line
402
417
                else:
403
 
                    assert isinstance(hunk_line, RemoveLine)
 
418
                    if not isinstance(hunk_line, RemoveLine):
 
419
                        raise AssertionError(hunk_line)
404
420
                line_no += 1
405
421
    if orig_lines is not None:
406
422
        for line in orig_lines: