/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 breezy/annotate.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""File annotate based on weave storage"""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
# TODO: Choice of more or less verbose formats:
20
22
#
21
23
# interposed: show more details between blocks of modified lines
30
32
 
31
33
from .lazy_import import lazy_import
32
34
lazy_import(globals(), """
33
 
 
34
 
import patiencediff
35
 
 
36
35
from breezy import (
 
36
    patiencediff,
37
37
    tsort,
38
38
    )
39
39
""")
40
40
from . import (
41
 
    config,
42
41
    errors,
43
42
    osutils,
44
43
    )
 
44
from .config import (
 
45
    NoEmailInUsername,
 
46
    NoWhoami,
 
47
    extract_email_address,
 
48
    )
45
49
from .repository import _strip_NULL_ghosts
46
50
from .revision import (
47
51
    CURRENT_REVISION,
50
54
 
51
55
 
52
56
def annotate_file_tree(tree, path, to_file, verbose=False, full=False,
53
 
                       show_ids=False, branch=None):
54
 
    """Annotate path in a tree.
 
57
    show_ids=False, branch=None, file_id=None):
 
58
    """Annotate file_id in a tree.
55
59
 
56
60
    The tree should already be read_locked() when annotate_file_tree is called.
57
61
 
62
66
        reasonable text width.
63
67
    :param full: XXXX Not sure what this does.
64
68
    :param show_ids: Show revision ids in the annotation output.
 
69
    :param file_id: The file_id to annotate (must match file path)
65
70
    :param branch: Branch to use for revision revno lookups
66
71
    """
67
72
    if branch is None:
69
74
    if to_file is None:
70
75
        to_file = sys.stdout
71
76
 
72
 
    encoding = osutils.get_terminal_encoding()
73
77
    # Handle the show_ids case
74
 
    annotations = list(tree.annotate_iter(path))
 
78
    annotations = list(tree.annotate_iter(path, file_id))
75
79
    if show_ids:
76
 
        return _show_id_annotations(annotations, to_file, full, encoding)
 
80
        return _show_id_annotations(annotations, to_file, full)
77
81
 
78
82
    if not getattr(tree, "get_revision_id", False):
79
83
        # Create a virtual revision to represent the current tree state.
83
87
        current_rev.parent_ids = tree.get_parent_ids()
84
88
        try:
85
89
            current_rev.committer = branch.get_config_stack().get('email')
86
 
        except errors.NoWhoami:
 
90
        except NoWhoami:
87
91
            current_rev.committer = 'local user'
88
92
        current_rev.message = "?"
89
93
        current_rev.timestamp = round(time.time(), 3)
90
94
        current_rev.timezone = osutils.local_time_offset()
91
95
    else:
92
96
        current_rev = None
93
 
    annotation = list(_expand_annotations(
94
 
        annotations, branch, current_rev))
95
 
    _print_annotations(annotation, verbose, to_file, full, encoding)
96
 
 
97
 
 
98
 
def _print_annotations(annotation, verbose, to_file, full, encoding):
 
97
    annotation = list(_expand_annotations(annotations, branch,
 
98
        current_rev))
 
99
    _print_annotations(annotation, verbose, to_file, full)
 
100
 
 
101
 
 
102
def _print_annotations(annotation, verbose, to_file, full):
99
103
    """Print annotations to to_file.
100
104
 
101
105
    :param to_file: The file to output the annotation to.
104
108
    :param full: XXXX Not sure what this does.
105
109
    """
106
110
    if len(annotation) == 0:
107
 
        max_origin_len = max_revno_len = 0
 
111
        max_origin_len = max_revno_len = max_revid_len = 0
108
112
    else:
109
113
        max_origin_len = max(len(x[1]) for x in annotation)
110
114
        max_revno_len = max(len(x[0]) for x in annotation)
 
115
        max_revid_len = max(len(x[3]) for x in annotation)
111
116
    if not verbose:
112
117
        max_revno_len = min(max_revno_len, 12)
113
118
    max_revno_len = max(max_revno_len, 3)
120
125
                                       max_origin_len, author, date_str)
121
126
        else:
122
127
            if len(revno_str) > max_revno_len:
123
 
                revno_str = revno_str[:max_revno_len - 1] + '>'
 
128
                revno_str = revno_str[:max_revno_len-1] + '>'
124
129
            anno = "%-*s %-7s " % (max_revno_len, revno_str, author[:7])
125
130
        if anno.lstrip() == "" and full:
126
131
            anno = prevanno
127
132
        # GZ 2017-05-21: Writing both unicode annotation and bytes from file
128
133
        # which the given to_file must cope with.
129
134
        to_file.write(anno)
130
 
        to_file.write('| %s\n' % (text.decode(encoding),))
 
135
        to_file.write('| %s\n' % (text,))
131
136
        prevanno = anno
132
137
 
133
138
 
134
 
def _show_id_annotations(annotations, to_file, full, encoding):
 
139
def _show_id_annotations(annotations, to_file, full):
135
140
    if not annotations:
136
141
        return
137
142
    last_rev_id = None
140
145
        if full or last_rev_id != origin:
141
146
            this = origin
142
147
        else:
143
 
            this = b''
144
 
        to_file.write('%*s | %s' % (
145
 
            max_origin_len, this.decode('utf-8'), text.decode(encoding)))
 
148
            this = ''
 
149
        to_file.write('%*s | %s' % (max_origin_len, this, text))
146
150
        last_rev_id = origin
147
151
    return
148
152
 
158
162
    :param branch: A locked branch to query for revision details.
159
163
    """
160
164
    repository = branch.repository
161
 
    revision_ids = set(o for o, t in annotations)
162
165
    if current_rev is not None:
163
166
        # This can probably become a function on MutableTree, get_revno_map
164
167
        # there, or something.
169
172
        #      Once KnownGraph gets an 'add_node()' function, we can use
170
173
        #      VF.get_known_graph_ancestry().
171
174
        graph = repository.get_graph()
172
 
        revision_graph = {
173
 
            key: value for key, value in
174
 
            graph.iter_ancestry(current_rev.parent_ids) if value is not None}
 
175
        revision_graph = dict(((key, value) for key, value in
 
176
            graph.iter_ancestry(current_rev.parent_ids) if value is not None))
175
177
        revision_graph = _strip_NULL_ghosts(revision_graph)
176
178
        revision_graph[last_revision] = current_rev.parent_ids
177
179
        merge_sorted_revisions = tsort.merge_sort(
179
181
            last_revision,
180
182
            None,
181
183
            generate_revno=True)
182
 
        revision_id_to_revno = {
183
 
            rev_id: revno
 
184
        revision_id_to_revno = dict((rev_id, revno)
184
185
            for seq_num, rev_id, depth, revno, end_of_merge in
185
 
            merge_sorted_revisions}
 
186
                merge_sorted_revisions)
186
187
    else:
187
 
        # TODO(jelmer): Only look up the revision ids that we need (i.e. those
188
 
        # in revision_ids). Possibly add a HPSS call that can look those up
189
 
        # in bulk over HPSS.
190
188
        revision_id_to_revno = branch.get_revision_id_to_revno_map()
191
189
    last_origin = None
 
190
    revision_ids = set(o for o, t in annotations)
192
191
    revisions = {}
193
192
    if CURRENT_REVISION in revision_ids:
194
193
        revision_id_to_revno[CURRENT_REVISION] = (
195
194
            "%d?" % (branch.revno() + 1),)
196
195
        revisions[CURRENT_REVISION] = current_rev
197
196
    revisions.update(
198
 
        entry for entry in
199
 
        repository.iter_revisions(revision_ids)
200
 
        if entry[1] is not None)
 
197
            entry for entry in
 
198
            repository.iter_revisions(revision_ids)
 
199
            if entry[1] is not None)
201
200
    for origin, text in annotations:
202
 
        text = text.rstrip(b'\r\n')
 
201
        text = text.rstrip('\r\n')
203
202
        if origin == last_origin:
204
203
            (revno_str, author, date_str) = ('', '', '')
205
204
        else:
207
206
            if origin not in revisions:
208
207
                (revno_str, author, date_str) = ('?', '?', '?')
209
208
            else:
210
 
                revno_str = '.'.join(
211
 
                    str(i) for i in revision_id_to_revno[origin])
 
209
                revno_str = '.'.join(str(i) for i in
 
210
                                            revision_id_to_revno[origin])
212
211
            rev = revisions[origin]
213
212
            tz = rev.timezone or 0
214
213
            date_str = time.strftime('%Y%m%d',
216
215
            # a lazy way to get something like the email address
217
216
            # TODO: Get real email address
218
217
            author = rev.get_apparent_authors()[0]
219
 
            _, email = config.parse_username(author)
220
 
            if email:
221
 
                author = email
 
218
            try:
 
219
                author = extract_email_address(author)
 
220
            except NoEmailInUsername:
 
221
                pass        # use the whole name
222
222
        yield (revno_str, author, date_str, origin, text)
223
223
 
224
224
 
278
278
    new_cur = 0
279
279
    if matching_blocks is None:
280
280
        plain_parent_lines = [l for r, l in parent_lines]
281
 
        matcher = patiencediff.PatienceSequenceMatcher(
282
 
            None, plain_parent_lines, new_lines)
 
281
        matcher = patiencediff.PatienceSequenceMatcher(None,
 
282
            plain_parent_lines, new_lines)
283
283
        matching_blocks = matcher.get_matching_blocks()
284
284
    lines = []
285
285
    for i, j, n in matching_blocks:
286
286
        for line in new_lines[new_cur:j]:
287
287
            lines.append((new_revision_id, line))
288
 
        lines.extend(parent_lines[i:i + n])
 
288
        lines.extend(parent_lines[i:i+n])
289
289
        new_cur = j + n
290
290
    return lines
291
291
 
297
297
 
298
298
_break_annotation_tie = None
299
299
 
300
 
 
301
300
def _old_break_annotation_tie(annotated_lines):
302
301
    """Chose an attribution between several possible ones.
303
302
 
354
353
    for right_idx, child_idx, match_len in match_blocks:
355
354
        # All the lines that don't match are just passed along
356
355
        if child_idx > last_child_idx:
357
 
            output_extend(child_lines[start_child + last_child_idx:
358
 
                                      start_child + child_idx])
 
356
            output_extend(child_lines[start_child + last_child_idx
 
357
                                      :start_child + child_idx])
359
358
        for offset in range(match_len):
360
 
            left = child_lines[start_child + child_idx + offset]
361
 
            right = right_lines[start_right + right_idx + offset]
 
359
            left = child_lines[start_child+child_idx+offset]
 
360
            right = right_lines[start_right+right_idx+offset]
362
361
            if left[0] == right[0]:
363
362
                # The annotations match, just return the left one
364
363
                output_append(left)
408
407
    # be the bulk of the lines, and they will need no further processing.
409
408
    lines = []
410
409
    lines_extend = lines.extend
411
 
    # The line just after the last match from the right side
412
 
    last_right_idx = 0
 
410
    last_right_idx = 0 # The line just after the last match from the right side
413
411
    last_left_idx = 0
414
412
    matching_left_and_right = _get_matching_blocks(right_parent_lines,
415
413
                                                   annotated_lines)
440
438
    from breezy._annotator_pyx import Annotator
441
439
except ImportError as e:
442
440
    osutils.failed_to_load_extension(e)
443
 
    from breezy._annotator_py import Annotator  # noqa: F401
 
441
    from breezy._annotator_py import Annotator