2009-05-14 23:59:13 -04:00
|
|
|
import os
|
|
|
|
import re
|
2009-05-10 02:01:09 -04:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
2009-05-14 23:59:13 -04:00
|
|
|
|
2009-04-17 16:51:06 -04:00
|
|
|
from method import Method, Argument
|
2009-05-10 02:01:09 -04:00
|
|
|
from method.vc import VcBlame
|
2009-04-17 16:51:06 -04:00
|
|
|
|
2009-05-10 22:45:16 -04:00
|
|
|
try:
|
2009-05-18 01:44:54 -04:00
|
|
|
from mercurial import hg, ui, node
|
2009-05-10 22:45:16 -04:00
|
|
|
from mercurial import commands as hgc
|
|
|
|
has_hg = True
|
|
|
|
except ImportError:
|
|
|
|
has_hg = False
|
|
|
|
|
2009-05-14 19:48:40 -04:00
|
|
|
class HgBase(object):
|
|
|
|
def _hg_check(self, w):
|
|
|
|
if not has_hg:
|
|
|
|
w.set_error("Mercurial is not installed")
|
|
|
|
return False
|
|
|
|
elif not hasattr(w.buffer, 'path'):
|
|
|
|
w.set_error("Buffer has no corresponding file")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2009-05-14 23:59:13 -04:00
|
|
|
def _hg_init(self, **kwargs):
|
|
|
|
ui_imp = ui.ui(**kwargs)
|
2009-05-14 19:48:40 -04:00
|
|
|
ui_imp.pushbuffer()
|
2020-09-03 23:50:36 -04:00
|
|
|
repo = hg.repository(ui=ui_imp, path='.'.encode('UTF-8'))
|
2009-05-14 19:48:40 -04:00
|
|
|
return ui_imp, repo
|
|
|
|
|
|
|
|
class HgBlame(VcBlame, HgBase):
|
2009-05-09 19:41:42 -04:00
|
|
|
"""Show buffer annotated with hg metadata"""
|
2009-05-14 23:59:13 -04:00
|
|
|
num_fields = 3
|
|
|
|
# user, rev, [changeset], date, content
|
|
|
|
line_re = re.compile(r'^ *([^ ]+) +(\d+) +[^ ]+ +(\d{4}-\d{2}-\d{2}): (.*)\n$')
|
|
|
|
prefix_fmt = '[g:d:*]%*s [c:d:*]%*s [b:d:*]%*s[d:d:*]'
|
2009-05-10 02:01:09 -04:00
|
|
|
_is_method = True
|
|
|
|
def _open_pipe(self, w, **vargs):
|
2009-05-14 23:59:13 -04:00
|
|
|
cmd = ("hg", 'blame', '-cnudq', w.buffer.path)
|
2009-05-10 02:01:09 -04:00
|
|
|
return Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
2009-05-09 19:41:42 -04:00
|
|
|
|
2009-05-14 19:48:40 -04:00
|
|
|
class HgLog(Method, HgBase):
|
2009-05-09 19:41:42 -04:00
|
|
|
"""Show hg log for this buffer"""
|
2009-05-09 22:57:05 -04:00
|
|
|
def _execute(self, w, **vargs):
|
2009-05-14 19:48:40 -04:00
|
|
|
if not self._hg_check(w):
|
|
|
|
return
|
|
|
|
ui_imp, repo = self._hg_init()
|
2021-04-23 00:14:03 -04:00
|
|
|
hgc.log(ui_imp, repo)
|
2020-09-03 23:50:36 -04:00
|
|
|
output = ui_imp.popbuffer().decode('UTF-8')
|
|
|
|
w.application.data_buffer('*Log*', output, switch_to=True)
|
2009-05-09 19:41:42 -04:00
|
|
|
|
2009-05-18 01:44:54 -04:00
|
|
|
class HgInfo(Method, HgBase):
|
|
|
|
"""Get some basic info from Mercurial about the current buffer"""
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if not self._hg_check(w):
|
|
|
|
return
|
|
|
|
base = os.path.basename(w.buffer.path)
|
|
|
|
ui_imp, repo = self._hg_init()
|
|
|
|
path = w.buffer.path.replace(os.getcwd() + '/', '')
|
2020-09-03 23:50:36 -04:00
|
|
|
#l = repo.file(path.encode('UTF-8'))
|
2009-05-18 01:44:54 -04:00
|
|
|
status = 'Unknown'
|
2020-09-03 23:50:36 -04:00
|
|
|
#rev = l.linkrev(l.rev(l.tip()))
|
|
|
|
fc = repo.filectx(path.encode('UTF-8'), b'tip')
|
|
|
|
user = ui_imp.shortuser(fc.user()).decode('UTF-8')
|
|
|
|
rev = node.hex(fc.node())
|
|
|
|
ctx = repo[fc.node()]
|
|
|
|
changeid = node.hex(ctx.node()[:6]).decode('UTF-8')
|
|
|
|
#user = ui_imp.shortuser(ctx.user())
|
2009-05-18 01:44:54 -04:00
|
|
|
msg = '%s %s %s:%s [%s]' % (base, status, rev, changeid, user)
|
|
|
|
w.set_error(msg)
|
|
|
|
|
2009-05-14 19:48:40 -04:00
|
|
|
class HgDiff(Method, HgBase):
|
2009-04-17 22:09:31 -04:00
|
|
|
"""Diff the current file with the version in Mercurial"""
|
2009-05-18 00:12:10 -04:00
|
|
|
def _get_revs(self, w, **vargs):
|
|
|
|
return []
|
2009-04-17 16:51:06 -04:00
|
|
|
def _execute(self, w, **vargs):
|
2009-05-14 19:48:40 -04:00
|
|
|
if not self._hg_check(w):
|
2009-04-17 16:51:06 -04:00
|
|
|
return
|
2009-05-14 19:48:40 -04:00
|
|
|
ui_imp, repo = self._hg_init()
|
2009-05-18 00:12:10 -04:00
|
|
|
revs = self._get_revs(w, **vargs)
|
|
|
|
try:
|
2009-10-01 19:38:20 -04:00
|
|
|
c = w.application.config
|
|
|
|
opts = {
|
|
|
|
'ignore_all_space': c.get('hg.diff.ignore_app_space'),
|
|
|
|
'ignore_space_change': c.get('hg.diff.ignore_space_change'),
|
|
|
|
'ignore-blank-lines': c.get('hg.diff.ignore_blank_lines'),
|
|
|
|
'unified': c.get('hg.diff.unified'),
|
|
|
|
}
|
|
|
|
hgc.diff(ui_imp, repo, w.buffer.path, rev=revs, **opts)
|
2020-08-31 20:58:27 -04:00
|
|
|
except Exception as e:
|
2009-05-18 00:12:10 -04:00
|
|
|
w.set_error(str(e))
|
|
|
|
return
|
2009-04-17 22:09:31 -04:00
|
|
|
s = ''.join(ui_imp.popbuffer())
|
|
|
|
w.application.data_buffer("*Diff*", s, switch_to=True, modename='diff')
|
2009-05-18 00:12:10 -04:00
|
|
|
|
|
|
|
class HgDiff2(HgDiff):
|
|
|
|
"""Diff the current file with a specific revision in Mercurial"""
|
|
|
|
args = [Argument("revision", type=type(""), prompt="Revision: ")]
|
|
|
|
def _get_revs(self, w, **vargs):
|
|
|
|
return [vargs['revision']]
|
|
|
|
|
|
|
|
class HgDiff3(HgDiff):
|
|
|
|
"""Diff the current file with a specific revision in Mercurial"""
|
|
|
|
args = [Argument("revision1", type=type(""), prompt="First Revision: "),
|
|
|
|
Argument("revision2", type=type(""), prompt="Second Revision: ")]
|
|
|
|
def _get_revs(self, w, **vargs):
|
|
|
|
return [vargs['revision1'], vargs['revision2']]
|