[PATCH 2 of 6 V2] hgweb: add revsetsearch() function when query can be parsed as a revset

Alexander Plavin alexander at plav.in
Fri Aug 16 15:03:26 CDT 2013


# HG changeset patch
# User Alexander Plavin <alexander at plav.in>
# Date 1375823774 -14400
#      Wed Aug 07 01:16:14 2013 +0400
# Node ID d7684354b9a2755149fc8b9740d2770634d3185e
# Parent  3d00be09128534292148c37c9317c5b5da638262
hgweb: add revsetsearch() function when query can be parsed as a revset

This function is used when all the conditions are met:
- 'reverse(%s)' % query string can be parsed to a revset tree
- this tree has depth more than two, i.e. the query has some part of
revset syntax used
- the repo can be actually matched against this tree, i.e. it has only existent
function/operators and revisions/tags/bookmarks specified are correct

Otherwise keywordsearch() or revsearch() functions are used as before.

Add several new tests for different parsing conditions and exception handling.

diff -r 3d00be091285 -r d7684354b9a2 mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py	Fri Aug 09 22:52:58 2013 +0400
+++ b/mercurial/hgweb/webcommands.py	Wed Aug 07 01:16:14 2013 +0400
@@ -16,6 +16,8 @@
 from mercurial import help as helpmod
 from mercurial import scmutil
 from mercurial.i18n import _
+from mercurial.error import ParseError, RepoLookupError, Abort
+from mercurial import parser, revset
 
 # __all__ is populated with the allowed commands. Be sure to add to it if
 # you're adding a new command, or the new command won't work.
@@ -141,18 +143,49 @@
 
             yield ctx
 
+    def revsetsearch():
+        revs = revset.match(web.repo.ui, revdef)(web.repo, list(web.repo))
+        for r in revs:
+            yield web.repo[r]
+
     searchfuncs = {
         'rev': revsearch,
         'kw': keywordsearch,
+        'revset': revsetsearch,
     }
 
     def getsearchmode():
         try:
             web.repo[query]
         except (error.RepoError, error.LookupError):
+            # query is not an exact revision pointer, need to
+            # decide if it's a revset expession or keywords
+            pass
+        else:
+            return 'rev'
+
+        try:
+            p = parser.parser(revset.tokenize, revset.elements)
+            tree, pos = p.parse(revdef)
+        except ParseError:
+            # can't parse to a revset tree
+            return 'kw'
+
+        if revset.depth(tree) <= 2:
+            # no revset syntax used
+            return 'kw'
+
+        mfunc = revset.match(None, revdef)
+        try:
+            # try running against empty subset
+            mfunc(web.repo, [])
+            # ParseError: wrongly placed tokens, wrongs arguments, etc
+            # RepoLookupError: no such revision, e.g. in 'revision:'
+            # Abort: bookmark/tag not exists
+        except (ParseError, RepoLookupError, Abort):
             return 'kw'
         else:
-            return 'rev'
+            return 'revset'
 
     def changelist(**map):
         count = 0
@@ -197,6 +230,7 @@
     morevars['revcount'] = revcount * 2
     morevars['rev'] = query
 
+    revdef = 'reverse(%s)' % query
     searchfunc = searchfuncs[getsearchmode()]
 
     tip = web.repo['tip']
diff -r 3d00be091285 -r d7684354b9a2 tests/test-hgweb-commands.t
--- a/tests/test-hgweb-commands.t	Fri Aug 09 22:52:58 2013 +0400
+++ b/tests/test-hgweb-commands.t	Wed Aug 07 01:16:14 2013 +0400
@@ -537,6 +537,101 @@
   $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=stable&style=raw' | grep 'revision:'
   revision:    2
 
+Search with revset syntax
+
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=tip^&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "tip^"
+  
+  changeset:   1d22e65f027e5a0609357e7d8e7508cd2ba5d2fe
+  revision:    2
+  user:        test
+  date:        Thu, 01 Jan 1970 00:00:00 +0000
+  summary:     branch
+  branch:      stable
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=last(all(),2)^&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "last(all(),2)^"
+  
+  changeset:   1d22e65f027e5a0609357e7d8e7508cd2ba5d2fe
+  revision:    2
+  user:        test
+  date:        Thu, 01 Jan 1970 00:00:00 +0000
+  summary:     branch
+  branch:      stable
+  
+  changeset:   a4f92ed23982be056b9852de5dfe873eaac7f0de
+  revision:    1
+  user:        test
+  date:        Thu, 01 Jan 1970 00:00:00 +0000
+  summary:     Added tag 1.0 for changeset 2ef0ac749a14
+  branch:      default
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=last(all(,2)^&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "last(all(,2)^"
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=last(al(),2)^&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "last(al(),2)^"
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=bookmark(anotherthing)&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "bookmark(anotherthing)"
+  
+  changeset:   2ef0ac749a14e4f57a5a822464a0902c6f7f448f
+  revision:    0
+  user:        test
+  date:        Thu, 01 Jan 1970 00:00:00 +0000
+  summary:     base
+  tag:         1.0
+  bookmark:    anotherthing
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=bookmark(abc)&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "bookmark(abc)"
+  
+  
+  $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'log?rev=deadbeef:&style=raw'
+  200 Script output follows
+  
+  
+  # HG changesets search
+  # Node ID cad8025a2e87f88c06259790adfa15acb4080123
+  # Query "deadbeef:"
+  
+  
+
 File-related
 
   $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT 'file/1/foo/?style=raw'


More information about the Mercurial-devel mailing list