remotenames: add functionality to hoist remotebookmarks
authorPulkit Goyal <7895pulkit@gmail.com>
Sun, 11 Mar 2018 16:17:51 +0530
changeset 37089 9938992c5bae
parent 37088 08890706366e
child 37090 a61fff493d98
remotenames: add functionality to hoist remotebookmarks This patch adds the functionality to hoist remotebookmarks to the top level namespace. The peer of which bookmarks should be hoisted can be set using `remotenames.hoistedpeer` config option. Only bookmarks can be hoisted. If a hoisted name and local bookmark exists of the same name, the local bookmark takes precedence. While I was here, I documented the default values of two other remotenames config options. Differential Revision: https://phab.mercurial-scm.org/D2807
hgext/remotenames.py
tests/test-logexchange.t
--- a/hgext/remotenames.py	Thu Mar 08 11:44:03 2018 -0800
+++ b/hgext/remotenames.py	Sun Mar 11 16:17:51 2018 +0530
@@ -14,10 +14,14 @@
 Config options to tweak the default behaviour:
 
 remotenames.bookmarks
-  Boolean value to enable or disable showing of remotebookmarks
+  Boolean value to enable or disable showing of remotebookmarks (default: True)
 
 remotenames.branches
-  Boolean value to enable or disable showing of remotebranches
+  Boolean value to enable or disable showing of remotebranches (default: True)
+
+remotenames.hoistedpeer
+  Name of the peer whose remotebookmarks should be hoisted into the top-level
+  namespace (default: 'default')
 """
 
 from __future__ import absolute_import
@@ -61,6 +65,9 @@
 configitem('remotenames', 'branches',
     default=True,
 )
+configitem('remotenames', 'hoistedpeer',
+    default='default',
+)
 
 class lazyremotenamedict(mutablemapping):
     """
@@ -173,6 +180,8 @@
     def _invalidatecache(self):
         self._nodetobmarks = None
         self._nodetobranch = None
+        self._hoisttonodes = None
+        self._nodetohoists = None
 
     def bmarktonodes(self):
         return self.bookmarks
@@ -197,6 +206,28 @@
                     self._nodetobranch.setdefault(node, []).append(name)
         return self._nodetobranch
 
+    def hoisttonodes(self, hoist):
+        if not self._hoisttonodes:
+            marktonodes = self.bmarktonodes()
+            self._hoisttonodes = {}
+            hoist += '/'
+            for name, node in marktonodes.iteritems():
+                if name.startswith(hoist):
+                    name = name[len(hoist):]
+                    self._hoisttonodes[name] = node
+        return self._hoisttonodes
+
+    def nodetohoists(self, hoist):
+        if not self._nodetohoists:
+            marktonodes = self.bmarktonodes()
+            self._nodetohoists = {}
+            hoist += '/'
+            for name, node in marktonodes.iteritems():
+                if name.startswith(hoist):
+                    name = name[len(hoist):]
+                    self._nodetohoists.setdefault(node[0], []).append(name)
+        return self._nodetohoists
+
 def reposetup(ui, repo):
     if not repo.local():
         return
@@ -217,6 +248,22 @@
                 repo._remotenames.nodetobmarks().get(node, []))
         repo.names.addnamespace(remotebookmarkns)
 
+        # hoisting only works if there are remote bookmarks
+        hoist = ui.config('remotenames', 'hoistedpeer')
+        if hoist:
+            hoistednamens = ns(
+                'hoistednames',
+                templatename='hoistednames',
+                colorname='hoistedname',
+                logfmt='hoisted name:  %s\n',
+                listnames = lambda repo:
+                    repo._remotenames.hoisttonodes(hoist).keys(),
+                namemap = lambda repo, name:
+                    repo._remotenames.hoisttonodes(hoist).get(name, []),
+                nodemap = lambda repo, node:
+                    repo._remotenames.nodetohoists(hoist).get(node, []))
+            repo.names.addnamespace(hoistednamens)
+
     if ui.configbool('remotenames', 'branches'):
         remotebranchns = ns(
             'remotebranches',
--- a/tests/test-logexchange.t	Thu Mar 08 11:44:03 2018 -0800
+++ b/tests/test-logexchange.t	Sun Mar 11 16:17:51 2018 +0530
@@ -145,6 +145,7 @@
   | |  bookmark:    bar
   | |  remote bookmark:  $TESTTMP/server2/bar
   | |  remote bookmark:  default/bar
+  | |  hoisted name:  bar
   | |  user:        test
   | |  date:        Thu Jan 01 00:00:00 1970 +0000
   | |  summary:     Added g
@@ -163,6 +164,7 @@
   |  bookmark:    foo
   |  remote bookmark:  $TESTTMP/server2/foo
   |  remote bookmark:  default/foo
+  |  hoisted name:  foo
   |  user:        test
   |  date:        Thu Jan 01 00:00:00 1970 +0000
   |  summary:     Added d
@@ -226,6 +228,28 @@
   |
   o  0:18d04c59bb5d [] ()
   
+The `hoistednames` template keyword
+
+  $ hg log -GT "{rev}:{node|short} ({hoistednames})"
+  @  8:3e1487808078 ()
+  |
+  | o  7:ec2426147f0e ()
+  | |
+  | o  6:87d6d6676308 (bar)
+  | |
+  | o  5:825660c69f0c ()
+  |/
+  o  4:aa98ab95a928 ()
+  |
+  o  3:62615734edd5 (foo)
+  |
+  o  2:28ad74487de9 ()
+  |
+  o  1:29becc82797a ()
+  |
+  o  0:18d04c59bb5d ()
+  
+
 Testing the revsets provided by remotenames extension
 
 `remotenames` revset
@@ -259,3 +283,47 @@
   o  3:62615734edd5 $TESTTMP/server2/foo default/foo
   |
   ~
+
+Updating to revision using hoisted name
+
+Deleting local bookmark to make sure we update to hoisted name only
+
+  $ hg bookmark -d bar
+
+  $ hg up bar
+  2 files updated, 0 files merged, 1 files removed, 0 files unresolved
+
+  $ hg log -r .
+  changeset:   6:87d6d6676308
+  remote bookmark:  $TESTTMP/server2/bar
+  remote bookmark:  default/bar
+  hoisted name:  bar
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Added g
+  
+When both local bookmark and hoisted name exists but on different revs
+
+  $ hg up 8
+  1 files updated, 0 files merged, 2 files removed, 0 files unresolved
+
+  $ hg bookmark foo
+  moving bookmark 'foo' forward from 62615734edd5
+
+Local bookmark should take precedence over hoisted name
+
+  $ hg up foo
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+  $ hg log -r .
+  changeset:   8:3e1487808078
+  branch:      wat
+  bookmark:    foo
+  tag:         tip
+  remote branch:  $TESTTMP/server2/wat
+  remote branch:  default/wat
+  parent:      4:aa98ab95a928
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     added bar
+