Patchwork D1548: remotenames: add functionality to store remotenames under .hg/hgremotenames/

login
register
mail settings
Submitter phabricator
Date Dec. 5, 2017, 10:23 p.m.
Message ID <110340f2ab1817237dcb4de4bf40732d@localhost.localdomain>
Download mbox | patch
Permalink /patch/25949/
State Not Applicable
Headers show

Comments

phabricator - Dec. 5, 2017, 10:23 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG8df8ce2cc5dd: remotenames: add functionality to store remotenames under .hg/hgremotenames/ (authored by pulkit, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1548?vs=3971&id=4139

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

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

CHANGE DETAILS




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

Patch

diff --git a/tests/test-remotenames.t b/tests/test-remotenames.t
--- a/tests/test-remotenames.t
+++ b/tests/test-remotenames.t
@@ -60,14 +60,16 @@ 
   adding remote bookmark bar
   adding remote bookmark foo
   new changesets 18d04c59bb5d:3e1487808078
+  (run 'hg heads' to see heads)
+
+  $ cat .hg/remotenames/bookmarks
+  0
   
-  Remotenames info
-  path: file:$TESTTMP/server
-  Bookmarks:
-  foo: 62615734edd52f06b6fb9c2beb429e4fe30d57b8
-  bar: 87d6d66763085b629e6d7ed56778c79827273022
-  Branches:
-  wat: ['3e1487808078543b0af6d10dadf5d46943578db0']
-  default: ['ec2426147f0e39dbc9cef599b066be6035ce691d']
+  87d6d66763085b629e6d7ed56778c79827273022\x00file:$TESTTMP/server\x00bar (esc)
+  62615734edd52f06b6fb9c2beb429e4fe30d57b8\x00file:$TESTTMP/server\x00foo (esc)
+
+  $ cat .hg/remotenames/branches
+  0
   
-  (run 'hg heads' to see heads)
+  ec2426147f0e39dbc9cef599b066be6035ce691d\x00file:$TESTTMP/server\x00default (esc)
+  3e1487808078543b0af6d10dadf5d46943578db0\x00file:$TESTTMP/server\x00wat (esc)
diff --git a/mercurial/remotenames.py b/mercurial/remotenames.py
--- a/mercurial/remotenames.py
+++ b/mercurial/remotenames.py
@@ -10,6 +10,44 @@ 
 
 from .node import hex
 
+from . import (
+    vfs as vfsmod,
+)
+
+# directory name in .hg/ in which remotenames files will be present
+remotenamedir = 'remotenames'
+
+def writeremotenamefile(repo, remotepath, names, nametype):
+    vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
+    f = vfs(nametype, 'w', atomictemp=True)
+    # write the storage version info on top of file
+    # version '0' represents the very initial version of the storage format
+    f.write('0\n\n')
+
+    for name, node in sorted(names.iteritems()):
+        if nametype == "branches":
+            for n in node:
+                f.write('%s\0%s\0%s\n' % (n, remotepath, name))
+        elif nametype == "bookmarks":
+            if node:
+                f.write('%s\0%s\0%s\n' % (node, remotepath, name))
+
+    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:
+            writeremotenamefile(repo, remotepath, bookmarks, 'bookmarks')
+        if branches:
+            writeremotenamefile(repo, remotepath, branches, 'branches')
+    finally:
+        wlock.release()
+
 def pullremotenames(localrepo, remoterepo):
     """
     pulls bookmarks and branches information of the remote repo during a
@@ -31,13 +69,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)