stack: allow to refer to changeset using "t2" form
authorPierre-Yves David <pierre-yves.david@fb.com>
Mon, 14 Mar 2016 18:39:19 +0000
changeset 1904 f52c02bf47b7
parent 1903 58cdf061d49a
child 1905 3b42478ef017
stack: allow to refer to changeset using "t2" form hg up "t0" is seen as "update to the first changeset of my current topic". Eventually we'll drop the "t2" form in favor of the planned generic indexing operator '.{t2}'.
hgext3rd/topic/__init__.py
hgext3rd/topic/discovery.py
hgext3rd/topic/stack.py
tests/test-topic-stack.t
--- a/hgext3rd/topic/__init__.py	Tue Mar 15 17:26:57 2016 +0000
+++ b/hgext3rd/topic/__init__.py	Mon Mar 14 18:39:19 2016 +0000
@@ -12,6 +12,7 @@
 """
 import functools
 import contextlib
+import re
 
 from mercurial.i18n import _
 from mercurial import branchmap
@@ -51,7 +52,22 @@
     return self.extra().get(constants.extrakey, '')
 context.basectx.topic = _contexttopic
 
+topicrev = re.compile(r'^t\d+$')
+
+
 def _namemap(repo, name):
+    if topicrev.match(name):
+        idx = int(name[1:])
+        topic = repo.currenttopic
+        if not topic:
+            raise error.Abort(_('cannot resolve "%s": no active topic') % name)
+        revs = list(stack.getstack(repo, topic))
+        try:
+            r = revs[idx]
+        except IndexError:
+            msg = _('cannot resolve "%s": topic "%s" has only %d changesets')
+            raise error.Abort(msg % (name, topic, len(revs)))
+        return [repo[r].node()]
     return [ctx.node() for ctx in
             repo.set('not public() and extra(topic, %s)', name)]
 
--- a/hgext3rd/topic/discovery.py	Tue Mar 15 17:26:57 2016 +0000
+++ b/hgext3rd/topic/discovery.py	Mon Mar 14 18:39:19 2016 +0000
@@ -107,5 +107,3 @@
     if repo.peer().capable('topics'):
         caps.append('topics')
     return caps
-
-
--- a/hgext3rd/topic/stack.py	Tue Mar 15 17:26:57 2016 +0000
+++ b/hgext3rd/topic/stack.py	Mon Mar 14 18:39:19 2016 +0000
@@ -8,7 +8,7 @@
 from mercurial import extensions
 from mercurial import obsolete
 
-def _getstack(repo, topic):
+def getstack(repo, topic):
     # XXX need sorting
     trevs = repo.revs("topic(%s) - obsolete()", topic)
     return _orderrevs(repo, trevs)
@@ -18,7 +18,7 @@
         topic = repo.currenttopic
     if not topic:
         raise error.Abort(_('no active topic to list'))
-    for idx, r in enumerate(_getstack(repo, topic)):
+    for idx, r in enumerate(getstack(repo, topic)):
         # super crude initial version
         l = "%d: %s\n" % (idx, repo[r].description().splitlines()[0])
         ui.write(l)
--- a/tests/test-topic-stack.t	Tue Mar 15 17:26:57 2016 +0000
+++ b/tests/test-topic-stack.t	Mon Mar 14 18:39:19 2016 +0000
@@ -69,12 +69,23 @@
   abort: no active topic to list
   [255]
 
+Test "t#" reference
+-------------------
+
+
+  $ hg up t1
+  abort: cannot resolve "t1": no active topic
+  [255]
+  $ hg topic foo
+  $ hg up t42
+  abort: cannot resolve "t42": topic "foo" has only 4 changesets
+  [255]
+  $ hg up t1
+  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+
 Case with some of the topic unstable
 ------------------------------------
 
-  $ hg up 'desc(c_d)'
-  switching to topic foo
-  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
   $ echo bbb > ddd
   $ hg commit --amend
   $ hg log -G