/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: Aaron Bentley
  • Date: 2007-11-22 18:21:17 UTC
  • mto: This revision was merged to the branch mainline in revision 3036.
  • Revision ID: abentley@panoramicfeedback.com-20071122182117-hs220sdlczwhy8zw
Update docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
421
421
 
422
422
 
423
423
class FileDiffer(object):
424
 
 
425
424
    """Base type for command object that compare files"""
 
425
 
426
426
    # The type or contents of the file were unsuitable for diffing
427
427
    CANNOT_DIFF = object()
428
428
    # The file has changed in a semantic way
431
431
    UNCHANGED = object()
432
432
 
433
433
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8'):
 
434
        """Constructor.
 
435
 
 
436
        :param old_tree: The tree to show as the old tree in the comparison
 
437
        :param new_tree: The tree to show as new in the comparison
 
438
        :param to_file: The file to write comparison data to
 
439
        :param path_encoding: The character encoding to write paths in
 
440
        """
434
441
        self.old_tree = old_tree
435
442
        self.new_tree = new_tree
436
443
        self.to_file = to_file
448
455
 
449
456
 
450
457
class KindChangeDiffer(object):
 
458
    """Special differ for file kind changes.
451
459
 
 
460
    Represents kind change as deletion + creation.  Uses the other differs
 
461
    to do this.
 
462
    """
452
463
    def __init__(self, differs):
453
464
        self.differs = differs
454
465
 
455
466
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
467
        """Perform comparison
 
468
 
 
469
        :param file_id: The file_id of the file to compare
 
470
        :param old_path: Path of the file in the old tree
 
471
        :param new_path: Path of the file in the new tree
 
472
        :param old_kind: Old file-kind of the file
 
473
        :param new_kind: New file-kind of the file
 
474
        """
456
475
        differs = [d for d in self.differs if d is not self]
457
476
        result = FileDiffer._diff_many(differs, file_id, old_path, new_path,
458
477
                                       old_kind, None)
465
484
class SymlinkDiffer(FileDiffer):
466
485
 
467
486
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
487
        """Perform comparison between two symlinks
 
488
 
 
489
        :param file_id: The file_id of the file to compare
 
490
        :param old_path: Path of the file in the old tree
 
491
        :param new_path: Path of the file in the new tree
 
492
        :param old_kind: Old file-kind of the file
 
493
        :param new_kind: New file-kind of the file
 
494
        """
468
495
        if 'symlink' not in (old_kind, new_kind):
469
496
            return self.CANNOT_DIFF
470
497
        if old_kind == 'symlink':
507
534
        self.path_encoding = path_encoding
508
535
 
509
536
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
537
        """Compare two files in unified diff format
 
538
 
 
539
        :param file_id: The file_id of the file to compare
 
540
        :param old_path: Path of the file in the old tree
 
541
        :param new_path: Path of the file in the new tree
 
542
        :param old_kind: Old file-kind of the file
 
543
        :param new_kind: New file-kind of the file
 
544
        """
510
545
        if 'file' not in (old_kind, new_kind):
511
546
            return self.CANNOT_DIFF
512
547
        from_file_id = to_file_id = file_id
555
590
 
556
591
 
557
592
class TreeDiffer(object):
 
593
    """Object for comparing the contents of two trees"""
558
594
 
 
595
    # list of factories that can provide instances of FileDiffer objects
 
596
    # may be extended by plugins.
559
597
    differ_factories = [SymlinkDiffer]
560
598
 
561
599
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
562
600
                 text_differ=None, extra_differs=None):
 
601
        """Constructor
 
602
 
 
603
        :param old_tree: Tree to show as old in the comparison
 
604
        :param new_tree: Tree to show as new in the comparison
 
605
        :param to_file: File to write comparision to
 
606
        :param path_encoding: Character encoding to write paths in
 
607
        :param text_differ: FileDiffer-type object to use as a last resort for
 
608
            diffing text files.
 
609
        :param extra_differs: FileDiffers to try before any other FileDiffers
 
610
        """
563
611
        if text_differ is None:
564
612
            text_differ = TextDiffer(old_tree, new_tree, to_file,
565
613
                                     path_encoding, '', '',  internal_diff)
578
626
 
579
627
    @classmethod
580
628
    def from_trees_options(klass, old_tree, new_tree, to_file,
581
 
                           external_diff_options, old_label, new_label,
582
 
                           path_encoding):
 
629
                           path_encoding, external_diff_options, old_label,
 
630
                           new_label):
 
631
        """Factory for producing a TreeDiffer.
 
632
 
 
633
        Designed to accept options used by show_diff_trees.
 
634
        :param old_tree: The tree to show as old in the comparison
 
635
        :param new_tree: The tree to show as new in the comparison
 
636
        :param to_file: File to write comparisons to
 
637
        :param path_encoding: Character encoding to use for writing paths
 
638
        :param external_diff_options: If supplied, use the installed diff
 
639
            binary to perform file comparison, using supplied options.
 
640
        :param old_label: Prefix to use for old file labels
 
641
        :param new_label: Prefix to use for new file labels
 
642
        """
583
643
        if external_diff_options:
584
644
            assert isinstance(external_diff_options, basestring)
585
645
            opts = external_diff_options.split()
592
652
        return klass(old_tree, new_tree, to_file, path_encoding, text_differ)
593
653
 
594
654
    def show_diff(self, specific_files, extra_trees=None):
 
655
        """Write tree diff to self.to_file
 
656
 
 
657
        :param sepecific_files: the specific files to compare (recursive)
 
658
        :param extra_trees: extra trees to use for mapping paths to file_ids
 
659
        """
595
660
        # TODO: Generation of pseudo-diffs for added/deleted files could
596
661
        # be usefully made into a much faster special case.
597
662
 
636
701
        return has_changes
637
702
 
638
703
    def diff(self, file_id, old_path, new_path):
 
704
        """Perform a diff of a single file
 
705
 
 
706
        :param file_id: file-id of the file
 
707
        :param old_path: The path of the file in the old tree
 
708
        :param new_path: The path of the file in the new tree
 
709
        """
639
710
        try:
640
711
            old_kind = self.old_tree.kind(file_id)
641
712
        except errors.NoSuchId: