sapling/hg_remotebranches.py

192 lines
7.0 KiB
Python
Raw Normal View History

2010-01-04 07:37:45 +03:00
import os
from mercurial import config
2010-08-20 14:19:26 +04:00
from mercurial import context
2010-01-04 07:37:45 +03:00
from mercurial import hg
from mercurial import node
from mercurial import ui
2010-01-11 01:56:16 +03:00
from mercurial import url
2010-01-04 07:37:45 +03:00
from mercurial import util
try:
from mercurial import revset
# force demandimport to load revset
revset.methods
except ImportError:
revset = None
2010-01-11 02:24:02 +03:00
from hgext import schemes
2010-01-04 07:37:45 +03:00
def reposetup(ui, repo):
if not repo.local():
return
2010-01-04 07:37:45 +03:00
opull = repo.pull
opush = repo.push
olookup = repo.lookup
ofindtags = repo._findtags
2010-01-04 07:37:45 +03:00
class remotebranchesrepo(repo.__class__):
def _findtags(self):
(tags, tagtypes) = ofindtags()
tags.update(self._remotebranches)
return (tags, tagtypes)
2010-01-04 07:37:45 +03:00
@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)
# look up the hash in the changelog directly
# to avoid infinite recursion if the hash is bogus
n = self.changelog._match(hash)
if n:
# we need rev since node will recurse lookup
ctx = context.changectx(self,
self.changelog.rev(n))
if not ctx.extra().get('close'):
remotebranches[name] = n
return remotebranches
def lookup(self, key):
try:
2010-01-04 07:37:45 +03:00
if key in self._remotebranches:
key = self._remotebranches[key]
except TypeError: # unhashable type
pass
return olookup(key)
2010-01-04 07:37:45 +03:00
def pull(self, remote, *args, **kwargs):
res = opull(remote, *args, **kwargs)
lock = self.lock()
try:
2010-01-04 07:37:45 +03:00
try:
path = self._activepath(remote)
if path:
self.saveremotebranches(path, remote.branchmap())
except Exception, e:
ui.debug('remote branches for path %s not saved: %s\n'
% (path, e))
finally:
lock.release()
return res
2010-03-22 22:03:52 +03:00
def push(self, remote, *args, **kwargs):
res = opush(remote, *args, **kwargs)
lock = self.lock()
try:
2010-03-22 22:03:52 +03:00
try:
path = self._activepath(remote)
if path:
self.saveremotebranches(path, remote.branchmap())
except Exception, e:
ui.debug('remote branches for path %s not saved: %s\n'
% (path, e))
finally:
lock.release()
return res
2010-01-04 07:37:45 +03:00
def _activepath(self, remote):
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():
for s in schemes.schemes.iterkeys():
if uri.startswith('%s://' % s):
# TODO: refactor schemes so we don't
# duplicate this logic
ui.note('performing schemes expansion with '
'scheme %s\n' % s)
scheme = hg.schemes[s]
parts = uri.split('://', 1)[1].split('/',
scheme.parts)
if len(parts) > scheme.parts:
tail = parts[-1]
parts = parts[:-1]
else:
tail = ''
context = dict((str(i+1), v) for i, v in
enumerate(parts))
uri = ''.join(scheme.templater.process(
scheme.url, context)) + tail
uri = self.ui.expandpath(uri)
if remote.local():
uri = os.path.realpath(uri)
rpath = remote.root
else:
rpath = remote._url
if uri.startswith('http'):
uri = url.getauthinfo(uri)[0]
uri = uri.rstrip('/')
rpath = rpath.rstrip('/')
if uri == rpath:
realpath = path
# prefer a non-default name to default
if path != 'default':
break
return realpath
2010-01-11 01:56:16 +03:00
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()
2010-01-04 07:37:45 +03:00
repo.__class__ = remotebranchesrepo
def upstream_revs(filt, repo, subset, x):
nodes = [node.hex(n) for name, n in
repo._remotebranches.iteritems() if filt(name)]
if not nodes: []
upstream = reduce(lambda x, y: x.update(y) or x,
map(lambda x: set(revset.ancestors(repo, subset, x)),
[('string', n) for n in nodes]),
set())
return [r for r in subset if r in upstream]
def upstream(repo, subset, x):
'''``upstream()``
Select changesets in an upstream repository according to remotebranches.
'''
args = revset.getargs(x, 0, 0, "upstream takes no arguments")
upstream_names = [s + '/' for s in
repo.ui.configlist('remotebranches', 'upstream')]
if not upstream_names:
filt = lambda x: True
else:
filt = lambda name: any(map(name.startswith, upstream_names))
return upstream_revs(filt, repo, subset, x)
def pushed(repo, subset, x):
'''``pushed()``
Select changesets in any remote repository according to remotebranches.
'''
args = revset.getargs(x, 0, 0, "pushed takes no arguments")
return upstream_revs(lambda x: True, repo, subset, x)
if revset is not None:
revset.symbols.update({'upstream': upstream,
'pushed': pushed})