52
def annotate_file_tree(tree, path, to_file, verbose=False, full=False,
53
show_ids=False, branch=None):
54
"""Annotate path in a tree.
56
def annotate_file_tree(tree, file_id, to_file, verbose=False, full=False,
57
show_ids=False, branch=None):
58
"""Annotate file_id in a tree.
56
60
The tree should already be read_locked() when annotate_file_tree is called.
58
62
:param tree: The tree to look for revision numbers and history from.
59
:param path: The path to annotate
63
:param file_id: The file_id to annotate.
60
64
:param to_file: The file to output the annotation to.
61
65
:param verbose: Show all details rather than truncating to ensure
62
66
reasonable text width.
69
73
if to_file is None:
70
74
to_file = sys.stdout
72
encoding = osutils.get_terminal_encoding()
73
76
# Handle the show_ids case
74
annotations = list(tree.annotate_iter(path))
77
annotations = list(tree.annotate_iter(file_id))
76
return _show_id_annotations(annotations, to_file, full, encoding)
79
return _show_id_annotations(annotations, to_file, full)
78
81
if not getattr(tree, "get_revision_id", False):
79
82
# Create a virtual revision to represent the current tree state.
83
86
current_rev.parent_ids = tree.get_parent_ids()
85
88
current_rev.committer = branch.get_config_stack().get('email')
86
except errors.NoWhoami:
87
90
current_rev.committer = 'local user'
88
91
current_rev.message = "?"
89
92
current_rev.timestamp = round(time.time(), 3)
90
93
current_rev.timezone = osutils.local_time_offset()
93
annotation = list(_expand_annotations(
94
annotations, branch, current_rev))
95
_print_annotations(annotation, verbose, to_file, full, encoding)
98
def _print_annotations(annotation, verbose, to_file, full, encoding):
96
annotation = list(_expand_annotations(annotations, branch,
98
_print_annotations(annotation, verbose, to_file, full)
101
def _print_annotations(annotation, verbose, to_file, full):
99
102
"""Print annotations to to_file.
101
104
:param to_file: The file to output the annotation to.
104
107
:param full: XXXX Not sure what this does.
106
109
if len(annotation) == 0:
107
max_origin_len = max_revno_len = 0
110
max_origin_len = max_revno_len = max_revid_len = 0
109
112
max_origin_len = max(len(x[1]) for x in annotation)
110
113
max_revno_len = max(len(x[0]) for x in annotation)
114
max_revid_len = max(len(x[3]) for x in annotation)
112
116
max_revno_len = min(max_revno_len, 12)
113
117
max_revno_len = max(max_revno_len, 3)
120
124
max_origin_len, author, date_str)
122
126
if len(revno_str) > max_revno_len:
123
revno_str = revno_str[:max_revno_len - 1] + '>'
127
revno_str = revno_str[:max_revno_len-1] + '>'
124
128
anno = "%-*s %-7s " % (max_revno_len, revno_str, author[:7])
125
129
if anno.lstrip() == "" and full:
127
131
# GZ 2017-05-21: Writing both unicode annotation and bytes from file
128
132
# which the given to_file must cope with.
129
133
to_file.write(anno)
130
to_file.write('| %s\n' % (text.decode(encoding),))
134
to_file.write('| %s\n' % (text,))
134
def _show_id_annotations(annotations, to_file, full, encoding):
138
def _show_id_annotations(annotations, to_file, full):
135
139
if not annotations:
137
141
last_rev_id = None
169
171
# Once KnownGraph gets an 'add_node()' function, we can use
170
172
# VF.get_known_graph_ancestry().
171
173
graph = repository.get_graph()
173
key: value for key, value in
174
graph.iter_ancestry(current_rev.parent_ids) if value is not None}
174
revision_graph = dict(((key, value) for key, value in
175
graph.iter_ancestry(current_rev.parent_ids) if value is not None))
175
176
revision_graph = _strip_NULL_ghosts(revision_graph)
176
177
revision_graph[last_revision] = current_rev.parent_ids
177
178
merge_sorted_revisions = tsort.merge_sort(
181
182
generate_revno=True)
182
revision_id_to_revno = {
183
revision_id_to_revno = dict((rev_id, revno)
184
184
for seq_num, rev_id, depth, revno, end_of_merge in
185
merge_sorted_revisions}
185
merge_sorted_revisions)
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
190
187
revision_id_to_revno = branch.get_revision_id_to_revno_map()
191
188
last_origin = None
189
revision_ids = set(o for o, t in annotations)
193
191
if CURRENT_REVISION in revision_ids:
194
192
revision_id_to_revno[CURRENT_REVISION] = (
195
193
"%d?" % (branch.revno() + 1),)
196
194
revisions[CURRENT_REVISION] = current_rev
197
195
revisions.update(
199
repository.iter_revisions(revision_ids)
200
if entry[1] is not None)
197
repository.iter_revisions(revision_ids)
198
if entry[1] is not None)
201
199
for origin, text in annotations:
202
text = text.rstrip(b'\r\n')
200
text = text.rstrip('\r\n')
203
201
if origin == last_origin:
204
(revno_str, author, date_str) = ('', '', '')
202
(revno_str, author, date_str) = ('','','')
206
204
last_origin = origin
207
205
if origin not in revisions:
208
(revno_str, author, date_str) = ('?', '?', '?')
206
(revno_str, author, date_str) = ('?','?','?')
210
revno_str = '.'.join(
211
str(i) for i in revision_id_to_revno[origin])
208
revno_str = '.'.join(str(i) for i in
209
revision_id_to_revno[origin])
212
210
rev = revisions[origin]
213
211
tz = rev.timezone or 0
214
212
date_str = time.strftime('%Y%m%d',
215
time.gmtime(rev.timestamp + tz))
213
osutils.gmtime(rev.timestamp + tz))
216
214
# a lazy way to get something like the email address
217
215
# TODO: Get real email address
218
216
author = rev.get_apparent_authors()[0]
219
_, email = config.parse_username(author)
218
author = extract_email_address(author)
219
except NoEmailInUsername:
220
pass # use the whole name
222
221
yield (revno_str, author, date_str, origin, text)
279
278
if matching_blocks is None:
280
279
plain_parent_lines = [l for r, l in parent_lines]
281
matcher = patiencediff.PatienceSequenceMatcher(
282
None, plain_parent_lines, new_lines)
280
matcher = patiencediff.PatienceSequenceMatcher(None,
281
plain_parent_lines, new_lines)
283
282
matching_blocks = matcher.get_matching_blocks()
285
284
for i, j, n in matching_blocks:
286
285
for line in new_lines[new_cur:j]:
287
286
lines.append((new_revision_id, line))
288
lines.extend(parent_lines[i:i + n])
287
lines.extend(parent_lines[i:i+n])
345
343
output_extend = output_lines.extend
346
344
output_append = output_lines.append
347
345
# We need to see if any of the unannotated lines match
348
plain_right_subset = [l for a, l in right_lines[start_right:end_right]]
346
plain_right_subset = [l for a,l in right_lines[start_right:end_right]]
349
347
plain_child_subset = plain_child_lines[start_child:end_child]
350
348
match_blocks = _get_matching_blocks(plain_right_subset, plain_child_subset)
354
352
for right_idx, child_idx, match_len in match_blocks:
355
353
# All the lines that don't match are just passed along
356
354
if child_idx > last_child_idx:
357
output_extend(child_lines[start_child + last_child_idx:
358
start_child + child_idx])
355
output_extend(child_lines[start_child + last_child_idx
356
:start_child + child_idx])
359
357
for offset in range(match_len):
360
left = child_lines[start_child + child_idx + offset]
361
right = right_lines[start_right + right_idx + offset]
358
left = child_lines[start_child+child_idx+offset]
359
right = right_lines[start_right+right_idx+offset]
362
360
if left[0] == right[0]:
363
361
# The annotations match, just return the left one
364
362
output_append(left)
408
406
# be the bulk of the lines, and they will need no further processing.
410
408
lines_extend = lines.extend
411
# The line just after the last match from the right side
409
last_right_idx = 0 # The line just after the last match from the right side
413
410
last_left_idx = 0
414
411
matching_left_and_right = _get_matching_blocks(right_parent_lines,