~abentley/bzrtools/bzrtools.dev

479 by Aaron Bentley
Tolerate commit ids with no email
1
from bzrlib import errors
292 by Aaron Bentley
Introduced branch-history command
2
from bzrlib.branch import Branch
3
from bzrlib.config import extract_email_address
4
from bzrtools import short_committer
5
def branch_history(branch):
6
    """Print history of a branch"""
7
    b = Branch.open_containing(branch)[0]
8
    descriptor = None
9
    start = None
336 by Aaron Bentley
Lock repo for branch-history
10
    b.repository.lock_read()
11
    try:
12
        for revno, revision in iter_revisiondata(b):
13
            new_descriptor = (revision.committer, 
14
                              revision.properties.get('branch-nick'))
15
            if descriptor is None:
16
                descriptor = new_descriptor
17
            if start is None:
18
                start = revno
19
            if branch_change(descriptor, new_descriptor):
20
                print_info(descriptor, start, revno - 1)
21
                start = revno
292 by Aaron Bentley
Introduced branch-history command
22
            descriptor = new_descriptor
336 by Aaron Bentley
Lock repo for branch-history
23
        print_info(descriptor, start, revno)
24
    finally:
25
        b.repository.unlock()
292 by Aaron Bentley
Introduced branch-history command
26
27
def branch_change(old_descriptor, new_descriptor):
479 by Aaron Bentley
Tolerate commit ids with no email
28
    try:
29
        old_email = extract_email_address(old_descriptor[0])
30
    except errors.NoEmailInUsername:
31
        old_email = None
32
    try:
33
        new_email = extract_email_address(new_descriptor[0])
34
    except errors.NoEmailInUsername:
35
        new_email = None
292 by Aaron Bentley
Introduced branch-history command
36
    if old_descriptor == new_descriptor:
37
        return False
38
    elif None not in (old_descriptor[1], new_descriptor[1]) and \
39
        old_descriptor[1] != new_descriptor[1]:
40
        return True
41
    elif short_committer(old_descriptor[0]) ==\
42
        short_committer(new_descriptor[0]):
43
        return False
479 by Aaron Bentley
Tolerate commit ids with no email
44
    elif old_descriptor[0].strip(' ') == new_email:
292 by Aaron Bentley
Introduced branch-history command
45
        return False
479 by Aaron Bentley
Tolerate commit ids with no email
46
    elif new_descriptor[0].strip(' ') == old_email:
292 by Aaron Bentley
Introduced branch-history command
47
        return False
48
    else:
49
        return True
50
51
def iter_revisiondata(branch):
52
    """Iterate through revno, Revision pairs in the revision history"""
53
    for no, revision_id in enumerate(branch.revision_history()):
308 by Aaron Bentley
got branch-history under test
54
        yield no+1, branch.repository.get_revision(revision_id)
292 by Aaron Bentley
Introduced branch-history command
55
56
def print_info(descriptor, start, end):
57
    """Print revision history"""
58
    descriptor_string = descriptor[0]
59
    if descriptor[1] is not None:
60
        descriptor_string += " / "+ descriptor[1]
61
    print descriptor_string
62
    if start != end:
63
        print "  %d .. %d" % (start, end)
64
    else:
65
        print "  %d" % start