Patchwork D1756: remotenames: introduce class to encapsulate remotenames info in an extension

login
register
mail settings
Submitter phabricator
Date Feb. 2, 2018, 7:47 a.m.
Message ID <c5974ba6df7435722980ab8da856b36a@localhost.localdomain>
Download mbox | patch
Permalink /patch/27215/
State Not Applicable
Headers show

Comments

phabricator - Feb. 2, 2018, 7:47 a.m.
pulkit updated this revision to Diff 5145.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1756?vs=4610&id=5145

REVISION DETAIL
  https://phab.mercurial-scm.org/D1756

AFFECTED FILES
  hgext/remotenames.py
  tests/test-help.t

CHANGE DETAILS




To: pulkit, #hg-reviewers, durin42
Cc: durin42, mercurial-devel

Patch

diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -274,6 +274,7 @@ 
        purge         command to delete untracked files from the working
                      directory
        relink        recreates hardlinks between repository clones
+       remotenames   showing remotebookmarks and remotebranches in UI
        schemes       extend schemes with shortcuts to repository swarms
        share         share a common history between several working directories
        shelve        save and restore changes to the working directory
diff --git a/hgext/remotenames.py b/hgext/remotenames.py
new file mode 100644
--- /dev/null
+++ b/hgext/remotenames.py
@@ -0,0 +1,77 @@ 
+# remotenames.py - extension to display remotenames
+#
+# Copyright 2017 Augie Fackler <raf@durin42.com>
+# Copyright 2017 Sean Farley <sean@farley.io>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+""" showing remotebookmarks and remotebranches in UI """
+
+from __future__ import absolute_import
+
+from mercurial import (
+    logexchange,
+)
+
+# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
+# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
+# be specifying the version(s) of Mercurial they are tested with, or
+# leave the attribute unspecified.
+testedwith = 'ships-with-hg-core'
+
+class remotenames(dict):
+    """
+    This class encapsulates all the remotenames state. It also contains
+    methods to access that state in convenient ways.
+    """
+
+    def __init__(self, repo, *args):
+        dict.__init__(self, *args)
+        self._repo = repo
+        self['bookmarks'] = {}
+        self['branches'] = {}
+        self.loadnames()
+        self._loadednames = True
+
+    def loadnames(self):
+        """ loads the remotenames information from the remotenames file """
+        for rtype in ('bookmarks', 'branches'):
+            for node, rpath, name in logexchange.readremotenamefile(self._repo,
+                                                                    rtype):
+                rname = rpath + '/' + name
+                self[rtype][rname] = [node]
+
+    def clearnames(self):
+        """ Clear all remote names state """
+        self['bookmarks'] = {}
+        self['branches'] = {}
+        self._invalidatecache()
+        self._loadednames = False
+
+    def _invalidatecache(self):
+        self._nodetobmarks = None
+        self._nodetobranch = None
+
+    def bmarktonodes(self):
+        return self['bookmarks']
+
+    def nodetobmarks(self):
+        if not self._nodetobmarks:
+            bmarktonodes = self.bmarktonodes()
+            self._nodetobmarks = {}
+            for name, node in bmarktonodes.iteritems():
+                self._nodetobmarks.setdefault(node[0], []).append(name)
+        return self._nodetobmarks
+
+    def branchtonodes(self):
+        return self['branches']
+
+    def nodetobranch(self):
+        if not self._nodetobranch:
+            branchtonodes = self.branchtonodes()
+            self._nodetobranch = {}
+            for name, nodes in branchtonodes.iteritems():
+                for node in nodes:
+                    self._nodetobranch.setdefault(node, []).append(name)
+        return self._nodetobranch