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
|
2014-10-03 20:43:48 +04:00
|
|
|
from mercurial import extensions
|
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
|
2014-03-18 22:46:10 +04:00
|
|
|
from mercurial import revset
|
|
|
|
from mercurial import templatekw
|
2012-06-20 19:23:51 +04:00
|
|
|
|
2010-01-11 02:24:02 +03:00
|
|
|
from hgext import schemes
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2014-10-03 20:43:48 +04:00
|
|
|
hasexchange = False
|
|
|
|
try:
|
|
|
|
from mercurial import exchange
|
|
|
|
hasexchange = bool(getattr(exchange, 'push', False))
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def expush(orig, repo, remote, *args, **kwargs):
|
|
|
|
res = orig(repo, remote, *args, **kwargs)
|
|
|
|
lock = repo.lock()
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
path = repo._activepath(remote)
|
|
|
|
if path:
|
2014-03-19 23:44:28 +04:00
|
|
|
# on a push, we don't want to keep obsolete heads since
|
|
|
|
# they won't show up as heads on the next pull, so we
|
|
|
|
# remove them here otherwise we would require the user
|
|
|
|
# to issue a pull to refresh .hg/remotebranches
|
|
|
|
bmap = {}
|
|
|
|
repo = repo.unfiltered()
|
|
|
|
for branch, nodes in remote.branchmap().iteritems():
|
|
|
|
bmap[branch] = [n for n in nodes if not repo[n].obsolete()]
|
|
|
|
repo.saveremotebranches(path, bmap)
|
2014-10-03 20:43:48 +04:00
|
|
|
except Exception, e:
|
|
|
|
ui.debug('remote branches for path %s not saved: %s\n'
|
|
|
|
% (path, e))
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
return res
|
|
|
|
|
|
|
|
def expull(orig, repo, remote, *args, **kwargs):
|
|
|
|
res = orig(repo, remote, *args, **kwargs)
|
|
|
|
lock = repo.lock()
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
path = repo._activepath(remote)
|
|
|
|
if path:
|
|
|
|
repo.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
|
|
|
|
|
|
|
|
if hasexchange:
|
|
|
|
extensions.wrapfunction(exchange, 'push', expush)
|
|
|
|
extensions.wrapfunction(exchange, 'pull', expull)
|
|
|
|
|
2010-01-04 07:37:45 +03:00
|
|
|
def reposetup(ui, repo):
|
2011-03-29 17:24:25 +04:00
|
|
|
if not repo.local():
|
|
|
|
return
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2014-10-03 20:49:56 +04:00
|
|
|
opull = getattr(repo.__class__, 'pull', False)
|
|
|
|
opush = getattr(repo.__class__, 'push', False)
|
2011-03-29 17:24:25 +04:00
|
|
|
olookup = repo.lookup
|
|
|
|
ofindtags = repo._findtags
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2014-10-03 20:49:56 +04:00
|
|
|
if opull or opush:
|
|
|
|
# Mercurial 3.1 and earlier use push/pull methods on the
|
|
|
|
# localrepo object instead of in the exchange module. Avoid
|
|
|
|
# reintroducing these methods into newer hg versions so we can
|
|
|
|
# continue to detect breakage.
|
|
|
|
class rbexchangerepo(repo.__class__):
|
|
|
|
def pull(self, remote, *args, **kwargs):
|
|
|
|
return expull(opull, self, remote, *args, **kwargs)
|
|
|
|
|
|
|
|
def push(self, remote, *args, **kwargs):
|
|
|
|
return expush(opush, self, remote, *args, **kwargs)
|
|
|
|
repo.__class__ = rbexchangerepo
|
|
|
|
|
2011-03-29 17:24:25 +04:00
|
|
|
class remotebranchesrepo(repo.__class__):
|
|
|
|
def _findtags(self):
|
|
|
|
(tags, tagtypes) = ofindtags()
|
2014-03-19 21:59:36 +04:00
|
|
|
for tag, n in self._remotebranches.iteritems():
|
|
|
|
tags[tag] = n
|
|
|
|
tagtypes[tag] = 'remote'
|
2011-03-29 17:24:25 +04:00
|
|
|
return (tags, tagtypes)
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2011-03-29 17:24:25 +04: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]
|
2011-03-29 17:24:25 +04:00
|
|
|
except TypeError: # unhashable type
|
|
|
|
pass
|
|
|
|
return olookup(key)
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2011-03-29 17:24:25 +04:00
|
|
|
def _activepath(self, remote):
|
|
|
|
realpath = ''
|
2014-03-18 22:46:10 +04:00
|
|
|
for path, uri in ui.configitems('paths'):
|
|
|
|
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 = getattr(remote, 'root', None)
|
|
|
|
if rpath is None:
|
|
|
|
# Maybe a localpeer? (hg@1ac628cd7113, 2.3)
|
|
|
|
rpath = getattr(getattr(remote, '_repo', None),
|
|
|
|
'root', None)
|
|
|
|
else:
|
|
|
|
rpath = remote._url
|
|
|
|
if uri.startswith('http'):
|
|
|
|
try:
|
|
|
|
uri = url.url(uri).authinfo()[0]
|
|
|
|
except AttributeError:
|
2011-04-08 03:53:39 +04:00
|
|
|
try:
|
2014-03-18 22:46:10 +04:00
|
|
|
uri = util.url(uri).authinfo()[0]
|
2011-04-08 03:53:39 +04:00
|
|
|
except AttributeError:
|
2014-03-18 22:46:10 +04:00
|
|
|
uri = url.getauthinfo(uri)[0]
|
|
|
|
uri = uri.rstrip('/')
|
|
|
|
rpath = rpath.rstrip('/')
|
|
|
|
if uri == rpath:
|
|
|
|
realpath = path
|
|
|
|
# prefer a non-default name to default
|
2014-03-18 22:46:10 +04:00
|
|
|
if path != 'default' and path != 'default-push':
|
2014-03-18 22:46:10 +04:00
|
|
|
break
|
2011-03-29 17:24:25 +04:00
|
|
|
return realpath
|
2010-01-11 01:56:16 +03:00
|
|
|
|
2011-03-29 17:24:25 +04:00
|
|
|
def saveremotebranches(self, remote, bm):
|
|
|
|
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))
|
2011-07-25 23:43:37 +04:00
|
|
|
alias_default = ui.configbool('remotebranches', 'alias.default')
|
|
|
|
if remote != 'default' and branch == 'default' and alias_default:
|
|
|
|
f.write('%s %s\n' % (node.hex(n), remote))
|
2011-03-29 17:24:25 +04:00
|
|
|
f.close()
|
2010-01-04 07:37:45 +03:00
|
|
|
|
2011-03-29 17:24:25 +04:00
|
|
|
repo.__class__ = remotebranchesrepo
|
2011-03-30 04:02:08 +04:00
|
|
|
|
2014-02-19 01:31:10 +04:00
|
|
|
try:
|
2014-03-11 00:56:53 +04:00
|
|
|
# Mercurial 3.0 adds laziness for revsets, which breaks returning lists.
|
2014-02-19 01:31:10 +04:00
|
|
|
baseset = revset.baseset
|
|
|
|
except AttributeError:
|
|
|
|
baseset = lambda x: x
|
|
|
|
|
2011-03-30 04:02:08 +04:00
|
|
|
def upstream_revs(filt, repo, subset, x):
|
2014-03-11 00:57:19 +04:00
|
|
|
upstream_tips = [node.hex(n) for name, n in
|
2011-03-30 04:02:08 +04:00
|
|
|
repo._remotebranches.iteritems() if filt(name)]
|
2014-03-11 00:57:19 +04:00
|
|
|
if not upstream_tips: []
|
2014-03-11 01:49:44 +04:00
|
|
|
|
|
|
|
ls = getattr(revset, 'lazyset', False)
|
|
|
|
if ls:
|
|
|
|
# If revset.lazyset exists (hg 3.0), use lazysets instead for
|
|
|
|
# speed.
|
|
|
|
tipancestors = repo.revs('::%ln', map(node.bin, upstream_tips))
|
|
|
|
def cond(n):
|
|
|
|
return n in tipancestors
|
|
|
|
return ls(subset, cond)
|
|
|
|
# 2.9 and earlier codepath
|
2011-03-30 04:02:08 +04:00
|
|
|
upstream = reduce(lambda x, y: x.update(y) or x,
|
|
|
|
map(lambda x: set(revset.ancestors(repo, subset, x)),
|
2014-03-11 00:57:19 +04:00
|
|
|
[('string', n) for n in upstream_tips]),
|
2011-03-30 04:02:08 +04:00
|
|
|
set())
|
2014-03-11 01:49:44 +04:00
|
|
|
return [r for r in subset if r in upstream]
|
2011-03-30 04:02:08 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2012-06-20 13:24:55 +04:00
|
|
|
def remotebranchesrevset(repo, subset, x):
|
|
|
|
"""``remotebranches()``
|
|
|
|
All remote branches heads.
|
|
|
|
"""
|
|
|
|
args = revset.getargs(x, 0, 0, "remotebranches takes no arguments")
|
|
|
|
remoterevs = set(repo[n].rev() for n in repo._remotebranches.itervalues())
|
2014-02-19 01:31:10 +04:00
|
|
|
return baseset([r for r in subset if r in remoterevs])
|
2012-06-20 13:24:55 +04:00
|
|
|
|
2014-03-18 22:46:10 +04:00
|
|
|
revset.symbols.update({'upstream': upstream,
|
|
|
|
'pushed': pushed,
|
|
|
|
'remotebranches': remotebranchesrevset})
|
2012-06-20 19:23:51 +04:00
|
|
|
|
|
|
|
def remotebrancheskw(**args):
|
|
|
|
""":remotebranches: List of strings. Any remote branch associated
|
|
|
|
with the changeset.
|
|
|
|
"""
|
|
|
|
repo, ctx = args['repo'], args['ctx']
|
|
|
|
remotenodes = {}
|
|
|
|
for name, node in repo._remotebranches.iteritems():
|
|
|
|
remotenodes.setdefault(node, []).append(name)
|
|
|
|
if ctx.node() in remotenodes:
|
|
|
|
names = sorted(remotenodes[ctx.node()])
|
|
|
|
return templatekw.showlist('remotebranch', names,
|
|
|
|
plural='remotebranches', **args)
|
|
|
|
|
2014-03-18 22:46:10 +04:00
|
|
|
templatekw.keywords['remotebranches'] = remotebrancheskw
|