hgext3rd/topic/revset.py
changeset 4322 41f38bf15b4c
parent 4290 09337aae08d4
child 4323 482992803db6
--- 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