1
from bzrlib import Branch
2
from bzrlib.commands import Command
9
def iter_anno_data(branch, file_id):
10
later_revision = branch.revno()
11
q = range(branch.revno())
13
later_text_id = branch.basis_tree().inventory[file_id].text_id
17
cur_tree = branch.revision_tree(branch.lookup_revision(revno))
18
if file_id not in cur_tree.inventory:
21
text_id = cur_tree.inventory[file_id].text_id
22
if text_id != later_text_id:
23
patch = get_patch(branch, revno, later_revision, file_id)
24
yield revno, patch.iter_inserted(), patch
25
later_revision = revno
26
later_text_id = text_id
27
yield progress.Progress("revisions", i)
29
def get_patch(branch, old_revno, new_revno, file_id):
30
old_tree = branch.revision_tree(branch.lookup_revision(old_revno))
31
new_tree = branch.revision_tree(branch.lookup_revision(new_revno))
32
if file_id in old_tree.inventory:
33
old_file = old_tree.get_file(file_id).readlines()
36
ud = difflib.unified_diff(old_file, new_tree.get_file(file_id).readlines())
37
return patches.parse_patch(ud)
39
class cmd_annotate(Command):
40
"""Show which revision added each line in a file"""
42
takes_args = ['filename']
43
def run(self, filename):
44
if not os.path.exists(filename):
45
raise BzrCommandError("The file %s does not exist." % filename)
46
branch = (Branch(filename))
47
file_id = branch.working_tree().path2id(filename)
49
raise BzrCommandError("The file %s is not versioned." % filename)
50
lines = branch.basis_tree().get_file(file_id)
51
total = branch.revno()
52
anno_d_iter = iter_anno_data(branch, file_id)
53
progress_bar = progress.ProgressBar()
55
for result in patches.iter_annotate_file(lines, anno_d_iter):
56
if isinstance(result, progress.Progress):
62
progress.clear_progress_bar()
63
for line in anno_lines:
64
sys.stdout.write("%4s:%s" % (str(line.log), line.text))