hgext/share.py
changeset 23548 141baca16059
parent 21772 5a4d1a6c605f
child 23614 cd79fb4d75fd
equal deleted inserted replaced
23547:21446f4d5c62 23548:141baca16059
     4 # GNU General Public License version 2 or any later version.
     4 # GNU General Public License version 2 or any later version.
     5 
     5 
     6 '''share a common history between several working directories'''
     6 '''share a common history between several working directories'''
     7 
     7 
     8 from mercurial.i18n import _
     8 from mercurial.i18n import _
     9 from mercurial import cmdutil, hg, util
     9 from mercurial import cmdutil, hg, util, extensions, bookmarks
       
    10 from mercurial.hg import repository, parseurl
       
    11 import errno
    10 
    12 
    11 cmdtable = {}
    13 cmdtable = {}
    12 command = cmdutil.command(cmdtable)
    14 command = cmdutil.command(cmdtable)
    13 testedwith = 'internal'
    15 testedwith = 'internal'
    14 
    16 
    65         destlock and destlock.release()
    67         destlock and destlock.release()
    66         lock and lock.release()
    68         lock and lock.release()
    67 
    69 
    68     # update store, spath, sopener and sjoin of repo
    70     # update store, spath, sopener and sjoin of repo
    69     repo.unfiltered().__init__(repo.baseui, repo.root)
    71     repo.unfiltered().__init__(repo.baseui, repo.root)
       
    72 
       
    73 def extsetup(ui):
       
    74     extensions.wrapfunction(bookmarks.bmstore, 'getbkfile', getbkfile)
       
    75     extensions.wrapfunction(bookmarks.bmstore, 'recordchange', recordchange)
       
    76     extensions.wrapfunction(bookmarks.bmstore, 'write', write)
       
    77 
       
    78 def _hassharedbookmarks(repo):
       
    79     """Returns whether this repo has shared bookmarks"""
       
    80     try:
       
    81         repo.vfs.read('bookmarks.shared')
       
    82         return True
       
    83     except IOError, inst:
       
    84         if inst.errno != errno.ENOENT:
       
    85             raise
       
    86         return False
       
    87 
       
    88 def _getsrcrepo(repo):
       
    89     """
       
    90     Returns the source repository object for a given shared repository.
       
    91     If repo is not a shared repository, return None.
       
    92     """
       
    93     srcrepo = None
       
    94     try:
       
    95         # strip because some tools write with newline after
       
    96         sharedpath = repo.vfs.read('sharedpath').strip()
       
    97         # the sharedpath always ends in the .hg; we want the path to the repo
       
    98         source = sharedpath.rsplit('/.hg', 1)[0]
       
    99         srcurl, branches = parseurl(source)
       
   100         srcrepo = repository(repo.ui, srcurl)
       
   101     except IOError, inst:
       
   102         if inst.errno != errno.ENOENT:
       
   103             raise
       
   104     return srcrepo
       
   105 
       
   106 def getbkfile(orig, self, repo):
       
   107     if _hassharedbookmarks(repo):
       
   108         srcrepo = _getsrcrepo(repo)
       
   109         if srcrepo is not None:
       
   110             repo = srcrepo
       
   111     return orig(self, repo)
       
   112 
       
   113 def recordchange(orig, self, tr):
       
   114     # Continue with write to local bookmarks file as usual
       
   115     orig(self, tr)
       
   116 
       
   117     if _hassharedbookmarks(self._repo):
       
   118         srcrepo = _getsrcrepo(self._repo)
       
   119         if srcrepo is not None:
       
   120             category = 'share-bookmarks'
       
   121             tr.addpostclose(category, lambda tr: self._writerepo(srcrepo))
       
   122 
       
   123 def write(orig, self):
       
   124     # First write local bookmarks file in case we ever unshare
       
   125     orig(self)
       
   126     if _hassharedbookmarks(self._repo):
       
   127         srcrepo = _getsrcrepo(self._repo)
       
   128         if srcrepo is not None:
       
   129             self._writerepo(srcrepo)