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

  • Committer: John Arbash Meinel
  • Date: 2006-09-20 14:51:03 UTC
  • mfrom: (0.8.23 version_info)
  • mto: This revision was merged to the branch mainline in revision 2028.
  • Revision ID: john@arbash-meinel.com-20060920145103-02725c6d6c886040
[merge] version-info plugin, and cleanup for layout in bzr

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
17
18
import os
18
19
import re
 
20
import subprocess
19
21
import sys
20
 
 
21
 
from bzrlib.lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
 
import errno
24
 
import subprocess
25
22
import tempfile
26
23
import time
27
24
 
28
25
from bzrlib import (
29
26
    errors,
30
27
    osutils,
31
 
    patiencediff,
32
 
    textfile,
33
 
    timestamp,
34
28
    )
35
 
""")
36
 
 
37
29
# compatability - plugins import compare_trees from diff!!!
38
30
# deprecated as of 0.10
39
31
from bzrlib.delta import compare_trees
40
 
from bzrlib.symbol_versioning import (
41
 
        deprecated_function,
42
 
        zero_eight,
43
 
        )
 
32
from bzrlib.errors import BzrError
 
33
from bzrlib.patiencediff import unified_diff
 
34
import bzrlib.patiencediff
 
35
from bzrlib.symbol_versioning import (deprecated_function,
 
36
        zero_eight)
 
37
from bzrlib.textfile import check_text_lines
44
38
from bzrlib.trace import mutter, warning
45
39
 
46
40
 
68
62
        return
69
63
    
70
64
    if allow_binary is False:
71
 
        textfile.check_text_lines(oldlines)
72
 
        textfile.check_text_lines(newlines)
 
65
        check_text_lines(oldlines)
 
66
        check_text_lines(newlines)
73
67
 
74
68
    if sequence_matcher is None:
75
 
        sequence_matcher = patiencediff.PatienceSequenceMatcher
76
 
    ud = patiencediff.unified_diff(oldlines, newlines,
 
69
        sequence_matcher = bzrlib.patiencediff.PatienceSequenceMatcher
 
70
    ud = unified_diff(oldlines, newlines,
77
71
                      fromfile=old_filename.encode(path_encoding),
78
72
                      tofile=new_filename.encode(path_encoding),
79
73
                      sequencematcher=sequence_matcher)
96
90
    print >>to_file
97
91
 
98
92
 
 
93
def _set_lang_C():
 
94
    """Set the env var LANG=C"""
 
95
    osutils.set_or_unset_env('LANG', 'C')
 
96
    osutils.set_or_unset_env('LC_ALL', None)
 
97
    osutils.set_or_unset_env('LC_CTYPE', None)
 
98
    osutils.set_or_unset_env('LANGUAGE', None)
 
99
 
 
100
 
99
101
def _spawn_external_diff(diffcmd, capture_errors=True):
100
102
    """Spawn the externall diff process, and return the child handle.
101
103
 
102
104
    :param diffcmd: The command list to spawn
103
 
    :param capture_errors: Capture stderr as well as setting LANG=C
104
 
        and LC_ALL=C. This lets us read and understand the output of diff,
105
 
        and respond to any errors.
 
105
    :param capture_errors: Capture stderr as well as setting LANG=C.
 
106
        This lets us read and understand the output of diff, and respond 
 
107
        to any errors.
106
108
    :return: A Popen object.
107
109
    """
108
110
    if capture_errors:
109
 
        # construct minimal environment
110
 
        env = {}
111
 
        path = os.environ.get('PATH')
112
 
        if path is not None:
113
 
            env['PATH'] = path
114
 
        env['LANGUAGE'] = 'C'   # on win32 only LANGUAGE has effect
115
 
        env['LANG'] = 'C'
116
 
        env['LC_ALL'] = 'C'
 
111
        if sys.platform == 'win32':
 
112
            # Win32 doesn't support preexec_fn, but that is
 
113
            # okay, because it doesn't support LANG either.
 
114
            preexec_fn = None
 
115
        else:
 
116
            preexec_fn = _set_lang_C
117
117
        stderr = subprocess.PIPE
118
118
    else:
119
 
        env = None
 
119
        preexec_fn = None
120
120
        stderr = None
121
121
 
122
122
    try:
124
124
                                stdin=subprocess.PIPE,
125
125
                                stdout=subprocess.PIPE,
126
126
                                stderr=stderr,
127
 
                                env=env)
 
127
                                preexec_fn=preexec_fn)
128
128
    except OSError, e:
129
129
        if e.errno == errno.ENOENT:
130
130
            raise errors.NoDiff(str(e))
212
212
            # Write out the new i18n diff response
213
213
            to_file.write(out+'\n')
214
214
            if pipe.returncode != 2:
215
 
                raise errors.BzrError(
216
 
                               'external diff failed with exit code 2'
217
 
                               ' when run with LANG=C and LC_ALL=C,'
218
 
                               ' but not when run natively: %r' % (diffcmd,))
 
215
                raise BzrError('external diff failed with exit code 2'
 
216
                               ' when run with LANG=C, but not when run'
 
217
                               ' natively: %r' % (diffcmd,))
219
218
 
220
219
            first_line = lang_c_out.split('\n', 1)[0]
221
220
            # Starting with diffutils 2.8.4 the word "binary" was dropped.
222
221
            m = re.match('^(binary )?files.*differ$', first_line, re.I)
223
222
            if m is None:
224
 
                raise errors.BzrError('external diff failed with exit code 2;'
225
 
                                      ' command: %r' % (diffcmd,))
 
223
                raise BzrError('external diff failed with exit code 2;'
 
224
                               ' command: %r' % (diffcmd,))
226
225
            else:
227
226
                # Binary files differ, just return
228
227
                return
237
236
            else:
238
237
                msg = 'exit code %d' % rc
239
238
                
240
 
            raise errors.BzrError('external diff failed with %s; command: %r' 
241
 
                                  % (rc, diffcmd))
 
239
            raise BzrError('external diff failed with %s; command: %r' 
 
240
                           % (rc, diffcmd))
242
241
 
243
242
 
244
243
    finally:
301
300
 
302
301
def diff_cmd_helper(tree, specific_files, external_diff_options, 
303
302
                    old_revision_spec=None, new_revision_spec=None,
304
 
                    revision_specs=None,
305
303
                    old_label='a/', new_label='b/'):
306
304
    """Helper for cmd_diff.
307
305
 
308
 
    :param tree:
 
306
   tree 
309
307
        A WorkingTree
310
308
 
311
 
    :param specific_files:
 
309
    specific_files
312
310
        The specific files to compare, or None
313
311
 
314
 
    :param external_diff_options:
 
312
    external_diff_options
315
313
        If non-None, run an external diff, and pass it these options
316
314
 
317
 
    :param old_revision_spec:
 
315
    old_revision_spec
318
316
        If None, use basis tree as old revision, otherwise use the tree for
319
317
        the specified revision. 
320
318
 
321
 
    :param new_revision_spec:
 
319
    new_revision_spec
322
320
        If None, use working tree as new revision, otherwise use the tree for
323
321
        the specified revision.
324
322
    
325
 
    :param revision_specs: 
326
 
        Zero, one or two RevisionSpecs from the command line, saying what revisions 
327
 
        to compare.  This can be passed as an alternative to the old_revision_spec 
328
 
        and new_revision_spec parameters.
329
 
 
330
323
    The more general form is show_diff_trees(), where the caller
331
324
    supplies any two trees.
332
325
    """
333
 
 
334
 
    # TODO: perhaps remove the old parameters old_revision_spec and
335
 
    # new_revision_spec, since this is only really for use from cmd_diff and
336
 
    # it now always passes through a sequence of revision_specs -- mbp
337
 
    # 20061221
338
 
 
339
326
    def spec_tree(spec):
340
327
        if tree:
341
328
            revision = spec.in_store(tree.branch)
344
331
        revision_id = revision.rev_id
345
332
        branch = revision.branch
346
333
        return branch.repository.revision_tree(revision_id)
347
 
 
348
 
    if revision_specs is not None:
349
 
        assert (old_revision_spec is None
350
 
                and new_revision_spec is None)
351
 
        if len(revision_specs) > 0:
352
 
            old_revision_spec = revision_specs[0]
353
 
        if len(revision_specs) > 1:
354
 
            new_revision_spec = revision_specs[1]
355
 
 
356
334
    if old_revision_spec is None:
357
335
        old_tree = tree.basis_tree()
358
336
    else:
359
337
        old_tree = spec_tree(old_revision_spec)
360
338
 
361
 
    if (new_revision_spec is None
362
 
        or new_revision_spec.spec is None):
 
339
    if new_revision_spec is None:
363
340
        new_tree = tree
364
341
    else:
365
342
        new_tree = spec_tree(new_revision_spec)
366
 
 
367
343
    if new_tree is not tree:
368
344
        extra_trees = (tree,)
369
345
    else:
392
368
    """
393
369
    old_tree.lock_read()
394
370
    try:
395
 
        if extra_trees is not None:
396
 
            for tree in extra_trees:
397
 
                tree.lock_read()
398
371
        new_tree.lock_read()
399
372
        try:
400
373
            return _show_diff_trees(old_tree, new_tree, to_file,
403
376
                                    extra_trees=extra_trees)
404
377
        finally:
405
378
            new_tree.unlock()
406
 
            if extra_trees is not None:
407
 
                for tree in extra_trees:
408
 
                    tree.unlock()
409
379
    finally:
410
380
        old_tree.unlock()
411
381
 
471
441
        has_changes = 1
472
442
        prop_str = get_prop_change(meta_modified)
473
443
        print >>to_file, '=== modified %s %r%s' % (kind, path.encode('utf8'), prop_str)
474
 
        # The file may be in a different location in the old tree (because
475
 
        # the containing dir was renamed, but the file itself was not)
476
 
        old_path = old_tree.id2path(file_id)
477
 
        old_name = '%s%s\t%s' % (old_label, old_path,
478
 
                                 _patch_header_date(old_tree, file_id, old_path))
 
444
        old_name = '%s%s\t%s' % (old_label, path,
 
445
                                 _patch_header_date(old_tree, file_id, path))
479
446
        new_name = '%s%s\t%s' % (new_label, path,
480
447
                                 _patch_header_date(new_tree, file_id, path))
481
448
        if text_modified:
488
455
 
489
456
def _patch_header_date(tree, file_id, path):
490
457
    """Returns a timestamp suitable for use in a patch header."""
491
 
    mtime = tree.get_file_mtime(file_id, path)
492
 
    assert mtime is not None, \
493
 
        "got an mtime of None for file-id %s, path %s in tree %s" % (
494
 
                file_id, path, tree)
495
 
    return timestamp.format_patch_date(mtime)
 
458
    tm = time.gmtime(tree.get_file_mtime(file_id, path))
 
459
    return time.strftime('%Y-%m-%d %H:%M:%S +0000', tm)
496
460
 
497
461
 
498
462
def _raise_if_nonexistent(paths, old_tree, new_tree):