sapling/hggit/hgrepo.py

82 lines
2.9 KiB
Python
Raw Normal View History

import os
2009-08-10 19:07:13 +04:00
from mercurial.node import bin
from mercurial import util
2009-06-02 01:57:19 +04:00
from git_handler import GitHandler
from gitrepo import gitrepo
def generate_repo_subclass(baseclass):
class hgrepo(baseclass):
def pull(self, remote, heads=None, force=False):
if isinstance(remote, gitrepo):
2014-02-20 02:12:38 +04:00
return self.githandler.fetch(remote.path, heads)
2009-07-07 20:46:14 +04:00
else: #pragma: no cover
return super(hgrepo, self).pull(remote, heads, force)
# TODO figure out something useful to do with the newbranch param
def push(self, remote, force=False, revs=None, newbranch=False):
if isinstance(remote, gitrepo):
2014-02-20 02:14:01 +04:00
return self.githandler.push(remote.path, revs, force)
2009-07-07 20:46:14 +04:00
else: #pragma: no cover
return super(hgrepo, self).push(remote, force, revs, newbranch)
2009-07-31 21:15:02 +04:00
def findoutgoing(self, remote, base=None, heads=None, force=False):
if isinstance(remote, gitrepo):
base, heads = self.githandler.get_refs(remote.path)
2009-07-31 21:15:02 +04:00
out, h = super(hgrepo, self).findoutgoing(remote, base, heads, force)
return out
else: #pragma: no cover
return super(hgrepo, self).findoutgoing(remote, base, heads, force)
def _findtags(self):
(tags, tagtypes) = super(hgrepo, self)._findtags()
for tag, rev in self.githandler.tags.iteritems():
tags[tag] = bin(rev)
tagtypes[tag] = 'git'
tags.update(self.gitrefs())
return (tags, tagtypes)
@util.propertycache
def githandler(self):
'''get the GitHandler for an hg repo
This only makes sense if the repo talks to at least one git remote.
'''
return GitHandler(self, self.ui)
def gitrefs(self):
tagfile = self.join(os.path.join('git-remote-refs'))
if os.path.exists(tagfile):
tf = open(tagfile, 'rb')
tagdata = tf.read().split('\n')
td = [line.split(' ', 1) for line in tagdata if line]
return dict([(name, bin(sha)) for sha, name in td])
return {}
def tags(self):
if hasattr(self, 'tagscache') and self.tagscache:
# Mercurial 1.4 and earlier.
return self.tagscache
elif hasattr(self, '_tags') and self._tags:
# Mercurial 1.5 and later.
return self._tags
tagscache = super(hgrepo, self).tags()
tagscache.update(self.gitrefs())
for tag, rev in self.githandler.tags.iteritems():
if tag in tagscache:
continue
tagscache[tag] = bin(rev)
if hasattr(self, '_tagstypecache'):
# Only present in Mercurial 1.3 and earlier.
self._tagstypecache[tag] = 'git'
return tagscache
return hgrepo