~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Vincent Ladeuil
  • Date: 2007-06-05 15:52:12 UTC
  • mto: (2485.8.44 bzr.connection.sharing)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070605155212-k2za98dhobeikxhn
Fix pull multiple connections.

* bzrlib/builtins.py:
(cmd_pull.run): If 'location' wasn't a bundle, the transport may
be reused.

* bzrlib/branch.py:
(Branch.open_from_transport): New method.

* bzrlib/bundle/__init__.py:
(read_mergeable_from_transport): New method.

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
    timestamp,
 
34
    )
 
35
""")
 
36
 
25
37
# compatability - plugins import compare_trees from diff!!!
26
38
# deprecated as of 0.10
27
39
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
 
40
from bzrlib.symbol_versioning import (
 
41
        deprecated_function,
 
42
        zero_eight,
 
43
        )
36
44
from bzrlib.trace import mutter, warning
37
45
 
38
46
 
60
68
        return
61
69
    
62
70
    if allow_binary is False:
63
 
        check_text_lines(oldlines)
64
 
        check_text_lines(newlines)
 
71
        textfile.check_text_lines(oldlines)
 
72
        textfile.check_text_lines(newlines)
65
73
 
66
74
    if sequence_matcher is None:
67
 
        sequence_matcher = bzrlib.patiencediff.PatienceSequenceMatcher
68
 
    ud = unified_diff(oldlines, newlines,
 
75
        sequence_matcher = patiencediff.PatienceSequenceMatcher
 
76
    ud = patiencediff.unified_diff(oldlines, newlines,
69
77
                      fromfile=old_filename.encode(path_encoding),
70
78
                      tofile=new_filename.encode(path_encoding),
71
79
                      sequencematcher=sequence_matcher)
88
96
    print >>to_file
89
97
 
90
98
 
91
 
def _set_lang_C():
92
 
    """Set the env var LANG=C"""
93
 
    os.environ['LANG'] = 'C'
94
 
 
95
 
 
96
99
def _spawn_external_diff(diffcmd, capture_errors=True):
97
100
    """Spawn the externall diff process, and return the child handle.
98
101
 
99
102
    :param diffcmd: The command list to spawn
100
 
    :param capture_errors: Capture stderr as well as setting LANG=C.
101
 
        This lets us read and understand the output of diff, and respond 
102
 
        to any errors.
 
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.
103
106
    :return: A Popen object.
104
107
    """
105
108
    if capture_errors:
106
 
        preexec_fn = _set_lang_C
 
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'
107
117
        stderr = subprocess.PIPE
108
118
    else:
109
 
        preexec_fn = None
 
119
        env = None
110
120
        stderr = None
111
121
 
112
122
    try:
114
124
                                stdin=subprocess.PIPE,
115
125
                                stdout=subprocess.PIPE,
116
126
                                stderr=stderr,
117
 
                                preexec_fn=preexec_fn)
 
127
                                env=env)
118
128
    except OSError, e:
119
129
        if e.errno == errno.ENOENT:
120
130
            raise errors.NoDiff(str(e))
192
202
            # 'diff' gives retcode == 2 for all sorts of errors
193
203
            # one of those is 'Binary files differ'.
194
204
            # Bad options could also be the problem.
195
 
            # 'Binary files' is not a real error, so we suppress that error
 
205
            # 'Binary files' is not a real error, so we suppress that error.
196
206
            lang_c_out = out
197
207
 
198
208
            # Since we got here, we want to make sure to give an i18n error
202
212
            # Write out the new i18n diff response
203
213
            to_file.write(out+'\n')
204
214
            if pipe.returncode != 2:
205
 
                raise BzrError('external diff failed with exit code 2'
206
 
                               ' when run with LANG=C, but not when run'
207
 
                               ' natively: %r' % (diffcmd,))
 
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,))
208
219
 
209
220
            first_line = lang_c_out.split('\n', 1)[0]
210
 
            m = re.match('^binary files.*differ$', first_line, re.I)
 
221
            # Starting with diffutils 2.8.4 the word "binary" was dropped.
 
222
            m = re.match('^(binary )?files.*differ$', first_line, re.I)
211
223
            if m is None:
212
 
                raise BzrError('external diff failed with exit code 2;'
213
 
                               ' command: %r' % (diffcmd,))
 
224
                raise errors.BzrError('external diff failed with exit code 2;'
 
225
                                      ' command: %r' % (diffcmd,))
214
226
            else:
215
227
                # Binary files differ, just return
216
228
                return
225
237
            else:
226
238
                msg = 'exit code %d' % rc
227
239
                
228
 
            raise BzrError('external diff failed with %s; command: %r' 
229
 
                           % (rc, diffcmd))
 
240
            raise errors.BzrError('external diff failed with %s; command: %r' 
 
241
                                  % (rc, diffcmd))
230
242
 
231
243
 
232
244
    finally:
289
301
 
290
302
def diff_cmd_helper(tree, specific_files, external_diff_options, 
291
303
                    old_revision_spec=None, new_revision_spec=None,
 
304
                    revision_specs=None,
292
305
                    old_label='a/', new_label='b/'):
293
306
    """Helper for cmd_diff.
294
307
 
295
 
   tree 
 
308
    :param tree:
296
309
        A WorkingTree
297
310
 
298
 
    specific_files
 
311
    :param specific_files:
299
312
        The specific files to compare, or None
300
313
 
301
 
    external_diff_options
 
314
    :param external_diff_options:
302
315
        If non-None, run an external diff, and pass it these options
303
316
 
304
 
    old_revision_spec
 
317
    :param old_revision_spec:
305
318
        If None, use basis tree as old revision, otherwise use the tree for
306
319
        the specified revision. 
307
320
 
308
 
    new_revision_spec
 
321
    :param new_revision_spec:
309
322
        If None, use working tree as new revision, otherwise use the tree for
310
323
        the specified revision.
311
324
    
 
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
 
312
330
    The more general form is show_diff_trees(), where the caller
313
331
    supplies any two trees.
314
332
    """
 
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
 
315
339
    def spec_tree(spec):
316
340
        if tree:
317
341
            revision = spec.in_store(tree.branch)
320
344
        revision_id = revision.rev_id
321
345
        branch = revision.branch
322
346
        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
 
323
356
    if old_revision_spec is None:
324
357
        old_tree = tree.basis_tree()
325
358
    else:
326
359
        old_tree = spec_tree(old_revision_spec)
327
360
 
328
 
    if new_revision_spec is None:
 
361
    if (new_revision_spec is None
 
362
        or new_revision_spec.spec is None):
329
363
        new_tree = tree
330
364
    else:
331
365
        new_tree = spec_tree(new_revision_spec)
 
366
 
332
367
    if new_tree is not tree:
333
368
        extra_trees = (tree,)
334
369
    else:
357
392
    """
358
393
    old_tree.lock_read()
359
394
    try:
 
395
        if extra_trees is not None:
 
396
            for tree in extra_trees:
 
397
                tree.lock_read()
360
398
        new_tree.lock_read()
361
399
        try:
362
400
            return _show_diff_trees(old_tree, new_tree, to_file,
365
403
                                    extra_trees=extra_trees)
366
404
        finally:
367
405
            new_tree.unlock()
 
406
            if extra_trees is not None:
 
407
                for tree in extra_trees:
 
408
                    tree.unlock()
368
409
    finally:
369
410
        old_tree.unlock()
370
411
 
430
471
        has_changes = 1
431
472
        prop_str = get_prop_change(meta_modified)
432
473
        print >>to_file, '=== modified %s %r%s' % (kind, path.encode('utf8'), prop_str)
433
 
        old_name = '%s%s\t%s' % (old_label, path,
434
 
                                 _patch_header_date(old_tree, file_id, path))
 
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))
435
479
        new_name = '%s%s\t%s' % (new_label, path,
436
480
                                 _patch_header_date(new_tree, file_id, path))
437
481
        if text_modified:
444
488
 
445
489
def _patch_header_date(tree, file_id, path):
446
490
    """Returns a timestamp suitable for use in a patch header."""
447
 
    tm = time.gmtime(tree.get_file_mtime(file_id, path))
448
 
    return time.strftime('%Y-%m-%d %H:%M:%S +0000', tm)
 
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)
449
496
 
450
497
 
451
498
def _raise_if_nonexistent(paths, old_tree, new_tree):