/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 breezy/diff.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from .lazy_import import lazy_import
26
26
lazy_import(globals(), """
27
27
import errno
 
28
import patiencediff
28
29
import subprocess
29
30
import tempfile
30
31
 
33
34
    controldir,
34
35
    errors,
35
36
    osutils,
36
 
    patiencediff,
37
37
    textfile,
38
38
    timestamp,
39
39
    views,
93
93
 
94
94
    if sequence_matcher is None:
95
95
        sequence_matcher = patiencediff.PatienceSequenceMatcher
96
 
    ud = patiencediff.unified_diff_bytes(oldlines, newlines,
97
 
                                         fromfile=old_label.encode(
98
 
                                             path_encoding, 'replace'),
99
 
                                         tofile=new_label.encode(
100
 
                                             path_encoding, 'replace'),
101
 
                                         n=context_lines, sequencematcher=sequence_matcher)
 
96
    ud = unified_diff_bytes(
 
97
        oldlines, newlines,
 
98
        fromfile=old_label.encode(path_encoding, 'replace'),
 
99
        tofile=new_label.encode(path_encoding, 'replace'),
 
100
        n=context_lines, sequencematcher=sequence_matcher)
102
101
 
103
102
    ud = list(ud)
104
103
    if len(ud) == 0:  # Identical contents, nothing to do
117
116
    to_file.write(b'\n')
118
117
 
119
118
 
 
119
def unified_diff_bytes(a, b, fromfile=b'', tofile=b'', fromfiledate=b'',
 
120
                       tofiledate=b'', n=3, lineterm=b'\n', sequencematcher=None):
 
121
    r"""
 
122
    Compare two sequences of lines; generate the delta as a unified diff.
 
123
 
 
124
    Unified diffs are a compact way of showing line changes and a few
 
125
    lines of context.  The number of context lines is set by 'n' which
 
126
    defaults to three.
 
127
 
 
128
    By default, the diff control lines (those with ---, +++, or @@) are
 
129
    created with a trailing newline.  This is helpful so that inputs
 
130
    created from file.readlines() result in diffs that are suitable for
 
131
    file.writelines() since both the inputs and outputs have trailing
 
132
    newlines.
 
133
 
 
134
    For inputs that do not have trailing newlines, set the lineterm
 
135
    argument to "" so that the output will be uniformly newline free.
 
136
 
 
137
    The unidiff format normally has a header for filenames and modification
 
138
    times.  Any or all of these may be specified using strings for
 
139
    'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.  The modification
 
140
    times are normally expressed in the format returned by time.ctime().
 
141
 
 
142
    Example:
 
143
 
 
144
    >>> for line in bytes_unified_diff(b'one two three four'.split(),
 
145
    ...             b'zero one tree four'.split(), b'Original', b'Current',
 
146
    ...             b'Sat Jan 26 23:30:50 1991', b'Fri Jun 06 10:20:52 2003',
 
147
    ...             lineterm=b''):
 
148
    ...     print line
 
149
    --- Original Sat Jan 26 23:30:50 1991
 
150
    +++ Current Fri Jun 06 10:20:52 2003
 
151
    @@ -1,4 +1,4 @@
 
152
    +zero
 
153
     one
 
154
    -two
 
155
    -three
 
156
    +tree
 
157
     four
 
158
    """
 
159
    if sequencematcher is None:
 
160
        sequencematcher = difflib.SequenceMatcher
 
161
 
 
162
    if fromfiledate:
 
163
        fromfiledate = b'\t' + bytes(fromfiledate)
 
164
    if tofiledate:
 
165
        tofiledate = b'\t' + bytes(tofiledate)
 
166
 
 
167
    started = False
 
168
    for group in sequencematcher(None, a, b).get_grouped_opcodes(n):
 
169
        if not started:
 
170
            yield b'--- %s%s%s' % (fromfile, fromfiledate, lineterm)
 
171
            yield b'+++ %s%s%s' % (tofile, tofiledate, lineterm)
 
172
            started = True
 
173
        i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
 
174
        yield b"@@ -%d,%d +%d,%d @@%s" % (i1 + 1, i2 - i1, j1 + 1, j2 - j1, lineterm)
 
175
        for tag, i1, i2, j1, j2 in group:
 
176
            if tag == 'equal':
 
177
                for line in a[i1:i2]:
 
178
                    yield b' ' + line
 
179
                continue
 
180
            if tag == 'replace' or tag == 'delete':
 
181
                for line in a[i1:i2]:
 
182
                    yield b'-' + line
 
183
            if tag == 'replace' or tag == 'insert':
 
184
                for line in b[j1:j2]:
 
185
                    yield b'+' + line
 
186
 
 
187
 
120
188
def _spawn_external_diff(diffcmd, capture_errors=True):
121
189
    """Spawn the external diff process, and return the child handle.
122
190