2015-08-09 05:42:58 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from .i18n import _
|
|
|
|
from . import (
|
|
|
|
templatekw,
|
|
|
|
util,
|
|
|
|
)
|
2014-12-14 23:29:28 +03:00
|
|
|
|
2014-12-15 02:04:40 +03:00
|
|
|
def tolist(val):
|
|
|
|
"""
|
|
|
|
a convenience method to return an empty list instead of None
|
|
|
|
"""
|
|
|
|
if val is None:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return [val]
|
|
|
|
|
2014-12-14 23:29:28 +03:00
|
|
|
class namespaces(object):
|
2014-12-22 00:06:24 +03:00
|
|
|
"""provides an interface to register and operate on multiple namespaces. See
|
|
|
|
the namespace class below for details on the namespace object.
|
2014-12-14 23:29:28 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
_names_version = 0
|
|
|
|
|
2014-12-15 06:11:44 +03:00
|
|
|
def __init__(self):
|
2014-12-14 23:29:28 +03:00
|
|
|
self._names = util.sortdict()
|
2014-10-17 22:25:51 +04:00
|
|
|
|
2014-12-14 23:53:50 +03:00
|
|
|
# we need current mercurial named objects (bookmarks, tags, and
|
|
|
|
# branches) to be initialized somewhere, so that place is here
|
2015-01-15 06:55:20 +03:00
|
|
|
bmknames = lambda repo: repo._bookmarks.keys()
|
|
|
|
bmknamemap = lambda repo, name: tolist(repo._bookmarks.get(name))
|
2016-03-17 02:12:43 +03:00
|
|
|
bmknodemap = lambda repo, node: repo.nodebookmarks(node)
|
2015-01-28 16:22:59 +03:00
|
|
|
n = namespace("bookmarks", templatename="bookmark",
|
|
|
|
# i18n: column positioning for "hg log"
|
|
|
|
logfmt=_("bookmark: %s\n"),
|
|
|
|
listnames=bmknames,
|
2015-01-15 06:55:20 +03:00
|
|
|
namemap=bmknamemap, nodemap=bmknodemap)
|
2014-12-20 04:27:20 +03:00
|
|
|
self.addnamespace(n)
|
2014-10-18 02:27:33 +04:00
|
|
|
|
2015-01-15 06:55:20 +03:00
|
|
|
tagnames = lambda repo: [t for t, n in repo.tagslist()]
|
|
|
|
tagnamemap = lambda repo, name: tolist(repo._tagscache.tags.get(name))
|
2016-03-17 02:12:43 +03:00
|
|
|
tagnodemap = lambda repo, node: repo.nodetags(node)
|
2015-01-28 16:22:59 +03:00
|
|
|
n = namespace("tags", templatename="tag",
|
|
|
|
# i18n: column positioning for "hg log"
|
|
|
|
logfmt=_("tag: %s\n"),
|
|
|
|
listnames=tagnames,
|
revset: mask specific names for named() predicate
Before this patch, revset predicate "tag()" and "named('tags')" differ
from each other, because the former doesn't include "tip" but the
latter does.
For equivalence, "named('tags')" shouldn't include the revision
corresponded to "tip". But just removing "tip" from the "tags"
namespace causes breaking backward compatibility, even though "tip"
itself is planned to be eliminated, as mentioned below.
http://selenic.com/pipermail/mercurial-devel/2015-February/066157.html
To mask specific names ("tip" in this case) for "named()" predicate,
this patch introduces "deprecated" into "namespaces", and makes
"named()" predicate examine whether each names are masked by the
namespace, to which they belong.
"named()" will really work correctly after 3.3.1 (see a3c326a7f57a for
detail), and fixing this on STABLE before 3.3.1 can prevent initial
users of "named()" from expecting "named('tags')" to include "tip".
It is reason why this patch is posted for STABLE, even though problem
itself isn't so serious.
This may have to be flagged as "(BC)", if applied on DEFAULT.
2015-02-05 08:45:49 +03:00
|
|
|
namemap=tagnamemap, nodemap=tagnodemap,
|
2017-02-11 03:56:29 +03:00
|
|
|
deprecated={'tip'})
|
2014-12-20 04:27:20 +03:00
|
|
|
self.addnamespace(n)
|
2014-12-14 23:53:50 +03:00
|
|
|
|
2015-01-15 06:55:20 +03:00
|
|
|
bnames = lambda repo: repo.branchmap().keys()
|
|
|
|
bnamemap = lambda repo, name: tolist(repo.branchtip(name, True))
|
|
|
|
bnodemap = lambda repo, node: [repo[node].branch()]
|
2015-01-28 16:22:59 +03:00
|
|
|
n = namespace("branches", templatename="branch",
|
|
|
|
# i18n: column positioning for "hg log"
|
|
|
|
logfmt=_("branch: %s\n"),
|
|
|
|
listnames=bnames,
|
2015-01-15 06:55:20 +03:00
|
|
|
namemap=bnamemap, nodemap=bnodemap)
|
2014-12-20 04:27:20 +03:00
|
|
|
self.addnamespace(n)
|
2014-10-18 02:27:12 +04:00
|
|
|
|
2014-12-22 00:56:32 +03:00
|
|
|
def __getitem__(self, namespace):
|
|
|
|
"""returns the namespace object"""
|
|
|
|
return self._names[namespace]
|
|
|
|
|
2014-12-22 20:07:37 +03:00
|
|
|
def __iter__(self):
|
|
|
|
return self._names.__iter__()
|
|
|
|
|
|
|
|
def iteritems(self):
|
|
|
|
return self._names.iteritems()
|
|
|
|
|
2014-12-20 04:27:20 +03:00
|
|
|
def addnamespace(self, namespace, order=None):
|
2014-12-22 00:06:24 +03:00
|
|
|
"""register a namespace
|
2014-10-17 22:25:51 +04:00
|
|
|
|
|
|
|
namespace: the name to be registered (in plural form)
|
|
|
|
order: optional argument to specify the order of namespaces
|
|
|
|
(e.g. 'branches' should be listed before 'bookmarks')
|
2014-12-22 00:06:24 +03:00
|
|
|
|
2014-10-17 22:25:51 +04:00
|
|
|
"""
|
|
|
|
if order is not None:
|
2014-12-20 04:27:20 +03:00
|
|
|
self._names.insert(order, namespace.name, namespace)
|
2014-10-17 22:25:51 +04:00
|
|
|
else:
|
2014-12-20 04:27:20 +03:00
|
|
|
self._names[namespace.name] = namespace
|
2014-10-18 02:28:40 +04:00
|
|
|
|
2014-10-17 10:19:09 +04:00
|
|
|
# we only generate a template keyword if one does not already exist
|
2014-12-20 04:27:20 +03:00
|
|
|
if namespace.name not in templatekw.keywords:
|
2014-10-17 10:19:09 +04:00
|
|
|
def generatekw(**args):
|
2014-12-20 04:27:20 +03:00
|
|
|
return templatekw.shownames(namespace.name, **args)
|
2014-10-17 10:19:09 +04:00
|
|
|
|
2014-12-20 04:27:20 +03:00
|
|
|
templatekw.keywords[namespace.name] = generatekw
|
2014-10-17 10:19:09 +04:00
|
|
|
|
2014-12-15 06:11:44 +03:00
|
|
|
def singlenode(self, repo, name):
|
2014-10-18 02:28:40 +04:00
|
|
|
"""
|
|
|
|
Return the 'best' node for the given name. Best means the first node
|
|
|
|
in the first nonempty list returned by a name-to-nodes mapping function
|
|
|
|
in the defined precedence order.
|
|
|
|
|
|
|
|
Raises a KeyError if there is no such node.
|
|
|
|
"""
|
|
|
|
for ns, v in self._names.iteritems():
|
2014-12-20 04:27:20 +03:00
|
|
|
n = v.namemap(repo, name)
|
2014-10-18 02:28:40 +04:00
|
|
|
if n:
|
|
|
|
# return max revision number
|
|
|
|
if len(n) > 1:
|
2014-12-15 06:11:44 +03:00
|
|
|
cl = repo.changelog
|
2014-10-18 02:28:40 +04:00
|
|
|
maxrev = max(cl.rev(node) for node in n)
|
|
|
|
return cl.node(maxrev)
|
|
|
|
return n[0]
|
|
|
|
raise KeyError(_('no such name: %s') % name)
|
2014-12-15 06:12:27 +03:00
|
|
|
|
2014-12-20 04:00:28 +03:00
|
|
|
class namespace(object):
|
|
|
|
"""provides an interface to a namespace
|
|
|
|
|
|
|
|
Namespaces are basically generic many-to-many mapping between some
|
|
|
|
(namespaced) names and nodes. The goal here is to control the pollution of
|
|
|
|
jamming things into tags or bookmarks (in extension-land) and to simplify
|
|
|
|
internal bits of mercurial: log output, tab completion, etc.
|
|
|
|
|
|
|
|
More precisely, we define a mapping of names to nodes, and a mapping from
|
|
|
|
nodes to names. Each mapping returns a list.
|
|
|
|
|
|
|
|
Furthermore, each name mapping will be passed a name to lookup which might
|
|
|
|
not be in its domain. In this case, each method should return an empty list
|
|
|
|
and not raise an error.
|
|
|
|
|
|
|
|
This namespace object will define the properties we need:
|
|
|
|
'name': the namespace (plural form)
|
|
|
|
'templatename': name to use for templating (usually the singular form
|
|
|
|
of the plural namespace name)
|
2014-12-16 01:09:00 +03:00
|
|
|
'listnames': list of all names in the namespace (usually the keys of a
|
|
|
|
dictionary)
|
2014-12-20 04:00:28 +03:00
|
|
|
'namemap': function that takes a name and returns a list of nodes
|
|
|
|
'nodemap': function that takes a node and returns a list of names
|
revset: mask specific names for named() predicate
Before this patch, revset predicate "tag()" and "named('tags')" differ
from each other, because the former doesn't include "tip" but the
latter does.
For equivalence, "named('tags')" shouldn't include the revision
corresponded to "tip". But just removing "tip" from the "tags"
namespace causes breaking backward compatibility, even though "tip"
itself is planned to be eliminated, as mentioned below.
http://selenic.com/pipermail/mercurial-devel/2015-February/066157.html
To mask specific names ("tip" in this case) for "named()" predicate,
this patch introduces "deprecated" into "namespaces", and makes
"named()" predicate examine whether each names are masked by the
namespace, to which they belong.
"named()" will really work correctly after 3.3.1 (see a3c326a7f57a for
detail), and fixing this on STABLE before 3.3.1 can prevent initial
users of "named()" from expecting "named('tags')" to include "tip".
It is reason why this patch is posted for STABLE, even though problem
itself isn't so serious.
This may have to be flagged as "(BC)", if applied on DEFAULT.
2015-02-05 08:45:49 +03:00
|
|
|
'deprecated': set of names to be masked for ordinary use
|
2014-12-20 04:00:28 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-01-15 07:11:02 +03:00
|
|
|
def __init__(self, name, templatename=None, logname=None, colorname=None,
|
revset: mask specific names for named() predicate
Before this patch, revset predicate "tag()" and "named('tags')" differ
from each other, because the former doesn't include "tip" but the
latter does.
For equivalence, "named('tags')" shouldn't include the revision
corresponded to "tip". But just removing "tip" from the "tags"
namespace causes breaking backward compatibility, even though "tip"
itself is planned to be eliminated, as mentioned below.
http://selenic.com/pipermail/mercurial-devel/2015-February/066157.html
To mask specific names ("tip" in this case) for "named()" predicate,
this patch introduces "deprecated" into "namespaces", and makes
"named()" predicate examine whether each names are masked by the
namespace, to which they belong.
"named()" will really work correctly after 3.3.1 (see a3c326a7f57a for
detail), and fixing this on STABLE before 3.3.1 can prevent initial
users of "named()" from expecting "named('tags')" to include "tip".
It is reason why this patch is posted for STABLE, even though problem
itself isn't so serious.
This may have to be flagged as "(BC)", if applied on DEFAULT.
2015-02-05 08:45:49 +03:00
|
|
|
logfmt=None, listnames=None, namemap=None, nodemap=None,
|
|
|
|
deprecated=None):
|
2014-12-20 04:00:28 +03:00
|
|
|
"""create a namespace
|
|
|
|
|
|
|
|
name: the namespace to be registered (in plural form)
|
|
|
|
templatename: the name to use for templating
|
2015-01-15 07:06:44 +03:00
|
|
|
logname: the name to use for log output; if not specified templatename
|
|
|
|
is used
|
2015-01-15 07:11:02 +03:00
|
|
|
colorname: the name to use for colored log output; if not specified
|
|
|
|
logname is used
|
2015-01-18 04:38:57 +03:00
|
|
|
logfmt: the format to use for (i18n-ed) log output; if not specified
|
2015-01-28 16:22:59 +03:00
|
|
|
it is composed from logname
|
2015-01-15 06:39:13 +03:00
|
|
|
listnames: function to list all names
|
2016-03-17 02:12:43 +03:00
|
|
|
namemap: function that inputs a name, output node(s)
|
|
|
|
nodemap: function that inputs a node, output name(s)
|
revset: mask specific names for named() predicate
Before this patch, revset predicate "tag()" and "named('tags')" differ
from each other, because the former doesn't include "tip" but the
latter does.
For equivalence, "named('tags')" shouldn't include the revision
corresponded to "tip". But just removing "tip" from the "tags"
namespace causes breaking backward compatibility, even though "tip"
itself is planned to be eliminated, as mentioned below.
http://selenic.com/pipermail/mercurial-devel/2015-February/066157.html
To mask specific names ("tip" in this case) for "named()" predicate,
this patch introduces "deprecated" into "namespaces", and makes
"named()" predicate examine whether each names are masked by the
namespace, to which they belong.
"named()" will really work correctly after 3.3.1 (see a3c326a7f57a for
detail), and fixing this on STABLE before 3.3.1 can prevent initial
users of "named()" from expecting "named('tags')" to include "tip".
It is reason why this patch is posted for STABLE, even though problem
itself isn't so serious.
This may have to be flagged as "(BC)", if applied on DEFAULT.
2015-02-05 08:45:49 +03:00
|
|
|
deprecated: set of names to be masked for ordinary use
|
2014-12-20 04:00:28 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.templatename = templatename
|
2015-01-15 07:06:44 +03:00
|
|
|
self.logname = logname
|
2015-01-15 07:11:02 +03:00
|
|
|
self.colorname = colorname
|
2015-01-28 16:22:59 +03:00
|
|
|
self.logfmt = logfmt
|
2014-12-16 01:09:00 +03:00
|
|
|
self.listnames = listnames
|
2014-12-20 04:00:28 +03:00
|
|
|
self.namemap = namemap
|
|
|
|
self.nodemap = nodemap
|
2014-12-20 04:17:17 +03:00
|
|
|
|
2015-01-15 07:06:44 +03:00
|
|
|
# if logname is not specified, use the template name as backup
|
|
|
|
if self.logname is None:
|
|
|
|
self.logname = self.templatename
|
|
|
|
|
2015-01-15 07:11:02 +03:00
|
|
|
# if colorname is not specified, just use the logname as a backup
|
|
|
|
if self.colorname is None:
|
|
|
|
self.colorname = self.logname
|
|
|
|
|
2015-01-28 16:22:59 +03:00
|
|
|
# if logfmt is not specified, compose it from logname as backup
|
|
|
|
if self.logfmt is None:
|
|
|
|
# i18n: column positioning for "hg log"
|
|
|
|
self.logfmt = ("%s:" % self.logname).ljust(13) + "%s\n"
|
|
|
|
|
revset: mask specific names for named() predicate
Before this patch, revset predicate "tag()" and "named('tags')" differ
from each other, because the former doesn't include "tip" but the
latter does.
For equivalence, "named('tags')" shouldn't include the revision
corresponded to "tip". But just removing "tip" from the "tags"
namespace causes breaking backward compatibility, even though "tip"
itself is planned to be eliminated, as mentioned below.
http://selenic.com/pipermail/mercurial-devel/2015-February/066157.html
To mask specific names ("tip" in this case) for "named()" predicate,
this patch introduces "deprecated" into "namespaces", and makes
"named()" predicate examine whether each names are masked by the
namespace, to which they belong.
"named()" will really work correctly after 3.3.1 (see a3c326a7f57a for
detail), and fixing this on STABLE before 3.3.1 can prevent initial
users of "named()" from expecting "named('tags')" to include "tip".
It is reason why this patch is posted for STABLE, even though problem
itself isn't so serious.
This may have to be flagged as "(BC)", if applied on DEFAULT.
2015-02-05 08:45:49 +03:00
|
|
|
if deprecated is None:
|
|
|
|
self.deprecated = set()
|
|
|
|
else:
|
|
|
|
self.deprecated = deprecated
|
|
|
|
|
2014-12-20 04:17:17 +03:00
|
|
|
def names(self, repo, node):
|
|
|
|
"""method that returns a (sorted) list of names in a namespace that
|
|
|
|
match a given node"""
|
|
|
|
return sorted(self.nodemap(repo, node))
|
2014-12-16 01:46:04 +03:00
|
|
|
|
|
|
|
def nodes(self, repo, name):
|
|
|
|
"""method that returns a list of nodes in a namespace that
|
|
|
|
match a given name.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return sorted(self.namemap(repo, name))
|