revset: add a topic() revset for querying topics
authorAugie Fackler <augie@google.com>
Wed, 10 Jun 2015 15:26:50 -0400
changeset 1843 0ba067a97d06
parent 1842 94bbc18daa99
child 1844 862cabc132fd
revset: add a topic() revset for querying topics
src/topic/__init__.py
src/topic/revset.py
tests/test-topic.t
--- a/src/topic/__init__.py	Wed Jun 10 15:01:36 2015 -0400
+++ b/src/topic/__init__.py	Wed Jun 10 15:26:50 2015 -0400
@@ -18,6 +18,8 @@
 from mercurial import phases
 from mercurial import util
 
+from . import revset as topicrevset
+
 cmdtable = {}
 command = cmdutil.command(cmdtable)
 
@@ -94,3 +96,4 @@
     return ret
 
 extensions.wrapcommand(commands.table, 'update', updatewrap)
+topicrevset.modsetup()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/topic/revset.py	Wed Jun 10 15:26:50 2015 -0400
@@ -0,0 +1,23 @@
+from mercurial import revset
+
+def topicset(repo, subset, x):
+    """`topic([topic])`
+    Specified topic or all changes with any topic specified.
+
+    If `topic` 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')
+    if args:
+        # match a specific topic
+        topic = revset.getstring(args[0], 'topic() argument must be a string')
+        _kind, _pattern, matcher = revset._stringmatcher(topic)
+    else:
+        matcher = lambda t: bool(t)
+    drafts = subset.filter(lambda r: repo[r].mutable())
+    return drafts.filter(lambda r: matcher(repo[r].extra().get('topic', '')))
+
+def modsetup():
+    revset.symbols.update({'topic': topicset})
--- a/tests/test-topic.t	Wed Jun 10 15:01:36 2015 -0400
+++ b/tests/test-topic.t	Wed Jun 10 15:26:50 2015 -0400
@@ -330,6 +330,89 @@
 
   $ hg topics --clear
 
+Topic revset
+  $ hg log -r 'topic()' -G
+  o  changeset:   9:0469d521db49
+  |  tag:         tip
+  |  topic:       fran
+  |  parent:      3:a53952faf762
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start on fran
+  |
+  $ hg log -r 'not topic()' -G
+  o    changeset:   8:ae074045b7a7
+  |\   parent:      7:54c943c1c167
+  | |  parent:      6:7c34953036d6
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Finish narf
+  | |
+  | o  changeset:   7:54c943c1c167
+  | |  parent:      5:c01515cfc331
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Narf is like `zort` or `poit`!
+  | |
+  o |  changeset:   6:7c34953036d6
+  | |  parent:      4:fb147b0b417c
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     narf!
+  | |
+  | @  changeset:   5:c01515cfc331
+  |/   user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     what is narf, pinky?
+  |
+  o  changeset:   4:fb147b0b417c
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start on narf
+  |
+  o  changeset:   3:a53952faf762
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Add file delta
+  |
+  o  changeset:   2:15d1eb11d2fa
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Add file gamma
+  |
+  o  changeset:   1:c692ea2c9224
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Add file beta
+  |
+  o  changeset:   0:c2b7d2f7d14b
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     Add file alpha
+  
+No matches because narf is already closed:
+  $ hg log -r 'topic(narf)' -G
+This regexp should match the topic `fran`:
+  $ hg log -r 'topic("re:.ra.")' -G
+  o  changeset:   9:0469d521db49
+  |  tag:         tip
+  |  topic:       fran
+  |  parent:      3:a53952faf762
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start on fran
+  |
+Exact match on fran:
+  $ hg log -r 'topic(fran)' -G
+  o  changeset:   9:0469d521db49
+  |  tag:         tip
+  |  topic:       fran
+  |  parent:      3:a53952faf762
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start on fran
+  |
+
 Move to fran, note that the topic activates, then deactivate the topic.
 
   $ hg co fran