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

  • Committer: John Arbash Meinel
  • Date: 2009-06-17 19:57:44 UTC
  • mto: This revision was merged to the branch mainline in revision 4522.
  • Revision ID: john@arbash-meinel.com-20090617195744-vugtpm7w05sfdmxm
Start implementing the reannotation functionality directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    errors,
22
22
    graph as _mod_graph,
23
23
    osutils,
 
24
    patiencediff,
24
25
    )
25
26
 
26
27
 
35
36
        """Create a new Annotator from a VersionedFile."""
36
37
        self._vf = vf
37
38
        self._parent_map = {}
38
 
        self._parent_lines_cache = {}
39
 
        self._parent_annotations_cache = {}
 
39
        self._lines_cache = {}
 
40
        self._annotations_cache = {}
40
41
        self._heads_provider = None
41
42
 
42
43
    def _get_needed_texts(self, key):
52
53
            self._heads_provider = _mod_graph.KnownGraph(self._parent_map)
53
54
        return self._heads_provider
54
55
 
 
56
    def _reannotate_one_parent(self, annotations, lines, key, parent_key):
 
57
        """Reannotate this text relative to its first parent."""
 
58
        parent_lines = self._lines_cache[parent_key]
 
59
        parent_annotations = self._annotations_cache[parent_key]
 
60
        # PatienceSequenceMatcher should probably be part of Policy
 
61
        matcher = patiencediff.PatienceSequenceMatcher(None,
 
62
            parent_lines, lines)
 
63
        matching_blocks = matcher.get_matching_blocks()
 
64
 
 
65
        for parent_idx, lines_idx, match_len in matching_blocks:
 
66
            # For all matching regions we copy across the parent annotations
 
67
            annotations[lines_idx:lines_idx + match_len] = \
 
68
                parent_annotations[parent_idx:parent_idx + match_len]
 
69
 
55
70
    def annotate(self, key):
56
71
        """Return annotated fulltext for the given key."""
57
72
        keys = self._get_needed_texts(key)
58
 
        reannotate = annotate.reannotate
59
73
        heads_provider = self._get_heads_provider
60
74
        for record in self._vf.get_record_stream(keys, 'topological', True):
61
 
            key = record.key
 
75
            this_key = record.key
62
76
            lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
63
 
            parents = self._parent_map[key]
64
 
            if parents is not None:
65
 
                parent_lines = [self._parent_lines_cache[parent]
66
 
                                for parent in parents]
67
 
            else:
68
 
                parent_lines = []
69
 
            self._parent_lines_cache[key] = list(reannotate(
70
 
                parent_lines, lines, key, None, heads_provider))
 
77
            annotations = [(this_key,)]*len(lines)
 
78
            self._lines_cache[this_key] = lines
 
79
            self._annotations_cache[this_key] = annotations
 
80
 
 
81
            parents = self._parent_map[this_key]
 
82
            if not parents:
 
83
                continue
 
84
            self._reannotate_one_parent(annotations, lines, key, parents[0])
71
85
        try:
72
 
            annotated = self._parent_lines_cache[key]
73
 
        except KeyError, e:
 
86
            annotations = self._annotations_cache[key]
 
87
        except KeyError:
74
88
            raise errors.RevisionNotPresent(key, self._vf)
75
 
        annotations = [(a,) for a,l in annotated]
76
 
        lines = [l for a,l in annotated]
77
 
        return annotations, lines
 
89
        return annotations, self._lines_cache[key]