/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
from __future__ import absolute_import
19
19
 
20
 
from .errors import (
21
 
    BzrError,
 
20
from bzrlib.errors import (
 
21
    BinaryFiles,
 
22
    MalformedHunkHeader,
 
23
    MalformedLine,
 
24
    MalformedPatchHeader,
 
25
    PatchConflict,
 
26
    PatchSyntax,
22
27
    )
23
28
 
24
29
import re
27
32
binary_files_re = 'Binary files (.*) and (.*) differ\n'
28
33
 
29
34
 
30
 
class PatchSyntax(BzrError):
31
 
    """Base class for patch syntax errors."""
32
 
 
33
 
 
34
 
class BinaryFiles(BzrError):
35
 
 
36
 
    _fmt = 'Binary files section encountered.'
37
 
 
38
 
    def __init__(self, orig_name, mod_name):
39
 
        self.orig_name = orig_name
40
 
        self.mod_name = mod_name
41
 
 
42
 
 
43
 
class MalformedPatchHeader(PatchSyntax):
44
 
 
45
 
    _fmt = "Malformed patch header.  %(desc)s\n%(line)r"
46
 
 
47
 
    def __init__(self, desc, line):
48
 
        self.desc = desc
49
 
        self.line = line
50
 
 
51
 
 
52
 
class MalformedLine(PatchSyntax):
53
 
 
54
 
    _fmt = "Malformed line.  %(desc)s\n%(line)r"
55
 
 
56
 
    def __init__(self, desc, line):
57
 
        self.desc = desc
58
 
        self.line = line
59
 
 
60
 
 
61
 
class PatchConflict(BzrError):
62
 
 
63
 
    _fmt = ('Text contents mismatch at line %(line_no)d.  Original has '
64
 
            '"%(orig_line)s", but patch says it should be "%(patch_line)s"')
65
 
 
66
 
    def __init__(self, line_no, orig_line, patch_line):
67
 
        self.line_no = line_no
68
 
        self.orig_line = orig_line.rstrip('\n')
69
 
        self.patch_line = patch_line.rstrip('\n')
70
 
 
71
 
 
72
 
class MalformedHunkHeader(PatchSyntax):
73
 
 
74
 
    _fmt = "Malformed hunk header.  %(desc)s\n%(line)r"
75
 
 
76
 
    def __init__(self, desc, line):
77
 
        self.desc = desc
78
 
        self.line = line
79
 
 
80
 
 
81
35
def get_patch_names(iter_lines):
82
 
    line = next(iter_lines)
 
36
    line = iter_lines.next()
83
37
    try:
84
38
        match = re.match(binary_files_re, line)
85
39
        if match is not None:
91
45
    except StopIteration:
92
46
        raise MalformedPatchHeader("No orig line", "")
93
47
    try:
94
 
        line = next(iter_lines)
 
48
        line = iter_lines.next()
95
49
        if not line.startswith("+++ "):
96
50
            raise PatchSyntax("No mod name")
97
51
        else:
127
81
        raise MalformedHunkHeader("Does not match format.", line)
128
82
    try:
129
83
        (orig, mod) = matches.group(1).split(" ")
130
 
    except (ValueError, IndexError) as e:
 
84
    except (ValueError, IndexError), e:
131
85
        raise MalformedHunkHeader(str(e), line)
132
86
    if not orig.startswith('-') or not mod.startswith('+'):
133
87
        raise MalformedHunkHeader("Positions don't start with + or -.", line)
134
88
    try:
135
89
        (orig_pos, orig_range) = parse_range(orig[1:])
136
90
        (mod_pos, mod_range) = parse_range(mod[1:])
137
 
    except (ValueError, IndexError) as e:
 
91
    except (ValueError, IndexError), e:
138
92
        raise MalformedHunkHeader(str(e), line)
139
93
    if mod_range < 0 or orig_range < 0:
140
94
        raise MalformedHunkHeader("Hunk range is negative", line)
290
244
        orig_size = 0
291
245
        mod_size = 0
292
246
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
293
 
            hunk_line = parse_line(next(iter_lines))
 
247
            hunk_line = parse_line(iter_lines.next())
294
248
            hunk.lines.append(hunk_line)
295
249
            if isinstance(hunk_line, (RemoveLine, ContextLine)):
296
250
                orig_size += 1
373
327
    iter_lines = iter_lines_handle_nl(iter_lines)
374
328
    try:
375
329
        (orig_name, mod_name) = get_patch_names(iter_lines)
376
 
    except BinaryFiles as e:
 
330
    except BinaryFiles, e:
377
331
        return BinaryPatch(e.orig_name, e.mod_name)
378
332
    else:
379
333
        patch = Patch(orig_name, mod_name)
529
483
        orig_lines = iter(orig_lines)
530
484
    for hunk in hunks:
531
485
        while line_no < hunk.orig_pos:
532
 
            orig_line = next(orig_lines)
 
486
            orig_line = orig_lines.next()
533
487
            yield orig_line
534
488
            line_no += 1
535
489
        for hunk_line in hunk.lines:
537
491
            if isinstance(hunk_line, InsertLine):
538
492
                yield hunk_line.contents
539
493
            elif isinstance(hunk_line, (ContextLine, RemoveLine)):
540
 
                orig_line = next(orig_lines)
 
494
                orig_line = orig_lines.next()
541
495
                if orig_line != hunk_line.contents:
542
496
                    raise PatchConflict(line_no, orig_line, "".join(seen_patch))
543
497
                if isinstance(hunk_line, ContextLine):