85
by Aaron Bentley
Added annotate plugin |
1 |
from bzrlib import Branch |
2 |
from bzrlib.commands import Command |
|
3 |
import os |
|
4 |
import progress |
|
5 |
import patches |
|
6 |
import difflib |
|
7 |
import sys |
|
8 |
||
9 |
def iter_anno_data(branch, file_id): |
|
10 |
later_revision = branch.revno() |
|
11 |
q = range(branch.revno()) |
|
12 |
q.reverse() |
|
13 |
later_text_id = branch.basis_tree().inventory[file_id].text_id |
|
14 |
i = 0 |
|
15 |
for revno in q: |
|
16 |
i += 1 |
|
17 |
cur_tree = branch.revision_tree(branch.lookup_revision(revno)) |
|
18 |
if file_id not in cur_tree.inventory: |
|
19 |
text_id = None |
|
20 |
else: |
|
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) |
|
28 |
||
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() |
|
34 |
else: |
|
35 |
old_file = [] |
|
36 |
ud = difflib.unified_diff(old_file, new_tree.get_file(file_id).readlines()) |
|
37 |
return patches.parse_patch(ud) |
|
38 |
||
39 |
class cmd_annotate(Command): |
|
40 |
"""Show which revision added each line in a file"""
|
|
41 |
||
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) |
|
48 |
if file_id is None: |
|
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() |
|
54 |
try: |
|
55 |
for result in patches.iter_annotate_file(lines, anno_d_iter): |
|
56 |
if isinstance(result, progress.Progress): |
|
57 |
result.total = total |
|
58 |
progress_bar(result) |
|
59 |
else: |
|
60 |
anno_lines = result |
|
61 |
finally: |
|
62 |
progress.clear_progress_bar() |
|
63 |
for line in anno_lines: |
|
64 |
sys.stdout.write("%4s:%s" % (str(line.log), line.text)) |
|
65 |