/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: Martin Pool
  • Date: 2007-09-14 06:31:28 UTC
  • mfrom: (2822 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2823.
  • Revision ID: mbp@sourcefrog.net-20070914063128-0p7mh6zfb4pzdg9p
merge trunk

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 difflib
17
18
import os
18
19
import re
19
20
import sys
34
35
    )
35
36
""")
36
37
 
37
 
# compatability - plugins import compare_trees from diff!!!
38
 
# deprecated as of 0.10
39
 
from bzrlib.delta import compare_trees
40
38
from bzrlib.symbol_versioning import (
41
39
        deprecated_function,
42
 
        zero_eight,
43
40
        )
44
41
from bzrlib.trace import mutter, warning
45
42
 
48
45
# invoke callbacks on an object.  That object can either accumulate a
49
46
# list, write them out directly, etc etc.
50
47
 
 
48
 
 
49
class _PrematchedMatcher(difflib.SequenceMatcher):
 
50
    """Allow SequenceMatcher operations to use predetermined blocks"""
 
51
 
 
52
    def __init__(self, matching_blocks):
 
53
        difflib.SequenceMatcher(self, None, None)
 
54
        self.matching_blocks = matching_blocks
 
55
        self.opcodes = None
 
56
 
 
57
 
51
58
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
52
59
                  allow_binary=False, sequence_matcher=None,
53
60
                  path_encoding='utf8'):
261
268
                        new_abspath, e)
262
269
 
263
270
 
264
 
@deprecated_function(zero_eight)
265
 
def show_diff(b, from_spec, specific_files, external_diff_options=None,
266
 
              revision2=None, output=None, b2=None):
267
 
    """Shortcut for showing the diff to the working tree.
268
 
 
269
 
    Please use show_diff_trees instead.
270
 
 
271
 
    b
272
 
        Branch.
273
 
 
274
 
    revision
275
 
        None for 'basis tree', or otherwise the old revision to compare against.
276
 
    
277
 
    The more general form is show_diff_trees(), where the caller
278
 
    supplies any two trees.
279
 
    """
280
 
    if output is None:
281
 
        output = sys.stdout
282
 
 
283
 
    if from_spec is None:
284
 
        old_tree = b.bzrdir.open_workingtree()
285
 
        if b2 is None:
286
 
            old_tree = old_tree = old_tree.basis_tree()
287
 
    else:
288
 
        old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
289
 
 
290
 
    if revision2 is None:
291
 
        if b2 is None:
292
 
            new_tree = b.bzrdir.open_workingtree()
293
 
        else:
294
 
            new_tree = b2.bzrdir.open_workingtree()
295
 
    else:
296
 
        new_tree = b.repository.revision_tree(revision2.in_history(b).rev_id)
297
 
 
298
 
    return show_diff_trees(old_tree, new_tree, output, specific_files,
299
 
                           external_diff_options)
300
 
 
301
 
 
302
271
def diff_cmd_helper(tree, specific_files, external_diff_options, 
303
272
                    old_revision_spec=None, new_revision_spec=None,
304
273
                    revision_specs=None,
378
347
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
379
348
                    external_diff_options=None,
380
349
                    old_label='a/', new_label='b/',
381
 
                    extra_trees=None):
 
350
                    extra_trees=None,
 
351
                    path_encoding='utf8'):
382
352
    """Show in text form the changes from one tree to another.
383
353
 
384
354
    to_files
389
359
 
390
360
    extra_trees
391
361
        If set, more Trees to use for looking up file ids
 
362
 
 
363
    path_encoding
 
364
        If set, the path will be encoded as specified, otherwise is supposed
 
365
        to be utf8
392
366
    """
393
367
    old_tree.lock_read()
394
368
    try:
400
374
            return _show_diff_trees(old_tree, new_tree, to_file,
401
375
                                    specific_files, external_diff_options,
402
376
                                    old_label=old_label, new_label=new_label,
403
 
                                    extra_trees=extra_trees)
 
377
                                    extra_trees=extra_trees,
 
378
                                    path_encoding=path_encoding)
404
379
        finally:
405
380
            new_tree.unlock()
406
381
            if extra_trees is not None:
411
386
 
412
387
 
413
388
def _show_diff_trees(old_tree, new_tree, to_file,
414
 
                     specific_files, external_diff_options, 
 
389
                     specific_files, external_diff_options, path_encoding,
415
390
                     old_label='a/', new_label='b/', extra_trees=None):
416
391
 
417
392
    # GNU Patch uses the epoch date to detect files that are being added
436
411
    has_changes = 0
437
412
    for path, file_id, kind in delta.removed:
438
413
        has_changes = 1
439
 
        print >>to_file, '=== removed %s %r' % (kind, path.encode('utf8'))
 
414
        path_encoded = path.encode(path_encoding, "replace")
 
415
        print >>to_file, "=== removed %s '%s'" % (kind, path_encoded)
440
416
        old_name = '%s%s\t%s' % (old_label, path,
441
417
                                 _patch_header_date(old_tree, file_id, path))
442
418
        new_name = '%s%s\t%s' % (new_label, path, EPOCH_DATE)
444
420
                                         new_name, None, None, to_file)
445
421
    for path, file_id, kind in delta.added:
446
422
        has_changes = 1
447
 
        print >>to_file, '=== added %s %r' % (kind, path.encode('utf8'))
 
423
        path_encoded = path.encode(path_encoding, "replace")
 
424
        print >>to_file, "=== added %s '%s'" % (kind, path_encoded)
448
425
        old_name = '%s%s\t%s' % (old_label, path, EPOCH_DATE)
449
426
        new_name = '%s%s\t%s' % (new_label, path,
450
427
                                 _patch_header_date(new_tree, file_id, path))
455
432
         text_modified, meta_modified) in delta.renamed:
456
433
        has_changes = 1
457
434
        prop_str = get_prop_change(meta_modified)
458
 
        print >>to_file, '=== renamed %s %r => %r%s' % (
459
 
                    kind, old_path.encode('utf8'),
460
 
                    new_path.encode('utf8'), prop_str)
 
435
        oldpath_encoded = old_path.encode(path_encoding, "replace")
 
436
        newpath_encoded = new_path.encode(path_encoding, "replace")
 
437
        print >>to_file, "=== renamed %s '%s' => '%s'%s" % (kind,
 
438
                            oldpath_encoded, newpath_encoded, prop_str)
461
439
        old_name = '%s%s\t%s' % (old_label, old_path,
462
440
                                 _patch_header_date(old_tree, file_id,
463
441
                                                    old_path))
470
448
    for path, file_id, kind, text_modified, meta_modified in delta.modified:
471
449
        has_changes = 1
472
450
        prop_str = get_prop_change(meta_modified)
473
 
        print >>to_file, '=== modified %s %r%s' % (kind, path.encode('utf8'), prop_str)
 
451
        path_encoded = path.encode(path_encoding, "replace")
 
452
        print >>to_file, "=== modified %s '%s'%s" % (kind,
 
453
                            path_encoded, prop_str)
474
454
        # The file may be in a different location in the old tree (because
475
455
        # the containing dir was renamed, but the file itself was not)
476
456
        old_path = old_tree.id2path(file_id)