comparison mercurial/cmdutil.py @ 10053:5c5c6295533d

cmdutil: replace showlist() closure with a function
author Patrick Mezard <pmezard@gmail.com>
date Sun, 13 Dec 2009 18:06:23 +0100
parents 0b0a46607ac9
children 1a85861f59af
comparison
equal deleted inserted replaced
10052:dfc3ed37d58d 10053:5c5c6295533d
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 from node import hex, nullid, nullrev, short 8 from node import hex, nullid, nullrev, short
9 from i18n import _ 9 from i18n import _
10 import os, sys, errno, re, glob 10 import os, sys, errno, re, glob
11 import mdiff, bdiff, util, templater, patch, error, encoding 11 import mdiff, bdiff, util, templater, patch, error, encoding, templatekw
12 import match as _match 12 import match as _match
13 13
14 revrangesep = ':' 14 revrangesep = ':'
15 15
16 def findpossible(cmd, table, strict=False): 16 def findpossible(cmd, table, strict=False):
808 return self._latesttagcache[rev] 808 return self._latesttagcache[rev]
809 809
810 def _show(self, ctx, copies, props): 810 def _show(self, ctx, copies, props):
811 '''show a single changeset or file revision''' 811 '''show a single changeset or file revision'''
812 812
813 def showlist(name, values, plural=None, **args): 813 showlist = templatekw.showlist
814 '''expand set of values. 814
815 name is name of key in template map. 815 def showbranches(templ, **args):
816 values is list of strings or dicts.
817 plural is plural of name, if not simply name + 's'.
818
819 expansion works like this, given name 'foo'.
820
821 if values is empty, expand 'no_foos'.
822
823 if 'foo' not in template map, return values as a string,
824 joined by space.
825
826 expand 'start_foos'.
827
828 for each value, expand 'foo'. if 'last_foo' in template
829 map, expand it instead of 'foo' for last key.
830
831 expand 'end_foos'.
832 '''
833 if plural: names = plural
834 else: names = name + 's'
835 if not values:
836 noname = 'no_' + names
837 if noname in self.t:
838 yield self.t(noname, **args)
839 return
840 if name not in self.t:
841 if isinstance(values[0], str):
842 yield ' '.join(values)
843 else:
844 for v in values:
845 yield dict(v, **args)
846 return
847 startname = 'start_' + names
848 if startname in self.t:
849 yield self.t(startname, **args)
850 vargs = args.copy()
851 def one(v, tag=name):
852 try:
853 vargs.update(v)
854 except (AttributeError, ValueError):
855 try:
856 for a, b in v:
857 vargs[a] = b
858 except ValueError:
859 vargs[name] = v
860 return self.t(tag, **vargs)
861 lastname = 'last_' + name
862 if lastname in self.t:
863 last = values.pop()
864 else:
865 last = None
866 for v in values:
867 yield one(v)
868 if last is not None:
869 yield one(last, tag=lastname)
870 endname = 'end_' + names
871 if endname in self.t:
872 yield self.t(endname, **args)
873
874 def showbranches(**args):
875 branch = ctx.branch() 816 branch = ctx.branch()
876 if branch != 'default': 817 if branch != 'default':
877 branch = encoding.tolocal(branch) 818 branch = encoding.tolocal(branch)
878 return showlist('branch', [branch], plural='branches', **args) 819 return showlist(templ, 'branch', [branch], plural='branches',
879 820 **args)
880 def showparents(**args): 821
822 def showparents(templ, **args):
881 parents = [[('rev', p.rev()), ('node', p.hex())] 823 parents = [[('rev', p.rev()), ('node', p.hex())]
882 for p in self._meaningful_parentrevs(ctx)] 824 for p in self._meaningful_parentrevs(ctx)]
883 return showlist('parent', parents, **args) 825 return showlist(templ, 'parent', parents, **args)
884 826
885 def showtags(**args): 827 def showtags(templ, **args):
886 return showlist('tag', ctx.tags(), **args) 828 return showlist(templ, 'tag', ctx.tags(), **args)
887 829
888 def showextras(**args): 830 def showextras(templ, **args):
889 for key, value in sorted(ctx.extra().items()): 831 for key, value in sorted(ctx.extra().items()):
890 args = args.copy() 832 args = args.copy()
891 args.update(dict(key=key, value=value)) 833 args.update(dict(key=key, value=value))
892 yield self.t('extra', **args) 834 yield templ('extra', **args)
893 835
894 def showcopies(**args): 836 def showcopies(templ, **args):
895 c = [{'name': x[0], 'source': x[1]} for x in copies] 837 c = [{'name': x[0], 'source': x[1]} for x in copies]
896 return showlist('file_copy', c, plural='file_copies', **args) 838 return showlist(templ, 'file_copy', c, plural='file_copies', **args)
897 839
898 files = [] 840 files = []
899 def getfiles(): 841 def getfiles():
900 if not files: 842 if not files:
901 files[:] = self.repo.status(ctx.parents()[0].node(), 843 files[:] = self.repo.status(ctx.parents()[0].node(),
902 ctx.node())[:3] 844 ctx.node())[:3]
903 return files 845 return files
904 def showfiles(**args): 846 def showfiles(templ, **args):
905 return showlist('file', ctx.files(), **args) 847 return showlist(templ, 'file', ctx.files(), **args)
906 def showmods(**args): 848 def showmods(templ, **args):
907 return showlist('file_mod', getfiles()[0], **args) 849 return showlist(templ, 'file_mod', getfiles()[0], **args)
908 def showadds(**args): 850 def showadds(templ, **args):
909 return showlist('file_add', getfiles()[1], **args) 851 return showlist(templ, 'file_add', getfiles()[1], **args)
910 def showdels(**args): 852 def showdels(templ, **args):
911 return showlist('file_del', getfiles()[2], **args) 853 return showlist(templ, 'file_del', getfiles()[2], **args)
912 def showmanifest(**args): 854 def showmanifest(templ, **args):
913 args = args.copy() 855 args = args.copy()
914 args.update(dict(rev=self.repo.manifest.rev(ctx.changeset()[0]), 856 args.update(dict(rev=self.repo.manifest.rev(ctx.changeset()[0]),
915 node=hex(ctx.changeset()[0]))) 857 node=hex(ctx.changeset()[0])))
916 return self.t('manifest', **args) 858 return templ('manifest', **args)
917 859
918 def showdiffstat(**args): 860 def showdiffstat(templ, **args):
919 diff = patch.diff(self.repo, ctx.parents()[0].node(), ctx.node()) 861 diff = patch.diff(self.repo, ctx.parents()[0].node(), ctx.node())
920 files, adds, removes = 0, 0, 0 862 files, adds, removes = 0, 0, 0
921 for i in patch.diffstatdata(util.iterlines(diff)): 863 for i in patch.diffstatdata(util.iterlines(diff)):
922 files += 1 864 files += 1
923 adds += i[1] 865 adds += i[1]
924 removes += i[2] 866 removes += i[2]
925 return '%s: +%s/-%s' % (files, adds, removes) 867 return '%s: +%s/-%s' % (files, adds, removes)
926 868
927 def showlatesttag(**args): 869 def showlatesttag(templ, **args):
928 return self._latesttaginfo(ctx.rev())[2] 870 return self._latesttaginfo(ctx.rev())[2]
929 def showlatesttagdistance(**args): 871 def showlatesttagdistance(templ, **args):
930 return self._latesttaginfo(ctx.rev())[1] 872 return self._latesttaginfo(ctx.rev())[1]
931 873
932 defprops = { 874 defprops = {
933 'author': ctx.user(), 875 'author': ctx.user(),
934 'branches': showbranches, 876 'branches': showbranches,
949 'latesttag': showlatesttag, 891 'latesttag': showlatesttag,
950 'latesttagdistance': showlatesttagdistance, 892 'latesttagdistance': showlatesttagdistance,
951 } 893 }
952 props = props.copy() 894 props = props.copy()
953 props.update(defprops) 895 props.update(defprops)
896 props['templ'] = self.t
954 897
955 # find correct templates for current mode 898 # find correct templates for current mode
956 899
957 tmplmodes = [ 900 tmplmodes = [
958 (True, None), 901 (True, None),