/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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