~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-01-11 05:54:26 UTC
  • mfrom: (5582.2.2 tags-sort-debversion)
  • Revision ID: pqm@pqm.ubuntu.com-20110111055426-pt4g3igwip5epm7q
(jelmer) Support adding new sort mechanisms to 'bzr tags'. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.lazy_import import lazy_import
22
22
lazy_import(globals(), """
23
23
import cStringIO
24
 
import itertools
25
 
import re
26
24
import sys
27
25
import time
28
26
 
5482
5480
    takes_options = [
5483
5481
        custom_help('directory',
5484
5482
            help='Branch whose tags should be displayed.'),
5485
 
        RegistryOption.from_kwargs('sort',
 
5483
        RegistryOption('sort',
5486
5484
            'Sort tags by different criteria.', title='Sorting',
5487
 
            natural='Sort numeric substrings as numbers:'
5488
 
                    ' suitable for version numbers. (default)',
5489
 
            alpha='Sort tags lexicographically.',
5490
 
            time='Sort tags chronologically.',
 
5485
            lazy_registry=('bzrlib.tag', 'tag_sort_methods')
5491
5486
            ),
5492
5487
        'show-ids',
5493
5488
        'revision',
5494
5489
    ]
5495
5490
 
5496
5491
    @display_command
5497
 
    def run(self,
5498
 
            directory='.',
5499
 
            sort='natural',
5500
 
            show_ids=False,
5501
 
            revision=None,
5502
 
            ):
 
5492
    def run(self, directory='.', sort=None, show_ids=False, revision=None):
 
5493
        from bzrlib.tag import tag_sort_methods
5503
5494
        branch, relpath = Branch.open_containing(directory)
5504
5495
 
5505
5496
        tags = branch.tags.get_tag_dict().items()
5514
5505
            # only show revisions between revid1 and revid2 (inclusive)
5515
5506
            tags = [(tag, revid) for tag, revid in tags if
5516
5507
                graph.is_between(revid, revid1, revid2)]
5517
 
        if sort == 'natural':
5518
 
            def natural_sort_key(tag):
5519
 
                return [f(s) for f,s in 
5520
 
                        zip(itertools.cycle((unicode.lower,int)),
5521
 
                                            re.split('([0-9]+)', tag[0]))]
5522
 
            tags.sort(key=natural_sort_key)
5523
 
        elif sort == 'alpha':
5524
 
            tags.sort()
5525
 
        elif sort == 'time':
5526
 
            timestamps = {}
5527
 
            for tag, revid in tags:
5528
 
                try:
5529
 
                    revobj = branch.repository.get_revision(revid)
5530
 
                except errors.NoSuchRevision:
5531
 
                    timestamp = sys.maxint # place them at the end
5532
 
                else:
5533
 
                    timestamp = revobj.timestamp
5534
 
                timestamps[revid] = timestamp
5535
 
            tags.sort(key=lambda x: timestamps[x[1]])
 
5508
        if sort is None:
 
5509
            sort = tag_sort_methods.get()
 
5510
        sort(branch, tags)
5536
5511
        if not show_ids:
5537
5512
            # [ (tag, revid), ... ] -> [ (tag, dotted_revno), ... ]
5538
5513
            for index, (tag, revid) in enumerate(tags):