Rev 2: Handle unicode parameters, and 'foo--bar' to match the start and end. in http://bzr.arbash-meinel.com/plugins/short_revid_spec

John Arbash Meinel john at arbash-meinel.com
Wed Apr 11 03:21:02 BST 2007


At http://bzr.arbash-meinel.com/plugins/short_revid_spec

------------------------------------------------------------
revno: 2
revision-id: john at arbash-meinel.com-20070411022059-tyw1hn9d3mlbvjav
parent: john at arbash-meinel.com-20070411014644-ghyfdebn2stler1h
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: short_revid_spec
timestamp: Tue 2007-04-10 21:20:59 -0500
message:
  Handle unicode parameters, and 'foo--bar' to match the start and end.
modified:
  srevid_spec.py                 srevid_spec.py-20070411002806-hc1qgc5ne5r41ral-2
  test_short_revid_spec.py       test_short_revid_spe-20070411002806-hc1qgc5ne5r41ral-3
-------------- next part --------------
=== modified file 'srevid_spec.py'
--- a/srevid_spec.py	2007-04-11 01:46:44 +0000
+++ b/srevid_spec.py	2007-04-11 02:20:59 +0000
@@ -32,6 +32,7 @@
 
     examples:
         srevid:abe7e7   -> Select revision id 'john at foo.com-2007-on2efb7e7'
+        srevid:joe-7e7  -> Select revision id 'joe at foo.com-2008-auoeeou7e7'
     """
 
     prefix = 'srevid:'
@@ -43,20 +44,41 @@
         # want to match against anything but strings.
         first = all_revision_ids.pop(0)
         assert first is None, 'first was not None: %s' % (first,)
-        matches = self._match_against_all(all_revision_ids)
+        prefix, suffix = self._spec_to_prefix_suffix(branch)
+        matches = self._match_against_all(prefix, suffix, all_revision_ids)
         return self._matches_to_revision_info(branch, matches)
 
-    def _match_against_all(self, revision_ids):
+    def _spec_to_prefix_suffix(self, branch):
+        """Convert self.spec into a prefix and suffix.
+
+        :param branch: We need this for error handling.
+        """
+        if isinstance(self.spec, unicode):
+            spec = self.spec.encode('utf8')
+        else:
+            spec = self.spec
+        pieces = spec.split('--')
+        if len(pieces) == 1:
+            return None, pieces[0]
+        elif len(pieces) == 2:
+            return pieces[0], pieces[1]
+        raise errors.InvalidRevisionSpec(self.user_spec, branch=branch,
+                                         extra='too many dashes')
+
+    def _match_against_all(self, prefix, suffix, revision_ids):
         """Match self.spec against a list of revisions.
 
+        :param prefix: A matching revision id should match this prefix (may be
+            None to not require a prefix.)
+        :param suffix: A matching revision id must match this suffix (should
+            not be None)
         :param revision_ids: A list of revisions to check against.
-        :param branch: Used when raising an exception because of a failure to
-            match. (Or too many matches)
         """
         matches = []
         for revision_id in revision_ids:
-            if revision_id.endswith(self.spec):
-                matches.append(revision_id)
+            if revision_id.endswith(suffix):
+                if prefix is None or revision_id.startswith(prefix):
+                    matches.append(revision_id)
         return matches
 
     def _matches_to_revision_info(self, branch, revision_ids):

=== modified file 'test_short_revid_spec.py'
--- a/test_short_revid_spec.py	2007-04-11 01:46:44 +0000
+++ b/test_short_revid_spec.py	2007-04-11 02:20:59 +0000
@@ -40,18 +40,20 @@
     def test_simple_ending(self):
         revision_ids = ['test-one', 'test-two']
         spec = revisionspec.RevisionSpec.from_string('srevid:two')
-        self.assertEqual(['test-two'], spec._match_against_all(revision_ids))
+        self.assertEqual(['test-two'],
+                         spec._match_against_all(None, 'two', revision_ids))
 
     def test_multiple_matches(self):
         """If a spec matches more than one revision, it Error"""
         revision_ids = ['test-one', 'test-two', 'test-three']
         spec = revisionspec.RevisionSpec.from_string('srevid:e')
         self.assertEqual(['test-one', 'test-three'],
-                         spec._match_against_all(revision_ids))
+                         spec._match_against_all(None, 'e', revision_ids))
     def test_no_matches(self):
         revision_ids = ['test-one', 'test-two', 'test-three']
         spec = revisionspec.RevisionSpec.from_string('srevid:four')
-        self.assertEqual([], spec._match_against_all(revision_ids))
+        self.assertEqual([],
+                         spec._match_against_all(None, 'four', revision_ids))
 
     def test_matches_to_info_none(self):
         spec = revisionspec.RevisionSpec.from_string('srevid:four')
@@ -72,6 +74,40 @@
                          e.extra)
         self.assertEqual('branch', e.branch)
 
+    def test_matches_with_dash(self):
+        """If there is a '--' we match the start and end of revision ids."""
+        spec = revisionspec.RevisionSpec.from_string('srevid:a--e')
+        revision_ids = ['a-one', 'a-two', 'b-three']
+        self.assertEqual(['a-one'],
+                         spec._match_against_all('a', 'e', revision_ids))
+
+    def test_fail_with_multiple_dashes(self):
+        """If there are multiple '--' it is an error."""
+        spec = revisionspec.RevisionSpec.from_string('srevid:a--b--e')
+        e = self.assertRaises(errors.InvalidRevisionSpec,
+                              spec._spec_to_prefix_suffix, 'branch')
+        self.assertEqual('srevid:a--b--e', e.spec)
+        self.assertEqual('\ntoo many dashes', e.extra)
+        self.assertEqual('branch', e.branch)
+
+    def test_unicode_spec(self):
+        """self.spec is Unicode from command line, make sure we handle it."""
+        revision_ids = [u'\xe5-2006-id1'.encode('utf8'),
+                        u'\xb5-2007-id2'.encode('utf8'),
+                        u'\xe5-2007-id2'.encode('utf8'),
+                        ]
+        spec = revisionspec.RevisionSpec.from_string(u'srevid:\xb5--id2')
+        prefix, suffix = spec._spec_to_prefix_suffix('branch')
+        # Because revision_ids are utf8 the prefix and suffix should be
+        self.assertEqual('\xc2\xb5', prefix)
+        self.assertEqual('id2', suffix)
+        self.assertIsInstance(prefix, str)
+        self.assertIsInstance(suffix, str)
+        self.assertEqual([revision_ids[1]],
+                         spec._match_against_all(prefix, suffix, revision_ids))
+
+    #TODO: Handle unicode parameters
+
 
 class TestSRevidWithBranch(tests.TestCaseWithTransport):
     """Tests of srevid: with a real branch."""
@@ -116,3 +152,11 @@
         self.assertEqual(self.revision_ids[2], info.rev_id)
         self.assertEqual(branch, info.branch)
         self.assertEqual(2, info.revno)
+
+    def test_matches_prefix(self):
+        branch = self.make_basic_branch()
+        spec = revisionspec.RevisionSpec.from_string('srevid:barry--rt')
+        info = spec.in_history(branch)
+        self.assertEqual(self.revision_ids[5], info.rev_id)
+        self.assertEqual(branch, info.branch)
+        self.assertEqual(5, info.revno)



More information about the bazaar-commits mailing list