from mercurial import hg, filelog, revlog, context, util
import os.path, re
def reposetup(ui, repo):
if not repo.local():
return
class kwrepo(repo.__class__):
def file(self, f):
if f[0] == '/':
f = f[1:]
return filelog.filelog(self.sopener, f, self, self.revlogversion)
class kwfilelog(filelog.filelog):
def __init__(self, opener, path, repo,
defversion=revlog.REVLOG_DEFAULT_VERSION):
super(kwfilelog, self).__init__(opener, path, defversion)
self._repo = repo
self._path = path
def read(self, node):
def gethgkw():
c = context.filectx(self._repo, self._path, fileid=node,
filelog=self)
filename = c.path()
for pat, opt in self._repo.ui.configitems('keyword'):
if opt == 'expand':
mf = util.matcher(self._repo.root,
'', [pat], [], [])[1]
if mf(filename):
return '$Hg: %s,v %s %s %s $' % (
os.path.basename(filename),
c.changectx(),
util.datestr(date=c.date(),
format=util.defaultdateformats[0]),
util.shortuser(c.user())
)
data = super(kwfilelog, self).read(node)
if not util.binary(data):
hgkw = gethgkw()
if hgkw:
data = data.replace('$Hg$', hgkw)
return data
def add(self, text, meta, tr, link, p1=None, p2=None):
if (not util.binary(text) and
self._repo.ui.config('keyword', 'remove', True)):
re_kw = re.compile(r'\$Hg: [^$]+?,v [a-z0-9]{12} [^$]+? \$')
text = re_kw.sub('$Hg$', text)
return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
filelog.filelog = kwfilelog
repo.__class__ = kwrepo