##// END OF EJS Templates
namespaces: add nodemap property...
Sean Farley -
r23607:0fd778ef default
parent child Browse files
Show More
@@ -1,90 +1,98 b''
1 from i18n import _
1 from i18n import _
2 from mercurial import util
2 from mercurial import util
3
3
4 def tolist(val):
4 def tolist(val):
5 """
5 """
6 a convenience method to return an empty list instead of None
6 a convenience method to return an empty list instead of None
7 """
7 """
8 if val is None:
8 if val is None:
9 return []
9 return []
10 else:
10 else:
11 return [val]
11 return [val]
12
12
13 class namespaces(object):
13 class namespaces(object):
14 """
14 """
15 provides an interface to register a generic many-to-many mapping between
15 provides an interface to register a generic many-to-many mapping between
16 some (namespaced) names and nodes. The goal here is to control the
16 some (namespaced) names and nodes. The goal here is to control the
17 pollution of jamming things into tags or bookmarks (in extension-land) and
17 pollution of jamming things into tags or bookmarks (in extension-land) and
18 to simplify internal bits of mercurial: log output, tab completion, etc.
18 to simplify internal bits of mercurial: log output, tab completion, etc.
19
19
20 More precisely, we define a list of names (the namespace) and a mapping of
20 More precisely, we define a list of names (the namespace), a mapping of
21 names to nodes. This name mapping returns a list of nodes.
21 names to nodes, and a mapping from nodes to names. Each mapping
22 returns a list of nodes.
22
23
23 Furthermore, each name mapping will be passed a name to lookup which might
24 Furthermore, each name mapping will be passed a name to lookup which might
24 not be in its domain. In this case, each method should return an empty list
25 not be in its domain. In this case, each method should return an empty list
25 and not raise an error.
26 and not raise an error.
26
27
27 We'll have a dictionary '_names' where each key is a namespace and
28 We'll have a dictionary '_names' where each key is a namespace and
28 its value is a dictionary of functions:
29 its value is a dictionary of functions:
29 'templatename': name to use for templating (usually the singular form
30 'templatename': name to use for templating (usually the singular form
30 of the plural namespace name)
31 of the plural namespace name)
31 'namemap': function that takes a name and returns a list of nodes
32 'namemap': function that takes a name and returns a list of nodes
33 'nodemap': function that takes a node and returns a list of names
32 """
34 """
33
35
34 _names_version = 0
36 _names_version = 0
35
37
36 def __init__(self):
38 def __init__(self):
37 self._names = util.sortdict()
39 self._names = util.sortdict()
38
40
39 addns = self.addnamespace
41 addns = self.addnamespace
40
42
41 # we need current mercurial named objects (bookmarks, tags, and
43 # we need current mercurial named objects (bookmarks, tags, and
42 # branches) to be initialized somewhere, so that place is here
44 # branches) to be initialized somewhere, so that place is here
43 addns("bookmarks", "bookmark",
45 addns("bookmarks", "bookmark",
44 lambda repo, name: tolist(repo._bookmarks.get(name)))
46 lambda repo, name: tolist(repo._bookmarks.get(name)),
47 lambda repo, name: repo.nodebookmarks(name))
45
48
46 addns("tags", "tag",
49 addns("tags", "tag",
47 lambda repo, name: tolist(repo._tagscache.tags.get(name)))
50 lambda repo, name: tolist(repo._tagscache.tags.get(name)),
51 lambda repo, name: repo.nodetags(name))
48
52
49 addns("branches", "branch",
53 addns("branches", "branch",
50 lambda repo, name: tolist(repo.branchtip(name)))
54 lambda repo, name: tolist(repo.branchtip(name)),
55 lambda repo, node: [repo[node].branch()])
51
56
52 def addnamespace(self, namespace, templatename, namemap, order=None):
57 def addnamespace(self, namespace, templatename, namemap, nodemap,
58 order=None):
53 """
59 """
54 register a namespace
60 register a namespace
55
61
56 namespace: the name to be registered (in plural form)
62 namespace: the name to be registered (in plural form)
57 templatename: the name to use for templating
63 templatename: the name to use for templating
58 namemap: function that inputs a node, output name(s)
64 namemap: function that inputs a node, output name(s)
65 nodemap: function that inputs a name, output node(s)
59 order: optional argument to specify the order of namespaces
66 order: optional argument to specify the order of namespaces
60 (e.g. 'branches' should be listed before 'bookmarks')
67 (e.g. 'branches' should be listed before 'bookmarks')
61 """
68 """
62 val = {'templatename': templatename,
69 val = {'templatename': templatename,
63 'namemap': namemap}
70 'namemap': namemap,
71 'nodemap': nodemap}
64 if order is not None:
72 if order is not None:
65 self._names.insert(order, namespace, val)
73 self._names.insert(order, namespace, val)
66 else:
74 else:
67 self._names[namespace] = val
75 self._names[namespace] = val
68
76
69 def singlenode(self, repo, name):
77 def singlenode(self, repo, name):
70 """
78 """
71 Return the 'best' node for the given name. Best means the first node
79 Return the 'best' node for the given name. Best means the first node
72 in the first nonempty list returned by a name-to-nodes mapping function
80 in the first nonempty list returned by a name-to-nodes mapping function
73 in the defined precedence order.
81 in the defined precedence order.
74
82
75 Raises a KeyError if there is no such node.
83 Raises a KeyError if there is no such node.
76 """
84 """
77 for ns, v in self._names.iteritems():
85 for ns, v in self._names.iteritems():
78 n = v['namemap'](repo, name)
86 n = v['namemap'](repo, name)
79 if n:
87 if n:
80 # return max revision number
88 # return max revision number
81 if len(n) > 1:
89 if len(n) > 1:
82 cl = repo.changelog
90 cl = repo.changelog
83 maxrev = max(cl.rev(node) for node in n)
91 maxrev = max(cl.rev(node) for node in n)
84 return cl.node(maxrev)
92 return cl.node(maxrev)
85 return n[0]
93 return n[0]
86 raise KeyError(_('no such name: %s') % name)
94 raise KeyError(_('no such name: %s') % name)
87
95
88 def templatename(self, namespace):
96 def templatename(self, namespace):
89 """method that returns the template name of a namespace"""
97 """method that returns the template name of a namespace"""
90 return self._names[namespace]['templatename']
98 return self._names[namespace]['templatename']
General Comments 0
You need to be logged in to leave comments. Login now