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