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