~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

merge only needs a lock_tree_write() on the working tree, not a full lock_write()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006 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
94
94
 
95
95
 
96
96
def hunk_from_header(line):
97
 
    import re
98
 
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
99
 
    if matches is None:
100
 
        raise MalformedHunkHeader("Does not match format.", 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)
101
100
    try:
102
 
        (orig, mod) = matches.group(1).split(" ")
103
 
    except (ValueError, IndexError), e:
 
101
        (orig, mod) = line[3:-4].split(" ")
 
102
    except Exception, e:
104
103
        raise MalformedHunkHeader(str(e), line)
105
104
    if not orig.startswith('-') or not mod.startswith('+'):
106
105
        raise MalformedHunkHeader("Positions don't start with + or -.", line)
107
106
    try:
108
107
        (orig_pos, orig_range) = parse_range(orig[1:])
109
108
        (mod_pos, mod_range) = parse_range(mod[1:])
110
 
    except (ValueError, IndexError), e:
 
109
    except Exception, e:
111
110
        raise MalformedHunkHeader(str(e), line)
112
111
    if mod_range < 0 or orig_range < 0:
113
112
        raise MalformedHunkHeader("Hunk range is negative", line)
114
 
    tail = matches.group(3)
115
 
    return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
 
113
    return Hunk(orig_pos, orig_range, mod_pos, mod_range)
116
114
 
117
115
 
118
116
class HunkLine:
172
170
 
173
171
 
174
172
class Hunk:
175
 
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
 
173
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
176
174
        self.orig_pos = orig_pos
177
175
        self.orig_range = orig_range
178
176
        self.mod_pos = mod_pos
179
177
        self.mod_range = mod_range
180
 
        self.tail = tail
181
178
        self.lines = []
182
179
 
183
180
    def get_header(self):
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)
 
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))
193
185
 
194
186
    def range_str(self, pos, range):
195
187
        """Return a file range, special-casing for 1-line files.
220
212
            return self.shift_to_mod_lines(pos)
221
213
 
222
214
    def shift_to_mod_lines(self, pos):
 
215
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
223
216
        position = self.orig_pos-1
224
217
        shift = 0
225
218
        for line in self.lines:
275
268
    def get_header(self):
276
269
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
277
270
 
278
 
    def stats_values(self):
279
 
        """Calculate the number of inserts and removes."""
 
271
    def stats_str(self):
 
272
        """Return a string of patch statistics"""
280
273
        removes = 0
281
274
        inserts = 0
282
275
        for hunk in self.hunks:
285
278
                     inserts+=1;
286
279
                elif isinstance(line, RemoveLine):
287
280
                     removes+=1;
288
 
        return (inserts, removes, len(self.hunks))
289
 
 
290
 
    def stats_str(self):
291
 
        """Return a string of patch statistics"""
292
281
        return "%i inserts, %i removes in %i hunks" % \
293
 
            self.stats_values()
 
282
            (inserts, removes, len(self.hunks))
294
283
 
295
284
    def pos_in_mod(self, position):
296
285
        newpos = position
327
316
 
328
317
def iter_file_patch(iter_lines):
329
318
    saved_lines = []
330
 
    orig_range = 0
331
319
    for line in iter_lines:
332
320
        if line.startswith('=== ') or line.startswith('*** '):
333
321
            continue
334
322
        if line.startswith('#'):
335
323
            continue
336
 
        elif orig_range > 0:
337
 
            if line.startswith('-') or line.startswith(' '):
338
 
                orig_range -= 1
339
324
        elif line.startswith('--- '):
340
325
            if len(saved_lines) > 0:
341
326
                yield saved_lines
342
327
            saved_lines = []
343
 
        elif line.startswith('@@'):
344
 
            hunk = hunk_from_header(line)
345
 
            orig_range = hunk.orig_range
346
328
        saved_lines.append(line)
347
329
    if len(saved_lines) > 0:
348
330
        yield saved_lines
358
340
    last_line = None
359
341
    for line in iter_lines:
360
342
        if line == NO_NL:
361
 
            if not last_line.endswith('\n'):
362
 
                raise AssertionError()
 
343
            assert last_line.endswith('\n')
363
344
            last_line = last_line[:-1]
364
345
            line = None
365
346
        if last_line is not None:
397
378
    """Iterate through a series of lines with a patch applied.
398
379
    This handles a single file, and does exact, not fuzzy patching.
399
380
    """
400
 
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
381
    if orig_lines is not None:
 
382
        orig_lines = orig_lines.__iter__()
 
383
    seen_patch = []
 
384
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
401
385
    get_patch_names(patch_lines)
402
 
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
403
 
 
404
 
 
405
 
def iter_patched_from_hunks(orig_lines, hunks):
406
 
    """Iterate through a series of lines with a patch applied.
407
 
    This handles a single file, and does exact, not fuzzy patching.
408
 
 
409
 
    :param orig_lines: The unpatched lines.
410
 
    :param hunks: An iterable of Hunk instances.
411
 
    """
412
 
    seen_patch = []
413
386
    line_no = 1
414
 
    if orig_lines is not None:
415
 
        orig_lines = iter(orig_lines)
416
 
    for hunk in hunks:
 
387
    for hunk in iter_hunks(patch_lines):
417
388
        while line_no < hunk.orig_pos:
418
389
            orig_line = orig_lines.next()
419
390
            yield orig_line
429
400
                if isinstance(hunk_line, ContextLine):
430
401
                    yield orig_line
431
402
                else:
432
 
                    if not isinstance(hunk_line, RemoveLine):
433
 
                        raise AssertionError(hunk_line)
 
403
                    assert isinstance(hunk_line, RemoveLine)
434
404
                line_no += 1
435
405
    if orig_lines is not None:
436
406
        for line in orig_lines: