comparison mercurial/revset.py @ 15140:353a1ba928f6

revset: add 'l' flag to formatspec for args This makes it easy to calculate a revset with lists: good = [1, 2, 3] bad = [10, 11, 12] between = repo.set('%ld::%ld', good, bad)
author Matt Mackall <mpm@selenic.com>
date Mon, 19 Sep 2011 16:28:44 -0500
parents 883d28233a4d
children b39d85be78a8
comparison
equal deleted inserted replaced
15139:0834e0bb445a 15140:353a1ba928f6
1057 %s = string(arg), escaped and single-quoted 1057 %s = string(arg), escaped and single-quoted
1058 %b = arg.branch(), escaped and single-quoted 1058 %b = arg.branch(), escaped and single-quoted
1059 %n = hex(arg), single-quoted 1059 %n = hex(arg), single-quoted
1060 %% = a literal '%' 1060 %% = a literal '%'
1061 1061
1062 Prefixing the type with 'l' specifies a list of that type.
1063
1062 >>> formatspec('%d:: and not %d::', 10, 20) 1064 >>> formatspec('%d:: and not %d::', 10, 20)
1063 '10:: and not 20::' 1065 '10:: and not 20::'
1064 >>> formatspec('keyword(%s)', 'foo\\xe9') 1066 >>> formatspec('keyword(%s)', 'foo\\xe9')
1065 "keyword('foo\\\\xe9')" 1067 "keyword('foo\\\\xe9')"
1066 >>> b = lambda: 'default' 1068 >>> b = lambda: 'default'
1067 >>> b.branch = b 1069 >>> b.branch = b
1068 >>> formatspec('branch(%b)', b) 1070 >>> formatspec('branch(%b)', b)
1069 "branch('default')" 1071 "branch('default')"
1072 >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
1073 "root(('a' or 'b' or 'c' or 'd'))"
1070 ''' 1074 '''
1071 1075
1072 def quote(s): 1076 def quote(s):
1073 return repr(str(s)) 1077 return repr(str(s))
1078
1079 def argtype(c, arg):
1080 if c == 'd':
1081 return str(int(arg))
1082 elif c == 's':
1083 return quote(arg)
1084 elif c == 'n':
1085 return quote(node.hex(arg))
1086 elif c == 'b':
1087 return quote(arg.branch())
1074 1088
1075 ret = '' 1089 ret = ''
1076 pos = 0 1090 pos = 0
1077 arg = 0 1091 arg = 0
1078 while pos < len(expr): 1092 while pos < len(expr):
1080 if c == '%': 1094 if c == '%':
1081 pos += 1 1095 pos += 1
1082 d = expr[pos] 1096 d = expr[pos]
1083 if d == '%': 1097 if d == '%':
1084 ret += d 1098 ret += d
1085 elif d == 'd': 1099 elif d in 'dsnb':
1086 ret += str(int(args[arg])) 1100 ret += argtype(d, args[arg])
1087 arg += 1 1101 arg += 1
1088 elif d == 's': 1102 elif d == 'l':
1089 ret += quote(args[arg]) 1103 # a list of some type
1090 arg += 1 1104 pos += 1
1091 elif d == 'n': 1105 d = expr[pos]
1092 ret += quote(node.hex(args[arg])) 1106 lv = ' or '.join(argtype(d, e) for e in args[arg])
1093 arg += 1 1107 ret += '(%s)' % lv
1094 elif d == 'b':
1095 ret += quote(args[arg].branch())
1096 arg += 1 1108 arg += 1
1097 else: 1109 else:
1098 raise util.Abort('unexpected revspec format character %s' % d) 1110 raise util.Abort('unexpected revspec format character %s' % d)
1099 else: 1111 else:
1100 ret += c 1112 ret += c