/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-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

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