hgext/remotenames.py
changeset 36060 cabe8ef5c71e
child 36061 be72f6420f3c
equal deleted inserted replaced
36059:62a428bf6359 36060:cabe8ef5c71e
       
     1 # remotenames.py - extension to display remotenames
       
     2 #
       
     3 # Copyright 2017 Augie Fackler <raf@durin42.com>
       
     4 # Copyright 2017 Sean Farley <sean@farley.io>
       
     5 #
       
     6 # This software may be used and distributed according to the terms of the
       
     7 # GNU General Public License version 2 or any later version.
       
     8 
       
     9 """ showing remotebookmarks and remotebranches in UI """
       
    10 
       
    11 from __future__ import absolute_import
       
    12 
       
    13 from mercurial import (
       
    14     logexchange,
       
    15 )
       
    16 
       
    17 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
       
    18 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
       
    19 # be specifying the version(s) of Mercurial they are tested with, or
       
    20 # leave the attribute unspecified.
       
    21 testedwith = 'ships-with-hg-core'
       
    22 
       
    23 class remotenames(dict):
       
    24     """
       
    25     This class encapsulates all the remotenames state. It also contains
       
    26     methods to access that state in convenient ways.
       
    27     """
       
    28 
       
    29     def __init__(self, repo, *args):
       
    30         dict.__init__(self, *args)
       
    31         self._repo = repo
       
    32         self['bookmarks'] = {}
       
    33         self['branches'] = {}
       
    34         self.loadnames()
       
    35         self._loadednames = True
       
    36 
       
    37     def loadnames(self):
       
    38         """ loads the remotenames information from the remotenames file """
       
    39         for rtype in ('bookmarks', 'branches'):
       
    40             for node, rpath, name in logexchange.readremotenamefile(self._repo,
       
    41                                                                     rtype):
       
    42                 rname = rpath + '/' + name
       
    43                 self[rtype][rname] = [node]
       
    44 
       
    45     def clearnames(self):
       
    46         """ Clear all remote names state """
       
    47         self['bookmarks'] = {}
       
    48         self['branches'] = {}
       
    49         self._invalidatecache()
       
    50         self._loadednames = False
       
    51 
       
    52     def _invalidatecache(self):
       
    53         self._nodetobmarks = None
       
    54         self._nodetobranch = None
       
    55 
       
    56     def bmarktonodes(self):
       
    57         return self['bookmarks']
       
    58 
       
    59     def nodetobmarks(self):
       
    60         if not self._nodetobmarks:
       
    61             bmarktonodes = self.bmarktonodes()
       
    62             self._nodetobmarks = {}
       
    63             for name, node in bmarktonodes.iteritems():
       
    64                 self._nodetobmarks.setdefault(node[0], []).append(name)
       
    65         return self._nodetobmarks
       
    66 
       
    67     def branchtonodes(self):
       
    68         return self['branches']
       
    69 
       
    70     def nodetobranch(self):
       
    71         if not self._nodetobranch:
       
    72             branchtonodes = self.branchtonodes()
       
    73             self._nodetobranch = {}
       
    74             for name, nodes in branchtonodes.iteritems():
       
    75                 for node in nodes:
       
    76                     self._nodetobranch.setdefault(node, []).append(name)
       
    77         return self._nodetobranch