diff mercurial/hgweb/webcommands.py @ 19722:bf15935b68a3

hgweb: add revset syntax support to search This mode 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 - no revset regexes are used in the query (strings which start with 're:') - only functions explicitly marked as safe in revset.py are used in the query Add several new tests for different parsing conditions and exception handling.
author Alexander Plavin <alexander@plav.in>
date Fri, 06 Sep 2013 13:30:56 +0400
parents 145636d31bb4
children 6907251122e3
line wrap: on
line diff
--- a/mercurial/hgweb/webcommands.py	Fri Sep 06 13:30:56 2013 +0400
+++ b/mercurial/hgweb/webcommands.py	Fri Sep 06 13:30:56 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 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.
@@ -111,6 +113,7 @@
 def _search(web, req, tmpl):
     MODE_REVISION = 'rev'
     MODE_KEYWORD = 'keyword'
+    MODE_REVSET = 'revset'
 
     def revsearch(ctx):
         yield ctx
@@ -143,19 +146,56 @@
 
             yield ctx
 
+    def revsetsearch(revs):
+        for r in revs:
+            yield web.repo[r]
+
     searchfuncs = {
         MODE_REVISION: revsearch,
         MODE_KEYWORD: keywordsearch,
+        MODE_REVSET: revsetsearch,
     }
 
     def getsearchmode(query):
         try:
             ctx = web.repo[query]
         except (error.RepoError, error.LookupError):
-            return MODE_KEYWORD, query
+            # query is not an exact revision pointer, need to
+            # decide if it's a revset expession or keywords
+            pass
         else:
             return MODE_REVISION, ctx
 
+        revdef = 'reverse(%s)' % query
+        try:
+            tree, pos = revset.parse(revdef)
+        except ParseError:
+            # can't parse to a revset tree
+            return MODE_KEYWORD, query
+
+        if revset.depth(tree) <= 2:
+            # no revset syntax used
+            return MODE_KEYWORD, query
+
+        if util.any((token, (value or '')[:3]) == ('string', 're:')
+                    for token, value, pos in revset.tokenize(revdef)):
+            return MODE_KEYWORD, query
+
+        funcsused = revset.funcsused(tree)
+        if not funcsused.issubset(revset.safesymbols):
+            return MODE_KEYWORD, query
+
+        mfunc = revset.match(web.repo.ui, revdef)
+        try:
+            revs = mfunc(web.repo, list(web.repo))
+            return MODE_REVSET, revs
+            # ParseError: wrongly placed tokens, wrongs arguments, etc
+            # RepoLookupError: no such revision, e.g. in 'revision:'
+            # Abort: bookmark/tag not exists
+            # LookupError: ambiguous identifier, e.g. in '(bc)' on a large repo
+        except (ParseError, RepoLookupError, Abort, LookupError):
+            return MODE_KEYWORD, query
+
     def changelist(**map):
         count = 0