mercurial/revset.py
changeset 14901 a773119f30ba
parent 14900 fc3d6f300d7d
child 15116 d8501bcbb221
equal deleted inserted replaced
14900:fc3d6f300d7d 14901:a773119f30ba
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 import re
     8 import re
     9 import parser, util, error, discovery, hbisect
     9 import parser, util, error, discovery, hbisect, node
    10 import bookmarks as bookmarksmod
    10 import bookmarks as bookmarksmod
    11 import match as matchmod
    11 import match as matchmod
    12 from i18n import _
    12 from i18n import _
    13 
    13 
    14 elements = {
    14 elements = {
  1024     weight, tree = optimize(tree, True)
  1024     weight, tree = optimize(tree, True)
  1025     def mfunc(repo, subset):
  1025     def mfunc(repo, subset):
  1026         return getset(repo, subset, tree)
  1026         return getset(repo, subset, tree)
  1027     return mfunc
  1027     return mfunc
  1028 
  1028 
       
  1029 def formatspec(expr, *args):
       
  1030     '''
       
  1031     This is a convenience function for using revsets internally, and
       
  1032     escapes arguments appropriately. Aliases are intentionally ignored
       
  1033     so that intended expression behavior isn't accidentally subverted.
       
  1034 
       
  1035     Supported arguments:
       
  1036 
       
  1037     %d = int(arg), no quoting
       
  1038     %s = string(arg), escaped and single-quoted
       
  1039     %b = arg.branch(), escaped and single-quoted
       
  1040     %n = hex(arg), single-quoted
       
  1041     %% = a literal '%'
       
  1042 
       
  1043     >>> formatspec('%d:: and not %d::', 10, 20)
       
  1044     '10:: and not 20::'
       
  1045     >>> formatspec('keyword(%s)', 'foo\\xe9')
       
  1046     "keyword('foo\\\\xe9')"
       
  1047     >>> b = lambda: 'default'
       
  1048     >>> b.branch = b
       
  1049     >>> formatspec('branch(%b)', b)
       
  1050     "branch('default')"
       
  1051     '''
       
  1052 
       
  1053     def quote(s):
       
  1054         return repr(str(s))
       
  1055 
       
  1056     ret = ''
       
  1057     pos = 0
       
  1058     arg = 0
       
  1059     while pos < len(expr):
       
  1060         c = expr[pos]
       
  1061         if c == '%':
       
  1062             pos += 1
       
  1063             d = expr[pos]
       
  1064             if d == '%':
       
  1065                 ret += d
       
  1066             elif d == 'd':
       
  1067                 ret += str(int(args[arg]))
       
  1068                 arg += 1
       
  1069             elif d == 's':
       
  1070                 ret += quote(args[arg])
       
  1071                 arg += 1
       
  1072             elif d == 'n':
       
  1073                 ret += quote(node.hex(args[arg]))
       
  1074                 arg += 1
       
  1075             elif d == 'b':
       
  1076                 ret += quote(args[arg].branch())
       
  1077                 arg += 1
       
  1078             else:
       
  1079                 raise util.Abort('unexpected revspec format character %s' % d)
       
  1080         else:
       
  1081             ret += c
       
  1082         pos += 1
       
  1083 
       
  1084     return ret
       
  1085 
  1029 # tell hggettext to extract docstrings from these functions:
  1086 # tell hggettext to extract docstrings from these functions:
  1030 i18nfunctions = symbols.values()
  1087 i18nfunctions = symbols.values()