mercurial/namespaces.py
author Martin von Zweigbergk <martinvonz@google.com>
Thu, 07 Jan 2021 12:22:39 -0800
changeset 46270 161313f9c467
parent 44728 59ad165f6cdb
child 48875 6000f5b25c9b
permissions -rw-r--r--
shelve: rewrite check for unknown shelf to delete The code would try to delete the shelf's .patch file and if that raised an exception, it would convert it to an `error.Abort`. This patch rewrites it so the check is done upfront. I find it easier to read that way. It's now clear enough that I removed the comment explaining it as well. As Joerg pointed out during review, another differences is that the old code would move a `.hg` file without its `.patch` friend to backup before it realized that the `.patch` file was missing. The new code will error out earlier and not move the `.hg` file, which seems like an improvement. That should only matter on corrupt `.hg/shelved/` directories, however. Differential Revision: https://phab.mercurial-scm.org/D9697
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25961
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     1
from __future__ import absolute_import
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     2
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     3
from .i18n import _
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     4
from . import (
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
     5
    pycompat,
36592
b0054f3c055a namespace: use registrar to add template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 35212
diff changeset
     6
    registrar,
25961
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     7
    templatekw,
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     8
    util,
c0c89b2d07be namespaces: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
     9
)
23553
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    10
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    11
23555
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    12
def tolist(val):
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    13
    """
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    14
    a convenience method to return an empty list instead of None
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    15
    """
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    16
    if val is None:
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    17
        return []
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    18
    else:
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    19
        return [val]
f08f6a7d4d5f namespaces: add a function to turn single results into lists
Sean Farley <sean.michael.farley@gmail.com>
parents: 23554
diff changeset
    20
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    21
23553
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    22
class namespaces(object):
23718
42908c3275c6 namespaces: update documentation and code indentation
Sean Farley <sean.michael.farley@gmail.com>
parents: 23717
diff changeset
    23
    """provides an interface to register and operate on multiple namespaces. See
42908c3275c6 namespaces: update documentation and code indentation
Sean Farley <sean.michael.farley@gmail.com>
parents: 23717
diff changeset
    24
    the namespace class below for details on the namespace object.
23553
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    25
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    26
    """
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    27
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    28
    _names_version = 0
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    29
23561
3c2419e07df5 namespaces: remove weakref; always pass in repo
Ryan McElroy <rmcelroy@fb.com>
parents: 23559
diff changeset
    30
    def __init__(self):
23553
7cebb6a8c75f namespaces: introduce a generic way to map between names and nodes
Sean Farley <sean.michael.farley@gmail.com>
parents:
diff changeset
    31
        self._names = util.sortdict()
35212
c7b45db8f317 log: translate column labels at once (issue5750)
Yuya Nishihara <yuya@tcha.org>
parents: 33048
diff changeset
    32
        columns = templatekw.getlogcolumns()
23554
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
    33
23558
3198aac7a95d namespaces: add bookmarks to the names data structure
Sean Farley <sean.michael.farley@gmail.com>
parents: 23557
diff changeset
    34
        # we need current mercurial named objects (bookmarks, tags, and
3198aac7a95d namespaces: add bookmarks to the names data structure
Sean Farley <sean.michael.farley@gmail.com>
parents: 23557
diff changeset
    35
        # branches) to be initialized somewhere, so that place is here
23873
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    36
        bmknames = lambda repo: repo._bookmarks.keys()
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    37
        bmknamemap = lambda repo, name: tolist(repo._bookmarks.get(name))
28567
ca52512ac709 namespaces: fix name/node confusion
timeless <timeless@mozdev.org>
parents: 25961
diff changeset
    38
        bmknodemap = lambda repo, node: repo.nodebookmarks(node)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    39
        n = namespace(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
            b"bookmarks",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
            templatename=b"bookmark",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    42
            logfmt=columns[b'bookmark'],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    43
            listnames=bmknames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    44
            namemap=bmknamemap,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    45
            nodemap=bmknodemap,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    46
            builtin=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    47
        )
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
    48
        self.addnamespace(n)
23562
59e703aecaf6 namespaces: add tags
Sean Farley <sean.michael.farley@gmail.com>
parents: 23561
diff changeset
    49
23873
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    50
        tagnames = lambda repo: [t for t, n in repo.tagslist()]
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    51
        tagnamemap = lambda repo, name: tolist(repo._tagscache.tags.get(name))
28567
ca52512ac709 namespaces: fix name/node confusion
timeless <timeless@mozdev.org>
parents: 25961
diff changeset
    52
        tagnodemap = lambda repo, node: repo.nodetags(node)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    53
        n = namespace(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
            b"tags",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
            templatename=b"tag",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
            logfmt=columns[b'tag'],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    57
            listnames=tagnames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    58
            namemap=tagnamemap,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    59
            nodemap=tagnodemap,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
            deprecated={b'tip'},
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    61
            builtin=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    62
        )
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
    63
        self.addnamespace(n)
23558
3198aac7a95d namespaces: add bookmarks to the names data structure
Sean Farley <sean.michael.farley@gmail.com>
parents: 23557
diff changeset
    64
23873
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    65
        bnames = lambda repo: repo.branchmap().keys()
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    66
        bnamemap = lambda repo, name: tolist(repo.branchtip(name, True))
9ef234021667 namespaces: use named args for namespace api
Sean Farley <sean.michael.farley@gmail.com>
parents: 23872
diff changeset
    67
        bnodemap = lambda repo, node: [repo[node].branch()]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    68
        n = namespace(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
            b"branches",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
            templatename=b"branch",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
            logfmt=columns[b'branch'],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    72
            listnames=bnames,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    73
            namemap=bnamemap,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    74
            nodemap=bnodemap,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    75
            builtin=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
    76
        )
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
    77
        self.addnamespace(n)
23563
114992041625 namespaces: add branches
Sean Farley <sean.michael.farley@gmail.com>
parents: 23562
diff changeset
    78
23736
d7324c242c3f namespaces: add __getitem__ property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23718
diff changeset
    79
    def __getitem__(self, namespace):
d7324c242c3f namespaces: add __getitem__ property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23718
diff changeset
    80
        """returns the namespace object"""
d7324c242c3f namespaces: add __getitem__ property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23718
diff changeset
    81
        return self._names[namespace]
d7324c242c3f namespaces: add __getitem__ property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23718
diff changeset
    82
23761
19d6271a70db namespaces: add __iter__ and iteritems methods
Sean Farley <sean.michael.farley@gmail.com>
parents: 23760
diff changeset
    83
    def __iter__(self):
19d6271a70db namespaces: add __iter__ and iteritems methods
Sean Farley <sean.michael.farley@gmail.com>
parents: 23760
diff changeset
    84
        return self._names.__iter__()
19d6271a70db namespaces: add __iter__ and iteritems methods
Sean Farley <sean.michael.farley@gmail.com>
parents: 23760
diff changeset
    85
44728
59ad165f6cdb templatekw: fix shownames() to check if namespace exists in repo (issue6301)
Yuya Nishihara <yuya@tcha.org>
parents: 43106
diff changeset
    86
    def get(self, namespace, default=None):
59ad165f6cdb templatekw: fix shownames() to check if namespace exists in repo (issue6301)
Yuya Nishihara <yuya@tcha.org>
parents: 43106
diff changeset
    87
        return self._names.get(namespace, default)
59ad165f6cdb templatekw: fix shownames() to check if namespace exists in repo (issue6301)
Yuya Nishihara <yuya@tcha.org>
parents: 43106
diff changeset
    88
32550
b98199a5c3e1 cleanup: rename all iteritems methods to items and add iteritems alias
Augie Fackler <raf@durin42.com>
parents: 32291
diff changeset
    89
    def items(self):
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    90
        return pycompat.iteritems(self._names)
23761
19d6271a70db namespaces: add __iter__ and iteritems methods
Sean Farley <sean.michael.farley@gmail.com>
parents: 23760
diff changeset
    91
32550
b98199a5c3e1 cleanup: rename all iteritems methods to items and add iteritems alias
Augie Fackler <raf@durin42.com>
parents: 32291
diff changeset
    92
    iteritems = items
b98199a5c3e1 cleanup: rename all iteritems methods to items and add iteritems alias
Augie Fackler <raf@durin42.com>
parents: 32291
diff changeset
    93
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
    94
    def addnamespace(self, namespace, order=None):
23718
42908c3275c6 namespaces: update documentation and code indentation
Sean Farley <sean.michael.farley@gmail.com>
parents: 23717
diff changeset
    95
        """register a namespace
23554
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
    96
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
    97
        namespace: the name to be registered (in plural form)
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
    98
        order: optional argument to specify the order of namespaces
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
    99
               (e.g. 'branches' should be listed before 'bookmarks')
23718
42908c3275c6 namespaces: update documentation and code indentation
Sean Farley <sean.michael.farley@gmail.com>
parents: 23717
diff changeset
   100
23554
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
   101
        """
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
   102
        if order is not None:
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
   103
            self._names.insert(order, namespace.name, namespace)
23554
75f9643cab1b namespaces: add a method to register new namespaces
Sean Farley <sean.michael.farley@gmail.com>
parents: 23553
diff changeset
   104
        else:
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
   105
            self._names[namespace.name] = namespace
23559
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   106
23610
9266d1dd6a6e namespaces: generate template keyword when registering a namespace
Sean Farley <sean.michael.farley@gmail.com>
parents: 23608
diff changeset
   107
        # we only generate a template keyword if one does not already exist
23717
d8663e6153c1 namespaces: use namespace object instead of dictionary
Sean Farley <sean.michael.farley@gmail.com>
parents: 23716
diff changeset
   108
        if namespace.name not in templatekw.keywords:
36592
b0054f3c055a namespace: use registrar to add template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 35212
diff changeset
   109
            templatekeyword = registrar.templatekeyword(templatekw.keywords)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   110
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   111
            @templatekeyword(namespace.name, requires={b'repo', b'ctx'})
36593
900e5ee44307 templatekw: switch namespace template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36592
diff changeset
   112
            def generatekw(context, mapping):
900e5ee44307 templatekw: switch namespace template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36592
diff changeset
   113
                return templatekw.shownames(context, mapping, namespace.name)
23610
9266d1dd6a6e namespaces: generate template keyword when registering a namespace
Sean Farley <sean.michael.farley@gmail.com>
parents: 23608
diff changeset
   114
23561
3c2419e07df5 namespaces: remove weakref; always pass in repo
Ryan McElroy <rmcelroy@fb.com>
parents: 23559
diff changeset
   115
    def singlenode(self, repo, name):
23559
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   116
        """
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   117
        Return the 'best' node for the given name. What's best is defined
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   118
        by the namespace's singlenode() function. The first match returned by
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   119
        a namespace in the defined precedence order is used.
23559
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   120
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   121
        Raises a KeyError if there is no such node.
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   122
        """
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
   123
        for ns, v in pycompat.iteritems(self._names):
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   124
            n = v.singlenode(repo, name)
23559
3b3a962e3677 namespaces: add a method to the first matching node for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23558
diff changeset
   125
            if n:
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   126
                return n
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   127
        raise KeyError(_(b'no such name: %s') % name)
23606
80e3cbe227d1 namespaces: add method to get template name of namespace
Sean Farley <sean.michael.farley@gmail.com>
parents: 23605
diff changeset
   128
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   129
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   130
class namespace(object):
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   131
    """provides an interface to a namespace
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   132
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   133
    Namespaces are basically generic many-to-many mapping between some
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   134
    (namespaced) names and nodes. The goal here is to control the pollution of
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   135
    jamming things into tags or bookmarks (in extension-land) and to simplify
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   136
    internal bits of mercurial: log output, tab completion, etc.
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   137
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   138
    More precisely, we define a mapping of names to nodes, and a mapping from
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   139
    nodes to names. Each mapping returns a list.
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   140
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   141
    Furthermore, each name mapping will be passed a name to lookup which might
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   142
    not be in its domain. In this case, each method should return an empty list
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   143
    and not raise an error.
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   144
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   145
    This namespace object will define the properties we need:
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   146
      'name': the namespace (plural form)
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   147
      'templatename': name to use for templating (usually the singular form
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   148
                      of the plural namespace name)
23760
50229b4c33be namespaces: add 'listnames' property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23739
diff changeset
   149
      'listnames': list of all names in the namespace (usually the keys of a
50229b4c33be namespaces: add 'listnames' property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23739
diff changeset
   150
                   dictionary)
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   151
      'namemap': function that takes a name and returns a list of nodes
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   152
      'nodemap': function that takes a node and returns a list of names
24151
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   153
      'deprecated': set of names to be masked for ordinary use
33048
46fa46608ca5 namespaces: record and expose whether namespace is built-in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32550
diff changeset
   154
      'builtin': bool indicating if this namespace is supported by core
46fa46608ca5 namespaces: record and expose whether namespace is built-in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32550
diff changeset
   155
                 Mercurial.
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   156
    """
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   157
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   158
    def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   159
        self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   160
        name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   161
        templatename=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   162
        logname=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   163
        colorname=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   164
        logfmt=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   165
        listnames=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   166
        namemap=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   167
        nodemap=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   168
        deprecated=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   169
        builtin=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   170
        singlenode=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38486
diff changeset
   171
    ):
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   172
        """create a namespace
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   173
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   174
        name: the namespace to be registered (in plural form)
23872
9f48242929a9 namespaces: make the constructor into named args
Sean Farley <sean.michael.farley@gmail.com>
parents: 23775
diff changeset
   175
        templatename: the name to use for templating
23874
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   176
        logname: the name to use for log output; if not specified templatename
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   177
                 is used
23875
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   178
        colorname: the name to use for colored log output; if not specified
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   179
                   logname is used
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24151
diff changeset
   180
        logfmt: the format to use for (i18n-ed) log output; if not specified
23967
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   181
                it is composed from logname
23760
50229b4c33be namespaces: add 'listnames' property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23739
diff changeset
   182
        listnames: function to list all names
28567
ca52512ac709 namespaces: fix name/node confusion
timeless <timeless@mozdev.org>
parents: 25961
diff changeset
   183
        namemap: function that inputs a name, output node(s)
ca52512ac709 namespaces: fix name/node confusion
timeless <timeless@mozdev.org>
parents: 25961
diff changeset
   184
        nodemap: function that inputs a node, output name(s)
24151
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   185
        deprecated: set of names to be masked for ordinary use
33048
46fa46608ca5 namespaces: record and expose whether namespace is built-in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32550
diff changeset
   186
        builtin: whether namespace is implemented by core Mercurial
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   187
        singlenode: function that inputs a name, output best node (or None)
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   188
        """
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   189
        self.name = name
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   190
        self.templatename = templatename
23874
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   191
        self.logname = logname
23875
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   192
        self.colorname = colorname
23967
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   193
        self.logfmt = logfmt
23760
50229b4c33be namespaces: add 'listnames' property
Sean Farley <sean.michael.farley@gmail.com>
parents: 23739
diff changeset
   194
        self.listnames = listnames
23715
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   195
        self.namemap = namemap
eee55c09010a namespaces: add a namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23610
diff changeset
   196
        self.nodemap = nodemap
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   197
        if singlenode:
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   198
            self.singlenode = singlenode
23716
f4828a8f6ae9 namespaces: copy implementation to new namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23715
diff changeset
   199
23874
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   200
        # if logname is not specified, use the template name as backup
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   201
        if self.logname is None:
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   202
            self.logname = self.templatename
fef1146b8442 namespaces: add logname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23873
diff changeset
   203
23875
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   204
        # if colorname is not specified, just use the logname as a backup
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   205
        if self.colorname is None:
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   206
            self.colorname = self.logname
e573dd08aeaf namespaces: add colorname member to namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23874
diff changeset
   207
23967
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   208
        # if logfmt is not specified, compose it from logname as backup
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   209
        if self.logfmt is None:
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   210
            # i18n: column positioning for "hg log"
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   211
            self.logfmt = (b"%s:" % self.logname).ljust(13) + b"%s\n"
23967
448bb32b8ee6 namespace: introduce logfmt to show l10n-ed messages for hg log correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23875
diff changeset
   212
24151
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   213
        if deprecated is None:
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   214
            self.deprecated = set()
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   215
        else:
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   216
            self.deprecated = deprecated
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23967
diff changeset
   217
33048
46fa46608ca5 namespaces: record and expose whether namespace is built-in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32550
diff changeset
   218
        self.builtin = builtin
46fa46608ca5 namespaces: record and expose whether namespace is built-in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32550
diff changeset
   219
23716
f4828a8f6ae9 namespaces: copy implementation to new namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23715
diff changeset
   220
    def names(self, repo, node):
f4828a8f6ae9 namespaces: copy implementation to new namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23715
diff changeset
   221
        """method that returns a (sorted) list of names in a namespace that
f4828a8f6ae9 namespaces: copy implementation to new namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23715
diff changeset
   222
        match a given node"""
f4828a8f6ae9 namespaces: copy implementation to new namespace object
Sean Farley <sean.michael.farley@gmail.com>
parents: 23715
diff changeset
   223
        return sorted(self.nodemap(repo, node))
23774
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   224
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   225
    def nodes(self, repo, name):
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   226
        """method that returns a list of nodes in a namespace that
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   227
        match a given name.
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   228
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   229
        """
b9537ee87961 namespaces: add method to return a list of nodes for a given name
Sean Farley <sean.michael.farley@gmail.com>
parents: 23761
diff changeset
   230
        return sorted(self.namemap(repo, name))
38486
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   231
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   232
    def singlenode(self, repo, name):
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   233
        """returns the best node for the given name
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   234
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   235
        By default, the best node is the node from nodes() with the highest
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   236
        revision number. It can be overriden by the namespace."""
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   237
        n = self.namemap(repo, name)
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   238
        if n:
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   239
            # return max revision number
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   240
            if len(n) > 1:
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   241
                cl = repo.changelog
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   242
                maxrev = max(cl.rev(node) for node in n)
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   243
                return cl.node(maxrev)
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   244
            return n[0]
4c0683655599 namespaces: let namespaces override singlenode() definition
Martin von Zweigbergk <martinvonz@google.com>
parents: 37068
diff changeset
   245
        return None