Commits

Augie Fackler committed 4c943de

Initial import of remotebranches.

Comments (0)

Files changed (2)

hg_remotebranches.py

+import os
+
+from mercurial import config
+from mercurial import hg
+from mercurial import node
+from mercurial import ui
+from mercurial import util
+
+
+def reposetup(ui, repo):
+    if repo.local():
+        opull = repo.pull
+        olookup = repo.lookup
+        ofindtags = repo._findtags
+
+        class remotebranchesrepo(repo.__class__):
+            def _findtags(self):
+                (tags, tagtypes) = ofindtags()
+                tags.update(self._remotebranches)
+                return (tags, tagtypes)
+
+            @util.propertycache
+            def _remotebranches(self):
+                remotebranches = {}
+                bfile = self.join('remotebranches')
+                if os.path.exists(bfile):
+                    f = open(bfile)
+                    for line in f:
+                        line = line.strip()
+                        if line:
+                            hash, name = line.split(' ', 1)
+                            remotebranches[name] = olookup(hash)
+                return remotebranches
+
+            def lookup(self, key):
+                if key in self._remotebranches:
+                    key = self._remotebranches[key]
+                return olookup(key)
+
+            def pull(self, remote, *args, **kwargs):
+                res = opull(remote, *args, **kwargs)
+                lock = self.lock()
+                try:
+                    conf = config.config()
+                    rc = self.join('hgrc')
+                    if os.path.exists(rc):
+                        fp = open(rc)
+                        conf.parse('.hgrc', fp.read())
+                        fp.close()
+                    realpath = ''
+                    if 'paths' in conf:
+                        for path, uri in conf['paths'].items():
+                            if uri.rstrip('/') == remote.path.rstrip('/'):
+                                realpath = path
+                                # prefer a non-default name to default
+                                if path != 'default':
+                                    break
+                        self.saveremotebranches(realpath, remote.branchmap())
+                finally:
+                    lock.release()
+                    return res
+
+            def saveremotebranches(self, remote, bm):
+                real = {}
+                bfile = self.join('remotebranches')
+                olddata = []
+                existed = os.path.exists(bfile)
+                if existed:
+                    f = open(bfile)
+                    olddata = [l for l in f
+                               if not l.split(' ', 1)[1].startswith(remote)]
+                f = open(bfile, 'w')
+                if existed:
+                    f.write(''.join(olddata))
+                for branch, nodes in bm.iteritems():
+                    for n in nodes:
+                        f.write('%s %s/%s\n' % (node.hex(n), remote, branch))
+                    real[branch] = [node.hex(x) for x in nodes]
+                f.close()
+
+        repo.__class__ = remotebranchesrepo
+try:
+    from setuptools import setup
+except:
+    from distutils.core import setup
+
+setup(
+    name='hg-remotebranches',
+    version='1.0.0',
+    author='Augie Fackler',
+    maintainer='Augie Fackler',
+    maintainer_email='durin42@gmail.com',
+#    url='',
+    description='Mark remote branch heads in Mercurial',
+    long_description="""
+This extension automatically creates a local tag-like marker
+during a pull from a remote server that has its path specifed
+in .hg/hgrc.
+    """.strip(),
+    keywords='hg mercurial',
+    license='GPLv2',
+    py_modules=['hg_remotebranches'],
+)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.