~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import errno
18
17
import os
19
18
import re
 
19
import sys
 
20
 
 
21
from bzrlib.lazy_import import lazy_import
 
22
lazy_import(globals(), """
 
23
import errno
20
24
import subprocess
21
 
import sys
22
25
import tempfile
23
26
import time
24
27
 
 
28
from bzrlib import (
 
29
    errors,
 
30
    osutils,
 
31
    patiencediff,
 
32
    textfile,
 
33
    )
 
34
""")
 
35
 
25
36
# compatability - plugins import compare_trees from diff!!!
26
37
# deprecated as of 0.10
27
38
from bzrlib.delta import compare_trees
28
 
from bzrlib.errors import BzrError
29
 
import bzrlib.errors as errors
30
 
import bzrlib.osutils
31
 
from bzrlib.patiencediff import unified_diff
32
 
import bzrlib.patiencediff
33
 
from bzrlib.symbol_versioning import (deprecated_function,
34
 
        zero_eight)
35
 
from bzrlib.textfile import check_text_lines
 
39
from bzrlib.symbol_versioning import (
 
40
        deprecated_function,
 
41
        zero_eight,
 
42
        )
36
43
from bzrlib.trace import mutter, warning
37
44
 
38
45
 
60
67
        return
61
68
    
62
69
    if allow_binary is False:
63
 
        check_text_lines(oldlines)
64
 
        check_text_lines(newlines)
 
70
        textfile.check_text_lines(oldlines)
 
71
        textfile.check_text_lines(newlines)
65
72
 
66
73
    if sequence_matcher is None:
67
 
        sequence_matcher = bzrlib.patiencediff.PatienceSequenceMatcher
68
 
    ud = unified_diff(oldlines, newlines,
 
74
        sequence_matcher = patiencediff.PatienceSequenceMatcher
 
75
    ud = patiencediff.unified_diff(oldlines, newlines,
69
76
                      fromfile=old_filename.encode(path_encoding),
70
77
                      tofile=new_filename.encode(path_encoding),
71
78
                      sequencematcher=sequence_matcher)
90
97
 
91
98
def _set_lang_C():
92
99
    """Set the env var LANG=C"""
93
 
    os.environ['LANG'] = 'C'
 
100
    osutils.set_or_unset_env('LANG', 'C')
 
101
    osutils.set_or_unset_env('LC_ALL', None)
 
102
    osutils.set_or_unset_env('LC_CTYPE', None)
 
103
    osutils.set_or_unset_env('LANGUAGE', None)
94
104
 
95
105
 
96
106
def _spawn_external_diff(diffcmd, capture_errors=True):
103
113
    :return: A Popen object.
104
114
    """
105
115
    if capture_errors:
106
 
        preexec_fn = _set_lang_C
 
116
        if sys.platform == 'win32':
 
117
            # Win32 doesn't support preexec_fn, but that is
 
118
            # okay, because it doesn't support LANG either.
 
119
            preexec_fn = None
 
120
        else:
 
121
            preexec_fn = _set_lang_C
107
122
        stderr = subprocess.PIPE
108
123
    else:
109
124
        preexec_fn = None
202
217
            # Write out the new i18n diff response
203
218
            to_file.write(out+'\n')
204
219
            if pipe.returncode != 2:
205
 
                raise BzrError('external diff failed with exit code 2'
 
220
                raise errors.BzrError(
 
221
                               'external diff failed with exit code 2'
206
222
                               ' when run with LANG=C, but not when run'
207
223
                               ' natively: %r' % (diffcmd,))
208
224
 
210
226
            # Starting with diffutils 2.8.4 the word "binary" was dropped.
211
227
            m = re.match('^(binary )?files.*differ$', first_line, re.I)
212
228
            if m is None:
213
 
                raise BzrError('external diff failed with exit code 2;'
214
 
                               ' command: %r' % (diffcmd,))
 
229
                raise errors.BzrError('external diff failed with exit code 2;'
 
230
                                      ' command: %r' % (diffcmd,))
215
231
            else:
216
232
                # Binary files differ, just return
217
233
                return
226
242
            else:
227
243
                msg = 'exit code %d' % rc
228
244
                
229
 
            raise BzrError('external diff failed with %s; command: %r' 
230
 
                           % (rc, diffcmd))
 
245
            raise errors.BzrError('external diff failed with %s; command: %r' 
 
246
                                  % (rc, diffcmd))
231
247
 
232
248
 
233
249
    finally: