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