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