##// END OF EJS Templates
revset: add formatspec convenience query builder
Matt Mackall -
r14901:a773119f default
parent child Browse files
Show More
@@ -6,7 +6,7 b''
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 _
@@ -1026,5 +1026,62 b' def match(ui, spec):'
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()
@@ -33,3 +33,6 b' doctest.testmod(mercurial.hgweb.hgwebdir'
33
33
34 import hgext.convert.cvsps
34 import hgext.convert.cvsps
35 doctest.testmod(hgext.convert.cvsps)
35 doctest.testmod(hgext.convert.cvsps)
36
37 import mercurial.revset
38 doctest.testmod(mercurial.revset)
General Comments 0
You need to be logged in to leave comments. Login now