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