2014-10-14 03:26:31 +04:00
|
|
|
"""Compatibility functions for old Mercurial versions and other utility
|
|
|
|
functions."""
|
2015-07-01 23:55:59 +03:00
|
|
|
import re
|
|
|
|
|
2012-08-28 18:09:01 +04:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
|
|
|
except ImportError:
|
|
|
|
from ordereddict import OrderedDict
|
2010-04-30 19:35:13 +04:00
|
|
|
|
2016-01-09 00:57:03 +03:00
|
|
|
from dulwich import errors
|
|
|
|
from mercurial import (
|
2016-01-09 00:58:47 +03:00
|
|
|
lock as lockmod,
|
2016-01-09 00:57:03 +03:00
|
|
|
util as hgutil,
|
|
|
|
)
|
|
|
|
|
2015-07-01 23:55:59 +03:00
|
|
|
gitschemes = ('git', 'git+ssh', 'git+http', 'git+https')
|
|
|
|
|
2012-08-06 20:30:33 +04:00
|
|
|
def parse_hgsub(lines):
|
|
|
|
"""Fills OrderedDict with hgsub file content passed as list of lines"""
|
2012-08-28 18:09:01 +04:00
|
|
|
rv = OrderedDict()
|
2012-08-06 20:30:33 +04:00
|
|
|
for l in lines:
|
2015-04-23 02:27:13 +03:00
|
|
|
ls = l.strip()
|
|
|
|
if not ls or ls[0] == '#':
|
|
|
|
continue
|
2012-08-06 20:30:33 +04:00
|
|
|
name, value = l.split('=', 1)
|
|
|
|
rv[name.strip()] = value.strip()
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def serialize_hgsub(data):
|
|
|
|
"""Produces a string from OrderedDict hgsub content"""
|
2015-04-23 02:27:13 +03:00
|
|
|
return ''.join(['%s = %s\n' % (n, v) for n, v in data.iteritems()])
|
2012-08-06 20:30:33 +04:00
|
|
|
|
|
|
|
def parse_hgsubstate(lines):
|
|
|
|
"""Fills OrderedDict with hgsubtate file content passed as list of lines"""
|
2012-08-28 18:09:01 +04:00
|
|
|
rv = OrderedDict()
|
2012-08-06 20:30:33 +04:00
|
|
|
for l in lines:
|
2015-04-23 02:27:13 +03:00
|
|
|
ls = l.strip()
|
|
|
|
if not ls or ls[0] == '#':
|
|
|
|
continue
|
2012-08-06 20:30:33 +04:00
|
|
|
value, name = l.split(' ', 1)
|
|
|
|
rv[name.strip()] = value.strip()
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def serialize_hgsubstate(data):
|
|
|
|
"""Produces a string from OrderedDict hgsubstate content"""
|
|
|
|
return ''.join(['%s %s\n' % (data[n], n) for n in sorted(data)])
|
2014-10-14 03:26:31 +04:00
|
|
|
|
|
|
|
def transform_notgit(f):
|
2015-04-23 02:27:13 +03:00
|
|
|
'''use as a decorator around functions that call into dulwich'''
|
2014-10-14 03:26:31 +04:00
|
|
|
def inner(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
except errors.NotGitRepository:
|
2014-12-06 03:40:21 +03:00
|
|
|
raise hgutil.Abort('not a git repository')
|
2014-10-14 03:26:31 +04:00
|
|
|
return inner
|
2015-07-01 23:55:59 +03:00
|
|
|
|
|
|
|
def isgitsshuri(uri):
|
|
|
|
"""Method that returns True if a uri looks like git-style uri
|
|
|
|
|
|
|
|
Tests:
|
|
|
|
|
|
|
|
>>> print isgitsshuri('http://fqdn.com/hg')
|
|
|
|
False
|
|
|
|
>>> print isgitsshuri('http://fqdn.com/test.git')
|
|
|
|
False
|
|
|
|
>>> print isgitsshuri('git@github.com:user/repo.git')
|
|
|
|
True
|
|
|
|
>>> print isgitsshuri('github-123.com:user/repo.git')
|
|
|
|
True
|
|
|
|
>>> print isgitsshuri('git@127.0.0.1:repo.git')
|
|
|
|
True
|
|
|
|
>>> print isgitsshuri('git@[2001:db8::1]:repository.git')
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
for scheme in gitschemes:
|
|
|
|
if uri.startswith('%s://' % scheme):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if uri.startswith('http:') or uri.startswith('https:'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
m = re.match(r'(?:.+@)*([\[]?[\w\d\.\:\-]+[\]]?):(.*)', uri)
|
|
|
|
if m:
|
|
|
|
# here we're being fairly conservative about what we consider to be git
|
|
|
|
# urls
|
|
|
|
giturl, repopath = m.groups()
|
|
|
|
# definitely a git repo
|
|
|
|
if repopath.endswith('.git'):
|
|
|
|
return True
|
|
|
|
# use a simple regex to check if it is a fqdn regex
|
|
|
|
fqdn_re = (r'(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}'
|
|
|
|
r'(?<!-)\.)+[a-zA-Z]{2,63}$)')
|
|
|
|
if re.match(fqdn_re, giturl):
|
|
|
|
return True
|
|
|
|
return False
|
2016-01-09 00:58:47 +03:00
|
|
|
|
2017-07-19 15:51:44 +03:00
|
|
|
def updatebookmarks(repo, changes, name='git_handler'):
|
2016-01-09 00:58:47 +03:00
|
|
|
"""abstract writing bookmarks for backwards compatibility"""
|
2017-07-19 15:51:44 +03:00
|
|
|
bms = repo._bookmarks
|
2016-01-09 00:58:47 +03:00
|
|
|
tr = lock = wlock = None
|
|
|
|
try:
|
|
|
|
wlock = repo.wlock()
|
|
|
|
lock = repo.lock()
|
|
|
|
tr = repo.transaction(name)
|
2017-07-19 15:51:44 +03:00
|
|
|
if hgutil.safehasattr(bms, 'applychanges'):
|
|
|
|
# applychanges was added in mercurial 4.3
|
|
|
|
bms.applychanges(repo, tr, changes)
|
2016-01-09 00:58:47 +03:00
|
|
|
else:
|
2017-07-19 15:51:44 +03:00
|
|
|
for name, node in changes:
|
|
|
|
if node is None:
|
|
|
|
del bms[name]
|
|
|
|
else:
|
|
|
|
bms[name] = node
|
|
|
|
if hgutil.safehasattr(bms, 'recordchange'):
|
|
|
|
# recordchange was added in mercurial 3.2
|
|
|
|
bms.recordchange(tr)
|
|
|
|
else:
|
|
|
|
bms.write()
|
2016-01-09 00:58:47 +03:00
|
|
|
tr.close()
|
|
|
|
finally:
|
|
|
|
lockmod.release(tr, lock, wlock)
|