1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
1
# Copyright (C) 2005-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
31
40
from bzrlib import (
37
44
from bzrlib.config import extract_email_address
38
45
from bzrlib.repository import _strip_NULL_ghosts
39
from bzrlib.revision import CURRENT_REVISION, Revision
46
from bzrlib.revision import (
50
from bzrlib.symbol_versioning import (
56
@deprecated_function(deprecated_in((2, 4, 0)))
42
57
def annotate_file(branch, rev_id, file_id, verbose=False, full=False,
43
58
to_file=None, show_ids=False):
44
59
"""Annotate file_id at revision rev_id in branch.
56
71
:param show_ids: Show revision ids in the annotation output.
61
# Handle the show_ids case
62
annotations = _annotations(branch.repository, file_id, rev_id)
64
return _show_id_annotations(annotations, to_file, full)
66
# Calculate the lengths of the various columns
67
annotation = list(_expand_annotations(annotations, branch))
68
_print_annotations(annotation, verbose, to_file, full)
73
tree = branch.repository.revision_tree(rev_id)
74
annotate_file_tree(tree, file_id, to_file, verbose=verbose,
75
full=full, show_ids=show_ids, branch=branch)
71
78
def annotate_file_tree(tree, file_id, to_file, verbose=False, full=False,
79
show_ids=False, branch=None):
73
80
"""Annotate file_id in a tree.
75
82
The tree should already be read_locked() when annotate_file_tree is called.
81
88
reasonable text width.
82
89
:param full: XXXX Not sure what this does.
83
90
:param show_ids: Show revision ids in the annotation output.
91
:param branch: Branch to use for revision revno lookups
85
rev_id = tree.last_revision()
88
98
# Handle the show_ids case
89
99
annotations = list(tree.annotate_iter(file_id))
91
101
return _show_id_annotations(annotations, to_file, full)
93
# Create a virtual revision to represent the current tree state.
94
# Should get some more pending commit attributes, like pending tags,
96
current_rev = Revision(CURRENT_REVISION)
97
current_rev.parent_ids = tree.get_parent_ids()
98
current_rev.committer = tree.branch.get_config().username()
99
current_rev.message = "?"
100
current_rev.timestamp = round(time.time(), 3)
101
current_rev.timezone = osutils.local_time_offset()
102
annotation = list(_expand_annotations(annotations, tree.branch,
103
if not getattr(tree, "get_revision_id", False):
104
# Create a virtual revision to represent the current tree state.
105
# Should get some more pending commit attributes, like pending tags,
107
current_rev = Revision(CURRENT_REVISION)
108
current_rev.parent_ids = tree.get_parent_ids()
110
current_rev.committer = branch.get_config_stack().get('email')
111
except errors.NoWhoami:
112
current_rev.committer = 'local user'
113
current_rev.message = "?"
114
current_rev.timestamp = round(time.time(), 3)
115
current_rev.timezone = osutils.local_time_offset()
118
annotation = list(_expand_annotations(annotations, branch,
104
120
_print_annotations(annotation, verbose, to_file, full)
168
def _annotations(repo, file_id, rev_id):
169
"""Return the list of (origin_revision_id, line_text) for a revision of a file in a repository."""
170
annotations = repo.texts.annotate((file_id, rev_id))
172
return [(key[-1], line) for (key, line) in annotations]
175
184
def _expand_annotations(annotations, branch, current_rev=None):
176
185
"""Expand a file's annotations into command line UI ready tuples.
185
194
repository = branch.repository
186
195
if current_rev is not None:
187
# This can probably become a function on MutableTree, get_revno_map there,
196
# This can probably become a function on MutableTree, get_revno_map
197
# there, or something.
189
198
last_revision = current_rev.revision_id
190
199
# XXX: Partially Cloned from branch, uses the old_get_graph, eep.
200
# XXX: The main difficulty is that we need to inject a single new node
201
# (current_rev) into the graph before it gets numbered, etc.
202
# Once KnownGraph gets an 'add_node()' function, we can use
203
# VF.get_known_graph_ancestry().
191
204
graph = repository.get_graph()
192
205
revision_graph = dict(((key, value) for key, value in
193
206
graph.iter_ancestry(current_rev.parent_ids) if value is not None))
310
323
def _get_matching_blocks(old, new):
311
matcher = patiencediff.PatienceSequenceMatcher(None,
324
matcher = patiencediff.PatienceSequenceMatcher(None, old, new)
313
325
return matcher.get_matching_blocks()
316
def _break_annotation_tie(annotated_lines):
328
_break_annotation_tie = None
330
def _old_break_annotation_tie(annotated_lines):
317
331
"""Chose an attribution between several possible ones.
319
333
:param annotated_lines: A list of tuples ((file_id, rev_id), line) where
394
408
# If the result is not stable, there is a risk a
395
409
# performance degradation as criss-cross merges will
396
410
# flip-flop the attribution.
397
output_append(_break_annotation_tie([left, right]))
411
if _break_annotation_tie is None:
413
_old_break_annotation_tie([left, right]))
415
output_append(_break_annotation_tie([left, right]))
398
416
last_child_idx = child_idx + match_len
444
462
# If left and right agree on a range, just push that into the output
445
463
lines_extend(annotated_lines[left_idx:left_idx + match_len])
468
from bzrlib._annotator_pyx import Annotator
469
except ImportError, e:
470
osutils.failed_to_load_extension(e)
471
from bzrlib._annotator_py import Annotator