~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
 
19
19
class PatchSyntax(Exception):
92
92
    range = int(range)
93
93
    return (pos, range)
94
94
 
95
 
 
 
95
 
96
96
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)
 
97
    import re
 
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:
162
164
        return InsertLine(line[1:])
163
165
    elif line.startswith("-"):
164
166
        return RemoveLine(line[1:])
165
 
    elif line == NO_NL:
166
 
        return NO_NL
167
167
    else:
168
168
        raise MalformedLine("Unknown line type", line)
169
169
__pychecker__=""
170
170
 
171
171
 
172
172
class Hunk:
173
 
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
 
173
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
174
174
        self.orig_pos = orig_pos
175
175
        self.orig_range = orig_range
176
176
        self.mod_pos = mod_pos
177
177
        self.mod_range = mod_range
 
178
        self.tail = tail
178
179
        self.lines = []
179
180
 
180
181
    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))
 
182
        if self.tail is None:
 
183
            tail_str = ''
 
184
        else:
 
185
            tail_str = ' ' + self.tail
 
186
        return "@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
 
187
                                                     self.orig_range),
 
188
                                      self.range_str(self.mod_pos,
 
189
                                                     self.mod_range),
 
190
                                      tail_str)
185
191
 
186
192
    def range_str(self, pos, range):
187
193
        """Return a file range, special-casing for 1-line files.
212
218
            return self.shift_to_mod_lines(pos)
213
219
 
214
220
    def shift_to_mod_lines(self, pos):
215
 
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
216
221
        position = self.orig_pos-1
217
222
        shift = 0
218
223
        for line in self.lines:
261
266
        self.hunks = []
262
267
 
263
268
    def __str__(self):
264
 
        ret = self.get_header() 
 
269
        ret = self.get_header()
265
270
        ret += "".join([str(h) for h in self.hunks])
266
271
        return ret
267
272
 
268
273
    def get_header(self):
269
274
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
270
275
 
271
 
    def stats_str(self):
272
 
        """Return a string of patch statistics"""
 
276
    def stats_values(self):
 
277
        """Calculate the number of inserts and removes."""
273
278
        removes = 0
274
279
        inserts = 0
275
280
        for hunk in self.hunks:
278
283
                     inserts+=1;
279
284
                elif isinstance(line, RemoveLine):
280
285
                     removes+=1;
 
286
        return (inserts, removes, len(self.hunks))
 
287
 
 
288
    def stats_str(self):
 
289
        """Return a string of patch statistics"""
281
290
        return "%i inserts, %i removes in %i hunks" % \
282
 
            (inserts, removes, len(self.hunks))
 
291
            self.stats_values()
283
292
 
284
293
    def pos_in_mod(self, position):
285
294
        newpos = position
289
298
                return None
290
299
            newpos += shift
291
300
        return newpos
292
 
            
 
301
 
293
302
    def iter_inserted(self):
294
303
        """Iteraties through inserted lines
295
 
        
 
304
 
296
305
        :return: Pair of line number, line
297
306
        :rtype: iterator of (int, InsertLine)
298
307
        """
307
316
 
308
317
 
309
318
def parse_patch(iter_lines):
 
319
    iter_lines = iter_lines_handle_nl(iter_lines)
310
320
    (orig_name, mod_name) = get_patch_names(iter_lines)
311
321
    patch = Patch(orig_name, mod_name)
312
322
    for hunk in iter_hunks(iter_lines):
347
357
    last_line = None
348
358
    for line in iter_lines:
349
359
        if line == NO_NL:
350
 
            assert last_line.endswith('\n')
 
360
            if not last_line.endswith('\n'):
 
361
                raise AssertionError()
351
362
            last_line = last_line[:-1]
352
363
            line = None
353
364
        if last_line is not None:
358
369
 
359
370
 
360
371
def parse_patches(iter_lines):
361
 
    iter_lines = iter_lines_handle_nl(iter_lines)
362
372
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
363
373
 
364
374
 
385
395
    """Iterate through a series of lines with a patch applied.
386
396
    This handles a single file, and does exact, not fuzzy patching.
387
397
    """
388
 
    if orig_lines is not None:
389
 
        orig_lines = orig_lines.__iter__()
 
398
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
399
    get_patch_names(patch_lines)
 
400
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
 
401
 
 
402
 
 
403
def iter_patched_from_hunks(orig_lines, hunks):
 
404
    """Iterate through a series of lines with a patch applied.
 
405
    This handles a single file, and does exact, not fuzzy patching.
 
406
 
 
407
    :param orig_lines: The unpatched lines.
 
408
    :param hunks: An iterable of Hunk instances.
 
409
    """
390
410
    seen_patch = []
391
 
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
392
 
    get_patch_names(patch_lines)
393
411
    line_no = 1
394
 
    for hunk in iter_hunks(patch_lines):
 
412
    if orig_lines is not None:
 
413
        orig_lines = iter(orig_lines)
 
414
    for hunk in hunks:
395
415
        while line_no < hunk.orig_pos:
396
416
            orig_line = orig_lines.next()
397
417
            yield orig_line
407
427
                if isinstance(hunk_line, ContextLine):
408
428
                    yield orig_line
409
429
                else:
410
 
                    assert isinstance(hunk_line, RemoveLine)
 
430
                    if not isinstance(hunk_line, RemoveLine):
 
431
                        raise AssertionError(hunk_line)
411
432
                line_no += 1
412
433
    if orig_lines is not None:
413
434
        for line in orig_lines: