diff hgext3rd/topic/revset.py @ 4322:41f38bf15b4c

topic: make revsets like 'foo#stack[0]' work 'stack' relation subscript function is another way to refer to s0, s1, etc. But instead of aborting in many cases it will simply return an empty set.
author Anton Shestakov <av6@dwimlabs.net>
date Sun, 16 Dec 2018 11:22:04 +0800
parents 09337aae08d4
children 482992803db6
line wrap: on
line diff
--- a/hgext3rd/topic/revset.py	Sat Dec 22 01:29:59 2018 -0500
+++ b/hgext3rd/topic/revset.py	Sun Dec 16 11:22:04 2018 +0800
@@ -106,3 +106,37 @@
     else:
         branch = repo[None].branch()
     return revset.baseset(stack.stack(repo, branch=branch, topic=topic)[1:]) & subset
+
+if util.safehasattr(revset, 'subscriptrelations'):
+    def stackrel(repo, subset, x, rel, n, order):
+        """This is a revset-flavored implementation of stack aliases.
+
+        The syntax is: rev#stack[n] or rev#s[n]. Plenty of logic is borrowed
+        from topic._namemap, but unlike that function, which prefers to abort
+        (e.g. when stack index is too high), this returns empty set to be more
+        revset-friendly.
+        """
+        if n < 0:
+            return revset.baseset()
+        s = revset.getset(repo, revset.fullreposet(repo), x)
+        if not s:
+            return revset.baseset()
+        revs = []
+        for r in s:
+            topic = repo[r].topic()
+            if topic:
+                st = stack.stack(repo, topic=topic)
+            else:
+                st = stack.stack(repo, branch=repo[r].branch())
+            try:
+                rev = list(st)[n]
+            except IndexError:
+                continue
+            if rev == -1 and n == 0:
+                continue
+            if rev not in revs:
+                revs.append(rev)
+        return subset & revset.baseset(revs)
+
+    revset.subscriptrelations['stack'] = stackrel
+    revset.subscriptrelations['s'] = stackrel