comparison mercurial/templatekw.py @ 10055:e400a511e63a

cmdutil: extract repo dependent closures in templatekw
author Patrick Mezard <pmezard@gmail.com>
date Sun, 13 Dec 2009 18:06:23 +0100
parents 1a85861f59af
children 1a114aca93fa
comparison
equal deleted inserted replaced
10054:1a85861f59af 10055:e400a511e63a
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> 3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com>
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, incorporated herein by reference. 6 # GNU General Public License version 2, incorporated herein by reference.
7 7
8 import encoding 8 from node import hex
9 import encoding, patch, util
9 10
10 def showlist(templ, name, values, plural=None, **args): 11 def showlist(templ, name, values, plural=None, **args):
11 '''expand set of values. 12 '''expand set of values.
12 name is name of key in template map. 13 name is name of key in template map.
13 values is list of strings or dicts. 14 values is list of strings or dicts.
66 yield one(last, tag=lastname) 67 yield one(last, tag=lastname)
67 endname = 'end_' + names 68 endname = 'end_' + names
68 if endname in templ: 69 if endname in templ:
69 yield templ(endname, **args) 70 yield templ(endname, **args)
70 71
71 def showauthor(ctx, templ, **args): 72 def showauthor(repo, ctx, templ, **args):
72 return ctx.user() 73 return ctx.user()
73 74
74 def showbranches(ctx, templ, **args): 75 def showbranches(repo, ctx, templ, **args):
75 branch = ctx.branch() 76 branch = ctx.branch()
76 if branch != 'default': 77 if branch != 'default':
77 branch = encoding.tolocal(branch) 78 branch = encoding.tolocal(branch)
78 return showlist(templ, 'branch', [branch], plural='branches', **args) 79 return showlist(templ, 'branch', [branch], plural='branches', **args)
79 80
80 def showdate(ctx, templ, **args): 81 def showdate(repo, ctx, templ, **args):
81 return ctx.date() 82 return ctx.date()
82 83
83 def showdescription(ctx, templ, **args): 84 def showdescription(repo, ctx, templ, **args):
84 return ctx.description().strip() 85 return ctx.description().strip()
85 86
86 def showextras(ctx, templ, **args): 87 def showdiffstat(repo, ctx, templ, **args):
88 diff = patch.diff(repo, ctx.parents()[0].node(), ctx.node())
89 files, adds, removes = 0, 0, 0
90 for i in patch.diffstatdata(util.iterlines(diff)):
91 files += 1
92 adds += i[1]
93 removes += i[2]
94 return '%s: +%s/-%s' % (files, adds, removes)
95
96 def showextras(repo, ctx, templ, **args):
87 for key, value in sorted(ctx.extra().items()): 97 for key, value in sorted(ctx.extra().items()):
88 args = args.copy() 98 args = args.copy()
89 args.update(dict(key=key, value=value)) 99 args.update(dict(key=key, value=value))
90 yield templ('extra', **args) 100 yield templ('extra', **args)
91 101
92 def showfiles(ctx, templ, **args): 102 def showfiles(repo, ctx, templ, **args):
93 return showlist(templ, 'file', ctx.files(), **args) 103 return showlist(templ, 'file', ctx.files(), **args)
94 104
95 def shownode(ctx, templ, **args): 105 def showmanifest(repo, ctx, templ, **args):
106 args = args.copy()
107 args.update(dict(rev=repo.manifest.rev(ctx.changeset()[0]),
108 node=hex(ctx.changeset()[0])))
109 return templ('manifest', **args)
110
111 def shownode(repo, ctx, templ, **args):
96 return ctx.hex() 112 return ctx.hex()
97 113
98 def showrev(ctx, templ, **args): 114 def showrev(repo, ctx, templ, **args):
99 return ctx.rev() 115 return ctx.rev()
100 116
101 def showtags(ctx, templ, **args): 117 def showtags(repo, ctx, templ, **args):
102 return showlist(templ, 'tag', ctx.tags(), **args) 118 return showlist(templ, 'tag', ctx.tags(), **args)
103 119
104 keywords = { 120 keywords = {
105 'author': showauthor, 121 'author': showauthor,
106 'branches': showbranches, 122 'branches': showbranches,
107 'date': showdate, 123 'date': showdate,
108 'desc': showdescription, 124 'desc': showdescription,
125 'diffstat': showdiffstat,
109 'extras': showextras, 126 'extras': showextras,
110 'files': showfiles, 127 'files': showfiles,
128 'manifest': showmanifest,
111 'node': shownode, 129 'node': shownode,
112 'rev': showrev, 130 'rev': showrev,
113 'tags': showtags, 131 'tags': showtags,
114 } 132 }
115 133