bumblebee-status/bumblebee/modules/git.py
Tobias Witek 580bc554ba [modules] Add new module to display git information
This module uses xcwd to retrieve the directory of the currently focused
window and, if any of the parent directories is a git repository,
displays basic information about the repository.

Right now, only the current branch name is displayed.
2019-01-20 14:09:42 +01:00

50 lines
1.3 KiB
Python

# pylint: disable=C0111,R0903
"""Print the branch and git status for the
currently focused window.
Requires:
* xcwd
* Python module 'pygit2'
"""
import os
import string
import pygit2
import bumblebee.input
import bumblebee.output
import bumblebee.engine
import bumblebee.util
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.gitinfo)
)
self._fmt = self.parameter("format", "{branch}")
def gitinfo(self, widget):
info = ""
directory = None
data = {
"branch": "n/a",
}
try:
directory = bumblebee.util.execute("xcwd").strip()
directory = self._get_git_root(directory)
repo = pygit2.Repository(directory)
data["branch"] = repo.head.shorthand
except Exception as e:
return e
return string.Formatter().vformat(self._fmt, (), data)
def _get_git_root(self, directory):
while directory != "/":
if os.path.exists(os.path.join(directory, ".git")):
return directory
directory = "/".join(directory.split("/")[0:-1])
return None
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4