diff hgext3rd/topic/revset.py @ 4061:ad4194399b47

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).
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 30 Aug 2018 21:05:17 +0800
parents 54eade86ac31
children 00c65abf99cd
line wrap: on
line diff
--- 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)