topic: handle ambiguous arguments to topic() revset
authorAnton Shestakov <av6@dwimlabs.net>
Thu, 30 Aug 2018 21:05:17 +0800
changeset 4061 ad4194399b47
parent 4060 54eade86ac31
child 4062 6774820c8a2e
topic: handle ambiguous arguments to topic() revset These arguments can be interpreted as either string or a revset. The decision is made based on existence of topic with such a name. This matches the behavior of branch() revset. The code needs to know all topics that ever existed in the repo, because some commands report "disappearance" of topics after certain operations, using this revset (e.g. via stack.stack or repo.revs).
CHANGELOG
hgext3rd/topic/revset.py
tests/test-topic.t
--- a/CHANGELOG	Thu Aug 30 20:21:17 2018 +0800
+++ b/CHANGELOG	Thu Aug 30 21:05:17 2018 +0800
@@ -8,6 +8,11 @@
   * pick: rename the "grab" command to "pick" to avoid ambiguity with graft
   * discovery: enable obshashrange based discovery by default
 
+topic
+
+  * revset: `topic("patterns")` now handle standard patterns ("re:", etc)
+  * revset: `topic(REVS)` matches revisions with same topic as REVS
+
 8.1.2 -- 2018-08-28
 -------------------
 
--- a/hgext3rd/topic/revset.py	Thu Aug 30 20:21:17 2018 +0800
+++ b/hgext3rd/topic/revset.py	Thu Aug 30 21:05:17 2018 +0800
@@ -30,8 +30,6 @@
 
     If `string` starts with `re:` the remainder of the name is treated
     as a regular expression.
-
-    TODO: make `topic(revset)` work the same as `branch(revset)`.
     """
     args = revset.getargs(x, 0, 1, 'topic takes one or no arguments')
 
@@ -46,9 +44,7 @@
         # not a string, but another revset
         pass
     else:
-        if topic == '.':
-            topic = repo['.'].extra().get('topic', '')
-        _kind, _pattern, matcher = mkmatcher(topic)
+        kind, pattern, matcher = mkmatcher(topic)
 
         def matches(r):
             topic = repo[r].topic()
@@ -56,7 +52,22 @@
                 return False
             return matcher(topic)
 
-        if True:
+        if kind == 'literal':
+            # note: falls through to the revset case if no topic with this name
+            # exists and pattern kind is not specified explicitly
+
+            alltopics = set([repo.currenttopic])
+            for r in repo.unfiltered().set('all()'):
+                alltopics.add(r.topic(force=True))
+            alltopics.discard('')
+
+            if pattern in alltopics:
+                return (subset & mutable).filter(matches)
+
+            if topic.startswith('literal:'):
+                raise error.RepoLookupError("topic '%s' does not exist"
+                                            % pattern)
+        else:
             return (subset & mutable).filter(matches)
 
     s = revset.getset(repo, revset.fullreposet(repo), x)
--- a/tests/test-topic.t	Thu Aug 30 20:21:17 2018 +0800
+++ b/tests/test-topic.t	Thu Aug 30 21:05:17 2018 +0800
@@ -741,6 +741,9 @@
   >   hg log -T '{rev}: {topic}\n' -r "$1"
   > }
 
+  $ tlog 'topic(9)'
+  9: fran
+  $ tlog 'topic(8)'
   $ tlog 'topic(head())'
   9: fran
   $ tlog 'topic(:)'
@@ -751,6 +754,15 @@
   9: fran
   $ tlog 'topic(wdir())'
   9: fran
+  $ tlog 'topic(nonsense)'
+  abort: unknown revision 'nonsense'!
+  [255]
+
+Pattern matching in topic() revset
+  $ tlog 'topic("re:nonsense")'
+  $ tlog 'topic("literal:nonsense")'
+  abort: topic 'nonsense' does not exist!
+  [255]
 
 Deactivate the topic.
   $ hg topics