2019-01-20 14:09:42 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Print the branch and git status for the
|
|
|
|
currently focused window.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
* xcwd
|
|
|
|
* Python module 'pygit2'
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import string
|
2019-01-20 19:27:04 +01:00
|
|
|
import logging
|
|
|
|
try:
|
|
|
|
import pygit2
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2019-01-20 14:09:42 +01:00
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
import bumblebee.util
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
2019-01-23 19:56:16 +01:00
|
|
|
widgets = []
|
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
2019-01-20 14:43:27 +01:00
|
|
|
self._engine = engine
|
2019-01-20 18:45:47 +01:00
|
|
|
self._error = False
|
2019-01-23 19:56:16 +01:00
|
|
|
self.update(self.widgets())
|
2019-01-20 14:43:27 +01:00
|
|
|
|
|
|
|
def hidden(self):
|
2019-01-20 18:45:47 +01:00
|
|
|
return self._error
|
2019-01-20 14:09:42 +01:00
|
|
|
|
2019-01-23 19:56:16 +01:00
|
|
|
def update(self, widgets):
|
|
|
|
state = {}
|
|
|
|
new_widgets = []
|
2019-01-20 14:09:42 +01:00
|
|
|
try:
|
|
|
|
directory = bumblebee.util.execute("xcwd").strip()
|
|
|
|
directory = self._get_git_root(directory)
|
|
|
|
repo = pygit2.Repository(directory)
|
2019-01-20 14:43:27 +01:00
|
|
|
|
2019-01-23 19:56:16 +01:00
|
|
|
new_widgets.append(bumblebee.output.Widget(name='git.main', full_text=repo.head.shorthand))
|
|
|
|
|
2019-01-20 14:43:27 +01:00
|
|
|
for filepath, flags in repo.status().items():
|
|
|
|
if flags == pygit2.GIT_STATUS_WT_NEW or \
|
|
|
|
flags == pygit2.GIT_STATUS_INDEX_NEW:
|
2019-01-23 19:56:16 +01:00
|
|
|
state['new'] = True
|
2019-01-20 14:43:27 +01:00
|
|
|
if flags == pygit2.GIT_STATUS_WT_DELETED or \
|
|
|
|
flags == pygit2.GIT_STATUS_INDEX_DELETED:
|
2019-01-23 19:56:16 +01:00
|
|
|
state['deleted'] = True
|
2019-01-20 14:43:27 +01:00
|
|
|
if flags == pygit2.GIT_STATUS_WT_MODIFIED or \
|
|
|
|
flags == pygit2.GIT_STATUS_INDEX_MODIFIED:
|
2019-01-23 19:56:16 +01:00
|
|
|
state['modified'] = True
|
2019-01-20 18:45:47 +01:00
|
|
|
self._error = False
|
2019-01-23 19:56:16 +01:00
|
|
|
if 'new' in state:
|
|
|
|
new_widgets.append(bumblebee.output.Widget(name='git.new'))
|
|
|
|
if 'modified' in state:
|
|
|
|
new_widgets.append(bumblebee.output.Widget(name='git.modified'))
|
|
|
|
if 'deleted' in state:
|
|
|
|
new_widgets.append(bumblebee.output.Widget(name='git.deleted'))
|
|
|
|
|
|
|
|
while len(widgets) > 0:
|
|
|
|
del widgets[0]
|
|
|
|
for widget in new_widgets:
|
|
|
|
widgets.append(widget)
|
|
|
|
|
2019-01-20 14:09:42 +01:00
|
|
|
except Exception as e:
|
2019-01-20 18:45:47 +01:00
|
|
|
self._error = True
|
2019-01-20 14:09:42 +01:00
|
|
|
|
2019-01-23 19:56:16 +01:00
|
|
|
def state(self, widget):
|
|
|
|
return widget.name.split('.')[1]
|
2019-01-20 14:09:42 +01:00
|
|
|
|
|
|
|
def _get_git_root(self, directory):
|
2019-01-20 14:17:10 +01:00
|
|
|
while len(directory) > 1:
|
2019-01-20 14:09:42 +01:00
|
|
|
if os.path.exists(os.path.join(directory, ".git")):
|
|
|
|
return directory
|
|
|
|
directory = "/".join(directory.split("/")[0:-1])
|
2019-01-20 14:17:10 +01:00
|
|
|
return "/"
|
2019-01-20 14:09:42 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|