Use hglib instead of mercurial directly.

This wrapper uses the command server without tying it to the mercurial
internals, which means it can support Python 3 and use a more liberal
license.
This commit is contained in:
Elliott Sales de Andrade 2016-11-01 00:12:37 -04:00
parent fb9f7dd8ef
commit 6f32ca001c
3 changed files with 19 additions and 17 deletions

View File

@ -22,8 +22,8 @@ Generic requirements
faster than python version of the client, but still slower than C version. faster than python version of the client, but still slower than C version.
* ``psutil`` python package. Required for some segments like cpu_percent. Some * ``psutil`` python package. Required for some segments like cpu_percent. Some
segments have linux-only fallbacks for ``psutil`` functionality. segments have linux-only fallbacks for ``psutil`` functionality.
* ``mercurial`` python package (note: *not* standalone executable). Required to * ``hglib`` python package *and* mercurial executable. Required to work with
work with mercurial repositories. mercurial repositories.
* ``pygit2`` python package or ``git`` executable. Required to work with ``git`` * ``pygit2`` python package or ``git`` executable. Required to work with ``git``
repositories. repositories.
* ``bzr`` python package (note: *not* standalone executable). Required to work * ``bzr`` python package (note: *not* standalone executable). Required to work
@ -35,8 +35,8 @@ Generic requirements
:py:func:`powerline.listers.i3wm.output_lister`. :py:func:`powerline.listers.i3wm.output_lister`.
.. note:: .. note::
Until mercurial and bazaar support Python-3 or PyPy powerline will not Until bazaar supports Python-3 or PyPy powerline will not support
support repository information when running in these interpreters. repository information when running in these interpreters.
.. _repository-root: .. _repository-root:

View File

@ -3,7 +3,7 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import os import os
from mercurial import hg, ui, match import hglib
from powerline.lib.vcs import get_branch_name, get_file_status from powerline.lib.vcs import get_branch_name, get_file_status
from powerline.lib.path import join from powerline.lib.path import join
@ -20,21 +20,23 @@ def branch_name_from_config_file(directory, config_file):
class Repository(object): class Repository(object):
__slots__ = ('directory', 'ui', 'create_watcher') __slots__ = ('directory', 'create_watcher')
statuses = 'MARDUI' # hg status -> (powerline file status, repo status flag)
repo_statuses = (1, 1, 1, 1, 2) statuses = {
b'M': ('M', 1), b'A': ('A', 1), b'R': ('R', 1), b'!': ('D', 1),
b'?': ('U', 2), b'I': ('I', 0)
}
repo_statuses_str = (None, 'D ', ' U', 'DU') repo_statuses_str = (None, 'D ', ' U', 'DU')
def __init__(self, directory, create_watcher): def __init__(self, directory, create_watcher):
self.directory = os.path.abspath(directory) self.directory = os.path.abspath(directory)
self.ui = ui.ui()
self.create_watcher = create_watcher self.create_watcher = create_watcher
def _repo(self, directory): def _repo(self, directory):
# Cannot create this object once and use always: when repository updates # Cannot create this object once and use always: when repository updates
# functions emit invalid results # functions emit invalid results
return hg.repository(self.ui, directory) return hglib.open(directory)
def status(self, path=None): def status(self, path=None):
'''Return status of repository or file. '''Return status of repository or file.
@ -63,17 +65,17 @@ class Repository(object):
def do_status(self, directory, path): def do_status(self, directory, path):
repo = self._repo(directory) repo = self._repo(directory)
if path: if path:
m = match.match(None, None, [path], exact=True) path = os.path.join(directory, path)
statuses = repo.status(match=m, unknown=True, ignored=True) statuses = repo.status(include=path, all=True)
for status, paths in zip(self.statuses, statuses): for status, paths in statuses:
if paths: if paths:
return status return self.statuses[status][0]
return None return None
else: else:
resulting_status = 0 resulting_status = 0
for status, paths in zip(self.repo_statuses, repo.status(unknown=True)): for status, paths in repo.status(all=True):
if paths: if paths:
resulting_status |= status resulting_status |= self.statuses[status][1]
return self.repo_statuses_str[resulting_status] return self.repo_statuses_str[resulting_status]
def branch(self): def branch(self):

View File

@ -33,7 +33,7 @@ else:
use_bzr = True use_bzr = True
try: try:
__import__('mercurial') __import__('hglib')
except ImportError: except ImportError:
use_mercurial = False use_mercurial = False
else: else: