~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Michael Ellerman
  • Date: 2006-03-12 01:04:54 UTC
  • mto: (325.1.2 bzrtools) (0.3.1 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060312010454-f03a25fc6d4fef16
Cope if there's bogus files in the shelf directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# based on Robert Collins code
2
 
 
3
 
"""Show all 'heads' in a repository"""
4
 
 
5
 
 
6
 
from bzrlib.commands import Command, display_command, register_command
7
 
from bzrlib.option import Option
8
 
 
9
 
 
10
 
class cmd_heads(Command):
11
 
    """Show all revisions in a repository not having descendants.
12
 
    """
13
 
    takes_options = [Option('by-date', help='Sort heads by date (descending)'),
14
 
                    ]
15
 
 
16
 
    encoding_type = "replace"
17
 
 
18
 
    @display_command
19
 
    def run(self, by_date=False):
20
 
        from bzrlib import errors
21
 
        from bzrlib.osutils import format_date
22
 
        import bzrlib.repository
23
 
 
24
 
        to_file = self.outf
25
 
 
26
 
        try:
27
 
            repo = bzrlib.branch.Branch.open_containing('.')[0].repository
28
 
        except errors.NotBranchError:
29
 
            try:
30
 
                repo = bzrlib.repository.Repository.open('.')
31
 
            except errors.NotBranchError:
32
 
                print >>to_file, \
33
 
                      ("You need to run this command "
34
 
                       "either from the root of a shared repository,\n"
35
 
                       "or from a branch.")
36
 
                return 3
37
 
 
38
 
        g = repo.get_revision_graph()
39
 
        possible_heads = set(g.keys())
40
 
        not_heads = set()
41
 
        for parents in g.values():
42
 
            not_heads.update(set(parents))
43
 
        
44
 
        heads = possible_heads.difference(not_heads)
45
 
 
46
 
        # TODO: use different sorting schemes instead of alphabetical sort
47
 
        heads = list(heads)
48
 
        heads.sort()
49
 
 
50
 
        if by_date:
51
 
            dates = {}
52
 
            for head in heads:
53
 
                rev = repo.get_revision(head)
54
 
                timestamp = rev.timestamp
55
 
                dates[timestamp] = head
56
 
            keys = dates.keys()
57
 
            keys.sort()
58
 
            keys.reverse()
59
 
            heads = []
60
 
            for k in keys:
61
 
                heads.append(dates[k])
62
 
        
63
 
        indent = ' '*2
64
 
        show_timezone = 'original'
65
 
        
66
 
        for head in heads:
67
 
            print >>to_file, "revision-id:", head
68
 
            rev = repo.get_revision(head)
69
 
            # borrowed from LongLogFormatter
70
 
            print >>to_file,  indent+'committer:', rev.committer
71
 
            try:
72
 
                print >>to_file, indent+'branch nick: %s' % \
73
 
                    rev.properties['branch-nick']
74
 
            except KeyError:
75
 
                pass
76
 
            date_str = format_date(rev.timestamp,
77
 
                                   rev.timezone or 0,
78
 
                                   show_timezone)
79
 
            print >>to_file,  indent+'timestamp: %s' % date_str
80
 
    
81
 
            print >>to_file,  indent+'message:'
82
 
            if not rev.message:
83
 
                print >>to_file,  indent+'  (no message)'
84
 
            else:
85
 
                message = rev.message.rstrip('\r\n')
86
 
                for l in message.split('\n'):
87
 
                    print >>to_file,  indent+'  ' + l
88
 
            print
89
 
 
90
 
        if not heads:
91
 
            print >>to_file, 'No heads found'
92
 
            return 1
93
 
#/class cmd_heads
94
 
 
95
 
 
96
 
register_command(cmd_heads)