changeset 1085:1003994dd497

share: host the git repository alongside the store Before this changeset, the internal git repository were always stored in '.hg/' even when the repository was a 'share' of another one. With this patch, hg-git respect the '.hg/sharedpath' file and stores the internal git repository and associated caches in the original repository. This allows multiple 'shares' to use the same git repository. This does not affect the 'intree' variant where the repository is directly stored inside the working copy.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 26 Oct 2017 18:20:00 +0200
parents 93d3f8cf9657
children c71dc6e5c62a
files hggit/__init__.py hggit/git_handler.py
diffstat 2 files changed, 26 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/__init__.py	Fri Nov 24 21:04:54 2017 +0100
+++ b/hggit/__init__.py	Thu Oct 26 18:20:00 2017 +0200
@@ -49,6 +49,7 @@
     revset,
     scmutil,
     templatekw,
+    vfs as vfsmod,
 )
 
 try:
@@ -214,13 +215,21 @@
              lambda *args: open(os.path.join(helpdir, 'git.rst')).read())
     insort(help.helptable, entry)
 
+def _gitvfs(repo):
+    """return a vfs suitable to read git related data"""
+    # Mercurial >= 3.3:  repo.shared()
+    if repo.sharedpath != repo.path:
+        return vfsmod.vfs(repo.sharedpath)
+    else:
+        return repo.vfs
+
 def reposetup(ui, repo):
     if not isinstance(repo, gitrepo.gitrepo):
 
         if (getattr(dirstate, 'rootcache', False) and
             (not ignoremod or getattr(ignore, 'readpats', False)) and
             hgutil.safehasattr(repo, 'vfs') and
-            os.path.exists(repo.vfs.join('git'))):
+            os.path.exists(_gitvfs(repo).join('git'))):
             # only install our dirstate wrapper if it has a hope of working
             import gitdirstate
             if ignoremod:
@@ -277,13 +286,14 @@
 def git_cleanup(ui, repo):
     '''clean up Git commit map after history editing'''
     new_map = []
-    for line in repo.vfs(GitHandler.map_file):
+    vfs = _gitvfs(repo)
+    for line in vfs(GitHandler.map_file):
         gitsha, hgsha = line.strip().split(' ', 1)
         if hgsha in repo:
             new_map.append('%s %s\n' % (gitsha, hgsha))
     wlock = repo.wlock()
     try:
-        f = repo.vfs(GitHandler.map_file, 'wb')
+        f = vfs(GitHandler.map_file, 'wb')
         map(f.write, new_map)
     finally:
         wlock.release()
--- a/hggit/git_handler.py	Fri Nov 24 21:04:54 2017 +0100
+++ b/hggit/git_handler.py	Thu Oct 26 18:20:00 2017 +0200
@@ -25,6 +25,7 @@
     phases,
     util as hgutil,
     url,
+    vfs as vfsmod,
 )
 
 import _ssh
@@ -105,11 +106,15 @@
     def __init__(self, dest_repo, ui):
         self.repo = dest_repo
         self.ui = ui
+        self.vfs = self.repo.vfs
 
+        # Mercurial >= 3.3:  repo.shared()
+        if dest_repo.sharedpath != dest_repo.path:
+            self.vfs = vfsmod.vfs(dest_repo.sharedpath)
         if compat.config(ui, 'bool', 'git', 'intree'):
             self.gitdir = self.repo.wvfs.join('.git')
         else:
-            self.gitdir = self.repo.vfs.join('git')
+            self.gitdir = self.vfs.join('git')
 
         self.init_author_file()
 
@@ -184,8 +189,8 @@
     def load_map(self):
         map_git_real = {}
         map_hg_real = {}
-        if os.path.exists(self.repo.vfs.join(self.map_file)):
-            for line in self.repo.vfs(self.map_file):
+        if os.path.exists(self.vfs.join(self.map_file)):
+            for line in self.vfs(self.map_file):
                 # format is <40 hex digits> <40 hex digits>\n
                 if len(line) != 82:
                     raise ValueError(
@@ -201,7 +206,7 @@
     def save_map(self, map_file):
         wlock = self.repo.wlock()
         try:
-            file = self.repo.vfs(map_file, 'w+', atomictemp=True)
+            file = self.vfs(map_file, 'w+', atomictemp=True)
             map_hg = self._map_hg
             buf = cStringIO.StringIO()
             bwrite = buf.write
@@ -216,13 +221,13 @@
 
     def load_tags(self):
         self.tags = {}
-        if os.path.exists(self.repo.vfs.join(self.tags_file)):
-            for line in self.repo.vfs(self.tags_file):
+        if os.path.exists(self.vfs.join(self.tags_file)):
+            for line in self.vfs(self.tags_file):
                 sha, name = line.strip().split(' ', 1)
                 self.tags[name] = sha
 
     def save_tags(self):
-        file = self.repo.vfs(self.tags_file, 'w+', atomictemp=True)
+        file = self.vfs(self.tags_file, 'w+', atomictemp=True)
         for name, sha in sorted(self.tags.iteritems()):
             if not self.repo.tagtype(name) == 'global':
                 file.write("%s %s\n" % (sha, name))
@@ -431,7 +436,7 @@
         return ret
 
     def clear(self):
-        mapfile = self.repo.vfs.join(self.map_file)
+        mapfile = self.vfs.join(self.map_file)
         if os.path.exists(self.gitdir):
             for root, dirs, files in os.walk(self.gitdir, topdown=False):
                 for name in files: