D4556: unionrepo: dynamically create repository type from base repository

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Thu Sep 13 02:02:12 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This is basically the same thing we just did for bundlerepo except
  for union repositories.
  
  .. api::
  
    ``unionrepo.unionrepository()`` is no longer usable on its own.
    
    To instantiate an instance, call ``unionrepo.instance()`` or
    ``unionrepo.makeunionrepository()``.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/unionrepo.py

CHANGE DETAILS

diff --git a/mercurial/unionrepo.py b/mercurial/unionrepo.py
--- a/mercurial/unionrepo.py
+++ b/mercurial/unionrepo.py
@@ -192,15 +192,18 @@
     def canpush(self):
         return False
 
-class unionrepository(localrepo.localrepository):
-    def __init__(self, ui, path, path2):
-        localrepo.localrepository.__init__(self, ui, path)
+class unionrepository(object):
+    """Represents the union of data in 2 repositories.
+
+    Instances are not usable if constructed directly. Use ``instance()``
+    or ``makeunionrepository()`` to create a usable instance.
+    """
+    def __init__(self, repo2, url):
+        self.repo2 = repo2
+        self._url = url
+
         self.ui.setconfig('phases', 'publish', False, 'unionrepo')
 
-        self._url = 'union:%s+%s' % (util.expandpath(path),
-                                     util.expandpath(path2))
-        self.repo2 = localrepo.localrepository(ui, path2)
-
     @localrepo.unfilteredpropertycache
     def changelog(self):
         return unionchangelog(self.svfs, self.repo2.svfs)
@@ -260,4 +263,22 @@
             repopath, repopath2 = s
     else:
         repopath, repopath2 = parentpath, path
-    return unionrepository(ui, repopath, repopath2)
+
+    return makeunionrepository(ui, repopath, repopath2)
+
+def makeunionrepository(ui, repopath1, repopath2):
+    """Make a union repository object from 2 local repo paths."""
+    repo1 = localrepo.instance(ui, repopath1, create=False)
+    repo2 = localrepo.instance(ui, repopath2, create=False)
+
+    url = 'union:%s+%s' % (util.expandpath(repopath1),
+                           util.expandpath(repopath2))
+
+    class derivedunionrepository(unionrepository, repo1.__class__):
+        pass
+
+    repo = repo1
+    repo.__class__ = derivedunionrepository
+    unionrepository.__init__(repo1, repo2, url)
+
+    return repo



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list