##// END OF EJS Templates
namespaces: add logname member to namespace object...
Sean Farley -
r23874:fef1146b default
parent child Browse files
Show More
@@ -1,150 +1,157 b''
1 from i18n import _
1 from i18n import _
2 from mercurial import util
2 from mercurial import util
3 import templatekw
3 import templatekw
4
4
5 def tolist(val):
5 def tolist(val):
6 """
6 """
7 a convenience method to return an empty list instead of None
7 a convenience method to return an empty list instead of None
8 """
8 """
9 if val is None:
9 if val is None:
10 return []
10 return []
11 else:
11 else:
12 return [val]
12 return [val]
13
13
14 class namespaces(object):
14 class namespaces(object):
15 """provides an interface to register and operate on multiple namespaces. See
15 """provides an interface to register and operate on multiple namespaces. See
16 the namespace class below for details on the namespace object.
16 the namespace class below for details on the namespace object.
17
17
18 """
18 """
19
19
20 _names_version = 0
20 _names_version = 0
21
21
22 def __init__(self):
22 def __init__(self):
23 self._names = util.sortdict()
23 self._names = util.sortdict()
24
24
25 # we need current mercurial named objects (bookmarks, tags, and
25 # we need current mercurial named objects (bookmarks, tags, and
26 # branches) to be initialized somewhere, so that place is here
26 # branches) to be initialized somewhere, so that place is here
27 bmknames = lambda repo: repo._bookmarks.keys()
27 bmknames = lambda repo: repo._bookmarks.keys()
28 bmknamemap = lambda repo, name: tolist(repo._bookmarks.get(name))
28 bmknamemap = lambda repo, name: tolist(repo._bookmarks.get(name))
29 bmknodemap = lambda repo, name: repo.nodebookmarks(name)
29 bmknodemap = lambda repo, name: repo.nodebookmarks(name)
30 n = namespace("bookmarks", templatename="bookmark", listnames=bmknames,
30 n = namespace("bookmarks", templatename="bookmark", listnames=bmknames,
31 namemap=bmknamemap, nodemap=bmknodemap)
31 namemap=bmknamemap, nodemap=bmknodemap)
32 self.addnamespace(n)
32 self.addnamespace(n)
33
33
34 tagnames = lambda repo: [t for t, n in repo.tagslist()]
34 tagnames = lambda repo: [t for t, n in repo.tagslist()]
35 tagnamemap = lambda repo, name: tolist(repo._tagscache.tags.get(name))
35 tagnamemap = lambda repo, name: tolist(repo._tagscache.tags.get(name))
36 tagnodemap = lambda repo, name: repo.nodetags(name)
36 tagnodemap = lambda repo, name: repo.nodetags(name)
37 n = namespace("tags", templatename="tag", listnames=tagnames,
37 n = namespace("tags", templatename="tag", listnames=tagnames,
38 namemap=tagnamemap, nodemap=tagnodemap)
38 namemap=tagnamemap, nodemap=tagnodemap)
39 self.addnamespace(n)
39 self.addnamespace(n)
40
40
41 bnames = lambda repo: repo.branchmap().keys()
41 bnames = lambda repo: repo.branchmap().keys()
42 bnamemap = lambda repo, name: tolist(repo.branchtip(name, True))
42 bnamemap = lambda repo, name: tolist(repo.branchtip(name, True))
43 bnodemap = lambda repo, node: [repo[node].branch()]
43 bnodemap = lambda repo, node: [repo[node].branch()]
44 n = namespace("branches", templatename="branch", listnames=bnames,
44 n = namespace("branches", templatename="branch", listnames=bnames,
45 namemap=bnamemap, nodemap=bnodemap)
45 namemap=bnamemap, nodemap=bnodemap)
46 self.addnamespace(n)
46 self.addnamespace(n)
47
47
48 def __getitem__(self, namespace):
48 def __getitem__(self, namespace):
49 """returns the namespace object"""
49 """returns the namespace object"""
50 return self._names[namespace]
50 return self._names[namespace]
51
51
52 def __iter__(self):
52 def __iter__(self):
53 return self._names.__iter__()
53 return self._names.__iter__()
54
54
55 def iteritems(self):
55 def iteritems(self):
56 return self._names.iteritems()
56 return self._names.iteritems()
57
57
58 def addnamespace(self, namespace, order=None):
58 def addnamespace(self, namespace, order=None):
59 """register a namespace
59 """register a namespace
60
60
61 namespace: the name to be registered (in plural form)
61 namespace: the name to be registered (in plural form)
62 order: optional argument to specify the order of namespaces
62 order: optional argument to specify the order of namespaces
63 (e.g. 'branches' should be listed before 'bookmarks')
63 (e.g. 'branches' should be listed before 'bookmarks')
64
64
65 """
65 """
66 if order is not None:
66 if order is not None:
67 self._names.insert(order, namespace.name, namespace)
67 self._names.insert(order, namespace.name, namespace)
68 else:
68 else:
69 self._names[namespace.name] = namespace
69 self._names[namespace.name] = namespace
70
70
71 # we only generate a template keyword if one does not already exist
71 # we only generate a template keyword if one does not already exist
72 if namespace.name not in templatekw.keywords:
72 if namespace.name not in templatekw.keywords:
73 def generatekw(**args):
73 def generatekw(**args):
74 return templatekw.shownames(namespace.name, **args)
74 return templatekw.shownames(namespace.name, **args)
75
75
76 templatekw.keywords[namespace.name] = generatekw
76 templatekw.keywords[namespace.name] = generatekw
77
77
78 def singlenode(self, repo, name):
78 def singlenode(self, repo, name):
79 """
79 """
80 Return the 'best' node for the given name. Best means the first node
80 Return the 'best' node for the given name. Best means the first node
81 in the first nonempty list returned by a name-to-nodes mapping function
81 in the first nonempty list returned by a name-to-nodes mapping function
82 in the defined precedence order.
82 in the defined precedence order.
83
83
84 Raises a KeyError if there is no such node.
84 Raises a KeyError if there is no such node.
85 """
85 """
86 for ns, v in self._names.iteritems():
86 for ns, v in self._names.iteritems():
87 n = v.namemap(repo, name)
87 n = v.namemap(repo, name)
88 if n:
88 if n:
89 # return max revision number
89 # return max revision number
90 if len(n) > 1:
90 if len(n) > 1:
91 cl = repo.changelog
91 cl = repo.changelog
92 maxrev = max(cl.rev(node) for node in n)
92 maxrev = max(cl.rev(node) for node in n)
93 return cl.node(maxrev)
93 return cl.node(maxrev)
94 return n[0]
94 return n[0]
95 raise KeyError(_('no such name: %s') % name)
95 raise KeyError(_('no such name: %s') % name)
96
96
97 class namespace(object):
97 class namespace(object):
98 """provides an interface to a namespace
98 """provides an interface to a namespace
99
99
100 Namespaces are basically generic many-to-many mapping between some
100 Namespaces are basically generic many-to-many mapping between some
101 (namespaced) names and nodes. The goal here is to control the pollution of
101 (namespaced) names and nodes. The goal here is to control the pollution of
102 jamming things into tags or bookmarks (in extension-land) and to simplify
102 jamming things into tags or bookmarks (in extension-land) and to simplify
103 internal bits of mercurial: log output, tab completion, etc.
103 internal bits of mercurial: log output, tab completion, etc.
104
104
105 More precisely, we define a mapping of names to nodes, and a mapping from
105 More precisely, we define a mapping of names to nodes, and a mapping from
106 nodes to names. Each mapping returns a list.
106 nodes to names. Each mapping returns a list.
107
107
108 Furthermore, each name mapping will be passed a name to lookup which might
108 Furthermore, each name mapping will be passed a name to lookup which might
109 not be in its domain. In this case, each method should return an empty list
109 not be in its domain. In this case, each method should return an empty list
110 and not raise an error.
110 and not raise an error.
111
111
112 This namespace object will define the properties we need:
112 This namespace object will define the properties we need:
113 'name': the namespace (plural form)
113 'name': the namespace (plural form)
114 'templatename': name to use for templating (usually the singular form
114 'templatename': name to use for templating (usually the singular form
115 of the plural namespace name)
115 of the plural namespace name)
116 'listnames': list of all names in the namespace (usually the keys of a
116 'listnames': list of all names in the namespace (usually the keys of a
117 dictionary)
117 dictionary)
118 'namemap': function that takes a name and returns a list of nodes
118 'namemap': function that takes a name and returns a list of nodes
119 'nodemap': function that takes a node and returns a list of names
119 'nodemap': function that takes a node and returns a list of names
120
120
121 """
121 """
122
122
123 def __init__(self, name, templatename=None, listnames=None, namemap=None,
123 def __init__(self, name, templatename=None, logname=None, listnames=None,
124 nodemap=None):
124 namemap=None, nodemap=None):
125 """create a namespace
125 """create a namespace
126
126
127 name: the namespace to be registered (in plural form)
127 name: the namespace to be registered (in plural form)
128 templatename: the name to use for templating
128 templatename: the name to use for templating
129 logname: the name to use for log output; if not specified templatename
130 is used
129 listnames: function to list all names
131 listnames: function to list all names
130 namemap: function that inputs a node, output name(s)
132 namemap: function that inputs a node, output name(s)
131 nodemap: function that inputs a name, output node(s)
133 nodemap: function that inputs a name, output node(s)
132
134
133 """
135 """
134 self.name = name
136 self.name = name
135 self.templatename = templatename
137 self.templatename = templatename
138 self.logname = logname
136 self.listnames = listnames
139 self.listnames = listnames
137 self.namemap = namemap
140 self.namemap = namemap
138 self.nodemap = nodemap
141 self.nodemap = nodemap
139
142
143 # if logname is not specified, use the template name as backup
144 if self.logname is None:
145 self.logname = self.templatename
146
140 def names(self, repo, node):
147 def names(self, repo, node):
141 """method that returns a (sorted) list of names in a namespace that
148 """method that returns a (sorted) list of names in a namespace that
142 match a given node"""
149 match a given node"""
143 return sorted(self.nodemap(repo, node))
150 return sorted(self.nodemap(repo, node))
144
151
145 def nodes(self, repo, name):
152 def nodes(self, repo, name):
146 """method that returns a list of nodes in a namespace that
153 """method that returns a list of nodes in a namespace that
147 match a given name.
154 match a given name.
148
155
149 """
156 """
150 return sorted(self.namemap(repo, name))
157 return sorted(self.namemap(repo, name))
General Comments 0
You need to be logged in to leave comments. Login now