2009-05-10 02:01:09 -04:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
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
|
|
|
|
import re
|
2009-04-17 16:51:06 -04:00
|
|
|
|
2009-05-10 22:45:16 -04:00
|
|
|
try:
|
|
|
|
from mercurial import hg, ui
|
|
|
|
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
|
|
|
|
def _hg_init(self):
|
|
|
|
ui_imp = ui.ui(verbose=True)
|
|
|
|
ui_imp.pushbuffer()
|
|
|
|
repo = hg.repository(ui=ui_imp, path='.')
|
|
|
|
return ui_imp, repo
|
|
|
|
|
|
|
|
class HgBlame(VcBlame, HgBase):
|
2009-05-09 19:41:42 -04:00
|
|
|
"""Show buffer annotated with hg metadata"""
|
2009-05-10 02:01:09 -04:00
|
|
|
line_re = re.compile(r'^ *(?P<user>[^ ]+) (?P<rev>\d+) (?P<date>\d{4}-\d{2}-\d{2}): (?P<content>.*)\n$')
|
|
|
|
prefix_fmt = '[b:d:*]%(rev)-5s [c:d:*]%(user)-10s [b:d:*]%(date)10s[d:d:*]'
|
|
|
|
_is_method = True
|
|
|
|
def _open_pipe(self, w, **vargs):
|
|
|
|
cmd = ("hg", 'blame', '-nudq', w.buffer.path)
|
|
|
|
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 HgBlame2(Method, HgBase):
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if not self._hg_check(w):
|
|
|
|
return
|
|
|
|
ui_imp, repo = self._hg_init()
|
|
|
|
hgc.identify(ui_imp, repo, w.buffer.path)
|
|
|
|
hgc.annotate(ui_imp, repo, w.buffer.path, user=None, date=None, rev=None)
|
|
|
|
s = ''.join(ui_imp.popbuffer())
|
|
|
|
w.application.data_buffer('*Blame*', s, switch_to=True)
|
|
|
|
|
|
|
|
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()
|
|
|
|
hgc.log(ui_imp, repo, user=None, rev=None, date=None)
|
|
|
|
s = ''.join(ui_imp.popbuffer())
|
|
|
|
w.application.data_buffer('*Log*', s, switch_to=True)
|
2009-05-09 19:41:42 -04:00
|
|
|
|
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-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-04-17 16:51:06 -04:00
|
|
|
hgc.diff(ui_imp, repo, w.buffer.path)
|
2009-04-17 22:09:31 -04:00
|
|
|
s = ''.join(ui_imp.popbuffer())
|
|
|
|
w.application.data_buffer("*Diff*", s, switch_to=True, modename='diff')
|