D939: remotenames: add functionality to store remotenames under .hg/hgremotenames/

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Fri Nov 10 14:01:07 EST 2017


pulkit updated this revision to Diff 3393.
pulkit edited the summary of this revision.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D939?vs=2436&id=3393

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

AFFECTED FILES
  mercurial/remotenames.py
  tests/test-remotenames.t

CHANGE DETAILS

diff --git a/tests/test-remotenames.t b/tests/test-remotenames.t
--- a/tests/test-remotenames.t
+++ b/tests/test-remotenames.t
@@ -59,14 +59,11 @@
   added 9 changesets with 9 changes to 9 files (+1 heads)
   adding remote bookmark bar
   adding remote bookmark foo
-  
-  Remotenames info
-  path: file:$TESTTMP/server
-  Bookmarks:
-  foo: 62615734edd52f06b6fb9c2beb429e4fe30d57b8
-  bar: 87d6d66763085b629e6d7ed56778c79827273022
-  Branches:
-  wat: ['3e1487808078543b0af6d10dadf5d46943578db0']
-  default: ['ec2426147f0e39dbc9cef599b066be6035ce691d']
-  
   (run 'hg heads' to see heads)
+
+  $ cat .hg/remotenames/bookmarks
+  87d6d66763085b629e6d7ed56778c79827273022 file:$TESTTMP/server bar
+  62615734edd52f06b6fb9c2beb429e4fe30d57b8 file:$TESTTMP/server foo
+  $ cat .hg/remotenames/branches
+  ec2426147f0e39dbc9cef599b066be6035ce691d file:$TESTTMP/server default
+  3e1487808078543b0af6d10dadf5d46943578db0 file:$TESTTMP/server wat
diff --git a/mercurial/remotenames.py b/mercurial/remotenames.py
--- a/mercurial/remotenames.py
+++ b/mercurial/remotenames.py
@@ -10,6 +10,62 @@
 
 from .node import hex
 
+from . import (
+    vfs as vfsmod,
+)
+
+# directory name in .hg/ in which remotenames files will be present
+remotenamedir = 'remotenames'
+
+def saveremotebookmarks(repo, remotepath, bookmarks):
+    """
+    save remote bookmarks in .hg/remotenames/bookmarks.
+    The format of the data stored will be:
+
+    `node remotepath bookmarkname`
+
+    bookmarks is a dictionary of remote bookmarks.
+    """
+    vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
+    f = vfs('bookmarks', 'w', atomictemp=True)
+
+    for bookmark, node in sorted(bookmarks.iteritems()):
+        if node:
+            f.write('%s %s %s\n' % (node, remotepath, bookmark))
+    f.close()
+
+def saveremotebranches(repo, remotepath, branches):
+    """
+    save remote branches is .hg/remotenames/branches.
+    The format of the data stored will be:
+
+    `node remotepath branchname`
+
+    branches is a dictionary of remote branches.
+    """
+    vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
+    f = vfs('branches', 'w', atomictemp=True)
+
+    for branch, nodes in sorted(branches.iteritems()):
+        for n in nodes:
+            f.write('%s %s %s\n' % (n, remotepath, branch))
+
+    f.close()
+
+def saveremotenames(repo, remotepath, branches=None, bookmarks=None):
+    """
+    save remotenames i.e. remotebookmarks and remotebranches in their
+    respective files under ".hg/remotenames/" directory.
+    """
+    wlock = repo.wlock()
+    try:
+        if bookmarks:
+            saveremotebookmarks(repo, remotepath, bookmarks)
+        if branches:
+            saveremotebranches(repo, remotepath, branches)
+    finally:
+        wlock.release()
+
 def pullremotenames(localrepo, remoterepo):
     """
     pulls bookmarks and branches information of the remote repo during a
@@ -31,13 +87,4 @@
             if node in repo and not repo[node].obsolete():
                 bmap[branch].append(hex(node))
 
-    # writing things to ui till the time we import the saving functionality
-    ui = localrepo.ui
-    ui.write("\nRemotenames info\npath: %s\n" % remotepath)
-    ui.write("Bookmarks:\n")
-    for bm, node in bookmarks.iteritems():
-        ui.write("%s: %s\n" % (bm, node))
-    ui.write("Branches:\n")
-    for branch, node in bmap.iteritems():
-        ui.write("%s: %s\n" % (branch, node))
-    ui.write("\n")
+    saveremotenames(localrepo, remotepath, bmap, bookmarks)



To: pulkit, #hg-reviewers, dlax, ryanmce
Cc: ryanmce, dlax, mercurial-devel


More information about the Mercurial-devel mailing list