Rev 5586: (jelmer) Support adding new sort mechanisms to 'bzr tags'. (Jelmer Vernooij) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Tue Jan 11 05:54:26 UTC 2011
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 5586 [merge]
revision-id: pqm at pqm.ubuntu.com-20110111055426-pt4g3igwip5epm7q
parent: pqm at pqm.ubuntu.com-20110111052144-eg3yyt9xcj2jwv93
parent: jelmer at samba.org-20110110224153-4ufrhg5ycatrwapz
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2011-01-11 05:54:26 +0000
message:
(jelmer) Support adding new sort mechanisms to 'bzr tags'. (Jelmer Vernooij)
modified:
bzrlib/builtins.py builtins.py-20050830033751-fc01482b9ca23183
bzrlib/tag.py tag.py-20070212110532-91cw79inah2cfozx-1
bzrlib/tests/blackbox/test_tags.py test_tags.py-20070116132048-5h4qak2cm22jlb9e-1
doc/en/release-notes/bzr-2.3.txt NEWS-20050323055033-4e00b5db738777ff
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py 2010-12-20 12:01:56 +0000
+++ b/bzrlib/builtins.py 2011-01-10 21:49:51 +0000
@@ -21,8 +21,6 @@
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import cStringIO
-import itertools
-import re
import sys
import time
@@ -5482,24 +5480,17 @@
takes_options = [
custom_help('directory',
help='Branch whose tags should be displayed.'),
- RegistryOption.from_kwargs('sort',
+ RegistryOption('sort',
'Sort tags by different criteria.', title='Sorting',
- natural='Sort numeric substrings as numbers:'
- ' suitable for version numbers. (default)',
- alpha='Sort tags lexicographically.',
- time='Sort tags chronologically.',
+ lazy_registry=('bzrlib.tag', 'tag_sort_methods')
),
'show-ids',
'revision',
]
@display_command
- def run(self,
- directory='.',
- sort='natural',
- show_ids=False,
- revision=None,
- ):
+ def run(self, directory='.', sort=None, show_ids=False, revision=None):
+ from bzrlib.tag import tag_sort_methods
branch, relpath = Branch.open_containing(directory)
tags = branch.tags.get_tag_dict().items()
@@ -5514,25 +5505,9 @@
# only show revisions between revid1 and revid2 (inclusive)
tags = [(tag, revid) for tag, revid in tags if
graph.is_between(revid, revid1, revid2)]
- if sort == 'natural':
- def natural_sort_key(tag):
- return [f(s) for f,s in
- zip(itertools.cycle((unicode.lower,int)),
- re.split('([0-9]+)', tag[0]))]
- tags.sort(key=natural_sort_key)
- elif sort == 'alpha':
- tags.sort()
- elif sort == 'time':
- timestamps = {}
- for tag, revid in tags:
- try:
- revobj = branch.repository.get_revision(revid)
- except errors.NoSuchRevision:
- timestamp = sys.maxint # place them at the end
- else:
- timestamp = revobj.timestamp
- timestamps[revid] = timestamp
- tags.sort(key=lambda x: timestamps[x[1]])
+ if sort is None:
+ sort = tag_sort_methods.get()
+ sort(branch, tags)
if not show_ids:
# [ (tag, revid), ... ] -> [ (tag, dotted_revno), ... ]
for index, (tag, revid) in enumerate(tags):
=== modified file 'bzrlib/tag.py'
--- a/bzrlib/tag.py 2010-11-18 00:30:51 +0000
+++ b/bzrlib/tag.py 2011-01-10 21:49:51 +0000
@@ -25,6 +25,12 @@
# NOTE: I was going to call this tags.py, but vim seems to think all files
# called tags* are ctags files... mbp 20070220.
+from bzrlib.registry import Registry
+from bzrlib.lazy_import import lazy_import
+lazy_import(globals(), """
+import itertools
+import re
+import sys
from bzrlib import (
bencode,
@@ -33,6 +39,7 @@
symbol_versioning,
trace,
)
+""")
class _Tags(object):
@@ -307,3 +314,50 @@
DeprecationWarning)
from_branch.tags.merge_to(to_branch.tags)
+
+def sort_natural(branch, tags):
+ """Sort tags, with numeric substrings as numbers.
+
+ :param branch: Branch
+ :param tags: List of tuples with tag name and revision id.
+ """
+ def natural_sort_key(tag):
+ return [f(s) for f,s in
+ zip(itertools.cycle((unicode.lower,int)),
+ re.split('([0-9]+)', tag[0]))]
+ tags.sort(key=natural_sort_key)
+
+
+def sort_alpha(branch, tags):
+ """Sort tags lexicographically, in place.
+
+ :param branch: Branch
+ :param tags: List of tuples with tag name and revision id.
+ """
+ tags.sort()
+
+
+def sort_time(branch, tags):
+ """Sort tags by time inline.
+
+ :param branch: Branch
+ :param tags: List of tuples with tag name and revision id.
+ """
+ timestamps = {}
+ for tag, revid in tags:
+ try:
+ revobj = branch.repository.get_revision(revid)
+ except errors.NoSuchRevision:
+ timestamp = sys.maxint # place them at the end
+ else:
+ timestamp = revobj.timestamp
+ timestamps[revid] = timestamp
+ tags.sort(key=lambda x: timestamps[x[1]])
+
+
+tag_sort_methods = Registry()
+tag_sort_methods.register("natural", sort_natural,
+ 'Sort numeric substrings as numbers. (default)')
+tag_sort_methods.register("alpha", sort_alpha, 'Sort tags lexicographically.')
+tag_sort_methods.register("time", sort_time, 'Sort tags chronologically.')
+tag_sort_methods.default_key = "natural"
=== modified file 'bzrlib/tests/blackbox/test_tags.py'
--- a/bzrlib/tests/blackbox/test_tags.py 2010-11-18 00:22:24 +0000
+++ b/bzrlib/tests/blackbox/test_tags.py 2011-01-10 21:49:51 +0000
@@ -278,6 +278,40 @@
error_regexes=["bzr: ERROR: Requested revision: '123.123' "
"does not exist in branch:"])
+ def test_sort_tags_custom(self):
+ def sort_by_dots(branch, tags):
+ def sort_key((tag, revid)):
+ return tag.count(".")
+ tags.sort(key=sort_key)
+
+ # Register a custom sort method
+ tag.tag_sort_methods.register("dots", sort_by_dots, "Sort by dots.")
+ self.addCleanup(tag.tag_sort_methods.remove, "dots")
+
+ tree1 = self.make_branch_and_tree('branch1')
+ tree1.commit(allow_pointless=True, message='revision 1',
+ rev_id='revid-1', timestamp=10)
+ tree1.commit(allow_pointless=True, message='revision 2',
+ rev_id='revid-2', timestamp=15)
+
+ b1 = tree1.branch
+
+ b1.tags.set_tag(u'tag..', 'revid-2')
+ b1.tags.set_tag(u'tag....', 'missing') # not present in repository
+ b1.tags.set_tag(u'tag.', 'revid-1')
+ b1.tags.set_tag(u'tag...', 'revid-1')
+ b1.tags.set_tag(u'tag....', 'revid-1')
+
+ # sorted by number of dots
+ out, err = self.run_bzr('tags --sort=dots -d branch1')
+ self.assertEquals(err, '')
+ self.assertEquals([
+ 'tag. 1',
+ 'tag.. 2',
+ 'tag... 1',
+ 'tag.... 1'],
+ out.splitlines())
+
def _check_tag_filter(self, argstr, expected_revnos):
#upper bound of laziness
out, err = self.run_bzr('tags ' + argstr)
=== modified file 'doc/en/release-notes/bzr-2.3.txt'
--- a/doc/en/release-notes/bzr-2.3.txt 2011-01-11 04:46:01 +0000
+++ b/doc/en/release-notes/bzr-2.3.txt 2011-01-11 05:54:26 +0000
@@ -40,6 +40,10 @@
designated by the action. This will *ignore* all differences that would
have been merge cleanly otherwise. (Vincent Ladeuil, #638451)
+* ``bzr tags``'s "sort" argument now allows registering custom sort
+ methods using the ``bzrlib.tag.tag_sort_methods`` registry.
+ (Jelmer Vernooij, #701244)
+
* ``bt.test_http`` was breaking ``os.environ`` by erasing the values saved by
``TestCase`` leading to ``bt.test_import_tariff`` failures.
(Vincent Ladeuil, #690563)
More information about the bazaar-commits
mailing list