hgext3rd/topic/revset.py
branchmercurial-4.8
changeset 4356 a71f2271ed76
parent 4339 0f015fe4f71f
child 4369 75276f858444
--- a/hgext3rd/topic/revset.py	Tue Jan 22 10:43:44 2019 -0500
+++ b/hgext3rd/topic/revset.py	Tue Jan 22 10:46:02 2019 -0500
@@ -106,3 +106,52 @@
     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.
+        """
+        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())
+            if n < 0:
+                st = list(st)[1:]
+            else:
+                st = list(st)
+            try:
+                rev = 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
+
+    def topicrel(repo, subset, x, rel, n, order):
+        ancestors = revset._ancestors
+        descendants = revset._descendants
+        subset = topicset(repo, subset, x)
+        if n <= 0:
+            n = -n
+            return ancestors(repo, subset, x, startdepth=n, stopdepth=n + 1)
+        else:
+            return descendants(repo, subset, x, startdepth=n, stopdepth=n + 1)
+
+    revset.subscriptrelations['topic'] = topicrel
+    revset.subscriptrelations['t'] = topicrel