changeset 35239:744d1c874a59

remotenames: add functions to read remotenames data from .hg/remotenames/ This patch functions which can be used to read remotenames data from .hg/remotenames/. The logic for the function which reads the remotenames file is taken from the remotenames extension. Previously reviewed as D940. Differential Revision: https://phab.mercurial-scm.org/D1550
author Pulkit Goyal <7895pulkit@gmail.com>
date Thu, 05 Oct 2017 01:31:53 +0530
parents 54bb94b715ad
children 2ea6e42ed15e
files mercurial/remotenames.py
diffstat 1 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/remotenames.py	Fri Nov 10 22:54:59 2017 +0530
+++ b/mercurial/remotenames.py	Thu Oct 05 01:31:53 2017 +0530
@@ -17,6 +17,46 @@
 # directory name in .hg/ in which remotenames files will be present
 remotenamedir = 'remotenames'
 
+def readremotenamefile(repo, filename):
+    """
+    reads a file from .hg/remotenames/ directory and yields it's content
+    filename: the file to be read
+    yield a tuple (node, remotepath, name)
+    """
+
+    vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
+    if not vfs.exists(filename):
+        return
+    f = vfs(filename)
+    lineno = 0
+    for line in f:
+        line = line.strip()
+        if not line:
+            continue
+        # contains the version number
+        if lineno == 0:
+            lineno += 1
+        try:
+            node, remote, rname = line.split('\0')
+            yield node, remote, rname
+        except ValueError:
+            pass
+
+    f.close()
+
+def readremotenames(repo):
+    """
+    read the details about the remotenames stored in .hg/remotenames/ and
+    yields a tuple (node, remotepath, name). It does not yields information
+    about whether an entry yielded is branch or bookmark. To get that
+    information, call the respective functions.
+    """
+
+    for bmentry in readremotenamefile(repo, 'bookmarks'):
+        yield bmentry
+    for branchentry in readremotenamefile(repo, 'branches'):
+        yield branchentry
+
 def writeremotenamefile(repo, remotepath, names, nametype):
     vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
     f = vfs(nametype, 'w', atomictemp=True)